Upload
imogen-thompson
View
226
Download
3
Embed Size (px)
Citation preview
Shortest Path Problem
• What is the length of the shortest path between s and any other vertex in the graph?
• For now, assume that length of path is defined as the number of edges in the path.
• In the case of the subway example, this would allow us to compute the answer to the question:
What is the length of the shortest path between a particular station s and any other subway station?
Breadth-first Search
Given:– Graph G=(V, E) in edge list representation– Selected source vertex, s
Systematically explore the edges of G to discover every vertex that is reachable from s.
Breadth-first Search
Breadth-first search is so named because it expands the frontier between discovered and undiscovered vertices uniformly across the breadth of the frontier.
To keep track of progress, breadth-first search colors each vertex white, gray or black:– All vertices start out white (undiscovered),
– The first time a vertex is discovered, its color is set to gray,
– When processing of the vertex is complete, its color is set to black.
Breadth-first Search Algorithm
Variables used in breadth first search: [v]: A vertex v is discovered by traversing the
adjacency list of an already discovered vertex v. We say that u is a predecessor of v: [v] = u.
• d[v]: The length of the shortest path from s to v.• color[v]: The color of the vertex v.• Q: a FIFO queue used to manage gray vertices.
Breadth-First Search: Initialization
Given graph G=(V,E), source vertex s V
Create empty queue Q
For each vertex u V – {s}
{
color[u] white
d[u] [u] NIL
}
Color[s] gray
D[s] 0
[s] NIL
Q {s}
0
3
2
1
5
4
Q =
s
Breadth-First Search: Initialization
Given graph G=(V,E), source vertex s V
Create empty queue Q
For each vertex u V – {s}
{
color[u] white
d[u] [u] NIL
}
Color[s] gray
D[s] 0
[s] NIL
Q {s}
0
3
2
1
5
4
Q =
s
d[u] = [, , , , , ][u] = [NIL, NIL, NIL, NIL, NIL, NIL]
Breadth-First Search: Initialization
Given graph G=(V,E), source vertex s V
Create empty queue Q
For each vertex u V – {s}
{
color[u] white
d[u] [u] NIL
}
Color[s] gray
D[s] 0
[s] NIL
Q {s}
0
3
2
1
5
4
Q =
s
0
d[u] = [0, , , , , ][u] = [NIL, NIL, NIL, NIL, NIL, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
0
d[u] = [0, , , , , ][u] = [NIL, NIL, NIL, NIL, NIL, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q = 1 2 30
u
d[u] = [0, 1, 1, 1, , ][u] = [NIL, 0, 0, 0, NIL, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
1 2 3
d[u] = [0, 1, 1, 1, , ][u] = [NIL, 0, 0, 0, NIL, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
2 31
d[u] = [0, 1, 1, 1, , ][u] = [NIL, 0, 0, 0, NIL, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
2 31 4
d[u] = [0, 1, 1, 1, 2, ][u] = [NIL, 0, 0, 0, 1, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
2 3 4
d[u] = [0, 1, 1, 1, 2, ][u] = [NIL, 0, 0, 0, 1, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
3 42
d[u] = [0, 1, 1, 1, 2, ][u] = [NIL, 0, 0, 0, 1, NIL]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
3 42 5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
3 4 5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
4 53
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
4 53
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
4 5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
54
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
54
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
5
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Main Loop
While Q {
u head[Q];for each v Adjacent[u]
if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Enqueue(Q,v)}
Dequeue(Q)color[u] black;
}
0
3
2
1
5
4
Q =
u
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Breadth-First Search: Result
0
3
2
1
5
4
s
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Given d[u] and [u]…
Q: What is the length of the shortest path from s to u? A: Retrieve value d[u].
Q: What is the shortest path from s to u?A: Follow predecessors stored in [u].
Breadth-First Search: Result
0
3
2
1
5
4
s
d[u] = [0, 1, 1, 1, 2, 2][u] = [NIL, 0, 0, 0, 1, 2]
Given d[u] and [u]…
Q: What is the length of the shortest path from s to 5? A: Retrieve value d[u].
Q: What is the shortest path from s to 5?A: Follow predecessors stored in [u]:
[5] = 2, [2] = 0, [2] = NILpath = {0,2,5}
Breadth-first Strategy: Summary
• Breadth-first strategy is used in many graph algorithms (not just shortest path).
• Systematically explore the edges of G to discover every vertex that is reachable from s.
• Basic approach:– Start from source vertex s– Color vertices as they are discovered and finished– Maintain a queue of discovered vertices– Explore graph by following edges from vertices already in queue
to discover new vertices
Graph Search
• Choice of container used to store discovered vertices…– If a queue is used as the container, we get
breadth first search.– If a stack is used as the container, we get
depth first search.
Professor Bumstead Gets Dressed
shirt
tie
jacket
socks
shoes
watchundershorts
pants
belt
Assume that Bumstead can only do one thing at time.
Produce a list of tasks in order such that no task has an edge connecting it to a task earlier in the list.
Topological Sort
Assume a directed acyclic graph G = (V,E).
A topological sort of G is a linear ordering of all its vertices such that if G contains an edge (u,v), then u appears before v in the ordering.
If the graph is cyclic, then no ordering is possible.
Topological Sort Example
shirt
tie
jacket
socks
shoes
watchundershorts
pants
belt
socksundershortspantsshoeswatchshirtbelttiejacket
Topological Sort Example
shirt tie jacketsocks shoes watchundershorts pants belt
shirt
tie
jacket
socks
shoes
watchundershorts
pants
belt
Depth-First Search: Initialization
Given graph G=(V,E)
Create empty stack S
For each vertex u V
{
color[u] white
d[u] [u] NIL
}
0
3
2
1
5
4
S =
Depth-First Search: Initialization
Given graph G=(V,E)
Create empty stack S
For each vertex u V
{
color[u] white
d[u] [u] NIL
}
0
3
2
1
5
4
S =
Depth-First Search: Main Loopfor s V if color[s] = white {
Push(S, s); color[s] gray while not Empty(S) {
u Pop(S)for each v Adjacent[u] if color[v] = white { color[v] gray d[v] d[u] + 1 [v] u Push(S,v) }
color[u] black; }
}
Recursive Depth First Search
Given graph G=(V,E)for each vertex u V[G]{ color[u] white [u] NIL}time 0
for each vertex u V[G] if color[u] = white
DFS-visit(u)
DFS(G)
color[u] grayd[u] time time + 1
for each vertex v Adjacent[u] if color[v] = white {
[v] uDFS-visit(v)
}color[u] blackf[u] time time + 1
DFS-visit(u)