Download
# Software Engineering Abdelghani Bellaachia Page Backtracking Examples Maze problem The bicycle lock problem Consider a lock with N switches each of which can be either or PDF document - DocSlides

celsa-spraggs | 2014-12-12 | General

### Presentations text content in Software Engineering Abdelghani Bellaachia Page Backtracking Examples Maze problem The bicycle lock problem Consider a lock with N switches each of which can be either or

Show

Page 1

Software Engineering Abdelghani Bellaachia Page Backtracking Examples: Maze problem The bicycle lock problem: Consider a lock with N switches, each of which can be either 0 or 1. We know that the combination that opens the lock should have at least 1's. Note: The total number of combinations is 2 The solution space can be modeled by a tree Finish Start

Page 2

Software Engineering Abdelghani Bellaachia Page Example: N=3 Characteristics Backtracking technique can be considered as an organized exhaustive search that often av oids searching all possibilities. The solution space can be organized as a tree called: search tree Use depth first search technique The search tree is pruned using a bounding function. Assumptions: X[1..n] contains the solution of the problem All p ossible values of X[i]are elements of a set ROOT 000 001 010 011 101 110 111 100 00 01 10 11

Page 3

Software Engineering Abdelghani Bellaachia Page General algorithm: Procedure backtrack(n) /* X is the solution vector */ Integer k; Begin k =1; Compute S ; /* compute the possible solution values for k=1 */ While k> 0 do While S <> do X[k] = a n element of S = S {X[k]}; If B(X[1], …, X[i],…, X[k]) = True Then Print the solution vector X; else begin k = k+1; Compute S End; End; End while; k = k 1; End while; End;

Page 4

Software Engineering Abdelghani Bellaachia Page Recursive solution: Procedure back_ recursive(k) begin For each X[k] in Sk do If B(X[1], …, X[i],…, X[k]) = True then Print the solution vector X; else begin Compute S Back_recursive(k+1); end if; end for; end; Examples: queen sum of subsets Hamiltonian cycle Gra ph coloring

Page 5

Software Engineering Abdelghani Bellaachia Page queen problem: Objective: place n queen in an n by n chessboard such that no two queens are not on: 1) same row 2) same column 3) same diagonal 4) 1), 2), and 3) form the bounding function Example: N=4 Brute forc e method or exhaustive search: takes O( Using condition 2) reduce the solution space to n Using condition 1) reduce the solution space to n! The solution vector is characterized as follows: X[I] contains the column positi on of the queen i in the ith row. = {1,2,…, n} S represents the number of columns for queen k in the kth row.

Page 6

Software Engineering Abdelghani Bellaachia Page Algorithm: Function bound(k) Integer i; Begin For i=1 to k 1 do /* for each row up to k 1 */ If X[i] = X[k] /* Are queens on the sam e row?*/ or |X[i] X[k]| = |i k| /* Are queens are on the same diagonal */ then return (False); endif; endfor; return(True); end;

Page 7

Software Engineering Abdelghani Bellaachia Page Sum of subsets Input: n distinct positive numbers w where 1<=I<=n and integer M Output: all combinat ions whose sum is M Solution: Use static binary tree where level I corresponds to the selection of w . The solution vector X is defined as follows: It a bit map vector where X[i] contains 1 if the is included; otherwise it contains 0; Example: n=4 (w1,w2,w3,w4) = (11,13,24,7) M = 31

Page 8

Software Engineering Abdelghani Bellaachia Page W1 W2 W3 W4 Bounding function: Function bound(k) Begin If and Then Return(True); Else Return(False); Endif; End; ROO 1000 S=11<3 0000 S=0<31 1100 S=11+13<31 1101 S=11+13+7 = 31 1000 S=11<3 1100 S=11+13<31 1110 S=11+13+24>31 1010 S=11+24>31 1100 S=11+13<3 1000 S=11<3

Page 9

Software Engineering Abdelghani Bellaachia Page Hamiltonian Cycle Defin ition: A Hamiltonian cycle in an undirected graph G=(V,E) is a simple cycle that passes through every vertex once. Input: a graph G with n vertices Output: Hamiltonian cycle Solution: Use dynamic tree where level i corresponds to the selection of the i th vertex. The solution vector X[1..n] is such that X[I] is the vertex in the cycle. Example: Start at vertex 1

Page 10

Software Engineering Abdelghani Bellaachia Page 10 3 4 5 3 4 5 6 3 4 5 6 7 3 4 5 6 7 8 2 3 4 5 6 7 8 4 3 1 X[2] X[3] X[8]

Page 11

Software Engineering Abdelghani Bellaachia Page 11 Bounding function: Function bound(k) integer i; begin for i=1 to k 1 do /* check f or distinctness */ If x[i]=x[k] then return(False); endif; endfor; If (X[k],X[k] 1) is an edge in G then return (True); else return(False); endif; end;

Page 12

Software Engineering Abdelghani Bellaachia Page 12 Graph coloring: Definition: A coloring of a graph G=(V,E) is a mapping F:V C w here C is a finite set of colors such that if is an element of E then F(v) is different from F(w); in other words, adjacent vertices are not assigned the same color. Input: Graph G of n vertices and a set of m colors in C={1,2,…,m} Output: col or the vertices of G such that no two adjacent vertices have the same color Solution: Use a static tree The solution vector X[1..n] is such X[i has the color of the ith node. Example: n=4 and m=3

Page 13

Software Engineering Abdelghani Bellaachia Page 13 Bounding function Function next_color(k) Integer i; begin for i=1 to k 1 do If ((k,i) is an edge) and (X[i]=X[k]) then return (False); endif; endfor; end; X[2] X[1] X[ 3] X[4]

We know that the combination that opens the lock should have at least 1s Note The total number of combinations is 2 The solution space can be modeled by a tree Finish Start brPage 2br Software Engineering Abdelghani Bellaachia Page Example N3 Charac ID: 22420

- Views :
**248**

**Direct Link:**- Link:https://www.docslides.com/celsa-spraggs/software-engineering-abdelghani
**Embed code:**

Download this pdf

DownloadNote - The PPT/PDF document "Software Engineering Abdelghani Bellaach..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

Page 1

Software Engineering Abdelghani Bellaachia Page Backtracking Examples: Maze problem The bicycle lock problem: Consider a lock with N switches, each of which can be either 0 or 1. We know that the combination that opens the lock should have at least 1's. Note: The total number of combinations is 2 The solution space can be modeled by a tree Finish Start

Page 2

Software Engineering Abdelghani Bellaachia Page Example: N=3 Characteristics Backtracking technique can be considered as an organized exhaustive search that often av oids searching all possibilities. The solution space can be organized as a tree called: search tree Use depth first search technique The search tree is pruned using a bounding function. Assumptions: X[1..n] contains the solution of the problem All p ossible values of X[i]are elements of a set ROOT 000 001 010 011 101 110 111 100 00 01 10 11

Page 3

Software Engineering Abdelghani Bellaachia Page General algorithm: Procedure backtrack(n) /* X is the solution vector */ Integer k; Begin k =1; Compute S ; /* compute the possible solution values for k=1 */ While k> 0 do While S <> do X[k] = a n element of S = S {X[k]}; If B(X[1], …, X[i],…, X[k]) = True Then Print the solution vector X; else begin k = k+1; Compute S End; End; End while; k = k 1; End while; End;

Page 4

Software Engineering Abdelghani Bellaachia Page Recursive solution: Procedure back_ recursive(k) begin For each X[k] in Sk do If B(X[1], …, X[i],…, X[k]) = True then Print the solution vector X; else begin Compute S Back_recursive(k+1); end if; end for; end; Examples: queen sum of subsets Hamiltonian cycle Gra ph coloring

Page 5

Software Engineering Abdelghani Bellaachia Page queen problem: Objective: place n queen in an n by n chessboard such that no two queens are not on: 1) same row 2) same column 3) same diagonal 4) 1), 2), and 3) form the bounding function Example: N=4 Brute forc e method or exhaustive search: takes O( Using condition 2) reduce the solution space to n Using condition 1) reduce the solution space to n! The solution vector is characterized as follows: X[I] contains the column positi on of the queen i in the ith row. = {1,2,…, n} S represents the number of columns for queen k in the kth row.

Page 6

Software Engineering Abdelghani Bellaachia Page Algorithm: Function bound(k) Integer i; Begin For i=1 to k 1 do /* for each row up to k 1 */ If X[i] = X[k] /* Are queens on the sam e row?*/ or |X[i] X[k]| = |i k| /* Are queens are on the same diagonal */ then return (False); endif; endfor; return(True); end;

Page 7

Software Engineering Abdelghani Bellaachia Page Sum of subsets Input: n distinct positive numbers w where 1<=I<=n and integer M Output: all combinat ions whose sum is M Solution: Use static binary tree where level I corresponds to the selection of w . The solution vector X is defined as follows: It a bit map vector where X[i] contains 1 if the is included; otherwise it contains 0; Example: n=4 (w1,w2,w3,w4) = (11,13,24,7) M = 31

Page 8

Software Engineering Abdelghani Bellaachia Page W1 W2 W3 W4 Bounding function: Function bound(k) Begin If and Then Return(True); Else Return(False); Endif; End; ROO 1000 S=11<3 0000 S=0<31 1100 S=11+13<31 1101 S=11+13+7 = 31 1000 S=11<3 1100 S=11+13<31 1110 S=11+13+24>31 1010 S=11+24>31 1100 S=11+13<3 1000 S=11<3

Page 9

Software Engineering Abdelghani Bellaachia Page Hamiltonian Cycle Defin ition: A Hamiltonian cycle in an undirected graph G=(V,E) is a simple cycle that passes through every vertex once. Input: a graph G with n vertices Output: Hamiltonian cycle Solution: Use dynamic tree where level i corresponds to the selection of the i th vertex. The solution vector X[1..n] is such that X[I] is the vertex in the cycle. Example: Start at vertex 1

Page 10

Software Engineering Abdelghani Bellaachia Page 10 3 4 5 3 4 5 6 3 4 5 6 7 3 4 5 6 7 8 2 3 4 5 6 7 8 4 3 1 X[2] X[3] X[8]

Page 11

Software Engineering Abdelghani Bellaachia Page 11 Bounding function: Function bound(k) integer i; begin for i=1 to k 1 do /* check f or distinctness */ If x[i]=x[k] then return(False); endif; endfor; If (X[k],X[k] 1) is an edge in G then return (True); else return(False); endif; end;

Page 12

Software Engineering Abdelghani Bellaachia Page 12 Graph coloring: Definition: A coloring of a graph G=(V,E) is a mapping F:V C w here C is a finite set of colors such that if is an element of E then F(v) is different from F(w); in other words, adjacent vertices are not assigned the same color. Input: Graph G of n vertices and a set of m colors in C={1,2,…,m} Output: col or the vertices of G such that no two adjacent vertices have the same color Solution: Use a static tree The solution vector X[1..n] is such X[i has the color of the ith node. Example: n=4 and m=3

Page 13

Software Engineering Abdelghani Bellaachia Page 13 Bounding function Function next_color(k) Integer i; begin for i=1 to k 1 do If ((k,i) is an edge) and (X[i]=X[k]) then return (False); endif; endfor; end; X[2] X[1] X[ 3] X[4]

Today's Top Docs

Related Slides