Graphs II - PowerPoint PPT Presentation

About This Presentation
Title:

Graphs II

Description:

Title: Graphs II Author: Stan Sclaroff Last modified by: gkollios Created Date: 4/27/2000 1:17:05 PM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 42
Provided by: StanSc2
Learn more at: https://www.cs.bu.edu
Category:

less

Transcript and Presenter's Notes

Title: Graphs II


1
Graphs II
  • Kruse and Ryba
  • Chapter 12

2
Undirected Graph Example Subway Map
3
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?

4
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.

5
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.

6
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.
  • 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.

7
Breadth-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 ?
8
Breadth-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
9
Breadth-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
10
Breadth-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
11
Breadth-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
12
Breadth-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
13
Breadth-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
14
Breadth-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
15
Breadth-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
16
Breadth-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
17
Breadth-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
18
Breadth-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
19
Breadth-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
20
Breadth-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
21
Breadth-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
22
Breadth-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
23
Breadth-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
24
Breadth-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
25
Breadth-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
26
Breadth-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
27
Breadth-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
28
Breadth-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
29
Breadth-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
30
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

31
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.

32
Professor Bumstead Gets Dressed
socks
shoes
undershorts
watch
pants
shirt
belt
tie
jacket
33
Professor 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
34
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.

35
Topological Sort Example
socks
socks undershorts pants shoes watch shirt belt tie
jacket
shoes
undershorts
watch
pants
shirt
belt
tie
jacket
36
Topological Sort Example
shirt
tie
jacket
socks
shoes
watch
undershorts
pants
belt
37
Depth-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 ?
38
Depth-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 ?
39
Depth-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
40
Recursive 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
41
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.
Write a Comment
User Comments (0)
About PowerShow.com