on-line

CS61B Lecture #36
Today’s Readings: Graph Structures: DSIJ, Chapter 12
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
1
Why Graphs?
• For expressing non-hierarchically related items
• Examples:
– Networks: pipelines, roads, assignment problems
– Representing processes: flow charts, Markov models
– Representing partial orderings: PERT charts, makefiles
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
2
Some Terminology
• A graph consists of
– A set of nodes (aka vertices )
– A set of edges: pairs of nodes.
– Nodes with an edge between are adjacent.
– Depending on problem, nodes or edges may have labels (or weights )
• Typically call node set V = {v0, . . .}, and edge set E .
• If the edges have an order (first, second), they are directed edges,
and we have a directed graph (digraph), otherwise an undirected
graph.
• Edges are incident to their nodes.
• Directed edges exit one node and enter the next.
• A cycle is a path without repeated edges leading from a node back
to itself (following arrows if directed).
• A graph is cyclic if it has a cycle, else acyclic. Abbreviation: Directed Acyclic Graph—DAG.
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
3
Some Pictures
Directed
b
Acyclic: a
d
b
b
a
d
c
c
b
b
3
1
2
c
Last modified: Thu Nov 20 12:13:28 2008
d
c
d
1
a
c
Cyclic: a
With Edge Labels: a
Undirected
b
e
1
d
a
0
2
3
e
d
c
CS61B: Lecture #36
4
Trees are Graphs
• A graph is connected if there is a (possibly directed) path between
every pair of nodes.
• That is, if one node of the pair is reachable from the other.
• A DAG is a (rooted) tree iff connected, and every node but the root
has exactly one parent.
• A connected, acyclic, undirected graph is also called a free tree.
Free: we’re free to pick the root; e.g.,
b
a
e
d
c
Last modified: Thu Nov 20 12:13:28 2008
b
a
d
c
d
e
b
a
c
e
CS61B: Lecture #36
5
Examples of Use
• Edge = Connecting road, with length.
Detroit
200
Chicago
• Edge = Must be completed before; Node label = time to complete.
Eat
1 hr
Sleep
8 hrs
Martin
George
• Edge = Begat
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
6
More Examples
• Edge = some relationship
potstickers
eats
loves
John
Mary
• Edge = next state might be (with probability)
0.9
hat
0.4
0.6
the
0.4
cat
in
0.1
bed
• Edge = next state in state machine, label is triggering input. (Start
at s. Being in state 4 means “there is a substring ‘001’ somewhere in
the input”.)
1
s
0
0
2
0
3
1
4
0,1
1
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
7
Representation
• Often useful to number the nodes, and use the numbers in edges.
• Edge list representation: each node contains some kind of list (e.g.,
linked list or array) of its successors (and possibly predecessors).
a
b
c
2: b
1: a
(2,3)
()
(3)
3: c
(1)
()
(1,2)
• Edge sets: Collection of all edges. For graph above:
{(1, 2), (1, 3), (2, 3)}
• Adjacency matrix: Represent connection with matrix entry:
1
2
3
Last modified: Thu Nov 20 12:13:28 2008
1
 0


 0



0

2
1
0
0
3
1 

1 

0
CS61B: Lecture #36
8
Traversing a Graph
• Many algorithms on graphs depend on traversing all or some nodes.
• Can’t quite use recursion because of cycles.
• Even in acyclic graphs, can get combinatorial explosions:
1
0
4
3
2
7
6
5
...
3N
8
Treat 0 as the root and do recursive traversal down the two edges
out of each node: Θ(2N ) operations!
• So typically try to visit each node constant # of times (e.g., once).
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
9
General Graph Traversal Algorithm
COLLECTION OF VERTICES fringe;
fringe = INITIAL COLLECTION;
while (! fringe.isEmpty()) {
Vertex v = fringe.REMOVE HIGHEST PRIORITY ITEM ();
if (! MARKED (v)) {
MARK (v);
VISIT (v);
For each edge (v,w) {
if (NEEDS PROCESSING (w))
Add w to fringe;
}
}
}
Replace COLLECTION OF VERTICES, INITIAL COLLECTION, etc.
with various types, expressions, or methods to different graph algorithms.
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
10
Example: Depth-First Traversal
Problem: Visit every node reachable from v once, visiting nodes further from start first.
Stack<Vertex> fringe;
fringe = stack containing {v};
while (! fringe.isEmpty()) {
Vertex v = fringe.pop ();
if (! marked (v)) {
mark (v);
VISIT (v);
For each edge (v,w) {
if (! marked (w))
fringe.push (w);
}
}
}
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
11
Depth-First Traversal Illustrated
Marked:
Fringe:
a
a
a
a
b
b
b
b
c
e
c
e
c
e
c
e
d
f
d
f
d
f
d
f
[a]
[b,d]
[c,e,d]
[d,f,e,d]
a
a
a
a
b
b
b
b
c
e
c
e
c
e
c
e
d
f
d
f
d
f
d
f
[f,e,d]
Last modified: Thu Nov 20 12:13:28 2008
[e,e,d]
[e,d]
[]
CS61B: Lecture #36
12
Topological Sorting
Problem: Given a DAG, find a linear order of nodes consistent with
the edges.
• That is, order the nodes v0, v1, . . . such that vk is never reachable
from vk′ if k ′ > k .
• Gmake does this. Also PERT charts.
C
A
B
D
F
E
G
H
⇓
[A,B,C,F,D,G,E,H], or
[A,C,B,D,F,E,G,H], or
[A,B,C,F,D,E,H,G], or
...
Last modified: Thu Nov 20 12:13:28 2008
Set<Vertex> fringe;
fringe = set of all nodes with no predecessors;
while (! fringe.isEmpty()) {
Vertex v = fringe.removeOne ();
add v to end of result list;
For each edge (v,w) {
decrease predecessor count of w;
if (predecessor count of w == 0)
fringe.add (w);
}
}
CS61B: Lecture #36
13
Topological Sort in Action
C0
A0
B1
A0
B0
C0
fringe
D2
E3
F1
G1
A0
B0
D1
F1
E3
G1
[A]
C0
C0
E1
F0
G0
A0
D0
B0
H1
[A,C,B,F]
A0
B0
H1
H1
Output: []
D0
C0
H1
[A,C,B,F,D]
Last modified: Thu Nov 20 12:13:28 2008
D0
F0
E3
G1
A0
B0
H1
D0
F0
E2
G1
H1
[A,C]
[A,C,B]
C0
A0
F0
...
E0
C0
G0
B0
D0
F0
E0
G0
H0
[A,C,B,F,D,E,G,H]
CS61B: Lecture #36
14
Shortest Paths: Dijkstra’s Algorithm
Problem: Given a graph (directed or undirected) with non-negative
edge weights, compute shortest paths from given source node, s, to
all nodes.
• “Shortest” = sum of weights along path is smallest.
• For each node, keep estimated distance from s, . . .
• . . . and of preceding node in shortest path from s.
PriorityQueue<Vertex> fringe;
For each node v { v.dist() = ∞; v.back() = null; }
s.dist() = 0;
fringe = priority queue ordered by smallest .dist();
add all vertices to fringe;
while (! fringe.isEmpty()) {
Vertex v = fringe.removeFirst ();
For each edge (v,w) {
if (v.dist() + weight(v,w) < w.dist())
{ w.dist() = v.dist() + weight(v,w); w.back() = v; }
}
}
Last modified: Thu Nov 20 12:13:28 2008
CS61B: Lecture #36
15
Example
C|∞
4
5
A|0
B|∞
2
3
7
D|∞
4
6
2
1
G|∞
2
A|0
2
B|2
2
3
E|∞
F|∞
1
D|3
4
6
2
3
H|∞
1
G|7
2
E|∞
A|0
B|2
2
3
7
3
G|6
1
D|3
4
A|0
2
6
2
1
D|3
4
6
2
E|5
1
F|∞
7
D|3
4
6
2
3
H|9
G|6
1
4
5
A|0
2
B|2
2
3
E|5
F|∞
C|5
3
5
1
H|∞
4
2
2
3
5
7
G|7
B|2
2
3
E|5
F|∞
2
C|5
3
5
B|2
2
3
5
2
1
H|∞
4
5
A|0
2
3
C|5
4
5
3
5
7
C|5
4
5
3
5
3
C|5
1
F|7
3
H|9
G|6
1
2
3
5
7
2
D|3
4
6
2
E|5
1
F|6
H|7
C|5
A|0
Final result:
B|2
2
3
3
G|6
Last modified: Thu Nov 20 12:13:28 2008
1
2
2
3
5
7
Shortest-path tree
4
5
D|3
4
6
2
E|5
1
F|6
X|d
processed node at distance d
Y|d
node in fringe at distance d
H|7
CS61B: Lecture #36
16