Unit V - Daa
Unit V - Daa
Unit V - Daa
Introduction to randomization
and approximation algorithm
Syllabus
Introduction –
• Randomization Algorithm
• Randomized hiring problem
• Randomized quick sort and its time complexity
• String matching algorithm
• Rabin Karp algorithm for string matching
• Approximation algorithm
• Vertex covering
• Introduction Complexity classes
• P type problems
• Introduction to NP type problems
• Hamiltonian cycle problem
RANDOMIZED ALGORITHMS
• Example:
• Consider a binary array where exactly half elements are 0
and half are 1. The task is to find index of any 1.
• A Las Vegas algorithm for this task is to keep picking a random
element until we find a 1. A Monte Carlo algorithm for the
same is to keep picking a random element until we either find 1
or we have tried maximum allowed times say k.
• The Las Vegas algorithm always finds an index of 1, but time
complexity is determined as expect value.
• therefore expected time complexity is O(1).
• The Monte Carlo Algorithm finds a 1 with probability [1 –
(1/2)k]. Time complexity of Monte Carlo is O(k) which is
deterministic
Applications and Scope
• Randomized algorithms have huge applications in
Cryptography.
• Load Balancing.
• Number-Theoretic Applications: Primality Testing
• Data Structures: Hashing, Sorting, Searching, Order
Statistics and Computational Geometry.
• Algebraic identities: Polynomial and matrix identity
verification. Interactive proof systems.
• Mathematical programming: Faster algorithms for linear
programming, Rounding linear program
• solutions to integer program solutions
• Graph algorithms: Minimum spanning trees, shortest paths,
minimum cuts.
• Counting and enumeration: Matrix permanent Counting
combinatorial structures.
• etc…
HIRING PROBLEM
• We will now begin our investigation of randomized algorithms
with a toy problem:
• You want to hire an office assistant from an employment
agency.
• You want to interview candidates and determine if they are
better than the current assistant and if so replace the current
assistant.
• You are going to eventually interview every candidate from a
pool of n candidates.
• You want to always have the best person for this job, so you will
replace an assistant with a better one as soon as you are done
the interview.
• However, there is a cost to fire and then hire someone.
• You want to know the expected price of following this strategy
until all n candidates have been interviewed.
Steps involved in HIRING PROBLEM
Total Cost and Cost of Hiring
Time complexity – Execution -
Comparison
Randomized quick sort
• In this article we will discuss how to implement QuickSort using random
pivoting. In QuickSort we first partition the array in place such that all
elements to the left of the pivot element are smaller, while all elements to
the right of the pivot are greater that the pivot. Then we recursively call
the same procedure for left and right subarrays.
• Unlike merge sort we don’t need to merge the two sorted arrays. Thus
Quicksort requires lesser auxillary space than Merge Sort, which is why it
is often preferred to Merge Sort.Using a randomly generated pivot we can
further improve the time complexity of QuickSort.
Randomized Algorithms
Input x Output y
Deterministic Algorithm
random bits r
Input x Output y r
Randomized Algorithm
Example: Randomized QuickSort
QuickSort ?
1 Pick a pivot element from array
2 Split array into 3 subarrays: those smaller than pivot,
those larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate
them.
Chandra (UIUC)
Randomized QuickSort
1 Pick a pivot element uniformly at random from the array
2 Split array into 3 subarrays: those smaller than pivot,
those larger than pivot, and the pivot itself.
3 Recursively sort the subarrays, and concatenate
them.
Example: Randomized QuickSort
Algorithm for Randomized quick sort
Algorithm for Randomized quick sort
• What is overall Time Complexity in Worst Case?
• In worst case, each partition divides array such that one side has
n/4 elements and other side has 3n/4 elements. The worst case
height of recursion tree is Log 3/4 n which is O(Log n).
• T(n) < T(n/4) + T(3n/4) + O(n)