Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.

Slides:



Advertisements
Similar presentations
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Advertisements

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Introduction to Algorithms
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer The most-well known algorithm design strategy:
CS4413 Divide-and-Conquer
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Using Divide and Conquer for Sorting
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Spring 2015 Lecture 5: QuickSort & Selection
CS 473Lecture X1 CS473-Algorithms I Lecture X Augmenting Data Structures.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
The Complexity of Algorithms and the Lower Bounds of Problems
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Merge Sort Solving Recurrences The Master Theorem.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
1 QuickSort Worst time:  (n 2 ) Expected time:  (nlgn) – Constants in the expected time are small Sorts in place.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Computer Algorithms Lecture 10 Quicksort Ch. 7 Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CS 477/677
Order Statistics ● The ith order statistic in a set of n elements is the ith smallest element ● The minimum is thus the 1st order statistic ● The maximum.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
CPSC 311 Section 502 Analysis of Algorithm Fall 2002 Department of Computer Science Texas A&M University.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
Analysis of Algorithms CS 477/677 Lecture 8 Instructor: Monica Nicolescu.
David Luebke 1 6/26/2016 CS 332: Algorithms Linear-Time Sorting Continued Medians and Order Statistics.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Analysis of Algorithms CS 477/677
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Data Structures Review Session
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Analysis of Algorithms
Algorithms Recurrences.
The Selection Problem.
Analysis of Algorithms CS 477/677
Presentation transcript:

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15

CS 477/677 - Lecture 15 Midterm Exam Tuesday, March 24 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed in class –homework-like problems All topics discussed so far, up to dynamic programming 2

General Advice for Study Understand how the algorithms are working –Work through the examples we did in class –“Narrate” for yourselves the main steps of the algorithms in a few sentences Know when or for what problems the algorithms are applicable Do not memorize algorithms CS 477/677 - Lecture 153

Analyzing Algorithms Alg.: MIN ( a[1], …, a[n] ) m ← a[1]; for i ← 2 to n if a[i] < m then m ← a[i]; Running time: –the number of primitive operations (steps) executed before termination T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] + (n-1) [the assignment in then] = 3n - 1 Order (rate) of growth: –The leading term of the formula –Expresses the asymptotic behavior of the algorithm T(n) grows like n 4

CS 477/677 - Lecture 15 Asymptotic Notations A way to describe behavior of functions in the limit –Abstracts away low-order terms and constant factors –How we indicate running times of algorithms –Describe the running time of an algorithm as n grows to  O notation: asymptotic “less than”: f(n) “≤” g(n)  notation: asymptotic “greater than”: f(n) “≥” g(n)  notation: asymptotic “equality”: f(n) “=” g(n) 5

CS 477/677 - Lecture 15 Exercise Order the following 6 functions in increasing order of their growth rates: –nlogn, log 2 n, n 2, 2 n,, n. log 2 n n nlogn n2n2 2n2n 6

CS 477/677 - Lecture 15 Running Time Analysis Algorithm Loop2(n) p=1 for i = 1 to 2n p = p*i Algorithm Loop3(n) p=1 for i = 1 to n 2 p = p*i O(n) O(n 2 ) 7

CS 477/677 - Lecture 15 Running Time Analysis Algorithm Loop4(n) s=0 for i = 1 to 2n for j = 1 to i s = s + i O(n 2 ) 8

CS 477/677 - Lecture 15 Recurrences Def.: Recurrence = an equation or inequality that describes a function in terms of its value on smaller inputs, and one or more base cases Recurrences arise when an algorithm contains recursive calls to itself Methods for solving recurrences –Substitution method –Iteration method –Recursion tree method –Master method Unless explicitly stated choose the simplest method for solving recurrences 9

CS 477/677 - Lecture 15 Example Recurrences T(n) = T(n-1) + nΘ(n 2 ) –Recursive algorithm that loops through the input to eliminate one item T(n) = T(n/2) + cΘ(lgn) –Recursive algorithm that halves the input in one step T(n) = T(n/2) + nΘ(n) –Recursive algorithm that halves the input but must examine every item in the input T(n) = 2T(n/2) + 1Θ(n) –Recursive algorithm that splits the input into 2 halves and does a constant amount of other work 10

CS 477/677 - Lecture 15 Analyzing Divide and Conquer Algorithms The recurrence is based on the three steps of the paradigm: –T(n) = running time on a problem of size n –Divide the problem into a subproblems, each of size n/b : takes –Conquer (solve) the subproblems: takes –Combine the solutions: takes  (1) if n ≤ c T(n) = D(n) aT(n/b) C(n) aT(n/b) + D(n) + C(n) otherwise 11

CS 477/677 - Lecture 15 Master’s method Used for solving recurrences of the form: where, a ≥ 1, b > 1, and f(n) > 0 Compare f(n) with n log b a : Case 1: if f(n) = O( n log b a -  ) for some  > 0, then: T(n) =  ( n log b a ) Case 2: if f(n) =  ( n log b a ), then: T(n) =  ( n log b a lgn ) Case 3: if f(n) =  ( n log b a +  ) for some  > 0, and if af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then: T(n) =  (f(n)) regularity condition 12

CS 477/677 - Lecture 15 Problem 1 Two different divide-and-conquer algorithms A and B have been designed for solving the problem . A partitions  into 4 subproblems each of size n/2, where n is the input size for , and it takes a total of  (n 1.5 ) time for the partition and combine steps. B partitions  into 4 subproblems each of size n/4, and it takes a total of  (n) time for the partition and combine steps. Which algorithm is preferable? Why? A:,  (case 1)  B:,  (case 2)  13

CS 477/677 - Lecture 15 Sorting Insertion sort –Design approach: –Sorts in place: –Best case: –Worst case: –n 2 comparisons, n 2 exchanges Bubble Sort –Design approach: –Sorts in place: –Running time: –n 2 comparisons, n 2 exchanges Yes  (n)  (n 2 ) incremental Yes  (n 2 ) incremental 14

CS 477/677 - Lecture 15 Sorting Selection sort –Design approach: –Sorts in place: –Running time: –n 2 comparisons, n exchanges Merge Sort –Design approach: –Sorts in place: –Running time: Yes  (n 2 ) incremental No  (nlgn) divide and conquer 15

CS 477/677 - Lecture 15 Quicksort –Idea: –Design approach: –Sorts in place: –Best case: –Worst case: Partition –Running time Randomized Quicksort Yes  (nlgn)  (n 2 ) Divide and conquer  (n) Partition the array A into 2 subarrays A[p..q] and A[q+1..r], such that each element of A[p..q] is smaller than or equal to each element in A[q+1..r]. Then sort the subarrays recursively.  (nlgn) – on average  (n 2 ) – in the worst case 16

CS 477/677 - Lecture 15 Randomized Algorithms The behavior is determined in part by values produced by a random-number generator –RANDOM (a, b) returns an integer r, where a ≤ r ≤ b and each of the b-a+1 possible values of r is equally likely Algorithm generates randomness in input No input can consistently elicit worst case behavior –Worst case occurs only if we get “unlucky” numbers from the random number generator 17

CS 477/677 - Lecture 15 Problem a) TRUEFALSE Worst case time complexity of QuickSort is  (nlgn). b) TRUE FALSE If and, then c) TRUEFALSE If and, then 18

CS 477/677 - Lecture 15 Problem Consider a modification to MERGE-SORT in which n/k sublists of length k are sorted using INSERTION-SORT and then merged using the standard merging mechanism. How long will it take, in the worst case, to sort the n/k sublists, each of size k, assuming the above modification? –Insertion sort takes  (k 2 ) time per k -element list in the worst case –Sorting n/k lists of k elements each takes  (k 2 n/k) =  (nk) worst-case time 19

CS 477/677 - Lecture 15 Medians and Order Statistics General Selection Problem: –select the i -th smallest element from a set of n distinct numbers Algorithms: Randomized select –Idea –Worst-case O(n) Partition the input array similarly with the approach used for Quicksort (use RANDOMIZED-PARTITION) Recurse on one side of the partition to look for the i-th element depending on where i is with respect to the pivot 20

CS 477/677 - Lecture 15 Problem a) What is the difference between the MAX-HEAP property and the binary search tree property? –The MAX-HEAP property states that a node in the heap is greater than or equal to both of its children –the binary search property states that a node in a tree is greater than or equal to the nodes in its left subtree and smaller than or equal to the nodes in its right subtree b) What is the lowest possible bound on comparison-based sorting algorithms? –nlgn c) Assuming the elements in a max-heap are distinct, what are the possible locations of the second-largest element? –The second largest element has to be a child of the root 21

CS 477/677 - Lecture 15 Questions What is the effect of calling MAX- HEAPIFY(A, i) when: –The element A[i] is larger than its children? Nothing happens –i > heap-size[A]/2 ? Nothing happens Can the min-heap property be used to print out the keys of an n -node heap in sorted order in O(n) time? –No, it doesn’t tell which subtree of a node contains the element to print before that node –In a heap, the largest element smaller than the node could be in either subtree 22

CS 477/677 - Lecture 15 Questions a) TRUEFALSE A reverse sorted array is always a max heap. b) What is the maximum number of nodes possible in a binary search tree of height h? - max number reached when all levels are full 23

CS 477/677 - Lecture 15 Problem Let x be the root node of a binary search tree (BST). Write an algorithm BSTHeight(x) that determines the height of the tree. Alg: BSTHeight(x) if (x==NULL) return -1; else return max (BSTHeight(left[x]), BSTHeight(right[x]))+1; 24

CS 477/677 - Lecture 15 Exercise We can sort a given set of n numbers by first building a binary search tree containing these numbers and then printing the numbers by an inorder tree walk. What are the worst-case and the best-case running times for this sorting algorithm? Alg.: TREE-SORT(A) let T be an empty binary search tree for i ← 1 to n do TREE-INSERT(T, A[i]) INORDER-TREE-WALK(root[T]) Worst-case: nodes are inserted in a linear chain:  (n 2 ) Best-case: tree-insert gives a balanced tree of height lgn :  (nlgn) 25

CS 477/677 - Lecture 15 Exercise In a binary search tree, are the insert and delete operations commutative? (deleting x and then y leaves the tree the same as deleting y and then x ) Insert: –Try to insert 4 followed by 6, then insert 6 followed by 4 –Inserts do not commute Delete –Delete 5 followed by 6, then 6 followed by 5 in the following tree –Deletes do not commute

CS 477/677 - Lecture 15 Red-Black Trees Properties Binary search trees with additional properties: 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to leaves contain the same number of black nodes 27

CS 477/677 - Lecture 15 Order-Statistic Tree Def.: Order-statistic tree: a red-black tree with additional information stored in each node size[x] contains the number of (internal) nodes in the subtree rooted at x (including x itself) size[x] = size[left[x]] + size[right[x]]

CS 477/677 - Lecture 15 Operations on Order-Statistic Trees OS-SELECT –Given an order-statistic tree, return a pointer to the node containing the i- th smallest key in the subtree rooted at x –Running time O(lgn) OS-RANK –Given a pointer to a node x in an order-statistic tree, return the rank of x in the linear order determined by an inorder walk of T –Running time O(lgn) 29

CS 477/677 - Lecture 15 Exercise In an OS-tree, the size field can be used to compute the rank’ of a node x, in the subtree for which x is the root. If we want to store this rank in each of the nodes, show how can we maintain this information during insertion and deletion Insertion add 1 to rank’[x] if z is inserted within x ’s left subtree leave rank’[x] unchanged if z is inserted within x ’s right subtree Deletion subtract 1 from rank’[x] whenever the deleted node y had been in x ’s left subtree rank’[x] = size[left]

CS 477/677 - Lecture 15 Exercise (cont.) We also need to handle the rotations that occur during insertion and deletion rank’(x) = r x rank’(y) = r y rank’(x) = r x rank’(y) = r y + rank’(x) 31

CS 477/677 - Lecture 15 Problem d) TRUE FALSE The sequence  23, 17, 14, 6, 13, 10, 1, 5, 7, 12  is a heap. 7, which is the right child of 6, is greater than its parent. e) TRUEFALSE The depths of nodes in a red-black tree can be efficiently maintained as fields in the nodes of the tree. No, because the depth of a node depends on the depth of its parent When the depth of a node changes, the depths of all nodes below it in the tree must be updated Updating the root node causes n - 1 other nodes to be updated 32

CS 477/677 - Lecture 15 Readings Chapters 1-4, 6-9,