24CombinatorialSearch

# 24CombinatorialSearch - Combinatorial Search subsets...

This preview shows pages 1–4. Sign up to view the full content.

Copyright © 2007 by Robert Sedgewick and Kevin Wayne. Combinatorial Search subsets permutations counting paths in a graph paths in a lattice 2 Overview Exhaustive search. Iterate through all elements of a search space. Backtracking. Systematic method for examining feasible solutions to a problem, by systematically eliminating infeasible solutions. Applicability. Huge range of problems (include NP-hard ones). Caveat. Search space is typically exponential in size ! effectiveness may be limited to relatively small instances. Caveat to the caveat. Backtracking may prune search space to reasonable size, even for relatively large instances 3 subsets permutations counting paths in a lattice path in a graph 4 Enumerating subsets: natural binary encoding Given n items, enumerate all 2 n subsets. ! count in binary from 0 to 2 n - 1. ! bit i represents item i ! if 0, in subset; if 1, not in subset 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 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 i binary subset complement

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
5 Enumerating subsets: natural binary encoding Given n items, enumerate all 2 n subsets. ! count in binary from 0 to 2 n - 1. ! bit i represents item i ! if 0, in subset; if 1, not in subset Note : bitflicking simpler in assembly language long N = 1 << n; for (long i = 0; i < N; i++) { for (int bit = 0; bit < n; bit++) { System.out.print(bit + " "); } System.out.println(); } 6 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 7 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. 8 Beckett: Java implementation public static void moves(int n, boolean enter) { if (n == 0) return; moves(n-1, true ); if (enter) System.out.println("enter " + n); else System.out.println("exit " + n); moves(n-1, false ); } % java Beckett 4 enter 1 enter 2 exit 1 enter 3 enter 1 exit 2 exit 1 enter 4 enter 1 enter 2 exit 1 exit 3 enter 1 exit 2 exit 1 stage directions for 3-actor play moves(3, true) reverse stage directions for 3-actor play moves(3, false)
9 More Applications of Gray Codes 3-bit rotary encoder Chinese ring puzzle 8-bit rotary encoder Towers of Hanoi 10 Scheduling Scheduling (set partitioning). Given n jobs of varying length, divide among two machines to minimize the time the last job finishes. Remark.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 06/10/2011 for the course COS 226 taught by Professor Kevinwayne during the Spring '08 term at Princeton.

### Page1 / 11

24CombinatorialSearch - Combinatorial Search subsets...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online