Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.

Similar presentations


Presentation on theme: "CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University."— Presentation transcript:

1 CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University

2 CPSC 311 O. Burchan Bayazit – Fall 02 Announcements There will be a quiz on next Tuesday (Sep 17) A new homework will be put to the web site today ( due date next Thursday (Sep 19)) Grades for the previous quiz is available

3 CPSC 311 O. Burchan Bayazit – Fall 02 Solving Recurrences: Master Method The master method provides a 'cookbook' method for solving recurrences of a certain form. Master Theorem: Let a  1 and b > 1 be constants, let f(n) be a function, and let T(n) be defined on nonnegative integers as: T(n) = aT(n/b) + f(n) Then, T(n) can be bounded asymptotically as follows: 1.T(n) =  (n log b a )if f(n) = O(n log b a-  ) for some constant  > 0 2.T(n) =  (n log b a logn) if f(n) =  (n log b a ) 3.T(n) =  ( f(n))if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n.

4 CPSC 311 O. Burchan Bayazit – Fall 02 Case 3 T(n) =  ( f(n)), if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n. T(n) = aT(n/b) + f(n) T(n) =3T(n/4)+nlgn a=3,b=4,f(n)=nlgn { PART I: Is f(n) =  (n log b a+  ) for some  >0? n log b a+  = n log 4 3+  =n 0.79+  f(n)=nlgn can we find  > 0 such that nlgn=  (n 0.79+  ) If we choose  =1-0.79 the lower bound becomes  (n ) And nlgn has a lower bound n i.e., f(nlgn)=  (n ) So the first part holds.

5 CPSC 311 O. Burchan Bayazit – Fall 02 Case 3 T(n) =  ( f(n)), if f(n) =  (n log b a+  ) for some constant  > 0 and if af(n/b)  cf(n) for some constant c < 1 and all sufficiently large n. T(n) = aT(n/b) + f(n) T(n) =3T(n/4)+nlgn a=3,b=4,f(n)=nlgn { Part II: is there a constant <1 that satisfies af(n/b)  cf(n) ? af(n/b) = 3f(n/4) So we should find a c such 3f(n/4) 1 3f(n/4)= 3(n/4)lg(n/4) and f(n)=nlgn (3/4)n lg(n)-(3/4)lg4 <=c.nlgn If we select c=3/4 this inequality holds so T(n)=  (nlgn)

6 CPSC 311 O. Burchan Bayazit – Fall 02 Sorting Algorithms Ch. 6 - 8 Slightly modified definition of the sorting problem: input: A collection of n data items where each data item has a key drawn from a linearly ordered set (e.g., ints, chars) output: A permutation (reordering) of the input sequence such that a' 1  a' 2 ...  a' n In practice, one usually sorts 'records' according to their key (the non-key data is called satellite data.) If the records are large, we may sort an array of pointers.

7 CPSC 311 O. Burchan Bayazit – Fall 02 Sorting Algorithms A sorting algorithm is in place if only a constant number of elements of the input array are ever stored outside the array. A sorting algorithm is comparison based if the only operation we can perform on keys is to compare two keys.

8 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort A Divide-and-Conquer algorithm Divide: the n-element array to be sorted into two n/2-element subarrays Conquer: Sort the subarrays recursively using merge sort Combine: Merge the two sorted subarrays to find sorted array

9 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort 52461387 Unsorted original array

10 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Divide the problem to two subproblems 52461387 Call merge sort on subarrays 52461387

11 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further divide subarrays 52461387 52461387

12 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Until the smallest array is reached 52461387 52461387

13 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Merge results of two subarrays 2546137852461387 52461387

14 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge 25461378 52461387

15 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge 2456137825461378 52461387

16 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort Further merge 24561378 52461387

17 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort 12345678 Final merge 24561378 52461387

18 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort 12345678 Result 52461387

19 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort - Algorithm Merge-Sort(A,start,end) 1. if start=end then { 2. return 3. } 4. half =  (start+end)/2  5. Merge-Sort (A,start,half) 6. Merge-Sort (A,half+1,end) 7. Merge (A,start,half,end)

20 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 24561378

21 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 2456378 1

22 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 45678 12 3

23 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 45678 123

24 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 5678 1234

25 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 678 12345

26 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 78 123456

27 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 8 1234567

28 CPSC 311 O. Burchan Bayazit – Fall 02 Merging 12345678

29 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Algorithm Merge(A,start,half,end) 1. n 1 = half-start+1; n 2  end-half; 2. for i= 1 to n 1 { 3. Left[i]= A[start+i-1] // copy left subarray 4. } 5. for i= 1 to n 2 { 6. R[i]  A[half+i] // copy right subarray 7. } 8. Left[n 1 +1] = Right[n 2 +1] =  9. i= j= 1 10. for k = start to end { 11. if Left[i]  Right[j] then { 12. A[k]=Left[i] 13. i= i+1 } 14. else { A[k]= Right[j] 15. j= j+1 } 16. } Running Time =  (n)

30 CPSC 311 O. Burchan Bayazit – Fall 02 Merge Sort - Analysis Merge-Sort(A,start,end) 1. if start=end then { 2. return 3. } 4. half =  (start+end)/2  5. Merge-Sort (A,start,half) 6. Merge-Sort (A,half+1,end) 7. Merge (A,start,half,end)  (1) T (n/2)  (n) T (n)=2T(n/2)+  (n)  (nlgn) using master method

31 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort heap concept complete binary tree, except may be missing some rightmost leaves on the bottom level. each node contains a key values in the nodes satisfy the heap property.

32 CPSC 311 O. Burchan Bayazit – Fall 02 Heap vs. Array 1 23 4 8 9 765 10 123456789 binary tree: an array implementation root is A[1] for element A[i] - left child is in position A[2i] - right child is in position A[2i + 1] - parent is in A[  i/2  ] Left child=2i i=2 Right child=2i+1 Parent =i/2

33 CPSC 311 O. Burchan Bayazit – Fall 02 Max-Heap Max-heap property: A[Parent(i)]  A[i] In the array representation of a max-heap, the root of the tree is in A[1], and given the index i of a node, Parent(i) LeftChild(i)RightChild(i) return (  i/2  ) return (2i) return (2i + 1) 20 18 15 11 10 9 6 2 4 5 3 1 2 3 4 5 6 7 8 9 10 11 A index keys 20 18 15 1110 9 6 2 4 5 3 1 23 4 8 9 765 1110 n = 11 height = 3 (# edges on longest leaf to root path)

34 CPSC 311 O. Burchan Bayazit – Fall 02 Min-Heap Min-heap property: A[Parent(i)]  A[i] Min-heaps are commonly used for priority queues in event-driven simulators. Parent(i) LeftChild(i)RightChild(i) return (  i/2  ) return (2i) return (2i + 1) 2 3 5 4 6 9 11 10 15 20 18 1 2 3 4 5 6 7 8 9 10 11 A index keys 2 3 5 4 6 9 11 10 152018 1 23 4 8 9 765 1110 n = 11 height = 3 (# edges on longest leaf to root path)

35 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Assume we have a max-heap 1 2 3 45 6 18 11 10 2 4 5

36 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 The largest number is the root (max-heap property)

37 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Take the root away

38 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Put the last one to the root

39 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Fix the max-heap

40 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Fix the max-heap

41 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Take the root away

42 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Put the last one to the root

43 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Fix the max-heap

44 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Take the root away

45 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Put the last one to the root

46 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 24 5 10 11 Fix the max-heap

47 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 24 5 10 11 Take the root away

48 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 2 4 5 10 11 Put the last one to the top

49 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 2 45 10 11 Take the top away

50 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 245 10 11 Put the last one

51 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort Basic Functions HEAPIFY: maintain (fix) max-heap property BUILD-HEAP: Create a max-heap from unsorted array (requires HEAPIFY) HEAPSORT: sorts an array (requires BUILD-HEAP and HEAPIFY) How it works? Build a max-heap (the root is the largest number) Take the largest number away, put the last number in the heap to the root position and if max-heap property is not satisfied, fix it. Repeat until all the elements are processed.

52 CPSC 311 O. Burchan Bayazit – Fall 02 Heapify (fix max-heap) Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies. 20 2 15 1011 9 6 2 8 5 3 Not satisfy max-heap

53 CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies. 20 11 15 102 9 6 2 8 5 3 Switch with the largest child Heapify (fix max-heap)

54 CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies. 20 11 15 102 9 6 2 8 5 3 Not satisfy max-heap Heapify (fix max-heap)

55 CPSC 311 O. Burchan Bayazit – Fall 02 Assume one node i does not satisfy MAX-HEAP property but its children are. Heapify function corrects this by recursively fixing inconsistencies. 20 11 15 105 9 6 2 8 2 3 Switch with the largest child Heapify (fix max-heap)

56 CPSC 311 O. Burchan Bayazit – Fall 02 Heapify (fix max-heap) Find the largest child Switch the node i with its largest child Recursively HEAPIFY for the subtree where node i placed

57 CPSC 311 O. Burchan Bayazit – Fall 02 Heapify(A,i,n) /* A: Array, I: node to be fixed, n:heap-size */ 1. left  2i; right  2i + 1 /*indices of left & right children of A[i] */ 2. largest  i; 3. if left  n and A[left] > A[i] then { 4.largest  left 5.} 5. if right  n and A[right] > A[largest] then { 6.largest  right 7.} 7. if largest  i then 8. swap(A[i], A[largest]) 9. Heapify(A, largest,n) T (n)=T(2n/3)+  (1) O (lgn) using master method Heapify (fix max-heap)

58 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Given unsorted array Starting from largest non-leaf node down to the first node, call Heapify for each node 1 2 3 45 6

59 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 4685 10 918 7 8 3 2 4 6 8 5 10 9 18 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

60 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 4685 10 918 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

61 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 41885 10 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

62 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 41885 10 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

63 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 101885 4 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

64 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 2 101885 4 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

65 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 8 1018 25 4 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

66 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 3 8 1018 25 4 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

67 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 18 8 103 25 4 96 7 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

68 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 18 8 107 25 4 96 3 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

69 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 8 18 8 107 25 4 96 3 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

70 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 18 8 8 107 25 4 96 3 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

71 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 18 10 8 87 25 4 96 3 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

72 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap 18 10 8 97 25 486 3 Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.}

73 CPSC 311 O. Burchan Bayazit – Fall 02 Build-Heap Build-Max-Heap(A,n) 1. for i   n/2  downto 1 { 2.Heapify(A, i,n) 3.} O(lgn) repeated (n/2 times) T(n)=O(nlgn) Can we do better? Yes T(n)=O(n)

74 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort Algorithm HeapSort(A,n) 1. Build-Heap(A,n) /* put all elements in heap */ 2. for i = n downto 2 { 3. swap A[1]  A[i] /* puts max in ith array position */ 4.Heapify(A,1,i-1) /* restore heap property */ 5.} Input: An n-element array A (unsorted). Output: An n-element array A in sorted order, smallest to largest. Running time of HeapSort 1 call to Build-Heap()  O(n) time n-1 calls to Heapify() each takes O(lgn) time  O(nlgn) time

75 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Assume we have a max-heap 1 2 3 45 6 18 11 10 2 4 5

76 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 The largest number is the root (max-heap property) 18 11 10 2 4 5

77 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Take the root away 18 11 10 2 4 5

78 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Put the last one to the root 18 11 10 2 4 5

79 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 1110 2 4 5 Fix the max-heap 18 11 10 2 4 5

80 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Fix the max-heap 18 11 10 2 4 5

81 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Take the root away 18 11 10 2 4 5

82 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 510 2 4 11 Put the last one to the root 18 11 10 2 4 5

83 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Fix the max-heap 18 11 10 2 4 5

84 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Take the root away 18 11 10 2 4 5

85 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 54 2 10 11 Put the last one to the root 18 11 10 2 4 5

86 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 24 5 10 11 Fix the max-heap 18 11 10 2 4 5

87 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 24 5 10 11 Take the root away 18 11 10 2 4 5

88 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 2 4 5 10 11 Put the last one to the top 18 11 10 2 4 5

89 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 2 45 10 11 Take the top away 18 11 10 2 4 5

90 CPSC 311 O. Burchan Bayazit – Fall 02 Heap Sort – How it works 18 245 10 11 Put the last one 18 11 10 2 4 5

91 CPSC 311 O. Burchan Bayazit – Fall 02 Priority Queues: Application for Heaps An application of max-priority queues is to schedule jobs on a shared processor. Need to be able to get the highest priority job Heap-Maximum(A,n) remove job from the queue Heap-Extract-Max(A,n) insert new jobs into queue Max-Heap-Insert(A, key,n) increase priority of jobs Heap-Increase-Key(A,i,key,n)

92 CPSC 311 O. Burchan Bayazit – Fall 02 Heap-Extract-Max Heap-Extract-Max(A,n) 1. max=A[1] 2. A[1]=A[n] 3. n=n-1 /* puts max in ith array position */ 4. Heapify(A,1,n) /* restore heap property */ 5. return max T(n)=O(lgn)

93 CPSC 311 O. Burchan Bayazit – Fall 02 Heap-Increase-Key Heap-Increase-Key(A,i,key,n) 1. A[i]=key 2. while I>1 and A[Parent[i]]<A[i] { 3. swap A[Parent[i]]  A[i] 4. i=Parent[i] T(n)=O(lgn)

94 CPSC 311 O. Burchan Bayazit – Fall 02 Max-Heap-Insert Max-Heap-Insert(A,i,key,n) 1. n=n+1 2. A[i]=-  3. Heap-Increase-Key(A,n,key,n) T(n)=O(lgn)


Download ppt "CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University."

Similar presentations


Ads by Google