of 30
8/9/2019 Graphs+Prims+Krush
1/31
8/9/2019 Graphs+Prims+Krush
2/31
GraphTerminology
8/9/2019 Graphs+Prims+Krush
3/31
Apath is a sequence of nodesv1, v2, , vN such that (vi,vi+1)E for 0
8/9/2019 Graphs+Prims+Krush
4/31
PIT
BOS
JFK
DTW
LAX
SFO
8/9/2019 Graphs+Prims+Krush
5/31
In a directed graph:
Theindegree of a node v is thenumber ofdistinct edges (w,v)E.
Theoutdegree of a node v is thenumber of
distinct edges (v,w)
E.
A node withindegree 0 is a root.
8/9/2019 Graphs+Prims+Krush
6/31
A dag is a directed acyclicgraph.
A tree is a connected acyclicundirected graph.
Aforest is an acyclicundirected graph (notnecessarilyconnected), i.e., eachconnectedcomponent is a tree.
8/9/2019 Graphs+Prims+Krush
7/31
Watch
Socks
Shoes
Undershorts
Pants
Belt Tie
Shirt
Jacket
a DAG implies an
ordering on events
8/9/2019 Graphs+Prims+Krush
8/31
Watch
Socks
Shoes
Undershorts
Pants
Belt Tie
Shirt
Jacket
In a complex DAG, it
can be hard to find a
schedule that obeys
all the constraints.
8/9/2019 Graphs+Prims+Krush
9/31
Graph Traversals
8/9/2019 Graphs+Prims+Krush
10/31
Both take time: O(V+E)
8/9/2019 Graphs+Prims+Krush
11/31
It is verycommon to use a stack to keep trackof:
nodes to be visited next, or
nodes that wehave already visited.
Typically, use of a stack leads to a depth-firstvisit order.
Depth-first visit order is aggressivein thesense that it examinescomplete paths.
8/9/2019 Graphs+Prims+Krush
12/31
It is verycommon to use a queue to keep trackof: nodes to be visited next, or
nodes that wehave already visited. Typically, use of a queue leads to a breadth-first
visit order.
Breadth-first visit order is cautiousin the
sense that it examinesevery path of lengthibeforegoing on to paths of lengthi+1.
8/9/2019 Graphs+Prims+Krush
13/31
8/9/2019 Graphs+Prims+Krush
14/31
Central office
8/9/2019 Graphs+Prims+Krush
15/31
Central office
Expensive!
8/9/2019 Graphs+Prims+Krush
16/31
Central office
Minimize the total length of wire connecting the customers
8/9/2019 Graphs+Prims+Krush
17/31
(see Weiss, Section 24.2.2)
it is a tree (i.e., it is acyclic) it covers all the vertices V
contains |V| - 1 edges
the total cost associated with tree edges is the
minimum among all possible spanning trees not necessarily unique
A minimum spanning tree is a subgraph of an
undirected weighted graph G, such that
8/9/2019 Graphs+Prims+Krush
18/31
Any timeyou want to visit all verticesin a graph atminimumcost (e.g., wire routing on printed circuitboards, sewer pipe layout, road planning)
Internet content distribution Idea: publisher produces web pages, content distribution
network replicates web pages to many locationsso consumerscan access at higher speed
Provides a heuristic for travelingsalesman problems.
8/9/2019 Graphs+Prims+Krush
19/31
a
ce
d
b2
45
9
6
4
5
5
a
ce
d
b2
45
9
6
4
5
5
8/9/2019 Graphs+Prims+Krush
20/31
Initialization
a. Pick a vertex r to be the root
b. Set D(r) = 0, parent(r) =null
c. For all vertices v V, v{ r, set D(v) =g
d. Insert all vertices into priority queue P,using distances as the keys
a
ce
d
b2
45
9
6
4
5
5
e a b c d
0 g g g g
Vertex Parent
e -
8/9/2019 Graphs+Prims+Krush
21/31
While P is not empty:
1. Select the next vertex u to add to the tree
u=P.deleteMin()
2. Update the weight of each vertex w adjacent to
u which is not in the tree (i.e., w P)
Ifweight(u,w) < D(w),
a.parent(w) = u
b. D(w) = weight(u,w)c. Update the priority queue to reflect
new distance forw
8/9/2019 Graphs+Prims+Krush
22/31
a
ce
d
b2
45
9
6
4
5
5
d b c a
4 5 5 g
Vertex Parent
e -
b e
c e
d e
The MST initially consists of the vertex e, and we update
the distances and parent for its adjacent vertices
Vertex Parent
e -
b -
c -
d -
d b c a
g g g g
e
0
8/9/2019 Graphs+Prims+Krush
23/31
a
ce
d
b2
45
9
6
4
5
5
a c b
2 4 5
Vertex Parent
e -
b e
c d
d e
a d
d b c a
4 5 5 g
Vertex Parent
e -
b e
c e
d e
8/9/2019 Graphs+Prims+Krush
24/31
a
ce
d
b2
45
9
6
4
5
5
c b
4 5
erte are t
e -
b ec d
d e
a d
a c b
2 4 5
erte are t
e -b e
c d
d e
a d
8/9/2019 Graphs+Prims+Krush
25/31
a
ce
d
b2
45
9
6
4
5
5
b
5
Vertex Parent
e -
b e
c dd e
a d
c b
4 5
Vertex Parente -
b e
c d
d e
a d
8/9/2019 Graphs+Prims+Krush
26/31
Vertex Parent
e -
e
c
e
a
a
ce
d
b
2
45
9
6
4
5
5
The final minimum spanning tree
b
5
Vert
ex Par
ente -
b e
c d
d e
a d
8/9/2019 Graphs+Prims+Krush
27/31
At eachstep, we add theedge(u,v) s.t. theweight of (u,v) isminimum among all edgeswhereu isin the tree and v isnot in the tree
Eachstep maintains a minimumspanning treeof the vertices that havebeenincluded thus far
When all verticeshavebeenincluded, wehavea MST for thegraph!
8/9/2019 Graphs+Prims+Krush
28/31
a
ce
d
b2
45
9
6
4
5
5
Create a forest of trees from the vertices
Repeatedly merge trees by adding safe edges
until only one tree remains
A safe edge is an edge of minimum weight which
does not create a cycle
forest: {a}, {b}, {c}, {d}, {e}
8/9/2019 Graphs+Prims+Krush
29/31
Initialization
a. Create a set for each vertex v V
b. Initialize the set of safe edges A
comprising the MST to the empty set
c. Sort edges by increasing weight
a
ce
d
b2
45
9
6
4
5
5
F={a}, {b}, {c}, {d}, {e}
A=
E= {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
8/9/2019 Graphs+Prims+Krush
30/31
For each edge (u,v) E in increasing orderwhile more than one set remains:
Ifu and v, belong to different sets Uand V
a. add edge (u,v) to the safe edge set
A=A {(u,v)}
b. merge the sets Uand V
F=F - U - V + (U V)
Return A
8/9/2019 Graphs+Prims+Krush
31/31
E= {(a,d), (c,d), (d,e), (a,c),
(b,e), (c,e), (b,d), (a,b)}
Forest
{a}, {b}, {c}, {d}, {e}
{a,d}, {b}, {c}, {e}
{a,d,c}, {b}, {e}{a,d,c,e}, {b}
{a,d,c,e,b}
A
{(a,d)}
{(a,d), (c,d)}{(a,d), (c,d), (d,e)}
{(a,d), (c,d), (d,e), (b,e)}
a
c e
d
b2
45
9
6
4
5
5