Unit 6 Exploring Graphs
Unit 6 Exploring Graphs
Exploring graphs
• These three techniques explore the tree from left to right. Three
corresponding techniques explore the tree from right to left.
– Let v and w be two nodes in the tree. In preorder we first number a node and
then number its subtress from left to right. Thus
• If prenum[v] <= prenum[w] then v is an ancestor of w OR v is to the left of w in the tree.
– In postorder we first number the subtrees of a node from left to right, and then
we number the node itself. Thus
• If postnum[v] >= postnum[w] then v is an ancestor of w OR v is to the right of w in the
tree.
– It follows that
• If prenum[v] <= prenum[w] and postnum[v] >= postnum[w] then v is an ancestor of w.
• Once the values of prenum and postnum have been calculated in a time in Θ(n), the
required condition can be checked in a time in Θ(1).
Prepared by : Prof. Madhuri Vaghasia 13
Breadth-first searching
• A breadth-first search (BFS)
A explores nodes nearest the
root before exploring nodes
B C
further away
• For example, after searching
A, then B, then C, the search
D E F G
proceeds with D, E, F, G
• Node are explored in the
H I J K order A B C D E F G H I J K L
MNOPQ
L M N O P Q • J will be found before N
v w x y
0
v w x y
Q: Ss
1 0
1
v w x y
Q: w r
1 0 2
1 2
v w x y
Q: r t x
1 0 2
2 1 2
v w x y
Q: t x v
1 0 2 3
2 1 2
v w x y
Q: x v u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
1 0 2 3
2 1 2 3
v w x y
Q: u y
1 0 2 3
2 1 2 3
v w x y
Q: y
1 0 2 3
2 1 2 3
v w x y
Q: Ø
1 2 3 1 2 3
• ANS: /null
4
/null
5
0/null
6
/null
4
/null
5
0/null
6
/null /null /null /null 1/3 1/3
Q 6 4 Q 4
1 2 3 1 2 3
/null /null 0/null /null /null 0/null
4 5 6 4 5 6
2/5 1/3 1/3 2/5 1/3 1/3
Q 2 Q
1 2 3
/null 3/4 0/null 1 2 3
/null 3/4 0/null
4 5 6
2/5 1/3 1/3
4 5 6
2/5 1/3 1/3
• procedure dfs(v)
{
{Node v has not previously been visited}
pnum pnum + 1
prenum[v] pnum
mark[v] visited
for each node w adjacent to v do
if mark[w] ≠ visited then dfs(w)
}
v t
w u
x y
10/11 z
Prepared by : Prof. Madhuri Vaghasia 40
Example
s2 = {a, c, b, f, e, d, h, g, i}
s3 = {a, b, d, c, e, g, f, h, i}
s4 = {a, c, f, b, e, h, d, g, i}
etc.
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
Weight 2 3 4 5
Profit 3 5 6 10
Capacity 8
4,4;
2,2;6 2,3;8 2,4;9 2,5;13 3,3;10 3,4;11 3,5;15
12
2,2,2,2;1
2
Weight 2 3 4 5
Profit 3 5 6 10
Capacity 8
Prepared by : Prof. Madhuri Vaghasia 76
Algorithm
function backtrack(i , r)
{ Calculates the value of the best load that can be constructed
using items of types I to n and whose total weight does not
exceed r }
b=0
{ Try each allowed kind of item in turn }
for k = i to n do
{
if (w[k]<=r) then
b = max(b , v[k]+ backpack (k, r-w[k]))
}
return b;
Prepared by : Prof. Madhuri Vaghasia 78
N-queen Problem
• Given is a chess board. A chess board has 8x8
fields. Is it possible to place 8 queens on this
board, so that no two queens can attack each
other?
• A queen can attack horizontally, vertically, and
on both diagonals, so it is pretty hard to place
several queens on one board so that they
don’t attack each other.