CSC 421: Algorithm Design & Analysis

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
Advanced Data Structures
Chapter 5: Decrease and Conquer
CS4413 Divide-and-Conquer
Divide And Conquer Distinguish between small and large instances. Small instances solved differently from large ones.
ISOM MIS 215 Module 7 – Sorting. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
Updated QuickSort Problem From a given set of n integers, find the missing integer from 0 to n using O(n) queries of type: “what is bit[j]
Chapter 19: Searching and Sorting Algorithms
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
(c) , University of Washington
Chapter 19 - basic definitions - order statistics ( findkth( ) ) - balanced binary search trees - Java implementations Binary Search Trees 1CSCI 3333 Data.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Chapter 12 Recursion, Complexity, and Searching and Sorting
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
1 CSC 427: Data Structures and Algorithm Analysis Fall 2008 Algorithm analysis, searching and sorting  best vs. average vs. worst case analysis  big-Oh.
Lecture 13 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
1 CSC 421: Algorithm Design & Analysis Spring 2013 Divide & conquer  divide-and-conquer approach  familiar examples: merge sort, quick sort  other examples:
Sorting Chapter Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order –Ascending or descending Some O(n.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 7.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching Topics Sequential Search Binary Search.
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
MA/CSSE 473 Day 14 Strassen's Algorithm: Matrix Multiplication Decrease and Conquer DFS.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
Recursion Powerful Tool
Searching and Sorting Algorithms
CSC 427: Data Structures and Algorithm Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Algorithm Design Methods
Chapter 4.
CSC 421: Algorithm Design & Analysis
CSC 427: Data Structures and Algorithm Analysis
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
CSC 143 Java Sorting.
CSC 380: Design and Analysis of Algorithms
CSC 421: Algorithm Design & Analysis
CS203 Lecture 15.
Presentation transcript:

CSC 421: Algorithm Design & Analysis Spring 2014 Decrease & conquer decrease by constant sequential search, insertion sort, topological sort decrease by constant factor binary search, fake coin problem decrease by variable amount BST search, selection problem

Decrease & Conquer based on exploiting the relationship between a solution to a given instance of a problem and a solution to a smaller instance once the relationship is established, it can be exploited either top-down or bottom-up EXAMPLE: sequential search of N-item list checks the first item, then searches the remaining sublist of N-1 items EXAMPLE: binary search of sorted N-item list checks the middle item, then searches the appropriate sublist of N/2 items 3 major variations of decrease & conquer decrease by a constant (e.g., sequential search decreases list size by 1) decrease by a constant factor (e.g., binary search decrease list size by factor of 2) decrease by a variable amount

Decrease by a constant decreasing the problem size by a constant (especially 1) is fairly common many iterative algorithms can be viewed this way e.g., sequential search, traversing a linked list many recursive algorithms also fit the pattern e.g., N! = (N-1)! * N aN = aN-1 * a EXAMPLE: insertion sort (decrease-by-constant description) to sort a list of N comparable items: sort the initial sublist of (N-1) items take the Nth item and insert it into the correct position

Top-down (recursive) definition can use recursion to implement this algorithm this a top-down approach, because it starts with the entire list & recursively works down to the base case

Bottom-up (iterative) definition for most decrease-by-constant algorithms top-down recursion is not necessary and in fact is less efficient instead, could use a loop to perform the same tasks in a bottom-up fashion

Big-Oh of insertion sort analyzing the recursive version cost function? worst case Big-Oh? analyzing the iterative version worst case Big-Oh?

Analysis of insertion sort insertion sort has advantages over other O(N2) sorts best case behavior of insertion sort? what is the best case scenario for a sort? does insertion sort take advantage of this scenario? does selection sort? what if a list is partially ordered? (a fairly common occurrence) does insertion sort take advantage?

Another example we considered the problem of topological sorting in 321 given a directed graph, find an ordering of the vertices such that if edge (vi, vj) is in the graph, then vi comes before vj in the ordering as long as there are no cycles in the graph, at least one (and possibly many) topological sorts exists C1, C2, C3, C4, C5 or C2, C1, C3, C4, C5 topological sorting is useful in many applications constructing a job schedule among interrelated tasks cell evaluation ordering in spreadsheet formulas power rankings of sports teams may sometimes generate a topological sort to verify that an ordering is possible, then invest resources into optimizing

DFS-based topological sort we considered an algorithm based on depth first search in 321 traverse the graph in a depth-first ordering (using a stack for reached vertices) when you reach a dead-end (i.e., pop a vertex off the stack), add it to a list finally, reverse the list C5 C4 C3 C3 C3 C3 C3 C1 C1 C1 C1 C1 C1 C1 C2 C5 C4 C3 C1 C2 C2 C1 C3 C4 C5

Decrease-by-1 topological sort alternatively, while the graph is nonempty identify a vertex with no incoming edges add the vertex to a list remove that vertex and all outgoing edges from it

Decrease by a constant factor a more efficient (but rare) variation of decrease & conquer occurs when you can decrease by an amount proportional to the problem size e.g. binary search  divides a problem of size N into a problem of size N/2 Cost(N) = Cost(N/2) + C can unwind Cost(N/2) = (Cost(N/4) + C) + C = Cost(N/4) + 2C can unwind Cost(N/4) = (Cost(N/8) + C) + 2C = Cost(N/8) + 3C can continue unwinding = … (a total of log2 N times) = Cost(1) + (log2N)*C = C log2 N + C' where C' = Cost(1)  O(log N)

Fake coin problem Given a scale and N coins, one of which is lighter than all the others. Identify the light coin using the minimal number of weighings. solution? cost function? big-Oh?

What doesn't fit here decrease by a constant factor is efficient, but very rare it is tempting to think of algorithms like merge sort & quick sort each divides the problem into subproblems whose size is proportional to the original key distinction: these algorithms require solving multiple subproblems, then somehow combining the results to solve the bigger problem we have a different name for these types of problems: divide & conquer NEXT WEEK

Decrease by a variable amount sometimes things are not so consistent the amount of the decrease may vary at each step, depending on the data EXAMPLE: searching/inserting in a binary search tree if the tree is full & balanced, then each check reduces the current tree into a subtree half the size however, if not full& balanced, the sizes of the subtrees can vary worst case: the larger subtree is selected at each check for a linear tree, this leads to O(N) searches/insertions in general, the worst case on each check is selecting the larger subtree recall: randomly added values produce enough balance to yield O(log N)

Selection problem suppose we want to determine the kth order statistic of a list i.e., find the kth largest element in the list of N items (special case, finding the median, is the N/2th order statistic) obviously, could sort the list then access the kth item directly O(N log N) could do k passes of selection sort (find 1st, 2nd, …, kth smallest items) O(k * N) or, we could utilize an algorithm similar to quick sort called quick select recall, quick sort works by: partitioning the list around a particular element (i.e., moving all items ≤ the pivot element to its left, all items > the pivot element to its right) recursively quick sorting each partition

Lomuto partition we will assume that the first item is always chosen as pivot value simple, but "better" strategies exist for choosing the pivot mark the first index as the pivot index traverse the list, for each item < the pivot value increment the pivot index & swap the item into that index finally, swap the pivot value into the pivot index 5 8 7 2 1 6 4 5 8 7 2 1 6 4 5 8 7 2 1 6 4 5 8 7 2 1 6 4 5 2 7 8 1 6 4 5 2 1 8 7 6 4 5 2 1 8 7 6 4 5 2 1 4 7 6 8 4 2 1 5 7 6 8

Lomuto partition implementation for both quick sort & quick select, we need to be able to partition a section of the list assume parameters left & right are the lower & upper indexes of the section to be partitioned

Quick select algorithm to find the kth value in a list: partition the list (using Lomuto's partitioning algorithm) let pivotIndex denote the index that separates the two partitions if k = pivotIndex+1, then return the value at pivotIndex if k < pivotIndex+1, then recursively search for the kth value in the left partition if k > pivotIndex+1, then recursively search for the (k – (pivotIndex+1))th value in the right partition 4 2 1 5 7 6 8 here, pivotIndex = 3 if k = 4, then answer is in index 3 = 5 if k = 2, then find 2nd value in if k = 5, then find 1st value in 4 2 1 7 6 8

Quick select implementation can be implemented recursively or iteratively recursive solution requires a private helper method that utilizes the left & right boundaries of the list section as with partition, the code must adjust for the changing boundaries

Efficiency of quick select analysis is similar to quick sort note that partitioning is O(N) if the partition is perfectly balanced: Cost(N) = Cost(N/2) + C1N + C2 can unwind Cost(N/2) = (Cost(N/4) + C1N/2 + C2) + C1N + C2 = Cost(N/4) + 3/2C1N + 2C2) can unwind Cost(N/4) = (Cost(N/8) + C1N/4 + C2) + 3/2C1N +2 C2 = Cost(N/8) + 7/4C1N + 3C2 can continue unwinding = … (a total of log2 N times) ≤ Cost(1) + 2C1N + (log2N)C2 note: 1 + 1/2 + 1/4 + 1/8 …  2 = 2C1 N + C2 log2N + C' where C' = Cost(1)  O(N) in the worst case, the pivot chosen is the smallest or largest value this reduces the algorithm to decrease-by-1  O(N2) as long as the partitioning is reasonably balanced, get O(N) behavior in practice there is a complex algorithm for choosing the pivot that guarantees linear performance