Download Exhaustive Search and Backtracking

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Exhaustive Search and Backtracking
Hao Jiang
Computer Science Department
1
Exhaustive Search
  Modern computers can compute millions of times per
second.
  Exhaustive search is possible for some small scale
problems.
  We study some systematic way to write such
programs.
2
An Example
Combination:
Given a set {1, 2, 3, 4, 5}, how many subsets does it have?
{}
{1}, {2}, {3}, …
{1,2},{1,3}, …
{1,2,3}, …
…
{1,2,3,4,5}
3
The Java Code
public class MyCombine {
public static void all_sub_string(int[] a, int n)
{
if (n == a.length)
{
for (int i = 0; i < a.length; i++)
System.out.print( a[i] + " ");
System.out.println();
return;
}
a[n] = 0;
all_sub_string(a, n+1);
a[n] = 1;
all_sub_string(a, n+1);
}
public static void main(String[] s)
{
int[] a = new int[3];
all_sub_string(a, 0);
}
}
4
Backtracking
backtrack(A, k)
if A={a[1], a[2], a[3], … a[k]} is a solution
report it
else
compute candidate set S(k)
foreach c in S(k)
a[k] = c
backtrack(A, k+1)
5
Permutation
public class MyPermute {
public static void all_perm(int[] a, int n)
{
if (n == a.length)
{
for (int i = 0; i < a.length; i++)
System.out.print( a[i] + " ");
System.out.println();
return;
}
public static void main(String[] s)
{
int[] a = new int[4];
all_perm(a, 0);
}
}
int[] v = new int[a.length];
for (int i = 0; i < n; i++)
v[a[i]] = 1;
for (int i = 0; i < a.length; i++)
{
if (v[i] == 0)
{
a[n] = i;
all_perm(a, n+1);
}
}
}
6
Splitting Money
  How many ways can we split n dollars into 25 cents,
10 cents, 5 cents and 1 cents.
7
The Code
public class Money {
public static void all(int[] a, int n, int m)
{
if (n < 0)
return;
if (n == 0 )
{
for (int i = 0; i < m; i++)
System.out.print(a[i] + " ");
System.out.println();
return;
}
int[] b = {25, 10, 5, 1};
for (int i = 0; i < b.length; i++)
{ a[m] = b[i];
all(a, n-b[i], m+1);
}
}
public static void main(String[] s)
{
int[] a = new int[1000];
all(a, 30, 0);
}
}
8
Robot Path
Start point
destination
How many paths do we have?
9
The Code
public class MyPath {
public static void all_path(int[][] a, int r, int c, int N)
{
if (r == N-1 && c == N-1)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.print( a[i][j] + " ");
System.out.println();
}
System.out.println();
return;
}
if (r < N-1)
{
a[r+1][c] = 1;
all_path(a, r+1, c, N);
a[r+1][c] = 0;
}
if (c < N-1)
{
a[r][c+1] = 1;
all_path(a, r, c+1, N);
a[r][c+1] = 0;
}
}
public static void main(String[] s)
{
int[][] a = new int[5][5];
a[0][0] = 1;
all_path(a, 0, 0, 5);
}
}
10
Eight Queen
11
Public class EightQ {
public static void all_q(int[] a, int n)
{
if (n == a.length)
{
for (int i = 0; i < a.length; i++)
{
for (int j = 0; j < a.length; j++)
{
if (a[i] == j)
System.out.print("Q ");
else
System.out.print("+ ");
}
System.out.println();
}
System.out.println();
return;
}
for (int i = 0; i < a.length; i++)
{
boolean isAvail = true;
for (int j = 0; j < n; j++)
{
if (a[j] == i)
isAvail = false;
if (i-a[j] == n-j)
isAvail = false;
if (a[j]-i == n-j)
isAvail = false;
}
The Code
if (isAvail)
{
a[n] = i;
all_q(a, n+1);
}
}
}
public static void main(String[] s)
{
int[] a = new int[8];
all_q(a, 0);
}
}
12
Backtracking and DFS
  Backtracking is essentially depth first search.
  Backtracking searches and builds up the tree at the
same time.
…
13
More Puzzles
  Sodoku
  15-puzzle
Can you write a backtrack program to solve the puzzles?
14
Related documents