Decision Problems Optimization problems minimum maximum smallest largest
Decision Problems Optimization problems: minimum, maximum, smallest, largest Traveling salesman, Clique, Vertex-Cover, Independent Set, Knapsack Satisfaction (SAT) problems: CNF, Constraint-SAT, Hamiltonian Circuit, Circuit-SAT Only optimization problems use the minimum, maximum, smallest, largest value: • To formulate the decision problem • As input parameter for Phase II of the non deterministic algorithms
Computing an Reduce and Conquer: Derek Drake’s: Divide and Conquer: compute(a, n) Brute Force: compute(a, n) { { { if (n=0) then ans 1 return 1 for i 1 to n do m floor(n/2) ans * a s compute(a, m) return ans if ( n is even) then compute(a, m) × } return s * s compute(a, n – m) if ( n is odd) then Complexity: O(n) } return a * s } T(n) = 2 T(n/2) +1 T(n) = T(n/2) +3 T(1) = 1 Complexity: O(n) Complexity: O(log 2 n)
Solving Recurrence Relations T(n) = 4 T(n/2) + n T(1) = 1 4 k+ 2 k-1 n + … + 2 n + n with n = 2 k O(n 2 ) T(n) = 4 T(n/2) + n 2 T(1) = 1 4 k+ kn 2 with n = 2 k O(n 2 log 2 n)
Merge Sort is Stable • We split A into two parts B (first half) and C (first half) • Sort B and C separately • When merging B and C, we compare elements in B against elements in C, if tides occur, we insert element of B back into A first and then elements in B
Quicksort
Quicksort • An element of the array is chosen. We call it the pivot element. • The array is rearranged such that - all the elements smaller than the pivot are moved before it - all the elements larger than the pivot are moved after it • Then Quicksort is called recursively for these two parts.
Quicksort - Algorithm Quicksort (A[L. . R]) if L < R then pivot = Partition( A[L. . R]) Quicksort (A[1. . L-1]) Quicksort (A[L+1. . . R)
Partition Algorithm Partition (A[L. . R]) p A[L]; i L; j R+1 while (i < j) do { repeat i i + 1 until A[i] ≥ p repeat j j - 1 until A[j] ≤ p if (i < j) then swap(A[i], A[j]) } swap(A[j], A[L]) return j
Example A [3 8 7 1 5 2 6 4]
Complexity Analysis • Best Case: every partition splits half of the array O(n log 2 n) • Worst Case: one array is empty; one has all elements O(n 2 ) • Average case: O(n log 2 n)
Binary Search Binary. Search(A[1. . n], el) Input: A[1. . n] sorted in ascending order Output: The position i such that A[i] = el or -1 if el is not in A[1. . n]
- Slides: 12