Permutations. Motivation Shuffling – Games – Music players Brute-force algorithms.

Slides:



Advertisements
Similar presentations
Chapter 9 continued: Quicksort
Advertisements

Introduction to Algorithms Quicksort
Incremental Linear Programming Linear programming involves finding a solution to the constraints, one that maximizes the given linear function of variables.
Heaps1 Part-D2 Heaps Heaps2 Recall Priority Queue ADT (§ 7.1.3) A priority queue stores a collection of entries Each entry is a pair (key, value)
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
CSC 2300 Data Structures & Algorithms March 16, 2007 Chapter 7. Sorting.
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
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.
Quicksort CS 3358 Data Structures. Sorting II/ Slide 2 Introduction Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case:
25 May Quick Sort (11.2) CSE 2011 Winter 2011.
Quicksort COMP171 Fall Sorting II/ Slide 2 Introduction * Fastest known sorting algorithm in practice * Average case: O(N log N) * Worst case: O(N.
CMPS1371 Introduction to Computing for Engineers SORTING.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 16. Quick Sort.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
CS421 - Course Information Website Syllabus Schedule The Book:
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Analysis of Algorithms CS 477/677
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
Chapter 1 Introduction Definition of Algorithm An algorithm is a finite sequence of precise instructions for performing a computation or for solving.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
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.
MA/CSSE 473 Day 13 Permutation Generation. MA/CSSE 473 Day 13 HW 6 due Monday, HW 7 next Thursday, Student Questions Tuesday’s exam Permutation generation.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Recursion, Complexity, and Sorting By Andrew Zeng.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
26 Sep 2014Lecture 3 1. Last lecture: Experimental observation & prediction Cost models: Counting the number of executions of Every single kind of command.
Genome Rearrangements Unoriented Blocks. Quick Review Looking at evolutionary change through reversals Find the shortest possible series of reversals.
Elementary Sorting Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Simple Iterative Sorting Sorting as a means to study data structures and algorithms Historical notes Swapping records Swapping pointers to records Description,
Survey of Sorting Ananda Gunawardena. Naïve sorting algorithms Bubble sort: scan for flips, until all are fixed Etc...
Sorting CS 110: Data Structures and Algorithms First Semester,
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Week 13 - Friday.  What did we talk about last time?  Sorting  Insertion sort  Merge sort  Started quicksort.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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 and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Dr. Sajib Datta CSE 1320 Arrays, Search and Sort.
Searching CS 110: Data Structures and Algorithms First Semester,
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
329 3/30/98 CSE 143 Searching and Sorting [Sections 12.4, ]
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
INTRO2CS Tirgul 8 1. Searching and Sorting  Tips for debugging  Binary search  Sorting algorithms:  Bogo sort  Bubble sort  Quick sort and maybe.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Sorting With Priority Queue In-place Extra O(N) space
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Algorithm Analysis CSE 2011 Winter September 2018.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Algorithm design and Analysis
EE 312 Software Design and Implementation I
Ch. 2: Getting Started.
Presentation transcript:

Permutations

Motivation Shuffling – Games – Music players Brute-force algorithms

Shuffling Given a sequence of values, return a random reordering of them Want unbiased shuffle (usually) Assume access to a fast, unbiased random number generator

Fisher-Yates Given sequence of n elements, generate a random number r between 1 and n Remove the r-th element from the sequence and add it to the output sequence Repeat on the leftover elements, until every element has been selected and placed in the output sequence

Fisher-Yates Unbiased – Each element is equally likely to be picked first – Given the first selection, each remaining element is equally likely to be picked second Fast – Smart implementation takes O(n) time

Implementing Fisher-Yates If input is a linked list, iterating through the list to find the r-th element costs linear time, so runtime falters to O(n 2 ). Ouch. If input is an array, the r-th element cannot be removed. – But we can get around this – Instead of removing selected elements, swap them to front of array, then repeat process on last n-1 elements, and so on

Other shuffles Generate a random number for every element in the list, then sort the list – O(n log n) runtime, due to sorting step – O(n) space requirement, for random numbers – The Housing Office uses this – Damn roompicks Random swapping – Biased

Lexicographic Permutation Generation Given a sequence of comparable elements, return the lexicographically next permutation Sequences a 1, a 2, …, a n and b 1, b 2, …, b n ; sequence a is said to be lexicographically earlier than sequence b if a j < b j, where j is the first index at which the two sequences differ – e.g. “act” < “cat”, “loop” < “pool”

Lexicographic Permutation Generation Find latest position j such that a j < a j+1 – If no j exists, our sequence is the last permutation Find latest position k such that a j < a k – k must be later than j, since j+1 satisfies this Swap a j and a k Reverse the subsequence a j+1, …, a n

Lexicographic Permutation Generation Each generation could take as long as O(n) – As it turns out, this is actually O(1) amortized – Consider the average position of j If we want to generate all n! permutations, we only need O(n!) time – Provided n is very small (think single-digit or low 10s), we can brute-force over all permutations

Steinhaus-Johnson-Trotter Given a sorted sequence, generates a new permutation (not necessarily next-lex) with each call Strange algorithm – Difficult to prove correct

Steinhaus-Johnson-Trotter Create a “mobility array” of length n associated with the sequence, where each element can be “left” or “right” Define a “mobile value” as an element in the sequence whose adjacent value in the direction of its mobility is smaller than the element itself

Steinhaus-Johnson-Trotter Pseudocode follows: while there exists a mobile value let k be the largest mobile value swap k and its neighbor in the direction of k’s mobility for each value s in the sequence if s > k reverse the direction of mobility of s

Steinhaus-Johnson-Trotter Cons: – Requires O(n) time (not amortized) and O(n) space – Requires initial input to be sorted – Does not use lexicographic ordering – If mobility array is lost, must restart Pros: – Has an impressive-sounding name – Might possibly get you women