Professional Documents
Culture Documents
Graph Traversals and Minimum Spanning Trees: 15-211: Fundamental Data Structures and Algorithms Rose Hoberman
Graph Traversals and Minimum Spanning Trees: 15-211: Fundamental Data Structures and Algorithms Rose Hoberman
and
Minimum Spanning Trees
Rose Hoberman
April 8, 2003
Announcements
Announcements
Readings:
Chapter 14
HW5:
Due in less than one week!
Monday, April 14, 2003, 11:59pm
BOS
DTW
SFO
PIT
JFK
LAX
In a directed graph:
Undershorts
Socks
Watch
Pants Shoes
Shirt
a DAG implies an
Belt Tie ordering on events
Jacket
Undershorts
Socks
Watch
Pants Shoes
Shirt
In a complex DAG, it
Belt Tie can be hard to find a
schedule that obeys
Jacket all the constraints.
15-211: Fundamental Data Rose Hoberman
Structures and Algorithms 11 April 8, 2003
Topological Sort
Topological Sort
Algorithm
Assume indegree is stored with each node.
Repeat until no nodes remain:
Choose a root and output it.
Remove the root and all its edges.
Performance
O(V2 + E), if linear search is used to find a root.
But...
Hint:
Prove that if at any point there are
unseen vertices but none of them have
an indegree of 0, a cycle must exist,
contradicting our assumption of a DAG.
do a DFS of graph G
as each vertex v is finished (all of its
children processed), insert it onto the front of
a linked list
return the linked list of vertices
Central office
Central office
Expensive!
Central office
9 9
b b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4
5 e 5 e
c c
5 e
c Vertex Parent
e -
c b b e
4 5 c d
d e
a d
5 e Vertex Parent
c e -
b b e
5 c d
d e
a d
5 e
c Vertex Parent
e -
b e
The final minimum spanning tree c d
d e
a d
Let G be a connected,
undirected graph
Let S be the set of
edges chosen by Prims x
algorithm before y
choosing an errorful
edge (x,y)
Let V' be the vertices incident with edges in S
Let T be a MST of G containing all edges in S, but not (x,y).
15-211: Fundamental Data Rose Hoberman
Structures and Algorithms 45 April 8, 2003
Correctness of Prims
w
v
Edge (x,y) is not in T, so
there must be a path in
T from x to y since T is x
connected. y
Inserting edge (x,y) into
T will create a cycle
There is exactly one edge on this cycle with exactly one
vertex in V, call this edge (v,w)
15-211: Fundamental Data Rose Hoberman
Structures and Algorithms 46 April 8, 2003
Correctness of Prims
...Contradiction
9 b
a 2 6
d forest: {a}, {b}, {c}, {d}, {e}
4 5
5 4
5 e
c
9 b
a 2 6 F = {a}, {b}, {c}, {d}, {e}
d A=
4 5
5 4
E = {(a,d), (c,d), (d,e), (a,c),
5 e (b,e), (c,e), (b,d), (a,b)}
c
Return A
K T
S
e
Let e be this first errorful edge.
Let K be the Kruskal spanning tree
Let S be the set of edges chosen by Kruskals algorithm
before choosing e
Let T be a MST containing all edges in S, but not e.