07 Graph Traversal
07 Graph Traversal
Kate Deibel
Summer 2012
Good job!
GRAPH TERMINOLOGY
2 edges here
Minimum? V = {A, B, C, D}
Maximum for undirected? E = {(C, B), (A, B),
Maximum for directed? (B, A), (C, D)}
Minimum? 0
Maximum for undirected? |V||V+1|/2 O(|V|
2
)
Maximum for directed? |V|2 O(|V|2)
Kingston 30 Edmonds
Bainbridge 35
Seattle
60
Bremerton
San Francisco
Dallas
Example path (that also happens to be a cycle):
[Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 27
Path Length and Cost
Path length: Number of edges in a path
Path cost: Sum of the weights of each edge
Example where
P= [ Seattle, Salt Lake City, Chicago, Dallas,
San Francisco, Seattle]
3.5 Chicago
Seattle
2 2 length(P) = 5
cost(P) = 11.5
2 Salt Lake City
2.5
2.5 2.5 Length is sometimes
called "unweighted cost"
3
San Francisco Dallas
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 28
Simple Paths and Cycles
A simple path repeats no vertices (except the
first might be the last):
[Seattle, Salt Lake City, San Francisco, Dallas]
[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]
C
A
G H
How does this relate to the trees
we know and "love"?
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 34
Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed: parent to
children
D E
Picking a root gives a unique B
rooted tree A
A
The tree is simply drawn
B C
differently and with C
undirected edges D E F
F
G H G H
G H D E
Another fact:
If an undirected graph is connected,
then |E| ≥ |V|-1 (pigeonhole principle)
D A B C D
A A F T F F
C
B T F F F
B
C F T F T
D F F F F
Space requirements: B T F F F
O(|V|2) C F T F T
D F F F F
Best for sparse or dense graphs? dense
D
A B /
A
C
B A /
B
C D B /
D /
Insert an edge: D /
Delete an edge:
Space requirements:
A B /
D
A B C / A /
C
B C D B /
D / C /
APPLICATIONS OF
GRAPHS: TRAVERSALS
Related Problems:
Is an undirected graph connected?
Is a digraph weakly/strongly connected?
For strongly, need a cycle back to starting node
Order processed: A, C, F, H, G, B, E, D
A different order but still a perfectly fine
traversal of the graph
Order processed: A, B, C, D, E, F, G, H
A "level-order" traversal
Performance:
Like BFS, IDFS finds shortest paths
Like DFS, IDFS uses less space
Some work is repeated but minor
compared to space savings
Easy:
Store the previous node along the path:
When processing u causes us to add v to the
search, set v.path field to be u)
When you reach the goal, follow path fields back to
where you started (and then reverse the answer)
What's an easy way to do the reversal? A Stack!!
Austin
1
3
San Francisco
2
Dallas
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg:
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 78
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 0 0 0
0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 79
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 80
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 81
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 82
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
CSE 351 CSE 333
CSE 352
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 83
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
CSE 352
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 84
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 85
Example
Output:
126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 86
Example
Output:
126 352
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 87
Example
Output:
126 352
CSE 331 CSE 440
142 440
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0 0
July 23, 2012 CSE 332 Data Abstractions, Summer 2012 88
Running Time?
labelEachVertexWithItsInDegree();
for(i=0; i < numVertices; i++) {
v = findNewVertexOfDegreeZero();
put v next in output
for each w adjacent to v
w.indegree--;
}
Using a queue:
Label each vertex with its in-degree,
Enqueue all 0-degree nodes
While queue is not empty
v = dequeue()
Output v and remove it from the graph
For each vertex u adjacent to v, decrement the in-degree
of u and if new degree is 0, enqueue it