Analysis of Algorithms CS 477/677

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)
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
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
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
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.
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
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 15.
Analysis of Algorithms CS 477/677
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
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.
Analysis of Algorithms CS 477/677
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,
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 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Analysis of Algorithms CS 477/677
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Binary Search Trees A binary search tree is a binary tree
Chapter 4 Divide-and-Conquer
Heaps, Heap Sort and Priority Queues
Randomized Algorithms
Augmenting Data Structures
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Chapter 4: Divide and Conquer
CS 583 Analysis of Algorithms
Ch 6: Heapsort Ming-Te Chi
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Randomized Algorithms
Data Structures Review Session
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Design and Analysis of Algorithms
CS 583 Analysis of Algorithms
Divide and Conquer (Merge Sort)
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Topic 5: Heap data structure heap sort Priority queue
Solving Recurrences Continued The Master Theorem
Analysis of Algorithms
Algorithms Recurrences.
The Selection Problem.
Design and Analysis of Algorithms
Quicksort Quick sort Correctness of partition - loop invariant
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

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

Midterm Exam Tuesday, October 16 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, including red-black trees, basic coverage of OS-TREES, Interval trees CS 477/677 - Lecture 13

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 13

Analyzing Algorithms Alg.: MIN (a[1], …, a[n]) Running time: 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 CS 477/677 - Lecture 13

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) CS 477/677 - Lecture 13

Exercise Order the following 6 functions in increasing order of their growth rates: nlogn, log2n, n2, 2n, , n. log2n n nlogn n2 2n CS 477/677 - Lecture 13

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

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

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 CS 477/677 - Lecture 13

Example Recurrences T(n) = T(n-1) + n Θ(n2) T(n) = T(n/2) + c Θ(lgn) 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 CS 477/677 - Lecture 13

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 CS 477/677 - Lecture 13

Master’s method Used for solving recurrences of the form: where, a ≥ 1, b > 1, and f(n) > 0 Compare f(n) with nlogba: Case 1: if f(n) = O(nlogba - 𝛆) for some 𝛆 > 0, then: T(n) = Θ(nlogba) Case 2: if f(n) = Θ(nlogba), then: T(n) = Θ(nlogba lgn) Case 3: if f(n) = 𝝮(nlogba +𝛆) 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 CS 477/677 - Lecture 13

Problem 1 A: , ⇒ (case 1) ⇒ B: , ⇒ (case 2) ⇒ Two different divide-and-conquer algorithms A and B have been designed for solving the problem P. A partitions P into 4 subproblems each of size n/2, where n is the input size for P, and it takes a total of Θ(n1.5) time for the partition and combine steps. B partitions P 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) ⇒ CS 477/677 - Lecture 13

Sorting Insertion sort Bubble Sort Design approach: Sorts in place: Best case: Worst case: n2 comparisons, n2 exchanges Bubble Sort Running time: incremental Yes Θ(n) Θ(n2) incremental Yes Θ(n2) CS 477/677 - Lecture 13

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

Quicksort Quicksort Partition Randomized Quicksort Idea: Design approach: Sorts in place: Best case: Worst case: Partition Running time Randomized Quicksort 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. Divide and conquer Yes Θ(nlgn) Θ(n2) Θ(n) Θ(nlgn) – on average Θ(n2) – in the worst case CS 477/677 - Lecture 13

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 CS 477/677 - Lecture 13

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

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 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 O(n) CS 477/677 - Lecture 13

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 CS 477/677 - Lecture 13

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? 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 CS 477/677 - Lecture 13

Questions What is the maximum number of nodes possible in a binary search tree of height h? - max number reached when all levels are full CS 477/677 - Lecture 13

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; CS 477/677 - Lecture 13

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

Order-Statistic Tree size[x] = size[left[x]] + size[right[x]] + 1 7 10 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) 7 10 3 8 15 1 4 9 11 19 size[x] = size[left[x]] + size[right[x]] + 1 CS 477/677 - Lecture 13

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 CS 477/677 - Lecture 13

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] + 1 CS 477/677 - Lecture 13

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

Question TRUE FALSE 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 CS 477/677 - Lecture 13

Readings Chapters 14, 15 CS 477/677 - Lecture 13