Now is the time. For all good men PERMUTATION GENERATION. Princeton University. Robert Sedgewick METHODS



Similar documents
Small Maximal Independent Sets and Faster Exact Graph Coloring

Generating Elementary Combinatorial Objects

Data Structures and Algorithms Written Examination

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

2. (a) Explain the strassen s matrix multiplication. (b) Write deletion algorithm, of Binary search tree. [8+8]

Binary search algorithm

Algorithms. Margaret M. Fleck. 18 October 2010

Why? A central concept in Computer Science. Algorithms are ubiquitous.

Mathematical Induction. Lecture 10-11

Data Structures. Chapter 8

Optimal Algorithms for 2 n AB Games A Graph-Partition Approach *

recursion, O(n), linked lists 6/14

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Collatz Sequence. Fibbonacci Sequence. n is even; Recurrence Relation: a n+1 = a n + a n 1.

arxiv: v1 [math.pr] 5 Dec 2011

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Class One: Degree Sequences

Warshall s Algorithm: Transitive Closure

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Computer Science 210: Data Structures. Searching

Mathematics for Algorithm and System Analysis

Analysis of Binary Search algorithm and Selection Sort algorithm

Part 2: Community Detection

Math 115 Spring 2011 Written Homework 5 Solutions

The positive minimum degree game on sparse graphs

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

each college c i C has a capacity q i - the maximum number of students it will admit

Chapter 3. Distribution Problems. 3.1 The idea of a distribution The twenty-fold way

V. Adamchik 1. Graph Theory. Victor Adamchik. Fall of 2005

Enumerating possible Sudoku grids

MapReduce and Distributed Data Analysis. Sergei Vassilvitskii Google Research

Pseudo code Tutorial and Exercises Teacher s Version

Triangle deletion. Ernie Croot. February 3, 2010

A REMARK ON ALMOST MOORE DIGRAPHS OF DEGREE THREE. 1. Introduction and Preliminaries

1.3 Conditionals and Loops

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Binary Multiplication

3.1 Solving Systems Using Tables and Graphs

Solutions to Homework 6

Outline 2.1 Graph Isomorphism 2.2 Automorphisms and Symmetry 2.3 Subgraphs, part 1

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

CIS 700: algorithms for Big Data

Distributed Dynamic Load Balancing for Iterative-Stencil Applications

Biostatistics 615/815

Binary Heap Algorithms

Midterm Practice Problems

COMBINATORIAL PROPERTIES OF THE HIGMAN-SIMS GRAPH. 1. Introduction

Sample Questions Csci 1112 A. Bellaachia

A NOTE ON OFF-DIAGONAL SMALL ON-LINE RAMSEY NUMBERS FOR PATHS


Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Social Media Mining. Graph Essentials

CS 2302 Data Structures Spring 2015

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Finding and counting given length cycles

CAD Algorithms. P and NP

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Just the Factors, Ma am

Exam study sheet for CS2711. List of topics

COUNTING INDEPENDENT SETS IN SOME CLASSES OF (ALMOST) REGULAR GRAPHS

Towards running complex models on big data

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

Guessing Game: NP-Complete?

Introduction to Parallel Programming and MapReduce

Test case design techniques II: Blackbox testing CISS

Yousef Saad University of Minnesota Computer Science and Engineering. CRM Montreal - April 30, 2008

Definition Given a graph G on n vertices, we define the following quantities:

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Lecture 11: Tail Recursion; Continuations

Regular Languages and Finite Automata

Approximation Algorithms

Closest Pair Problem

Discrete Mathematics Problems

Scheduling Shop Scheduling. Tim Nieberg

Asking Hard Graph Questions. Paul Burkhardt. February 3, 2014

SHARP BOUNDS FOR THE SUM OF THE SQUARES OF THE DEGREES OF A GRAPH

PES Institute of Technology-BSC QUESTION BANK

ALGEBRA. sequence, term, nth term, consecutive, rule, relationship, generate, predict, continue increase, decrease finite, infinite

6. Standard Algorithms

Euler Paths and Euler Circuits

Sequential Data Structures

Test case design techniques I: Whitebox testing CISS

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Persistent Binary Search Trees

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Cycles in a Graph Whose Lengths Differ by One or Two

Single machine parallel batch scheduling with unbounded capacity

International Journal of Software and Web Sciences (IJSWS)

4.2 Sorting and Searching

Static Load Balancing

GRAPH THEORY LECTURE 4: TREES

Applied Algorithm Design Lecture 5

Automatic Test Data Synthesis using UML Sequence Diagrams

Optimal Binary Search Trees Meet Object Oriented Programming

Transcription:

Now is the time For all good men PERMUTATION GENERATION METHODS To come to the aid Of their party Robert Sedgewick Princeton University

Motivation PROBLEM Generate all N! permutations of N elements Q: Why? Basic research on a fundamental problem Compute exact answers for insights into combinatorial problems Structural basis for backtracking algorithms Numerous published algorithms, dating back to 1650s CAVEATS N is between 10 and 20 can be the basis for extremely dumb algorithms processing a perm often costs much more than generating it

N is between 10 and 20 N number of perms million/sec billion/sec trillion/sec 10 3628800 11 39916800 seconds 12 479001600 minutes 13 6227020800 hours seconds 14 87178291200 day minute 15 1307674368000 weeks minutes 16 20922789888000 months hours seconds 17 355687428096000 years days minutes 18 6402373705728000 months hours 19 121645100408832000 20 2432902008176640000 insignificant years days impossible month

Digression: analysis of graph algorithms Typical graph-processing scenario: input graph as a sequence of edges (vertex pairs) build adjacency-lists representation run graph-processing algorithm Q: Does the order of the edges in the input matter? A: Of course! Q: How? A: It depends on the graph Q: How? There are 2 V2 graphs, so full employment for algorithm analysts

Digression (continued) Ex: compute a spanning forest (DFS, stop when V vertices hit) best case cost: V (right edge appears first on all lists) Complete digraph on V vertices worst case: V 2 average: V ln V (Kapidakis, 1990) Same graph with single outlier worst case: O(V 2 ) average: O(V 2 ) Can we estimate the average for a given graph? Is there a simple way to reorder the edges to speed things up? What impact does edge order have on other graph algorithms?

Digression: analysis of graph algorithms Insight needed, so generate perms to study graphs No shortage of interesting graphs with fewer than 10 edges Algorithm to compute average generate perms, run graph algorithm Goal of analysis faster algorithm to compute average

Method 1: backtracking Compute all perms of a global array by exchanging each element to the end, then recursively permuting the others exch (int i, int j) { int t = p[i]; p[i] = p[j]; p[j] = t; generate(int N) { int c; if (N == 1) doit(); for (c = 1; c <= N; c++) { exch(c, N); generate(n-1); exch(c, N); Invoke by calling generate(n); B C C D B D D C C A D A B D D A B A B C C A B A C B D C D B C D A C A D D B A D A B D B A C A B D D B B C C A A D D C C A A B B D D A A B B C C A A A A A A B B B B B B C C C C C C D D D D D D Problem: Too many (2N!) exchanges (!)

Method 2: Plain changes Sweep first element back and forth to insert it into every position in each perm of the other elements A B B C C A B A C B A C C C A A B B A B B B C C C A A C C C D D D A A D D D B B B A B A C C B B A C C A D D C C A D D A B B D D A B C C A D D A B B D D A B B A C C B B A C C A D D D D D A A D D D B B B A A B B B C C C A A C C C Generates all perms with N! exchanges of adjacent elements Dates back to 1650s (bell ringing patterns in English churches) Exercise: recursive implementation with constant time per exch

General single-exch recursive scheme Eliminate first exch in backtracking exch (int i, int j) { int t = p[i]; p[i] = p[j]; p[j] = t; generate(int N) { int c; if (N == 1) doit(); for (c = 1; c <= N; c++) { generate(n-1); exch(?, N); Detail(?): Where is new item for p[n] each time?

Index table computation Q: how do we find a new element for the end? A: compute an index table from the (known) perm for N-1 A B C A B C A C B A A C C B so all perms of 3 takes B into B C C B B A A C A 1 1 A C D A A D D B A B B B B B C C C C B C so all perms of 4 takes into C A A D D A B D C D D D C C B B A A D A 1 2 3 A B B C D E E A B C B C C E E A A C C D C D E A A B C D D E D A A B B D D E E B E E D D C C B B A A 3 1 3 1 and so forth Exercise: Write a program to compute this table

Method 3: general recursive single-exch 1 1 Use precomputed index table 1 2 3 3 1 3 1 Generates perms with N! exchanges 3 4 3 2 3 5 3 1 5 3 1 Simple recursive algorithm 5 2 7 2 1 2 3 7 1 5 5 3 3 7 1 generate(int N) 7 8 1 6 5 4 9 2 3 { int c; 9 7 5 3 1 9 7 5 3 1 if (N == 1) doit(); for (c = 1; c <= N; c++) { generate(n-1); exch(b[n][c], N); No need to insist on particular sequence for last element specifies (N 1)! (N 2)!...3! 2! different algorithms Table size is N(N-1)/2 but N is less than 20 Do we need the table?

Method 4: Heap s* algorithm Index table is not needed Q: where can we find the next element to put at the end? A: at 1 if N is odd; i if N is even A B C A B C B A A C C B C C B B A A A B C A B C D B A D B A A C D A C D D C B D C B B A A C C B B D D A A B C A A D D C C D D B B C C C B B A A A A B B D D D D C C A A B B C C D D D D D D D D C C C C C C B B B B B B A A A A A A Exercise: Prove that it works! *Note: no relationship between Heap and heap data structure

Implementation of Heap s method (recursive) Simple recursive function generate(int N) { int c; if (N == 1) { doit(); return; for (c = 1; c <= N; c++) { generate(n-1); exch(n % 2? 1 : c, N) N! exchanges Starting point for code optimization techniques

Implementation of Heap s method (recursive) Simple recursive function easily adapts to backtracking generate(int N) { int c; if (test(n)) return; for (c = 1; c <= N; c++) { generate(n-1); exch(n % 2? 1 : c, N) N! exchanges saved when test succeeds

Factorial counting Count using a mixed-radix number system for (n = 1; n <= N; n++) c[n] = 1; for (n = 1; n <= N; ) if (c[n] < n) { c[n]++; n = 1; else c[n++] = 1; Values of digit i range from 1 to i (Can derive code by systematic recursion removal) 1-1 correspondence with permutations ABCD BACD BACD BDCA commonly used to generate random perms for (i = 1; i <=N i++) exch(i, random(i)); Use as control structure to generate perms 1111 1211 1121 1221 1131 1231 1112 1212 1122 1222 1132 1232 1113 1213 1123 1223 1133 1233 1114 1214 1124 1224 1134 1234

Implementation of Heap s method (nonrecursive) generate(int N) { int n, t, M; for (n = 1; n <= N; n++) { p[n] = n; c[n] = 1; doit(); for (n = 1; n <= N; ) { if (c[n] < n) { exch(n % 2? 1 : c, N) c[n]++; n = 1; doit(); else c[n++] = 1; Plain changes and most other algs also fit this schema

Analysis of Heap s method Most statements are executed N! times (by design) except B(N): the number of tests for N equal to 1 (loop iterations) A(N): the extra cost for N odd Recurrence for B B(N) = NB(N 1) + 1 for N > 1 with B(1) = 1 Solve by dividing by N! and telescoping B(N) N! = Therefore B(N 1) + 1 (N 1)! N! = 1 + 1 2! + 1 3! +... + 1 N! B(N) = N! (e 1) and similarly A(N) = N! /e Typical running time: 19N! +A(N) + 10B(N) 36.55N! worthwhile to lower constant huge quantity

Improved version of Heap s method (recursive) generate(int N) { int c; if (N == 3) { doit(); p1 = p[1]; p2 = p[2]; p3 = p[3]; p[2] = p1; p[1] = p2; doit(); p[1] = p3; p[3] = p2; doit(); p[1] = p1; p[2] = p3; doit(); p[1] = p2; p[3] = p1; doit(); p[1] = p3; p[2] = p2; doit(); return; for (c = 1; c <= N; c++) { generate(n-1); exch(n % 2? 1 : c, N) N! exchanges Starting point for code optimization techniques

Bottom line Quick empirical study on this machine (N = 12) Heap (recursive) cc -O4 415.2 secs 54.1 secs Java 442.8 secs Heap (nonrecursive) inline N = 2 84.0 secs 92.4 secs inline N = 3 cc -O4 51.7 secs 3.2 secs about (1/6) billion perms/second Lower Bound: about 2N! register transfers

References Heap, Permutations by interchanges, Computer Journal, 1963 Knuth, The Art of Computer Programming, vol. 4 sec. 7.2.1.1 //www-cs-faculty.stanford.edu/~knuth/taocp.html Ord-Smith, Generation of permutation sequences, Computer Journal, 1970-71 Sedgewick, Permutation Generation Methods, Computing Surveys, 1977 Trotter, Perm (Algorithm 115), CACM, 1962 Wells, Elements of combinatorial computing, 1961 [see surveys for many more]

Digression: analysis of graph algorithms Initial results (Dagstuhl, 2002) 60 60 60 60 60 50 50 50 50 50 40 40 40 40 40 30 30 30 30 30 20 20 20 20 20 10 10 10 10 10 0 0 0 0 0