Download presentation
Presentation is loading. Please wait.
Published byHector Kennedy Modified over 9 years ago
2
Decision Problems Optimization problems : minimum, maximum, smallest, largest Satisfaction (SAT) problems : Traveling salesman, Clique, Vertex-Cover, Independent Set, Knapsack 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
3
Computing a n Brute Force: compute(a, n) { ans 1 for i 1 to n do ans ans * a return ans } Complexity: O(n) Divide and Conquer: compute(a, n) { if (n=0) then return 1 m floor(n/2) return compute(a,m) × compute(a, n – m) } T(n) = 2T(n/2) +1 T(1) = 1 Complexity: O(n) Derek Drake’s: compute(a, n) { if (n=0) then return 1 m floor(n/2) s compute(a,m) if ( n is even) then return s * s if ( n is odd) then return a * s * s } T(n) = T(n/2) +3 T(1) = 1 Complexity: O(log 2 n) Reduce and Conquer:
4
Solving Recurrence Relations T(n) = 4T(n/2) + n T(1) = 1 O(n 2 ) T(n) = 4T(n/2) + n 2 T(1) = 1 O(n 2 log 2 n) 4 k + 2 k-1 n + … + 2n + n with n = 2 k 4 k + kn 2 with n = 2 k
5
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
6
Quicksort
7
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.
8
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)
9
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
10
Example A [3 8 7 1 5 2 6 4]
11
Complexity Analysis Best Case: every partition splits half of the array Worst Case: one array is empty; one has all elements Average case: O(n log 2 n) O(n 2 ) O(n log 2 n)
12
Binary Search BinarySearch(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]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.