Professional Documents
Culture Documents
b=3
Level 0
d=1
m=1
Level 1
Level 2
Breadth-first
Worst case time is proportional to number of nodes visited
Worst case space is proportional to maximal length of Q
Brian Williams, Spring 03 11
Worst Case Time for Depth-first
Worst case time T is proportional to number of nodes visited
Level 0 1
Level 1
b*1
Level 2
b*b
...
Level m b*b m-1
Tdfs = bm + … b + 1
b * Tdfs = bm+1 + bm + … b Solve recurrence
[b – 1] * Tdfs = bm+1 – 1
Level 1
b*1
Level 2
b*b
...
b*b m-1
Order Notation
• T = O(e) if T = c * e for some constant c
Level 1
b-1
Level m
b-1
...
b
• If a node is queued its parent and parent’s siblings have been queued.
è Sdfs = (b-1)*m+1
• Sdfs = O(b*m)
Brian Williams, Spring 03 15
Cost and Performance
Which is better, depth-first or breadth-first?
S C
A B G
A
D C D G
D
S
C G C G
B
Level 1 b
...
Level d bd
Level d+1 bd+1- b
Level 1 b
...
Level d bd
Level d+1 bd+1- b
First
Level 1 reached
Level d G
Level d+1 G
Level 0
S
Level 1 A B
Level 2
D C D G
Level 3 C G C G
Level 0
S
Level 1 A B
Level 2
D C D G
Level 3 C G C G
Level 0
S
Level 1 A B
Level 2
D C D G
Level 3 C G C G
Level d C G C G 1*bd