Professional Documents
Culture Documents
Lec13 PDF
Lec13 PDF
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.
Lecture 13
Searching II
Readings
CLRS 22.2-22.3
Recall:
graph search: explore a graph
b
Adj
Lecture 13
Searching II
...
s
level
last level
level 1
level 2
BFS (V,Adj,s):
level = { s: }
parent = {s : None }
i=1
frontier = [s]
while frontier:
next = [ ]
for u in frontier:
for v in Adj [u]:
if v not in level:
level[v] = i
parent[v] = u
next.append(v)
frontier = next
i+=1
previous level, i 1
next level, i
Lecture 13
Searching II
Example:
level
level 1
1
a
2
frontier = {s}
frontier1 = {a, x}
frontier2 = {z, d, c}
frontier3 = {f, v}
(not x, c, d)
level 3
level 2
Analysis:
vertex V enters next (& then frontier)
base case: v = s
= Adj[v] looped through only once
O(E) time
- O(V + E) to also list vertices unreachable from v (those still not assigned level)
LINEAR TIME
Shortest Paths:
for every vertex v, fewest edges to get from s to v is
Lecture 13
Searching II
parent
= {s: None}
search from
start vertex s
(only see
stuff reachable
from s)
explore
entire graph
(could do same
to extend BFS)
Lecture 13
Searching II
Example:
S1
forward
edge
back
edge
cross edge
S2
6
back edge
Figure 6: Depth-First Traversal
Edge Classication: