0% found this document useful (0 votes)
828 views8 pages

Backtracking Dfs Bfs

The document discusses backtracking, depth-first search (DFS), and breadth-first search (BFS) algorithms. It provides examples of using backtracking to solve graph coloring and subset sum problems. DFS traverses deeper nodes before shallower nodes, resulting in a depth-first traversal, while BFS traverses all nodes at the current level before moving to deeper levels, resulting in a breadth-first traversal. The document compares DFS and BFS approaches on sample problems and trees.

Uploaded by

katzumato
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
828 views8 pages

Backtracking Dfs Bfs

The document discusses backtracking, depth-first search (DFS), and breadth-first search (BFS) algorithms. It provides examples of using backtracking to solve graph coloring and subset sum problems. DFS traverses deeper nodes before shallower nodes, resulting in a depth-first traversal, while BFS traverses all nodes at the current level before moving to deeper levels, resulting in a breadth-first traversal. The document compares DFS and BFS approaches on sample problems and trees.

Uploaded by

katzumato
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

45378874.doc p.

Notes on Backtracking, DFS (Depth First Search) and BFS


(Breadth First Search)

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

Consider the following problem. Given a set of positive integers


xl, x2, . . ., xn, find a subset of this set of integers that has M as its sum. Output 'TRUE' if
such subset exists and output 'FALSE' otherwise. How can backtracking be used to solve
this problem?

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.

** numbers are sorted in descending order

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;

Mark the order of traversal in the above tree with numbers.

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

Note that for trees DFS always uses BACKTRACKING.

[Exercise] Mark the order of traversal if BFS is used.

[Exercise] Rewrite the function exist if the numbers are not sorted.

BFS

We like to begin the discussion with the following problem:


column (j)
num[i,j] 1 2 3 4
1 2 4 3 5
row (i) 2 4 2 5 6
3 6 3 4 2
4 4 3 5 6

You start from (1,1).


At (x,y), you can go to (x+1,y) or (x+1,y+1).
The problem is to find the path so that the sum of num[i,j] is the minimum.
The output is somewhat like:

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.

Delete the said nodes.

Since there is no need to backtrack, BFS is always implemented with the use of QUEUE.
There are two pointers HEAD and TAIL:

HEAD - first node to be processed


TAIL - Last nodes to be processed.
H T
(1,1)
2
(-1,-1)

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..

Carry on the process until all elements are traversed.

(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,2) (2,2) (2,2)

What is the minimum accumulated sum ? __________________________________

How to find and output the path corresponding to the said sum?

Write down the path involved.

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

What is the minimum accumulated sum ? __________________________________


Write down the path involved.

Solve the problem by completing the program:


program pathfinding;
45378874.doc p.6

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;

procedure dequeue(var a,b,c,d,s:integer);


begin
a:=q[head].x;
b:=q[head].y;
c:=q[head].px;
d:=q[head].py;
s:=q[head].s;
head:=head+1;
end;

begin
assign(fl,'dfs.txt');
reset(fl);
{ readln(fl,v);
readln(fl,n); }

for i:=1 to 10 do begin


for j:=1 to 10 do
read(fl,num[i,j]);
readln(fl);
end;

head:=1;
tail:=0;

enqueue(1,1,-1,-1,num[1,1]);
45378874.doc p.7

while notempty do begin


dequeue(a,b,c,d,s);

if a<10 then begin {if the node is not at the base}


{process (a+1,b) and (a+1,b+1)}

{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]

In your drive, there are two programs bfs.exe and dfs.exe.


Run the programs and compare the time needed for 23x23 integers.

Time needed by bfs is _______________________


Time needed by dfs is _______________________

Explain the discrepancy

[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

Program Name: PROGRAM1.EXE

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 Input File


SAMPLE1.TXT
4
25
12
46
35
45378874.doc p.8

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

Program name: PROGRAM1.EXE


Name of input file: hkoi01f?.TXT ? being 1 to 3
Name of output file: OUTPUT1.TXT
Maximum execution time for each data set: 2 seconds
Many orientation activities in a university clash with one another in time allocation. A
student may only participate in some of them.
The beginning and ending time of any activity is given. A student participating in an
activity is required to arrive on or before the starting time and leave on or after the ending
time. Two activities, x and y, can be both attended by a student if and only if the ending
time of x is not later than the starting time of y or the ending time of y is not later than the
starting time of x.

Write a program to schedule the activities for a student so as to maximise the


number of activities that this student is able to attend.

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]

You might also like