Lecture 3 Induction Algorithm design techniques: induction

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Advertisements

CSCE 3110 Data Structures & Algorithm Analysis
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Analysis of Algorithms CS 477/677
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
10/14/ Algorithms1 Algorithms - Ch2 - Sorting.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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.
Heapsort. Heapsort is a comparison-based sorting algorithm, and is part of the selection sort family. Although somewhat slower in practice on most machines.
Getting Started Introduction to Algorithms Jeff Chastine.
Lecture 3 Induction & Sort(1) Algorithm design techniques: induction Selection sort, Insertion sort, Shell sort...
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
Sorting Data Structures and Algorithms (60-254). Sorting Sorting is one of the most well-studied problems in Computer Science The ultimate reference on.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Recursion Powerful Tool
Advanced Sorting 7 2  9 4   2   4   7
Prof. U V THETE Dept. of Computer Science YMA
Lecture 2 Algorithm Analysis
Lower Bounds & Sorting in Linear Time
Sorting.
Analysis of Algorithms CS 477/677
Recursive Algorithms Section 5.4.
Chapter 7: Sorting (Insertion Sort, Shellsort)
Lecture 4 Sorting Networks
Unit 1. Sorting and Divide and Conquer
CS 3343: Analysis of Algorithms
Chapter 2 (16M) Sorting and Searching
Lecture 5 HeapSort Priority queue and Heapsort
Lecture 4 Divide-and-Conquer
Algorithm Design and Analysis (ADA)
Sorting in linear time Idea: if we can assume there are only k possible values to sort, we have extra information about where each element might need.
Analysis of Algorithms CS 477/677
Lecture 5 Algorithm Analysis
Linear Sorting Sections 10.4
ICS 353: Design and Analysis of Algorithms
Chapter 8-2: Sorting in Linear Time
Data Structures and Algorithms
Lecture 5 Algorithm Analysis
Ch 2: Getting Started Ming-Te Chi
Sorting … and Insertion Sort.
ICS 353: Design and Analysis of Algorithms
ICS 353: Design and Analysis of Algorithms
Lower Bounds & Sorting in Linear Time
Lecture 5 Algorithm Analysis
Algorithms: the big picture
Divide-and-Conquer 7 2  9 4   2   4   7
Ch. 2: Getting Started.
Chapter 8-2: Sorting in Linear Time
Algorithms CSCI 235, Spring 2019 Lecture 18 Linear Sorting
Elementary Sorting Algorithms
Analysis of Algorithms
Introduction To Algorithms
The Selection Problem.
ICS 353: Design and Analysis of Algorithms
Divide and Conquer Merge sort and quick sort Binary search
Lecture 5 Algorithm Analysis
Algorithms and Data Structures Lecture II
Quick Sort & Merge Sort Technique
Presentation transcript:

Lecture 3 Induction Algorithm design techniques: induction Selection sort, Insertion sort, Shell sort ...

ROADMAP Algorithm design techniques: induction Majority element Permutation Radix sort Selection sort, Insertion sort, Shell sort ... Proofs and comparisons 11/19/2018 Xiaojuan Cai

Techniques based on Recursions Induction or Tail recursion Iteration, Proof: invariants by induction Non-overlapping subproblems Divide-and-conquer Overlapping subproblems Dynamic programming 11/19/2018 Xiaojuan Cai

Techniques based on Recursions Induction or Tail recursion Iteration, Proof: invariants by induction Non-overlapping subproblems Divide-and-conquer Overlapping subproblems Dynamic programming 11/19/2018 Xiaojuan Cai

Induction example Selection sort Problem size: 6 9, 8, 9, 6, 2, 56 2, 8, 9, 6, 9, 56 Algorithm Design: problem size n --> n-1 Correctness Proof: by mathematical induction. 11/19/2018 Xiaojuan Cai

Finding majority element Problem: MajorityElement Input: a sequence of integers A[1...n] Output: An integer a in A that appears more than n/2 times, otherwise, output none. 1 2 3 3 4 2 2 2 3 2 none 1 5 1 1 4 2 1 3 1 1 1 Observation If two different elements in the original sequence are removed, then the majority in the original sequence remains the majority in the new sequence. 11/19/2018 Xiaojuan Cai

Finding majority element 11/19/2018 Xiaojuan Cai

Proof of correctness Lemma (Invariants) Given a[1..N], Candidates(i) returns the majority element of a[i...N] if there exists one. Proof. (Inductive proof) Base step. Lemma holds for i = N Inductive step. Induction Hypothesis. Assume for j > i, the lemma holds. Prove it holds for i. 11/19/2018 Xiaojuan Cai

Permutation Problem: Permuation Input: an array of n elements Output: the permutations of n elements 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 11/19/2018 Xiaojuan Cai

Permutation1 11/19/2018 Xiaojuan Cai

Permutation2 11/19/2018 Xiaojuan Cai

Which output is in alphabetical order? Quiz Which output is in alphabetical order? A. permutation1 B. permutation2 C. both D. none of above Lemma1. Assume P[1,..,m,m+1,…,n] = a_1,…,a_m,a_m+1,…,a_n, then Perm1(m) will output a_1,…,a_m-1 + all the permutations of a_m,a_m+1,…,a_n. And after Perm1(m), P does not change the value Lemma2. Before perm2(m), there are n-m + 1 slots in P, and Perm2(m) will fill these slots with all the permutations of m,m+1,…,n. And after Perm2(m), P does not change.

All numbers in the array consists of EXACTLY k digits. Radix sort All numbers in the array consists of EXACTLY k digits. 7467 1247 3275 6792 9187 9134 4675 1239 6792 9134 3275 4675 7467 1247 9187 1239 9134 1239 1247 7467 3275 4675 9187 6792 9134 9187 1239 1247 3275 7467 4675 6792 1239 1247 3275 4675 6792 7467 9134 9187

Proof of correctness Lemma (Invariants) In Radix sort, if the i-th digits are sorted, then the numbers consisting of i, i-1, ..,1 digits are sorted.. Proof. (Inductive proof) Base step. Lemma holds for i = 1 Inductive step. Induction Hypothesis. Assume for j < i, the lemma holds. Prove it holds for i. 11/19/2018 Xiaojuan Cai

Where are we? Algorithm design techniques: induction Permutation Majority element Radix sort Selection sort, Insertion sort, Shell sort ... Proofs and comparisons 11/19/2018 Xiaojuan Cai

? Gypsy folk dance Selection sort 11/19/2018 Xiaojuan Cai

? Romanian folk dance Insertion sort 11/19/2018 Xiaojuan Cai

? Shell sort Hungarian folk dance 11/19/2018 Xiaojuan Cai

Selection sort Lemma (Invariants) Entries the left of ↑ (including ↑) fixed and in ascending order. No entry to right of ↑ is smaller than any entry to the left of ↑. ↑ in final order 11/19/2018 Xiaojuan Cai

Insertion sort Lemma (Invariants) Entries to the left of ↑ (including ↑) are in ascending order. Entries to the right of ↑ have not yet been seen. in order ↑ not yet seen 11/19/2018 Xiaojuan Cai

Shell sort (3-1) 2 13 6 24 43 1 51 9 10 Correctness: The last round of shell sort is insertion sort. 11/19/2018 Xiaojuan Cai

Comparison http://www.sorting-algorithms.com/ worst average best remarks selection N 2 / 2 N exchanges insertion N 2 / 4 N for small N or partially ordered shell ? tight code, subquadratic 11/19/2018 Xiaojuan Cai

Stability and in-place Definition: Stability A stable sort preserves the relative order of items with equal keys. Definition: In-place A sorting algorithm is in-place if the extra memory it uses ≤ c log N. in-place? stable? selection ? insertion shell 11/19/2018 Xiaojuan Cai

tight code, subquadratic Comparison in-place? stable? worst average best remarks selection x N 2 / 2 N exchanges insertion N 2 / 4 N for small N or partially ordered shell ? tight code, subquadratic 11/19/2018 Xiaojuan Cai

Conclusion Algorithm design techniques: induction Permutation Majority element Radix sort Selection sort, Insertion sort, Shell sort ... Proofs and comparisons 11/19/2018 Xiaojuan Cai

Next permutation 1 3 2 Next_perm 2 1 3 1 4 3 2 5 Next_perm 1 4 3 5 2 11/19/2018 Xiaojuan Cai