Title: SEMINAR 13:
1- SEMINAR 13
- Applications of backtracking
2Problem 1
- Maps coloring Let us consider a geographical
map containing n countries. Propose a coloring of
the map by using mgt4 colors such that any two
neighboring countries have different colors
3Problem 1
- Problem formalization Let us consider that the
neighborhood relation between countries is
represented as a matrix N of n rows and n columns
as follows - 0 if i and j
are not neighbors - N(i,j)
- 1 if i and j
are neighbors - Find a map coloring S(s1,,sn) with sk in
1,,m denoting the color corresponding to
country k such that for all pairs (i,j) with
N(i,j)1 the elements si and sj are different
(siltgtsj)
4Problem 1
- Solution representation
- S(s1,,sn) with sk representing the
color associated to country k - 2. Sets A1,,An 1,2,,m. Each set
will be processed in the natural order of the
elements (starting from 1 to m) - Continuation conditions a partial solution
(s1,s2,,sk) should satisfy siltgtsj for all pairs
(i,j) with N(i,j)1 - For each k it suffices to check that
skltgtsj for all pairs i in 1,2,,k-1 with
N(i,k)1 - 4. Criterion to decide when a partial
solution is a final one k n (all countries
have been colored)
5Problem 1
- Recursive algorithm
- Coloring(k)
- IF kn1 THEN WRITE s1..n
- ELSE
- FOR j1,m DO
- skj
- IF validation(s1..k)
- THEN coloring(k1)
-
Validation algorithm Validation(s1..k) FOR
i1,k-1 DO IF Ni,j1 AND sisk THEN
RETURN False RETURN True
Call Coloring(1)
6Problem 2
- Path finding. Let us consider a set of n towns.
There is a network of routes between these towns.
Generate all routes which connect two given
towns such that the route doesnt cross twice the
same town
Routes from Arad to Constanta 1-gt7-gt3-gt4 1-gt2-gt3
-gt4 1-gt6-gt5-gt3-gt4 1-gt6-gt5-gt2-gt3-gt4
Towns 1.Arad 2.Brasov 3.Bucuresti 4.Constanta 5.
Iasi 6.Satu-Mare 7.Timisoara
Satu Mare
Iasi
Brasov
Arad
Timisoara
Constanta
Bucuresti
7Problem 2
- Problem formalization Let us consider that the
connection information is stored in a matrix C
as follows - 0 if there doesnt exist a
direct route between i and j - C(i,j)
- 1 if there is a direct
route between i and j - Find all routes S(s1,,sm) with sk in 1,,n
denoting the town visited at moment k such that - s1 is the starting town
- sm is the destination town
- siltgtsj for all i ltgtj (a town is visited only
once) - C(si,si1)1 (there exists a direct route between
towns visited at successive moments)
8Problem 2
- Solution representation
- S(s1,,sm) with sk representing the
town visited at moment k - 2. Sets A1,,An 1,2,,n. Each set
will be processed in the natural order of the
elements (starting from 1 to n) - Continuation conditions a partial solution
(s1,s2,,sk) should satisfy - skltgtsj for all i in 1,2,,k-1
- C(sk-1,sk)1
- Criterion to decide when a partial solution is a
final one - sk destination town
9Problem 2
- Recursive algorithm
- routes(k)
- IF sk-1destination town THEN WRITE s1..k-1
- ELSE
- FOR j1,n DO
- skj
- IF validation(s1..k)
- THEN routes(k1)
-
Validation algorithm Validation(s1..k) IF
Csk-1,sk0 THEN RETURN False FOR i1,k-1
DO IF sisk THEN RETURN
False RETURN True
Call s1starting town routes(2)
10Problem 3
- Maze problem. Let us consider a maze defined on
a nxn grid. Find a path in the maze which starts
from the position (1,1) and finishes in (nxn)
Only white cells can be accessed. From a given
cell (i,j) one can pass in one of the following
neighboring positions
(i-1,j)
(i,j)
(i,j-1)
(i,j1)
(i1,j)
Remark cells on the border have
fewer neighbours
11Problem 3
- Problem formalization. The maze is stored as a
nxn matrix - 0 free cell
- M(i,j)
- 1 occupied cell
- Find a path S(s1,,sm) with sk in
1,,nx1,,n denoting the indices
corresponding to the cell visited at moment k - s1 is the starting cell (1,1)
- sm is the destination cell (n,n)
- skltgtsqj for all k ltgtq (a cell is visited at
most once) - M(sk)0 (each visited cell is a free one)
- sk and sk1 are neighborhood cells
12Problem 3
- Solution representation
- S(s1,,sn) with sk representing the
cell visited at moment k - Sets A1,,An are subsets of 1,2,,nx1,2,,n
. For each cell (i,j) there is a set of at most 4
neighbors - Continuation conditions a partial solution
(s1,s2,,sk) should satisfy - skltgtsq for all q in 1,2,,k-1
- M(sk)0
- sk-1 and sk are neighbours
- Criterion to decide when a partial solution is a
final one - sk (n,n)
13Problem 3
maze(k) IF sk-1(n,n) THEN WRITE s1..k ELSE
sk.isk-1.i-1 sk.jsk-1.j IF
validation(s1..k)THEN maze(k1)
sk.isk-1.i1 sk.jsk-1.j IF
validation(s1..k)THEN maze(k1)
sk.isk-1.i sk.jsk-1.j-1 IF
validation(s1..k)THEN maze(k1)
sk.isk-1.i-1 sk.jsk-1.j1 IF
validation(s1..k)THEN maze(k1)
14Problem 3
validation(s1..k) IF sk.ilt1 OR sk.igtn OR
sk.jlt1 OR sk.jgtn THEN RETURN False IF
Msk.i,sk.j1 THEN RETURN False FOR q1,k-1
DO IF sk.isq.i AND sk.jsq.j THEN
RETURN False RETURN True
Call of algorithm maze s1.i1 s1.j1 maze(2
)