Lec 12
Lec 12
006- Introduction to
Algorithms
Ponytail No ponytail
Beard Erik ?
3
Unit #4 Overview: Searching
Today: Introduction to Games and Graphs
• Rubik’s cube, Pocket cube, Game space
• Graph definitions, representation, searching
Tuesday: Graph algorithms and analysis
• Breadth First Search, Depth First Search
• Queues, Stacks, Augmentation, Topological sort
Thursday: Networks in biology and real world
• Network/node properties, metrics, motifs, clusters
• Dynamic processes, epidemics, growth, resilience
Last time: Games and Graphs
Pocket Cube
• 2 × 2 × 2 Rubik’s cube
• Start with any colors
• Moves are quarter
turns of any face
• “Solve” by making
each side one color
Searching for a solution path
27 two-away
6 neighbors
1 turn
d
c b c
• V={a,b,c,d}
• V = {a,b,c}
• E={{a,b}, {a,c}, {b,c},
{b,d}, {c,d}} • E = {(a,c), (a,b) (b,c), (c,b)}
Graph Representation
• Adjacency lists • Incidence lists
(a,c) (a,b) /
a c b / a
b c / b (b,c) /
c c (c,b) /
b /
• Adjacency matrix • Implicit representation
a (1) b (2) c (3)
a
Neighbors(a)
[c,b]
0 1 1 a
(1)
Neighbors(b)
[b]
0 0 1 b (2)
?
Searching Graph
• We want to get from current Rubik state to
“solved” state
• How do we explore?
Breadth First Search
• start with vertex v
• list all its neighbors (distance 1)
• then all their neighbors (distance 2)
• etc.
• algorithm starting at s:
define frontier F
initially F={s}
repeat F=all neighbors of vertices in F
until all vertices found
Depth First Search
• Like exploring a maze
• From current vertex, move to another
• Until you get stuck
• Then backtrack till you find a new place to
explore
• Exploring a maze • “left-hand” rule
How to handle cycles: BFS/DFS
• What happens if unknowingly revisit a vertex?
Will eventually happen if graph contains a cycle
• BFS: get wrong notion of distance
• DFS: may get in circles
• Solution: mark vertices
BFS: if you’ve seen it before, ignore
DFS: if you’ve seen it before, back up
Breadth First Search (BFS)
BFS algorithm outline
• Initial vertex s
Level 0
s
• For i=1,…
grow level i Level
3
Find all neighbors of level i-1
vertices
Level
2
(except those already seen)
Level
1
i.e. level i contains vertices
reachable via a path of i edges
and no fewer
BFS example
1 0 2
a s d f 3
1
2
z x c v 3
2
z
a d f
s
x
c
v
BFS algorithm outline
• Initial vertex s
Level 0
• For i=1,…
grow level i s
1
2
z x c v 3
2
z
The
only
edges
a
d
f
not
traversed
by
BFS
s
link
verHces
x
c
v
within
the
same
level
BFS Algorithm
• BFS(V,Adj,s)
level={s: 0}; parent = {s: None}; i=1
frontier=[s] #previous level, i-1
while frontier
next=[] #next level, i
for u in frontier
for v in Adj[u]
if v not in level #not yet seen
level[v] = i #level of u+1
parent[v] = u
next.append(v)
frontier = next
i += 1
BFS Analysis: Runtime
• Naïve analysis: outer loop |V| * inner loop |V|
• Vertex v appears at the frontier at most once
Since then it has a level
And nodes with a level aren’t added again
Total time spent adding nodes to frontier O(n)
• Adj[v] only scanned once
Just when v is in frontier
Total time ∑vAdj[v]
| |
• This sum counts each “outgoing” edge
• So O(m) time spend scanning adjacency lists
• Total: O(m+n) time --- “Linear time”
For sparse graphs |V|+|E| is much better than |V|2
BFS Analysis: Correctness
i.e. why are all nodes reachable from s explored?
(we’ll actually prove a stronger claim)
2
(in
tree)
3
(in
tree)
c
7
(cross
edge)
b
d
b
c
a
s
d
DFS Runtime Analysis
• Quite similar to BFS
• DFS-visit only called once per vertex v
Since next time v is in parent set
• Edge list of v scanned only once (in that call)
• So time in DFS-visit is:
1 per vertex + 1 per edge
• So time is O(n+m)
DFS Correctness?
• Trickier than BFS
• Can use induction on length of shortest path from
starting vertex
Inductive Hypothesis:
“each vertex at distance k is visited (eventually)”
Induction Step:
• Suppose vertex v at distance k.
Then some u at shortest distance k-1 with edge (u,v)
Can decompose into su at shortest distance k-1, and (u,v)
• By inductive hypothesis: u is visited (eventually)
• By algorithm: every edge out of u is checked
If v wasn’t previously visited, it gets visited from u (eventually)
Edge Classification
• Tree edge used to get to new child
• Back edge leads from node to ancestor in tree
• Forward edge leads to descendant in tree
• Cross edge leads to a different subtree
• To label what edge is of what type, keep global
time counter and store interval during which
vertex is on recursion stack
tree
edge
Back
edge
Cross
edge
Forward
edge
BFS vs. DFS
The ‘frontier’ of BFS exploration
1 0 2
a s d f 3
1
2
z x c v 3
2
z
The
only
edges
a
d
f
not
traversed
by
BFS
s
link
verHces
x
c
v
within
the
same
level
The tree of DFS exploration
1
(in
tree)
s
5
(forward
edge)
a
2
(in
tree)
3
(in
tree)
c
7
(cross
edge)
b
d
b
c
a
s
d
BFS/DFS Algorithm Summary
• Maintain “todo list” of vertices to be scanned
41