Backtracking Dfs Bfs
Backtracking Dfs Bfs
Backtracking
There are problems that can be solved only by performing an exhaustive search of all
possible solutions. One way to search systematically for a solution is to use a decision
tree, where each internal vertex represents a decision and each leaf a possible solution.
To find a solution via backtracking, first make a sequence of decisions in an attempt to
reach a solution as long as this is possible. The sequence of decisions can be represented
by a path in the decision tree. Once it is known that no solution can result from any
further sequence of decisions, backtrack to the parent of the current vertex and work
toward a solution with another series of decisions, if this is possible. The procedure
continues until a solution is found, or it is established that no solution exists. The
following examples illustrate the usefulness of backtracking.
How can backtracking be used to decide whether a graph can be colored using n colors?
Solution: We can solve this problem using backtracking in the following way. First pick
some vertex a and assign it color 1. Then pick a second vertex b, and if b is not adjacent
to a, assign it color 1. Otherwise, assign color 2 to b. Then go on to a third vertex c. Use
color 1, if possible, for c. Otherwise use color 2, if this is possible. Only if neither color 1
nor color 2 can be used should color 3 be used. Continue this process as long as it is
possible to assign one of the n colors to each additional vertex, always using the first
allowable color in the list. If a vertex is reached that cannot be colored by any of the n
colors, backtrack to the last assignment made and change the coloring of the last vertex
colored, if possible, using the next allowable color in the list. If it is not possible to
change this coloring, backtrack further to previous assignments, one step back at a time,
until is possible to change a coloring of a vertex. Then continue assigning colors of
additional vertices as long as possible. If a coloring using n colors exists, backtracking
will produce it. (Unfortunately this procedure can be extremely inefficient.)
In particular, consider the problem of coloring the graph shown in Figure 10 with three
colors. The tree shown in Figure 10 illustrates how backtracking can be used to construct
a 3-coloring. In this procedure, red is used first, then blue, and finally green. This simple
example can obviously be done without backtracking, but it is a good illustration of the
technique.
In this tree, the initial path from the root, which represents the assignment of red to a,
45378874.doc p.2
leads to a coloring with a red, b blue, c red, and d green. It is impossible to color e using
any of the three colors when a, b, c, and d are colored in this way. So, backtrack to the
parent of the vertex representing this coloring. Since no other color can be used for d,
backtrack one more level. Then change the color of c to green. We obtain a coloring of
the graph by then assigning red to d and green to e.
DFS
Solution: We start with a sum with no terms. We build up the sum by successively adding
terms. An integer in the sequence is included if the sum remains less than M when this
integer is added to the sum. If a sum is reached so that the addition of any term is greater
than M, backtrack by dropping the last term of the sum.
Figure 12 displays a backtracking solution to the problem of finding a subset of {31, 27,
15, 11, 7, 5} with sum equal to 39.
Suppose the integers are saved in num ( array [1..6] of integers). The result is printed by a
function call: writeln(exist(1,39)) and definition of exist() is
function exist(ind,sum:integer):boolean;
begin
if sum=0 then exist:=true else if (ind>6) or (sum<num[ind]) then exist:=false else
exist:=exist(ind+1,sum-num[ind]) or exist(ind+1,sum);
end;
With respect to a tree, when deeper nodes are traversed before the nodes in the same
level, the traversal is called DFS.
When nodes in the same level are traversed before the deeper ones, it is called BFS.
45378874.doc p.3
[Exercise] Rewrite the function exist if the numbers are not sorted.
BFS
the sum is 10
path is
1,1
2,2
3,2
4,2
Draw a tree to show the traversal if BFS is used. Each note contains the ordered pairs and
the cumulative sum. Number the order of traversal. mark an ‘x’ besides the nodes which
will not be added to the data structure.
(1,1) 2
45378874.doc p.4
(2,1) 6 (2,2) 4
Some nodes can be deleted from the tree to reduce no. of traversal. State the reason why
they can be deleted.
Since there is no need to backtrack, BFS is always implemented with the use of QUEUE.
There are two pointers HEAD and TAIL:
accumulated position of
parent node
sum current node
H T
(1,1) (2,1) (2,2)
2 6 4
(-1,-1) (1,1) (1,1)
* (1,1) will lead to (2,1) and (2,2)
H T
(1,1) (2,1) (2,2) (3,1) (3,2)
2 6 4 12 9
(-1,-1) (1,1) (1,1) (2,1) (2,1)
* (2,1) will lead to (3,1) and (3,2)
45378874.doc p.5
H T
(1,1) (2,1) (2,2) (3,1) (3,2) (3,3)
2 6 4 12 7 8
(-1,-1) (1,1) (1,1) (2,1) (2,2) (2,2)
* (2,2) will lead to (3,2) and (3,3)
State the reason why the sum and the parent of the 5th node is changed to new ones..
How to find and output the path corresponding to the said sum?
According to BFS, all nodes of the previous levels have been traversed before the
traversal of the lower ones, the processing is FIFO, so the use of queue is
appropriate.
[Exercise 1]
Write down the contents of the queue related to the problem similar to above with I=5
and j=5:
num[I,j] column (j)
1 2 3 4 5
1 2 4 3 5 2
row (i) 2 4 2 5 6 8
3 6 3 4 2 4
4 4 3 5 6 2
5 0 8 9 0 2
uses wincrt;
type
queuetype=record
x,y:integer;
px,py:integer; {x, y of parent}
s:integer; {accumulated sum on the path}
end;
var
v,e,i,j,n,a,b,c,d,head,tail,s,sum:integer;
num:array[1..50,1..50] of integer;
q:array[1..100] of queuetype;
exist:boolean;
fl:text;
procedure p(a,b,c,d:integer);
var
i:integer;
begin
writeln(a:5,b:5);
for i:=1 to tail do if (q[i].x=c)and(q[i].y=d) then p(c,d,q[i].px,q[i].py);
end;
function notempty:boolean;
begin
notempty:=(tail>=head);
end;
procedure enqueue(a,b,parentx,parenty,sum:integer);
begin
tail:=tail+1;
q[tail].x:=a;
q[tail].y:=b;
q[tail].px:=parentx;
q[tail].py:=parenty;
q[tail].s:=sum;
end;
begin
assign(fl,'dfs.txt');
reset(fl);
{ readln(fl,v);
readln(fl,n); }
head:=1;
tail:=0;
enqueue(1,1,-1,-1,num[1,1]);
45378874.doc p.7
{check if the node has been checked and added to the queue}
.
.
There is a data file containing at least 100 x 100 integers, namely 'bfs.dat' , in your drive.
The sum found for the first 100x100 integers is ___________________ and the path
involved is:
[Summary]
[Further exercise 1]
Solve question 1 in HKOI 1999
There are two data files in hkoi99f2.txt - hkoi99f5.txt in your drive.
[Exercise 2] Dominoes
Each domino consists of 2 parts, the left part and the right part. There is an integer (1 - 6)
in each part. Two dominoes can be connected at one end if they have the SAME integer
in the part of connection. You are given a number of dominoes (less than 15), and asked
to connect some of these dominoes in one single straight chain such that the total of the
integers on this chain is the maximum. For example, four dominoes 2|5, 1|2, 4|6 and 3|5
can be connected as 1|2 2|5 5|3 (with 4|6 not connected) and the total of integers is 18.
Write a program to read the given dominoes from a text file and output to the screen the
maximum total of the connected chain.
Input File
The first line consists of an integer n, which is the number of given dominoes. There are
n subsequent lines. Each of these lines consists of two integers which represent the
integers stored in the two parts of a domino.
Sample Run
(Any values after "?" are inputs by the user during program execution. Others are outputs
by the program.)
Data File?hkoi99f5.TXT
Maximum total = 18
[Further exercise 2]
Solve question 1 in HKOI 2001
There are two data files in hkoi01f1.txt - hkoi01f3.txt in your drive.
Question 1 Activities
Input
In the first line of the input file, there is an integer n (1 ≤ n ≤ 750), indicating the
number of activities available. There are n subsequent lines. Each line contains two
strings of number, indicating the beginning time and ending time of an activity. The
time is represented in the format of YYYYMMDDHH where the year is either 1999
or 2000. The activities are numbered from 1 to n according to their order in the
input file.
Output
The first line of the output file should contain a number, k, showing the maximum
number of activities that a student can attend. The following k lines should contain
the activity number of these activities, arranged in ascending order. Output any one
set if there are more than one set of possible solutions.
Sample Input File
5
2000080809 2000080810
2000080811 2000080815
2000080812 2000080814
2000080813 2000080815
2000080814 2000080818
Sample Output File
3
1
3
5
[Solution of Exercise 1]