CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.

Slides:



Advertisements
Similar presentations
2. Getting Started Heejin Park College of Information and Communications Hanyang University.
Advertisements

Introduction to Algorithms Quicksort
Divide and Conquer (Merge Sort)
Order Analysis of Algorithms Debdeep Mukhopadhyay IIT Madras.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Chapter 2. Getting Started. Outline Familiarize you with the to think about the design and analysis of algorithms Familiarize you with the framework to.
A Basic Study on the Algorithm Analysis Chapter 2. Getting Started 한양대학교 정보보호 및 알고리즘 연구실 이재준 담당교수님 : 박희진 교수님 1.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Spring 2015 Lecture 5: QuickSort & Selection
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
CMPS1371 Introduction to Computing for Engineers SORTING.
Quicksort Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
CS421 - Course Information Website Syllabus Schedule The Book:
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Quicksort CIS 606 Spring Quicksort Worst-case running time: Θ(n 2 ). Expected running time: Θ(n lg n). Constants hidden in Θ(n lg n) are small.
Fall 2008 Insertion Sort – review of loop invariants.
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.
Analysis of Algorithms CS 477/677
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
Introduction CIS 606 Spring The sorting problem Input: A sequence of n numbers 〈 a 1, a 2, …, a n 〉. Output: A permutation (reordering) 〈 a’ 1,
Unit 1. Sorting and Divide and Conquer. Lecture 1 Introduction to Algorithm and Sorting.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Introduction to Algorithm design and analysis
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture three Dr. Hamdy M. Mousa.
Getting Started Introduction to Algorithms Jeff Chastine.
Mudasser Naseer 1 11/5/2015 CSC 201: Design and Analysis of Algorithms Lecture # 8 Some Examples of Recursion Linear-Time Sorting Algorithms.
September 17, 2001 Algorithms and Data Structures Lecture II Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CS 61B Data Structures and Programming Methodology July 21, 2008 David Sun.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
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.
Foundations II: Data Structures and Algorithms
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.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
1 Computer Algorithms Tutorial 2 Mathematical Induction Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
September 18, Algorithms and Data Structures Lecture II Simonas Šaltenis Aalborg University
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Lecture 2 Algorithm Analysis
Introduction to Algorithms
Fundamentals of Algorithms MCS - 2 Lecture # 11
Unit 1. Sorting and Divide and Conquer
Analysis of Algorithms CS 477/677
Proving correctness.
Ch 7: Quicksort Ming-Te Chi
Divide and Conquer (Merge Sort)
Ch 2: Getting Started Ming-Te Chi
Divide and Conquer Algorithms Part I
Divide and Conquer (Merge Sort)
Ch. 2: Getting Started.
Analysis of Algorithms
nalysis of lgorithms A A Universidad Nacional de Colombia
Algorithms and Data Structures Lecture II
Presentation transcript:

CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html Let’s try it out …

CSC317 2 Toward merge sort

CSC317 3 What is it going to cost us? Divide Left and Right: constant C 1 Sort Left with Insertion Sort: SortRight:withInsertionSort Merge: C 3 n Total: (Have we gained from Insertion sort ( Cn 2 )?

CSC317 4 Another example: find min Input: array A Output: minimum value in array A Cost? C 1 n

CSC317 5 Is splitting the array in half going to help us? Let’s see: Divide Left and Right: constant C 1 Find min Left: Find min Right: Merge: C 3 n Total: Have we gained from find min on full array ( Cn )?

CSC317 6 Back to merge sort: If we can split once, can we do it more often?

CSC317 7 Merge sort: pseudo code (step-by-step) Merge-Sort If array larger than size 1 Divide array into Left and Right arrays // divide Merge-Sort(Left array) // conquer left; recursive call Merge-Sort(Right array) // conquer right; recursive call Merge sorted Left and Right arrays // combine

CSC317 8 Merge sort: pseudo code (more formal) length of array A // divide (split in half) // conquer left // conquer right // combine At what step do we have most work? Merging step, otherwise we just split arrays

CSC317 9 Merge sort: run time analysis Total work in each step: Divide: constant Combine: Cn Conquer: recursively solve two subproblems, each size n/2 We’ll write out the recursion as combination recursion grows like n log 2 n Compared to insertion sort is this good or bad? And how did we end up here?

CSC What is this? A recursion tree where combination recursion is Cn

CSC What is this? A recursion tree where combination recursion is Cn

CSC Cn Next step in the recursion: Cn/2

CSC Cn Let’s keep on going (I mean splitting)! Cn/2 Cn/4 C C C CCCC Each row adds to how much work? C

CSC Cn Cn/2 Cn/4 C C C CCCC C Cn Cost per level stays the same!

CSC Cn Cn/2 Cn/4 C C C CCCC C Level 1: Level 2: Level 0: Level k:

CSC We know that n = 1 and We need to find level k (height of tree) number of levels, each level needs work Cn Total work: Cn lg 2 (n) CONCLUSION: grows as n lg 2 (n) Insert sort: n 2 Is mergesort always faster? Any disadvantages?

CSC Correctness and loop invariants How do we know that an algorithm is correct (i.e. always gives the right answer? We use loop invariants (what does that mean?). Invariant = something that does not change Loop invariant = a property about the algorithm that does not change at every iteration before the loop Usually the property we would like to prove is correct about the algorithm! Intuitive, but we would like to state mathematically

CSC Loop invariant example: insertion sort Question: What invariant property makes this algorithm correct?

CSC Loop invariant example: insertion sort Question: What invariant property makes this algorithm correct? Answer: Before each iteration of the for loop, the elements thus far are sorted. Next question: Can we state that mathematically? KEY

CSC Loop invariant example: insertion sort KEY Insertion sort loop invariant: at the start of each iteration of the for loop, A[1,...,j-1] consists of elements originally in A[1,...,j-1] but in sorted order.

CSC Can we prove this? 3 steps: Initialization: Algorithm is true prior to first iteration of the loop (base case). Maintenance: If it is true before an iteration of the loop it remains true before the next iteration (like an induction step). Termination: When the loop terminates, the invariant gives a useful property that shows the algorithm is correct.

CSC Initialization: Algorithm is true prior to first iteration of the loop (base case). When j=2, A[1] is just one element, which is the original element in A[1], and must be already sorted 1. for j = 2 to n 2. key = A[ j ] 3. Insert key into sorted array A[1,...,j-1] by comparing and swapping into correct position Insert sort pseudocode j=2

CSC Maintenance: If it is true before an iteration of the loop it remains true before the next iteration (like an induction step). KEY true for j-1 1. for j = 2 to n 2. key = A[ j ] 3. Insert key into sorted array A[1,...,j-1] by comparing and swapping into correct position Might not be true in loop. But make sure here that it remains sorted, by pairwise swaps.

CSC Maintenance: If it is true before an iteration of the loop it remains true before the next iteration (like an induction step). KEY So will remain true for j 1. for j = 2 to n 2. key = A[ j ] 3. Insert key into sorted array A[1,...,j-1] by comparing and swapping into correct position We make sure here that it remains sorted, by pairwise swaps

CSC Maintenance: If it is true before an iteration of the loop it remains true before the next iteration (like an induction step). KEY If A[1,...,j-1] sorted before iteration of loop, then for key=A[j], we pairwise swap it into correct position; so now A[1,...,j] is also sorted. Also, A[1,...,j-1] includes elements originally in A[1,...,j-1]. Then A[1,...,j] includes those elements and the element A[j], so must include elements originally in A[1,...,j]

CSC Termination: When the loop terminates, the invariant gives a useful property that shows the algorithm is correct. When the loop terminates (i.e. j=n+1) A[1,...,j] must be in sorted order, which is A[1,…,n] or the entire array. true for j= n for j = 2 to n 2. key = A[ j ] 3. Insert key into sorted array A[1,...,j-1] by comparing and swapping into correct position

CSC Loop invariants example: find min Animation example (Burt Rosenberg) Input: array A Output: minimum value in array A Question: What invariant would make this algorithm correct?

CSC Input: array A Output: minimum value in array A Question: What invariant would make this algorithm correct? Answer: At each point of the algorithm, the current min is known. More formally …

CSC Input: array A Output: minimum value in array A Loop invariant: At each iteration of the for loop, min is the smallest Element in A[1,…,i-1].

CSC Loop invariant of merge: Loop invariant: At the start of each iteration of the for loop, A[p,...,k-1] contains the k-p smallest elements, in sorted order. Also, L[i] and R[j] are the smallest elements of their arrays not yet copied back into A. k J k i i j