Title: Graphs II
1Graphs II
- Kruse and Ryba
- Chapter 12
2Undirected Graph Example Subway Map
3Shortest 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?
4Breadth-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.
5Breadth-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.
6Breadth-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. - dv The length of the shortest path from s to
v. - colorv The color of the vertex v.
- Q a FIFO queue used to manage gray vertices.
7Breadth-First Search Initialization
1
4
Given graph G(V,E), source vertex s ? V Create
empty queue Q For each vertex u ? V
s coloru ? white du ? ? ?u ?
NIL Colors ? gray Ds ? 0 ?s ? NIL Q ?
s
0
2
5
3
s
Q ?
8Breadth-First Search Initialization
1
4
Given graph G(V,E), source vertex s ? V Create
empty queue Q For each vertex u ? V
s coloru ? white du ? ? ?u ?
NIL Colors ? gray Ds ? 0 ?s ? NIL Q ?
s
0
2
5
3
s
Q ?
du ?, ?, ?, ?, ?, ? ?u NIL, NIL, NIL,
NIL, NIL, NIL
9Breadth-First Search Initialization
1
4
Given graph G(V,E), source vertex s ? V Create
empty queue Q For each vertex u ? V
s coloru ? white du ? ? ?u ?
NIL Colors ? gray Ds ? 0 ?s ? NIL Q ?
s
0
2
5
3
s
Q ?
0
du 0, ?, ?, ?, ?, ? ?u NIL, NIL, NIL,
NIL, NIL, NIL
10Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
0
du 0, ?, ?, ?, ?, ? ?u NIL, NIL, NIL,
NIL, NIL, NIL
11Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
1
2
3
0
du 0, 1, 1, 1, ?, ? ?u NIL, 0, 0, 0,
NIL, NIL
12Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
1
2
3
du 0, 1, 1, 1, ?, ? ?u NIL, 0, 0, 0,
NIL, NIL
13Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
2
3
1
du 0, 1, 1, 1, ?, ? ?u NIL, 0, 0, 0,
NIL, NIL
14Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
2
3
1
4
du 0, 1, 1, 1, 2, ? ?u NIL, 0, 0, 0,
1, NIL
15Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
2
3
4
du 0, 1, 1, 1, 2, ? ?u NIL, 0, 0, 0,
1, NIL
16Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
3
4
2
du 0, 1, 1, 1, 2, ? ?u NIL, 0, 0, 0,
1, NIL
17Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
3
4
2
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
18Breadth-First Search Main Loop
u
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
Q ?
3
4
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
19Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
4
5
3
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
20Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
4
5
3
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
21Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
4
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
22Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
u
0
2
5
3
Q ?
5
4
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
23Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
u
0
2
5
3
Q ?
5
4
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
24Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
u
0
2
5
3
Q ?
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
25Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
26Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
5
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
27Breadth-First Search Main Loop
1
4
While Q ? ? u ? headQ for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du 1
?v ? u Enqueue(Q,v) Dequeue(Q) co
loru ? black
0
2
5
3
u
Q ?
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
28Breadth-First Search Result
1
4
Given du and ?u Q What is the length of
the shortest path from s to u? A Retrieve value
du. Q What is the shortest path from s to
u? A Follow predecessors stored in ?u.
0
2
5
3
s
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
29Breadth-First Search Result
1
4
Given du and ?u Q What is the length of
the shortest path from s to 5? A Retrieve value
du. Q What is the shortest path from s to
5? A Follow predecessors stored in ?u
0
2
5
3
s
du 0, 1, 1, 1, 2, 2 ?u NIL, 0, 0, 0,
1, 2
?5 2, ?2 0, ?2 NIL path 0,2,5
30Breadth-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
31Graph 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.
32Professor Bumstead Gets Dressed
socks
shoes
undershorts
watch
pants
shirt
belt
tie
jacket
33Professor Bumstead Gets Dressed
socks
shoes
undershorts
watch
pants
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.
shirt
belt
tie
jacket
34Topological 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.
35Topological Sort Example
socks
socks undershorts pants shoes watch shirt belt tie
jacket
shoes
undershorts
watch
pants
shirt
belt
tie
jacket
36Topological Sort Example
shirt
tie
jacket
socks
shoes
watch
undershorts
pants
belt
37Depth-First Search Initialization
1
4
Given graph G(V,E) Create empty stack S For each
vertex u ? V coloru ? white du ? ? ?u
? NIL
0
2
5
3
S ?
38Depth-First Search Initialization
1
4
Given graph G(V,E) Create empty stack S For each
vertex u ? V coloru ? white du ? ? ?u
? NIL
0
2
5
3
S ?
39Depth-First Search Main Loop
for s? V if colors white
Push(S, s) colors ? gray while not
Empty(S) u ? Pop(S) for each v ?
Adjacentu if colorv white
colorv ? gray dv ? du
1 ?v ? u Push(S,v)
coloru ? black
40Recursive Depth First Search
DFS(G)
DFS-visit(u)
Given graph G(V,E) for each vertex u ? VG
coloru ? white ?u ? NIL time ?
0 for each vertex u ? VG if coloru
white DFS-visit(u)
coloru ? gray du ? time ? time 1 for each
vertex v ? Adjacentu if colorv white
?v ? u DFS-visit(v) coloru
? black fu ? time ? time 1
41Graph 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.