• No results found

Backtracking Dfs Bfs

N/A
N/A
Protected

Academic year: 2021

Share "Backtracking Dfs Bfs"

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

Notes

Notes on

on

Backtracking,

Backtracking, DFS

DFS (Depth

(Depth First

First Search) and

Search) and BFS

BFS

(Breadth First Search)

(Breadth First Search)

Backtracking

Backtracking

There are problems that can be

There are problems that can be solved only by performing an exhaustive search solved only by performing an exhaustive search of allof all  possible solutions. One way to search systematically for a solution is to use a decision  possible solutions. One way to search systematically for a solution is to use a decision

tree, where each internal vertex represents a

tree, where each internal vertex represents a decision and each leaf a decision and each leaf a possible solution.possible solution. T

To find a solution via backtracking, o find a solution via backtracking, first make a sequence of decisions in an first make a sequence of decisions in an attempt toattempt to reach a solution as long as

reach a solution as long as this is possible. The sequence of decisions can be this is possible. The sequence of decisions can be representedrepresented  by a path in the de

 by a path in the decision tree. Once it is known that ncision tree. Once it is known that no solution can result from anyo solution can result from any further sequence of decisions, backtrack to the parent of the current vertex and work  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 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

continues until a solution is found, or it is established that no solution exists. Theno solution exists. The following examples illustrate the usefulness of backtracking.

following examples illustrate the usefulness of backtracking.

How can backtracking be used to decide whether a graph can be colored using n colors? How can backtracking be used to decide whether a graph can be colored using n colors? Solution: W

Solution: We can e can solve this problem using solve this problem using backtracking in the backtracking in the following wayfollowing way. First pick . First pick  some vertex a and assign it color 1. Then pick a second vertex b, and if b is not adjacent 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 a, assign it color 1. Otherwise, assign color 2 to b. Then go on to to b. Then go on to a third vertex c. Usea third vertex c. Use color 1, if possible, for c. Otherwise use color 2

color 1, if possible, for c. Otherwise use color 2 , if this is possible. Only if neither color 1, 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 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

 possible to assign one of the n colors to each additional vertex, always using the colors to each additional vertex, always using the firstfirst allowable color in the list. If a vertex is reach

allowable color in the list. If a vertex is reached that cannot be coed that cannot be colored by any of the nlored by any of the n colors, backtrack to the last assignment made

colors, backtrack to the last assignment made and change the coloring and change the coloring of the last vertexof the last vertex colored, if possible, using the next allowable color in the list. If it is not possible to colored, if possible, using the next allowable color in the list. If it is not possible to change this coloring, backtrack further to p

change this coloring, backtrack further to previous assignments, one step back at a revious assignments, one step back at a time,time, until is possible to change a coloring of a vertex. Then continue assigning colors of  until is possible to change a coloring of a vertex. Then continue assigning colors of  additional vertices as long as possible. If a coloring u

additional vertices as long as possible. If a coloring u sing n colors exists, backtrackingsing n colors exists, backtracking will produce it. (Unfortunately this procedure can be extremely inefficient.)

will produce it. (Unfortunately this procedure can be extremely inefficient.) In particular, consider the problem of coloring the graph shown in

In particular, consider the problem of coloring the graph shown in Figure 10 with threeFigure 10 with three colors. The tree shown in Figure 10 illustrates how backtracking can

colors. The tree shown in Figure 10 illustrates how backtracking can be used to constructbe used to construct a 3-coloring. In this procedure, red is used

a 3-coloring. In this procedure, red is used first, then blue, and finally green. This simplefirst, then blue, and finally green. This simple example can obviously be done

example can obviously be done without backtracking, but it is a good without backtracking, but it is a good illustration of theillustration of the technique.

technique.

In this tree, the initial path from the root, which

(2)

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.

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

(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) H T (1,1) 2 (-1,-1) (2,1) 6 (1,1) (2,2) 4 (1,1)

* (1,1) will lead to (2,1) and (2,2)

H T (1,1) 2 (-1,-1) (2,1) 6 (1,1) (2,2) 4 (1,1) (3,1) 12 (2,1) (3,2) 9 (2,1)

* (2,1) will lead to (3,1) and (3,2)  position of 

current node accumulated

sum  parent node

(5)

H T (1,1) 2 (-1,-1) (2,1) 6 (1,1) (2,2) 4 (1,1) (3,1) 12 (2,1) (3,2) 7 (2,2) (3,3) 8 (2,2)

* (2,2) will lead to (3,2) and (3,3)

State the reason why the sum and the parent of the 5thnode is changed to new ones..

Carry on the process until all elements are traversed. (1,1) 2 (-1,-1) (2,1) 6 (1,1) (2,2) 4 (1,1) (3,1) 12 (2,2) (3,2) 7 (2,2) (3,3) 8 (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;

(6)

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]);

(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 2 5 1 2 4 6 3 5

(8)

(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 aren 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 ton 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 followingk 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

References

Related documents

If a customer wants to use an alternate form of currency for a contract-based payment, refer to the Manual Payment method described in the subsection Editing a

Finally, various versions are available with an output DIN flange or a pump connection, suitable for direct mounting of a hydraulic pump according to ISO standard 7653 (type

Michael Porter's US inspired City Growth Strategy (CGS) initiative presented an alternative approach to urban economic regeneration within the U K characterised by a focus on

As journalist George Padmore proclaimed, ‘Ever since Britain’s economic dependence upon the United States for loans on the one hand, and military reliance in the event of a third

As apparent from the overview of current approaches to event processing in a semantic context in section 4.2, the majority of approaches uses a point in time based model for the

Over the past decade we have been working with colleagues from around the globe and from a variety of disciplines to quantify human capital management and use the resultant

The study of fixed points for non-compatible mappings can be extended to the class of non-expansive or Lipschitz mapping pairs even without continuity of the mappings involved

Options b and c do not apply for the Central and Western Pyrenees, although they might explain part of the activity of the normal faults located in the Eastern Pyrenees and next