Presentation is loading. Please wait.

Presentation is loading. Please wait.

MS 101: Algorithms Instructor Neelima Gupta

Similar presentations


Presentation on theme: "MS 101: Algorithms Instructor Neelima Gupta"— Presentation transcript:

1 MS 101: Algorithms Instructor Neelima Gupta ngupta@cs.du.ac.in

2 Table Of Contents Introduction to some tools to designing algorithms through Sorting Iterative Divide and Conquer

3 Iterative Algorithms: Insertion Sort – an example x 1,x 2,........., x i-1,x i,.......…,x n For I = 2 to n Insert the ith element x i in the partially sorted list x 1,x 2,........., x i-1. (at r th position)

4 An Example: Insertion Sort 30104020 1234

5 An Example: Insertion Sort 10304020 1234

6 An Example: Insertion Sort 10304020 1234

7 An Example: Insertion Sort 103040 1234

8 An Example: Insertion Sort 1030 40 1234

9 An Example: Insertion Sort 10203040 1234

10 An Example: Insertion Sort 10203040 1234 Done!

11 Analysis: Insertion Sort InsertionSort(A, n) { for i = 2 to n { key = A[i] j = i - 1; while (j > 0) and (A[j] > key) { A[j+1] = A[j] j = j - 1 } A[j+1] = key } } How many times will this loop execute?

12 Insertion Sort Statement Effort InsertionSort(A, n) { for i = 2 to n { c 1 n – 1) key = A[i] c 2 (n-1) j = i - 1; c 3 (n-1) while (j > 0) and (A[j] > key) { c 4 T A[j+1] = A[j] c 5 (T-(n-1)) j = j - 1 c 6 (T-(n-1)) } 0 A[j+1] = key c 7 (n-1) } 0 } T = t 2 + t 3 + … + t n where t i is number of while expression evaluations for the i th for loop iteration

13 Analyzing Insertion Sort T(n) = c 1 n + c 2 (n-1) + c 3 (n-1) + c 4 T + c 5 (T - (n-1)) + c 6 (T - (n-1)) + c 7 (n-1) = c 8 T + c 9 n + c 10 What can T be? –Best case -- inner loop body never executed t i = 1  T(n) is a linear function –Worst case -- inner loop body executed for all previous elements t i = i  T(n) is a quadratic function –Average case ???

14 Analysis Simplifications –Ignore actual and abstract statement costs –Order of growth is the interesting measure: Highest-order term is what counts –Remember, we are doing asymptotic analysis –As the input size grows larger it is the high order term that dominates

15 Insertion Sort Is O(n 2 ) Proof –Suppose runtime is an 2 + bn + c If any of a, b, and c are less than 0 replace the constant with its absolute value –an 2 + bn + c  (a + b + c)n 2 + (a + b + c)n + (a + b + c) –  3(a + b + c)n 2 for n  1 –Let c’ = 3(a + b + c) and let n 0 = 1 Question –Is InsertionSort O(n 3 )? –Is InsertionSort O(n)?

16 Big O Fact A polynomial of degree k is O(n k ) Proof: –Suppose f(n) = b k n k + b k-1 n k-1 + … + b 1 n + b 0 Let a i = | b i | –f(n)  a k n k + a k-1 n k-1 + … + a 1 n + a 0

17 Lower Bound Notation We say InsertionSort’s run time is  (n) Proof: –Suppose run time is an + b Assume a and b are positive (what if b is negative?) –an  an + b

18 Analysis of Algorithms Before we move ahead, let us define the notion of analysis of algorithms more formally

19 Input Size Time and space complexity –This is generally a function of the input size –How we characterize input size depends: Sorting: number of input items Multiplication: total number of bits Graph algorithms: number of nodes & edges Etc

20 Cases to Analyze algorithms Worst case –Provides an upper bound on running time : The big O bound –An absolute guarantee Average case –Provides the expected running time –Very useful, but treat with care: what is “average”? Random (equally likely) inputs Real-life inputs Best Case –Minimum time the algorithm will take on any input: Omega bound. Will come back to this again later.

21 Avg. case Analysis of Insertion Sort Let x i be the random variable which represents the number of comparisons required to insert i th element of the input array in the sorted sub array of first i-1 elements. X : x 1,x 2,..................…,x n E(X) = Σx i p(x i ) where E(X) is expected value X And, p(x i ) is probability of inserting x i in the r th position 1≤r≤i

22 Avg. case Analysis of Insertion Sort Let x i be the random variable which represents the i th element of the input array that has to be inserted in the sorted sub array of sorted first i-1 elements. X : x 1,x 2,..................…,x n E(X) = Σx r p(x r ) where E(X) is expected value X And, p(x r ) is probability of inserting x i in the r th position 1≤r≤i

23 x 1,x 2,........., x i-1,x i,.......…,x n How many comparisons it makes to insert i th element in r th position? (at r th position)

24 Position# of Comparisions i1 i-12 i-23.. 2i-1 1i-1 Note: Here, both position 2 and 1 have # of Comparisions equal to i-1. Why? Because to insert element at position 2 we have to compare with previously first element. and after that comparison we know which of them come first and which at second.

25 For n number of elements, expected time taken is, T = n Σ i=2 (1/i) { i-1 Σ k=1 k + (i-1) } where 1/i is the probability to insert at r th position in the i possible positions. E(X 1 + X 2 +.............+X n ) = n Σ i=1 E(X i ) Where,Xi is expected value of inserting X i element. T = (n-1)(n-4) / 4 Therefore average case of insertion sort takes Θ(n 2 )

26 Divide and Conquer: Merge Sort – an example T(n) = 2T(n/2) +  (n) when n > 1 T(1) = 1 What is T(n)? MergeSort(A, left, right) {T(n) if (left < right) {  (1) mid = floor((left + right) / 2);  (1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right);  (n) }

27 What is the –Worst case –Average Case –Best Case for Merge Sort?

28 Merge Sort Vs Insertion Sort What is the advantage of merge sort? What is the advantage of insertion sort?

29 Merge Sort Vs Insertion Sort contd.. Merge Sort is faster but does not take advantage if the list is already partially sorted. Insertion Sort takes advantage if the list is already partially sorted.

30 Quick Sort Algorithm and Analysis to be skipped for MCS 101 Skip slides upto 21.

31 A Recursive Sorting Algorithm void quickSort(int a[], int first, int last) { if(first >= last) return; // Done: we have an empty array // The difficult algorithm is in partition int position = partition ( a, first, last ); // Recursively Quicksort left, then right quickSort(a, first, position-1); // sort left quickSort(a, position+1, last); // sort right }

32 Partitioning Pick one item from the array--call it the pivot Partition the items in the array around the pivot so all elements to the left are  to the pivot and all elements to the right are greater than the pivot Use recursion to sort the two partitions pivot partition: items > pivot partition 1: items  pivot

33 Before and After –Let's sort integers Pick the leftmost one (27) as the pivot value The array before call to partition(a, 0, n-1) Rearranged array after partition is done 27 14 9 22 8 41 56 31 14 53 99 11 2 24 24 14 9 22 8 14 11 2 27 53 99 56 31 41 pivotitems < pivotitems > pivot

34 Analyzing Quicksort Worst Case? –Partition is always unbalanced Worst Case input? –Already-sorted input Best case? –Partition is perfectly balanced

35 Worst Case of Quicksort In the worst case: T(1) =  (1) T(n) = T(n - 1) +  (n) Does the recursion look familiar? T(n) =  (n 2 )

36 Best Case of Quicksort In the best case: T(n) = 2T(n/2) +  (n) Does the recursion familiar? T(n) =  (n lg n)

37 Analyzing Quicksort: Average Case an intuitive explanation/example: –Suppose that partition() always produces a 9- to-1 split. This looks quite unbalanced! –The recurrence is: T(n) = T(9n/10) + T(n/10) + n –T(n) = θ (n log n) –Such an imbalanced partition and θ(n log n) time?

38 Analyzing Quicksort: Average Case Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits –Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 1) –What happens if we bad-split root node, then good-split the resulting size (n-1) node?

39 Analyzing Quicksort: Average Case Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits –Pretend for intuition that they alternate between best- case (n/2 : n/2) and worst-case (n-1 : 1) –What happens if we bad-split root node, then good- split the resulting size (n-1) node? We end up with three subarrays, size 1, (n-1)/2, (n-1)/2 Combined cost of splits = n + n -1 = 2n -1 = O(n) No worse than if we had good-split the root node!

40 Analyzing Quicksort: Average Case Intuitively, the O(n) cost of a bad split (or 2 or 3 bad splits) can be absorbed into the O(n) cost of each good split Thus running time of alternating bad and good splits is still O(n lg n), with slightly higher constants How can we be more rigorous?

41 Analyzing Quicksort: Average Case For simplicity, assume: –All inputs distinct (no repeats) –Slightly different partition() procedure partition around a random element, which is not included in subarrays all splits (0:n-1, 1:n-2, 2:n-3, …, n-1:0) equally likely What is the probability of a particular split happening? Answer: 1/n

42 Analyzing Quicksort: Average Case So partition generates splits (0:n-1, 1:n-2, 2:n-3, …, n-2:1, n-1:0) each with probability 1/n If T(n) is the expected running time, What is each term under the summation for? What is the  (n) term for?

43 Quicksort Vs Merge Sort Merge Sort takes O(n lg n) in the worst case Quick Sort takes O(n 2 ) in the worst case So why would anybody use Qsort instead of merge sort? –Because in practice, Qsort is quick as the worst case doesn’t happen often.

44 Heap What is a heap? Heap is a complete binary tree in which all the levels are completely filled except possibly the last.

45 Heap property In addition to above, a heap satisfies the following property: A[parent(i)] >= A[i] i.e the value at a child is no more than that of its parent.

46 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

47 Maintaining the heap property: Heapify If the heap property is violated at node I, it is restored as follows: Swap it with the largest of its children say C and, If the heap property at C is satisfied we are done else recursively maintain the property at C.

48 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

49 Time to heapify The time to heapify satisfies the following recurrence relation T(n) <= T(2n/3) + theta(1) since the children’s subtree has at most 2n/3 nodes, worst case occurring when the last level is exactly half full. Hence T(n) = O(height) of the tree = O(lg n)

50 Constructing a heap Insert all the elements in the array. For i = floor(n/2) down to 1 heapify at i

51 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

52 Time to construct heap First look : n/2 heapify => O(n lg n) More careful analysis: At most ceil(n/2 h+1 ) nodes at height h, therefore time is ∑ {h = 0 to lg n} ceil(n/2 h+1 ) O(h) = O(n)

53 Heapsort Construct the heap For I = n down to 1 Swap the root with i th element and heapify at the root leaving the i th element. Time: O(n) + O(n lg n) = O(lg n)

54 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

55 Heap as priorityQ Heapsort is a very good algorithm but a good implementation of Qsort beats it in practice. PriorityQ is a data structure that supports the following operations: –Insert(S,x) –Max(S) –ExtractMax(S)

56 Insert Insert at the end and move up the heap until the heap property is satisfied. Time = O(height) = O(lg n)

57 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

58 ExtractMax Max(S) is straight forward and is O(1) ExtractMax: Return the value at the root Swap it with the last element and heapify at the root leaving the last element. Time: O(height) =O(lg n)

59 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort

60 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √

61 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort

62 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort X

63 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort X –Heap Sort ? Find out yourself!

64 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort X –Heap Sort ? –Quick Sort

65 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort X –Heap Sort ? –Quick Sort - appears to be in-place but

66 In-place sorting A sorting algorithm is said to be in-place if it takes at most constant amount of extra space. Which of the algorithms you have studied are in-place? –Insertion Sort √ –Merge Sort X –Heap Sort ? –Quick Sort it is not

67 Reminder for Assignment 5 Show that Insertion sort works correctly by using Induction.

68 Assignment 6 Problem 2.38 of Sara Baase. Give linear time algorithm for the problem. Problem 2.19. For part (b) number of comparisons should be exactly 7.

69 Instructions for Minor If you’ll use more than one method to solve a problem, you will be evaluated out of half of the marks. For example, if you solve a Q of 4 marks in 2 ways, one of them I’ll strike off randomly and will evaluate out of 2 marks.

70 Stable sorting

71 Tools for designing algorithms Build up the solution Iteratively: –Example ? Divide and Conquer –Example ?

72 Tools for designing algorithms Build up the solution Iteratively: –Example : Insertion Sort Divide and Conquer –Example : Merge Sort and QSort Other tools to follow later

73 Up Next Lower Bounds Linear-Time Sorting Algorithms

74 The End


Download ppt "MS 101: Algorithms Instructor Neelima Gupta"

Similar presentations


Ads by Google