10/6/10
1
Paths in graphs
The classic 15puzzle
Graph G = (V,E)
V = {configurations of puzzle}
E: edges between neighboring configurations
c
a
g
e
b
f
j
d
i
h
a
b
c
d
e
f
h
i
explore(G,a):
Finds a path from a to i.
But this isn’t the shortest possible path!
Distances in graphs
Distance between two nodes
= length of shortest path between them
c
a
g
e
b
f
j
d
i
h
a
b
c
d
e
f
h
i
distance 0
distance 1
distance 2
distance 3
Physical model:
Vertex – pingpong ball
Edge – piece of string
dist(a,e) = ?
dist(d,g) = ?
Suppose we want to compute
distances from some starting node s
to all other nodes in G.
Strategy: layerbylayer
first, nodes at distance 0
then, nodes at distance 1
then, nodes at distance 2, etc.
Breadthfirst search
Suppose we have seen all nodes at
distance
·
d.
How to get the next layer?
Solution:
A node is at distance d+1 if:
it is adjacent to some node at
distance d
it hasn’t been seen yet
procedure bfs(G,s)
input: graph G = (V,E); node s in V
output: for each node u, dist[u] is
set to its distance from s
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Spring '08
 staff
 Graph Theory, 2 g, dist, 300 G

Click to edit the document details