Docsity

Generating Solutions to Problems: Enumerating Subsets and Permutations, Slides of Data Representation and Algorithm Design

Toggle sidebar
bg1
Combinatorial Search
2
Overview
Exhaustive search. Iterate through all elements of a search space.
Backtracking. Systematic method for generating all solutions
to a problem, by successively augmenting partial solutions.
Applicability. Huge range of problems (include NP-hard ones).
Caveat. Search space is typically exponential in size !
effectiveness is limited to relatively small instances.
3
Enumerating Subsets
4
Scheduling
Scheduling (set partitioning). Given n jobs of varying length, divide
among two machines to minimize the time the last job finishes.
Remark. NP-hard.
1.41
1.73
2.00
2.23
1
2
3
4
lengthjob
or, equivalently, difference
between finish times
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Generating Solutions to Problems: Enumerating Subsets and Permutations and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

Combinatorial Search

2

Overview

Exhaustive search. Iterate through all elements of a search space.

Backtracking. Systematic method for generating all solutions

to a problem, by successively augmenting partial solutions.

Applicability. Huge range of problems (include NP-hard ones).

Caveat. Search space is typically exponential in size!

effectiveness is limited to relatively small instances.

3

Enumerating Subsets

4

Scheduling

Scheduling (set partitioning). Given n jobs of varying length, divide

among two machines to minimize the time the last job finishes.

Remark. NP-hard.

  1. 41
  2. 73
  3. 00
  4. 23 1 2 3 4 job length or, equivalently, difference between finish times

5

Enumerating Subsets: Natural Binary Code

Enumerating subsets. Given n items, enumerate all 2n^ subsets.

! Count in binary from 0 to 2n^ - 1.

! Look at binary representation.

0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 empty 1 2 2 1 3 3 1 3 2 3 2 1 4 4 1 4 2 4 2 1 4 3 4 3 1 4 3 2 4 3 2 1 integer binary code machine one 4 3 2 1 4 3 2 4 3 1 4 3 4 2 1 4 2 4 1 4 3 2 1 3 2 3 1 3 2 1 2 1 empty machine two 6

Enumerating Subsets: Natural Binary Code

Enumerating subsets. Given n items, enumerate all 2n^ subsets.

! Count in binary from 0 to 2n^ - 1.

! Look at binary representation.

long N = 1 << n; for (long i = 0 ; i < N; i++) { for (int bit = 0 ; bit < n; bit++) { if (((i >> bit) & 1 ) == 1 ) System.out.print(bit + " "); } System.out.println(); } 7

Samuel Beckett

Quad. Starting with empty stage, 4 characters enter and exit

one at a time, such that each subset of actors appears exactly once.

ruler function 8

Enumerating Subsets: Binary Reflected Gray Code

Binary reflected Gray code. The n-bit code is:

! the (n-1) bit code with a 0 prepended to each word, followed by

! the (n-1) bit code in reverse order, with a 1 prepended to each word.

13

Exploiting Symmetry

Exploit symmetry.

! Half of schedules are redundant.

! Fix job n on machine one! twice as fast.

14

Space-Time Tradeoff

Space-time tradeoff.

! Enumerate all subsets of first n/2 jobs; sort by gap.

! Enumerate all subsets of last n/2 jobs; for each subset, binary

search to find for best matching subset among first n/2 jobs.

! Reduces running time from 2n^ to 2n/2^ log n by consuming 2n/2^ memory.

1 2 3 4 job length

5 6

  1. 14 ( 1 2 3 )
  2. 32 ( 2 3 )
  3. 68 ( 1 3 )
  4. 14 ( 1 2 )
    1. 14 ( 3 )
    1. 68 ( 2 )
    1. 32 ( 1 )
    1. 14 (empty)
  1. 58 ( 4 5 6 )
  2. 12 ( 5 6 )
    1. 42 ( 4 6 )
  1. 48 ( 4 5 )
    1. 88 ( 6 )
  1. 42 ( 5 )
    1. 12 ( 4 )
    1. 58 (empty) gap (subset) gap (subset) m^ baetscth ( 15. 21 43 ) (^11. 124 )^ -^1 (. 31 )^4 ( 15. 21 43 ) (^ - e^5 m.p^1 t^4 y)^1 (. 21 )^4 -^1 (. 31 )^4 (^ - e^5 m.p^1 t^4 y) gap (^ - 10. 24 43 ) ( 10. 20 24 )^ - (^03. 752 ) ( 1 02. 236 6 )^ - (^04. 256 ) ( 20. 47 26 ) ( 30. 50 26 ) ( 10. 24 43 ) 15

Enumerating Permutations

16

8 - Queens Problem

8-queens problem. Place 8 queens on a chessboard so that

no queen can attack any other queen.

Representation. Can represent solution as a permutation:

q[i] = column of queen in row i.

1 2 3 4 5 6 7 8 int[] q = { 5 , 7 , 1 , 3 , 8 , 6 , 4 , 2 }; queens i and j can attack each other if |q[i] + i| = |q[j] + j|

17

Enumerating Permutations

Permutations. Given n items, enumerate all n! permutations.

order matters 3 1 2 4 3 1 4 2 3 2 1 4 3 2 4 1 3 4 1 2 3 4 2 1 3 - element permutations 4 - element permutations 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 4 1 3 2 4 3 1

(^1) p (^) efromllouwtaetdio bny o afn y 2 3 42 p (^) efromlluotwaetdio bny o af n 1 y 3 4 3 pe fromlloutwaetdio bn yo afn 1 y 2 4 3 pe fromllouwtaetdio bn yo afn 1 y 2 4 18

Enumerating all Permutations

To enumerate all permutations of a set of n elements:

! For each element ai

  • put ai first, then append
  • a permutation of the remaining elements (a 0 , …, ai- 1 , ai+ 1 , …, an- 1 ) 1 2 3 4 1 2 3 4 2 1 3 4 3 2 1 4 4 2 3 1 … (^) … … 2 1 3 4 2 3 1 4 2 4 3 1 2 1 3 4 2 1 4 3 2 1 3 4 2 1 4 3 2 3 1 4 2 3 4 1 2 3 1 4 2 3 4 1 2 4 3 1 2 4 1 3 2 4 3 1 2 4 1 3 19

Enumerating All Permutations: Java Implementation

private static void enumerate(int[] a, int n) { int N = a.length; if (n == N) printPermutations(a); for (int i = n; i < N; i++) { swap(q, i, n); enumerate(a, n+ 1 ); swap(q, n, i); } } permutations of a[n], …, a[N-1] int N = 4 ; int[] a = { 1 , 2 , 3 , 4 }; enumerate(a, N); cleans up after itself 20

Pruning

25 8 5 2 1 3 4 3 8 1 2 6 9 9 6 7 4 5 7

Sudoku. Fill 9-by-9 grid so that every row, column, and box contains

the digits 1 through 9.

Remark. Natural generalization is NP-hard.

Sudoku

2 9 3 4 1 6 9 4 6 5 7 3 8 1 5 6 7 8 2 4 9 1 7 6 9 8 5 2 5 9 3 4 2 6 7 8 1 5 7 4 3 2 3 4 8 1 6 7 5 1 5 3 9 8 2 4 7 8 6 2 9 3 1 26 2 1 7 8 5 3 4 3 8 1 2 6 9 9 6 7 4 5

Sudoku. Fill 9-by-9 grid so that every row, column, and box contains

the digits 1 through 9.

Remark. Natural generalization is NP-hard.

Sudoku

2 9 3 4 1 6 9 4 6 5 7 3 8 1 5 6 7 8 2 4 9 1 7 6 9 8 5 2 5 9 3 4 2 6 7 8 1 5 7 4 3 2 3 4 8 1 6 7 5 1 5 3 9 8 2 4 7 8 6 2 9 3 1 27

Sudoku

Linearize. Treat 9-by-9 array as an array of length 81.

Enumerate all assignments. Count from 0 to 9^81 - 1 in base 9.

7 1 8 3 4 5 6 3 8 … 80 0 1 2 3 4 5 6 7 8 80 8 5 2 1 3 4 3 8 1 2 6 9 9 6 7 4 5 7 2 9 3 4 1 6 9 4 6 5 7 3 8 1 5 6 7 8 2 4 9 1 7 6 9 8 5 2 5 9 3 4 2 6 7 8 1 5 7 4 3 2 3 4 8 1 6 7 5 1 5 3 9 8 2 4 7 8 6 2 9 3 1 using digits 1 to 9 28

Backtracking. Iterate through elements of search space.

! For each empty cell, there are 9 possible choices.

! Make one choice and recur.

! If you reach a contradiction, go back to previous choice,

and make next available choice.

Pruning. Stop as soon as you reach a contradiction.

Improvements.

! Choose most constrained cell to examine next.

! Knuth's "dancing links."

Sudoku: Backtracking Solution

29

Sudoku: Java Implementation

private static void solve(int[] board, int cell) { // found the solution if (cell == 81 ) { show(board); return; } // skip over cell n since it has fixed value if (board[cell] != 0 ) { solve(board, cell + 1 ); return; } // try all 9 possibilities for (int n = 1 ; n <= 9 ; n++) { if (isConsistent(board, cell, n)) { board[cell] = n; solve(board, cell + 1 ); } } board[cell] = 0 ; } (^) cleans up after itself don't bother if a Sudoku constraint is already violated int[] board = { 7 , 0 , 8 , 0 , 0 , 0 , 3 , … }; solve(board, 0 ); 30

Enumerating all Paths in a Grid

31

All Paths on a Grid

All paths. Enumerate all simple paths on a grid of adjacent sites.

Application. Self-avoiding lattice walk to model polymer chains.

no atoms can occupy same position at same time 32

Boggle

Boggle. Find all words that can be formed by tracing a simple path of

adjacent cubes (left, right, up, down, diagonal).

Pruning. Stop as soon as no word in dictionary contains string of

letters on current path as a prefix! use a trie.

B A X X X

X C A C K

X K R X X

X T X X X

X X X X X

B BA BAX

37

Hamilton Path: Backtracking Solution

Backtracking solution. To find Hamilton path starting at v:

! Add v to current path.

! For each vertex w adjacent to v

  • find a simple path starting at w using all remaining vertices

! Remove v from current path.

How to implement?

! To keep track of path: use a stack.

! To record which vertices are on the path: use a boolean array.

! To recursively visit vertices: use depth-first search.

Heuristic. Choose vertex with fewest unvisited neighbors.

38

Hamilton Path: Java Implementation

public class HamiltonPath { private boolean[] onPath; private Stack path = new Stack(); public HamiltonPath(Graph G) { onPath = new boolean[G.V()]; for (int v = 0 ; v < G.V(); v++) dfs(G, v); } private void dfs(Digraph G, int v) { path.push(v); onPath[v] = true; if (path.size() == G.V()) System.out.println(path); for (int w : G.adj(v)) if (!onPath[w]) dfs(G, w); path.pop(); onPath[v] = false; } } don't bother further exploration if w is already on the current path add v to the current path remove v from the current path 39

The Longest Path

Woh-oh-oh-oh, find the longest path! Woh-oh-oh-oh, find the longest path! If you said P is NP tonight, There would still be papers left to write, I have a weakness, I'm addicted to completeness, And I keep searching for the longest path. The algorithm I would like to see Is of polynomial degree, But it's elusive: Nobody has found conclusive Evidence that we can find a longest path. I have been hard working for so long. I swear it's right, and he marks it wrong. Some how I'll feel sorry when it's done: GPA 2. 1 Is more than I hope for. Garey, Johnson, Karp and other men (and women) Tried to make it order N log N. Am I a mad fool If I spend my life in grad school, Forever following the longest path? Woh-oh-oh-oh, find the longest path! Woh-oh-oh-oh, find the longest path! Woh-oh-oh-oh, find the longest path. Recorded by Dan Barrett in 1988 while a student at Johns Hopkins during a difficult algorithms final.