MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSC 421: Algorithm Design & Analysis
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 2 Some of the sides are exported from different sources.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
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.
Chapter 5: Decrease and Conquer
Connected Components, Directed Graphs, Topological Sort COMP171.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
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.
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
MA/CSSE 473 Day 17 Permutations by lexicographic order number.
MA/CSSE 473 Day 15 BFS Topological Sort Combinatorial Object Generation Intro.
RAIK 283: Data Structures & Algorithms
UNIT-II DECREASE-AND-CONQUER ANALYSIS AND DESIGN OF ALGORITHMS CHAPTER 5:
MA/CSSE 473 Day 14 Permutations wrap-up Subset generation (Horner’s method)
5.6 Generating Permutations and Combinations Generating Permutations Many different algorithms have been developed to generate the n! permutations of this.
FUNCTIONS Definition Let A = {1, 2, 3,..., n}, and f : A → A be a bijective function; then f is called a permutation on n. QUESTION: for a set with N elements.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Chapter 4 Generating Permutations and Combinations.
MA/CSSE 473 Day 16 Combinatorial Object Generation Permutations.
MA/CSSE 473 Day 11 Knuth interview Amortization (growable Array) Brute Force Examples.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
CSG3F3/ Desain dan Analisis Algoritma
MA/CSSE 473 Day 15 Return Exam Student questions Towers of Hanoi
Sort Algorithm.
MA/CSSE 473 Day 12 Interpolation Search Insertion Sort quick review
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Redraw these graphs so that none of the line intersect except at the vertices B C D E F G H.
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
MA/CSSE 473 Day 16 Answers to your questions Divide and Conquer
CSC 421: Algorithm Design & Analysis
Design and Analysis of Algorithm
An Introduction to Sorting
Chapter 14 Graph Algorithms
Teach A level Computing: Algorithms and Data Structures
Depth-First Search.
Algorithm Design Methods
Chapter 5.
Sorting Chapter 13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Quicksort and Mergesort
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
More Graph Algorithms.
Graph Search Applications
Bubble, Selection & Insertion sort
Graphs Graph transversals.
Graphs Chapter 13.
"Learning how to learn is life's most important skill. " - Tony Buzan
Graphs Chapter 11 Objectives Upon completion you will be able to:
Sorting Algorithms Ellysa N. Kosinaya.
Data Structures Review Session
Topological Sort CSE 373 Data Structures Lecture 19.
Lectures on Graph Algorithms: searching, testing and sorting
Connected Components, Directed Graphs, Topological Sort
Brute Force A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples: Computing.
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
3. Brute Force Selection sort Brute-Force string matching
Recall Brute Force as a Problem Solving Technique
CSC 421: Algorithm Design & Analysis
3. Brute Force Selection sort Brute-Force string matching
Trevor Brown DC 2338, Office hour M3-4pm
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CSE 417: Algorithms and Computational Complexity
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Analysis and design of algorithm
CSC 421: Algorithm Design & Analysis
Algorithm Course Algorithms Lecture 3 Sorting Algorithm-1
3. Brute Force Selection sort Brute-Force string matching
Presentation transcript:

MA/CSSE 473 Day 13 Finish Topological Sort Permutation Generation Take Knuth Fascicle to class.

MA/CSSE 473 Day 13 Student Questions Finish Topological Sort Permutation generation Pass around attendance sheet.

Recap: Topologically sort a DAG DAG = Directed Aclyclic Graph Linearly order the vertices of the DAG so that for every edge e, e's tail vertex precedes its head vertex in the ordering.

DFS-based Algorithm DFS-based algorithm for topological sorting Perform DFS traversal, noting the order vertices are popped off the traversal stack Reversing order solves topological sorting problem Back edges encountered?→ NOT a dag! Example: Efficiency: a b c d Efficiency is the same as DFS algorithm e f g h

Source Removal Algorithm Repeatedly identify and remove a source (a vertex with no incoming edges) and all the edges incident to it until either no vertex is left (problem is solved) or there is no source among remaining vertices (not a dag) Example: Efficiency: same as efficiency of the DFS-based algorithm a b c d e f g h

Application: Spreadsheet program What is an allowable order of computation of the cells' values? Draw, or at least start a dependency graph. An edge from each cell to cells that directly depend on it. Go down the columns, from left to right, Stop after B3.

Cycles cause a problem!

Combinatorial object Generation Permutations Subsets Combinatorial object Generation

Combinatorial Object Generation Generation of permutations, combinations, subsets. This is a big topic in CS We will just scratch the surface of this subject. Permutations of a list of elements (no duplicates) Subsets of a set Evidence: The book I received in 2008 The Art of Computer Programming, Volume 4, Fascicle 2 Generating All Tuples and Permutations 127 pages of very small print.

Permutations We generate all permutations of the numbers 1..n. Permutations of any other collection of n distinct objects can be obtained from these by a simple mapping. How would a "decrease by 1" approach work? Find all permutations of 1.. n-1 Insert n into each position of each such permutation We'd like to do it in a way that minimizes the change from one permutation to the next. It turns out we can do it so that we always get the next permutation by swapping two adjacent elements.

First approach we might think of for each permutation of 1..n-1 for i=0..n-1 insert n in position i That is, we do the insertion of n into each smaller permutation from left to right each time However, to get "minimal change", we alternate: Insert n L-to-R in one permutation of 1..n-1 Insert n R-to-L in the next permutation of 1..n-1 Etc.

Example Bottom-up generation of permutations of 123 Example: Do the first few permutations for n=4

Johnson-Trotter Approach integrates the insertion of n with the generation of permutations of 1..n-1 Does it by keeping track of which direction each number is currently moving The number k is mobile if its arrow points to an adjacent element that is smaller than itself In this example, 4 and 3 are mobile

Johnson-Trotter Approach The number k is mobile if its arrow points to an adjacent element that is smaller than itself. In this example, 4 and 3 are mobile To get the next permutation, exchange the largest mobile number (call it k) with its neighbor. Then reverse directions of all numbers that are larger than k. Initialize: All arrows point left. Do the first few steps for n=4:                 1 2 3 4 1 2 4 3 1 4 2 3 4 1 2 3                 4 1 3 2 1 4 3 2 1 3 4 2 1 3 2 4                 3 1 2 4 3 1 4 2 3 4 1 2 4 3 1 2                 4 3 2 1 3 4 2 1 3 2 4 1 3 2 1 4

Johnson-Trotter Driver

Johnson-Trotter background code

Johnson-Trotter major methods

Lexicographic Permutation Generation Generate the permutations of 1..n in "natural" order. Let's do it recursively. Solution on next Slide. Do not include it in the slides placed online before class. Do this as a live coding exercise. Before class, copy PermutationStartingCode.py to PermutationSection1 and PermutationSection2 files.

Lexicographic Permutation Code The book describes a "permute in place" algorithm. We will look at that one later.

Permutations and order number permutation 0123 12 2013 1 0132 13 2031 2 0213 14 2103 3 0231 15 2130 4 0312 16 2301 5 0321 17 2310 6 1023 18 3012 7 1032 19 3021 8 1203 20 3102 9 1230 21 3120 10 1302 22 3201 11 1320 23 3210 Given a permutation of 0, 1, …, n-1, can we directly find the next permutation in the lexicographic sequence? Given a permutation of 0..n-1, can we determine its permutation sequence number? Discuss these, with examples: Given n and i, can we directly generate the ith permutation of 0, …, n-1?

Discovery time (with two partners) Which permutation follows each of these in lexicographic order? 183647520 471638520 Try to write an algorithm for generating the next permutation, with only the current permutation as input. If the lexicographic permutations of the numbers [0, 1, 2, 3, 4, 5] are numbered starting with 0, what is the number of the permutation 14032? General form? How to calculate efficiency? In the lexicographic ordering of permutations of [0, 1, 2, 3, 4, 5], which permutation is number 541? How to calculate efficiently? After 183647520: 183650247 After 471638520: 471650238 Algorithm: Scan from right, find first place where p[i] < p[i+1]. find j, position of smallest among numbers in p[i+1]…p[n-1] that are greater than p[i]. exchange p[i] and p[j], then reverse p[i+1] ..p[n-1]