Title: Data Structures Graphs
1 Data StructuresGraphs
2Graph ADT?
- Not quite an ADToperations not clear
- A formalism for representing relationships
between objects - Graph G (V,E)
- Set of verticesV v1,v2,,vn
- Set of edgesE e1,e2,,em where each ei
connects twovertices (vi1,vi2)
V Han, Leia, Luke E (Luke, Leia),
(Han, Leia), (Leia, Han)
3Examples of Graphs
- The web
- Vertices are webpages
- Each edge is a link from one page to another
- Call graph of a program
- Vertices are subroutines
- Edges are calls and returns
- Social networks
- Vertices are people
- Edges connect friends
4Graph Definitions
- In directed graphs, edges have a direction
- In undirected graphs, they dont (are two-way)
- v is adjacent to u if (u,v) ? E
5Weighted Graphs
Each edge has an associated weight or cost.
20
Clinton
Mukilteo
30
Kingston
Edmonds
35
Bainbridge
Seattle
60
Bremerton
6Paths and Cycles
- A path is a list of vertices v1, v2, , vn such
that (vi, vi1) ? E for all 0 ? i lt n. - A cycle is a path that begins and ends at the
same node.
Chicago
Seattle
Salt Lake City
San Francisco
Dallas
- p Seattle, Salt Lake City, Chicago, Dallas,
San Francisco, Seattle
7Path Length and Cost
- Path length the number of edges in the path
- Path cost the sum of the costs of each edge
Chicago
3.5
Seattle
2
2
Salt Lake City
2
2.5
2.5
2.5
3
San Francisco
Dallas
length(p) 5
cost(p) 11.5
8More DefinitionsSimple Paths and Cycles
- A simple path repeats no vertices (except that
the first can also be the last) - p Seattle, Salt Lake City, San Francisco,
Dallas - p Seattle, Salt Lake City, Dallas, San
Francisco, Seattle - A cycle is a path that starts and ends at the
same node - p Seattle, Salt Lake City, Dallas, San
Francisco, Seattle - p Seattle, Salt Lake City, Seattle, San
Francisco, Seattle - A simple cycle is a cycle that is also a simple
path (in undirected graphs, no edge can be
repeated)
9Trees as Graphs
- Every tree is a graph with some restrictions
- the tree is directed
- there are no cycles (directed or undirected)
- there is a directed path from the root to every
node
A
B
C
D
E
F
H
G
10Directed Acyclic Graphs (DAGs)
- DAGs are directed graphs with no (directed)
cycles.
Aside If program call-graph is a DAG, then all
procedure calls can be in-lined
Tree ? DAG ? Graph
11Rep 1 Adjacency Matrix
- A V x V array in which an element (u,v) is
true if and only if there is an edge from u to v
Han
Luke
Leia
Han
Luke
Leia
Runtimes Iterate over vertices? Iterate over
edges? Iterate edges adj. to vertex? Existence of
edge?
Space requirements?
12Rep 2 Adjacency List
- A V-ary list (array) in which each entry stores
a list (linked list) of all adjacent vertices
Han
Luke
Leia
Runtimes Iterate over vertices? Iterate over
edges? Iterate edges adj. to vertex? Existence of
edge?
Space requirements?
13Some ApplicationsMoving Around Washington
Whats the shortest way to get from Seattle to
Pullman? Edge labels
Distance
14Some ApplicationsMoving Around Washington
Whats the fastest way to get from Seattle to
Pullman? Edge labels
Distance, speed limit
15Some ApplicationsReliability of Communication
If Wenatchees phone exchange goes down,can
Seattle still talk to Pullman?
16Some ApplicationsBus Routes in Downtown Seattle
If were at 3rd and Pine, how can we get to 1st
and University using Metro? How about 4th and
Seneca?
17Application Topological Sort
This is a partial ordering, for sorting we had a
total ordering
- Given a directed graph, G (V,E), output all the
vertices in V such that no vertex is output
before any other vertex with an edge to it.
Minimize and DO a topo sort
Is the output unique?
18Topological Sort Take One
- Label each vertex with its in-degree ( of
inbound edges) - While there are vertices remaining
- Choose a vertex v of in-degree zero output v
- Reduce the in-degree of all vertices adjacent to
v - Remove v from the list of vertices
Runtime
19- void Graphtopsort()
- Vertex v, w
- labelEachVertexWithItsIn-degree()
-
- for (int counter0 counter lt NUM_VERTICES
counter) - v findNewVertexOfDegreeZero()
-
- v.topologicalNum counter
- for each w adjacent to v
- w.indegree--
-
Time?
Time?
Time?
Whats the bottleneck?
O(depends)
20Topological Sort Take Two
- Label each vertex with its in-degree
- Initialize a queue Q to contain all in-degree
zero vertices - While Q not empty
- v Q.dequeue output v
- Reduce the in-degree of all vertices adjacent to
v - If new in-degree of any such vertex u is
zeroQ.enqueue(u)
Note could use a stack, list, set,
box, instead of a queue
Runtime
21- void Graphtopsort()
- Queue q(NUM_VERTICES) int counter 0 Vertex
v, w - labelEachVertexWithItsIn-degree()
- q.makeEmpty()
- for each vertex v
- if (v.indegree 0)
- q.enqueue(v)
- while (!q.isEmpty())
- v q.dequeue()
- v.topologicalNum counter
- for each w adjacent to v
- if (--w.indegree 0)
- q.enqueue(w)
-
intialize the queue
get a vertex with indegree 0
insert new eligible vertices
Runtime
O(V E)
22Graph Connectivity
- Undirected graphs are connected if there is a
path between any two vertices - Directed graphs are strongly connected if there
is a path from any one vertex to any other - Directed graphs are weakly connected if there is
a path between any two vertices, ignoring
direction - A complete graph has an edge between every pair
of vertices
23Graph Traversals
- Breadth-first search (and depth-first search)
work for arbitrary (directed or undirected)
graphs - not just mazes! - Must mark visited vertices so you do not go into
an infinite loop! - Either can be used to determine connectivity
- Is there a path between two given vertices?
- Is the graph (weakly) connected?
- Which one
- Uses a queue?
- Uses a stack?
- Always finds the shortest path (for unweighted
graphs)?
24CSE 326 Data StructuresGraph Traversals
- James Fogarty
- Autumn 2007
25Graph Connectivity
- Undirected graphs are connected if there is a
path between any two vertices - Directed graphs are strongly connected if there
is a path from any one vertex to any other - Directed graphs are weakly connected if there is
a path between any two vertices, ignoring
direction - A complete graph has an edge between every pair
of vertices
26Graph Traversals
- Breadth-first search (and depth-first search)
work for arbitrary (directed or undirected)
graphs - not just mazes! - Must mark visited vertices. Why?
- So you do not go into an infinite loop! Its not
a tree. - Either can be used to determine connectivity
- Is there a path between two given vertices?
- Is the graph (weakly/strongly) connected?
- Which one
- Uses a queue?
- Uses a stack?
- Always finds the shortest path (for unweighted
graphs)?
27The Shortest Path Problem
- Given a graph G, edge costs ci,j, and vertices s
and t in G, find the shortest path from s to t. - For a path p v0 v1 v2 vk
- unweighted length of path p k
(a.k.a. length) - weighted length of path p ?i0..k-1 ci,i1
(a.k.a cost) - Path length equals path cost when ?
ci,j 1 for every edge
28Single Source Shortest Paths (SSSP)
- Given a graph G, edge costs ci,j, and vertex s,
find the shortest paths from s to all vertices in
G. - Is this harder or easier than the previous
problem?
Potentially harder, but unfortunately nobetter
algorithms known for the former! So well solve
this instead!
29All Pairs Shortest Paths (APSP)
- Given a graph G and edge costs ci,j, find the
shortest paths between all pairs of vertices in
G. - Is this harder or easier than SSSP?
- Could we use SSSP as a subroutine to solve this?
Yes. Will take time V . T(SSSP) to
run. However, better algorithms known. WONT
COVER IN THIS CLASS
What would be the running time?
30Depth-First Graph Search
Open Stack Criteria Pop
- DFS( Start, Goal_test)
- push(Start, Open)
- repeat
- if (empty(Open)) then return fail
- Node pop(Open)
- if (Goal_test(Node)) then return Node
- for each Child of node do
- if (Child not already visited) then
push(Child, Open) - Mark Node as visited
- end
31Breadth-First Graph Search
Open Queue Criteria Dequeue (FIFO)
- BFS( Start, Goal_test)
- enqueue(Start, Open)
- repeat
- if (empty(Open)) then return fail
- Node dequeue(Open)
- if (Goal_test(Node)) then return Node
- for each Child of node do
- if (Child not already visited) then
enqueue(Child, Open) - Mark Node as visited
- end
32Comparison DFS versus BFS
- Depth-first search
- Does not always find shortest paths
- Must be careful to mark visited vertices, or you
could go into an infinite loop if there is a
cycle - Breadth-first search
- Always finds shortest paths optimal solutions
- Marking visited nodes can improve efficiency, but
even without doing so search is guaranteed to
terminate - Is BFS always preferable?
33DFS Space Requirements
- Assume
- Longest path in graph is length d
- Highest number of out-edges is k
- DFS stack grows at most to size dk
- For k10, d15, size is 150
34BFS Space Requirements
- Assume
- Distance from start to a goal is d
- Highest number of out edges is k BFS
- Queue could grow to size kd
- For k10, d15, size is 1,000,000,000,000,000
35Conclusion
- For large graphs, DFS is hugely more memory
efficient, if we can limit the maximum path
length to some fixed d. - If we knew the distance from the start to the
goal in advance, we can just not add any children
to stack after level d - But what if we dont know d in advance?
36Iterative-Deepening DFS (I)
- Bounded_DFS(Start, Goal_test, Limit)
- Start.dist 0
- push(Start, Open)
- repeat
- if (empty(Open)) then return fail
- Node pop(Open)
- if (Goal_test(Node)) then return Node
- if (Node.dist ?Limit) then return fail
- for each Child of node do
- if (Child not already i-visited) then
- Child.dist Node.dist 1
- push(Child, Open)
- Mark Node as i-visited
- end
37Iterative-Deepening DFS (II)
- IDFS_Search(Start, Goal_test)
- i 1
- repeat
- answer Bounded_DFS(Start, Goal_test, i)
- if (answer ! fail) then return answer
- i i1
- end
38Analysis of IDFS
- Work performed with limit lt actual distance to G
is wasted but the wasted work is usually small
compared to amount of work done during the last
iteration
Ignore low order terms!
Same time complexity as BFS Same space complexity
as (bounded) DFS
39Saving the Path
- Our pseudocode returns the goal node found, but
not the path to it - How can we remember the path?
- Add a field to each node, that points to the
previous node along the path - Follow pointers from goal back to start to
recover path
40Example
Seattle
Salt Lake City
San Francisco
Dallas
41Example (Unweighted Graph)
Seattle
Salt Lake City
San Francisco
Dallas
42Example (Unweighted Graph)
Seattle
Salt Lake City
San Francisco
Dallas
43Graph Search, Saving Path
- Search( Start, Goal_test, Criteria)
- insert(Start, Open)
- repeat
- if (empty(Open)) then return fail
- select Node from Open using Criteria
- if (Goal_test(Node)) then return Node
- for each Child of node do
- if (Child not already visited) then
- Child.previous Node
- Insert( Child, Open )
- Mark Node as visited
- end
44Weighted SSSPThe Quest For Food
Home
Ben Jerrys
Cedars
Neelams
40
U Village
285
Delfinos
70
25
75
365
375
Coke Closet
350
The Ave
10
350
25
HUB
ALLEN
35
35
Café Allegro
15
Schultzys
Vending Machine in EE1
15,356
Parents Home
Can we calculate shortest distance to all nodes
from Allen Center?
45Weighted SSSPThe Quest For Food
Home
Ben Jerrys
Cedars
Neelams
40
U Village
285
Delfinos
70
25
75
365
375
Coke Closet
350
The Ave
5
10
25
HUB
ALLEN
35
35
Café Allegro
15
Schultzys
Vending Machine in EE1
15,356
Parents Home
Can we calculate shortest distance to all nodes
from Allen Center?
46Edsger Wybe Dijkstra (1930-2002)
- Invented concepts of structured programming,
synchronization, weakest precondition, and
"semaphores" for controlling computer processes.
The Oxford English Dictionary cites his use of
the words "vector" and "stack" in a computing
context. - Believed programming should be taught without
computers - 1972 Turing Award
- In their capacity as a tool, computers will be
but a ripple on the surface of our culture. In
their capacity as intellectual challenge, they
are without precedent in the cultural history of
mankind.
47General Graph Search Algorithm
Open some data structure (e.g., stack, queue,
heap) Criteria some method for removing an
element from Open
- Search( Start, Goal_test, Criteria)
- insert(Start, Open)
- repeat
- if (empty(Open)) then return fail
- select Node from Open using Criteria
- if (Goal_test(Node)) then return Node
- for each Child of node do
- if (Child not already visited) then Insert(
Child, Open ) - Mark Node as visited
- end
48Shortest Path for Weighted Graphs
- Given a graph G (V, E) with edge costs c(e),
and a vertex s ? V, find the shortest (lowest
cost) path from s to every vertex in V - Assume only positive edge costs
49Dijkstras Algorithm for Single Source Shortest
Path
- Similar to breadth-first search, but uses a heap
instead of a queue - Always select (expand) the vertex that has a
lowest-cost path to the start vertex - Correctly handles the case where the lowest-cost
(shortest) path to a vertex is not the one with
fewest edges
50CSE 326 Data StructuresDijkstras Algorithm
51Dijkstra, Edsger Wybe
You must know Dijkstra!
- Legendary figure in computer science was a
professor at University of Texas. - Supported teaching introductory computer courses
without computers (pencil and paper programming) - Supposedly wouldnt (until very late in life)
read his e-mail so, his staff had to print out
messages and put them in his box.
E.W. Dijkstra (1930-2002)
1972 Turning Award Winner, Programming
Languages, semaphores, and
52Dijkstras Algorithm Idea
- Adapt BFS to handle weighted graphs
- Two kinds of vertices
- Finished or known vertices
- Shortest distance hasbeen computed
- Unknown vertices
- Have tentative distance
53Dijkstras Algorithm Idea
- At each step
- Pick closest unknown vertex
- Add it to known vertices
- Update distances
(the red 3)
54Dijkstras Algorithm Pseudocode
- Initialize the cost of each node to ?
- Initialize the cost of the source to 0
- While there are unknown nodes left in the graph
- Select an unknown node b with the lowest cost
- Mark b as known
- For each node a adjacent to b
- as cost min(as old cost, bs cost cost of
(b, a)) - as prev path node b
Will examine performance later
55Important Features
- Once a vertex is made known, the cost of the
shortest path to that node is known - While a vertex is still not known, another
shorter path to it might still be found - The shortest path itself can found by following
the backward pointers stored in node.path
56Dijkstras Algorithm in action
?
?
?
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
?
C
2
11
D
?
1
E
?
7
Vertex Visited? Cost Found by
A 0
B ??
C ??
D ??
E ??
F ??
G ??
H ??
57Dijkstras Algorithm in action
2
?
?
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
1
C
2
11
D
4
1
E
?
7
Vertex Visited? Cost Found by
A Y 0
B lt2 A
C lt1 A
D lt4 A
E ??
F ??
G ??
H ??
58Dijkstras Algorithm in action
2
?
?
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
1
C
2
11
D
4
1
E
12
7
Vertex Visited? Cost Found by
A Y 0
B lt2 A
C Y 1 A
D lt4 A
E lt12 C
F ??
G ??
H ??
59Dijkstras Algorithm in action
2
4
?
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
1
C
2
11
D
4
1
E
12
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D lt4 A
E lt12 C
F lt4 B
G ??
H ??
60Dijkstras Algorithm in action
2
4
?
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
1
C
2
11
D
4
1
E
12
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D Y 4 A
E lt12 C
F lt4 B
G ??
H ??
61Dijkstras Algorithm in action
2
4
7
0
2
2
B
3
A
F
H
1
1
2
10
4
9
?
G
3
1
C
2
11
D
4
1
E
12
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D Y 4 A
E lt12 C
F Y 4 B
G ??
H lt7 F
62Dijkstras Algorithm in action
2
4
7
0
2
2
B
3
A
F
H
1
1
2
10
4
9
8
G
3
1
C
2
11
D
4
1
E
12
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D Y 4 A
E lt12 C
F Y 4 B
G lt8 H
H Y 7 F
63Dijkstras Algorithm in action
2
4
7
0
2
2
B
3
A
F
H
1
1
2
10
4
9
8
G
3
1
C
2
11
D
4
1
E
11
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D Y 4 A
E lt11 G
F Y 4 B
G Y 8 H
H Y 7 F
64Dijkstras Algorithm in action
2
4
7
0
2
2
B
3
A
F
H
1
1
2
10
4
9
8
G
3
1
C
2
11
D
4
1
E
11
7
Vertex Visited? Cost Found by
A Y 0
B Y 2 A
C Y 1 A
D Y 4 A
E Y 11 G
F Y 4 B
G Y 8 H
H Y 7 F
65Your turn
V Visited? Cost Found by
v0
v1
v2
v3
v4
v5
v6
66Answer
V Visited? Cost Found by
v0 Y 0
V1 Y 6 V3
V2 Y 2 V0
V3 Y 1 V0
V4 Y 2 V3
V5 Y 4 V2
V6 Y 6 V3
67Dijkstras Alg Implementation
O(VV E) for unopt O(VlogV ElogV) for heap
Time V to initialize
- Initialize the cost of each node to ?
- Initialize the cost of the source to 0
- While there are unknown nodes left in the graph
- Select the unknown node b with the lowest cost
- Mark b as known
- For each node a adjacent to b
- as cost min(as old cost, bs cost cost of
(b, a)) - as prev path node b (if we updated as cost)
V times
V if scan array
log V if deletemin
Examine each edge once ever time E
E x decreaseKey
O(1) if just look at array
What data structures should we use? Running time?
Priority queue simple dictionary (to find node
in the PQ)
O( V logV E logV) O(E logV) if
connected
68- void Graphdijkstra(Vertex s)
- Vertex v,w
-
- Initialize s.dist 0 and set dist of all other
vertices to infinity - while (there exist unknown vertices, find the
one b with the smallest distance) - b.known true
- for each a adjacent to b
- if (!a.known)
- if (b.dist weight(b,a) lt a.dist)
- a.dist (b.dist weight(b,a))
- a.path b
-
-
Sounds like deleteMin on a heap
Sounds like adjacency lists
Sounds like decreaseKey
Running time O(E log V) there are E
edges to examine, and each one causes a heap
operation of time O(log V)
69Dijkstras Algorithm Summary
- Classic algorithm for solving SSSP in weighted
graphs without negative weights - A greedy algorithm (irrevocably makes decisions
without considering future consequences) - Intuition for correctness
- shortest path from source vertex to itself is 0
- cost of going to adjacent nodes is at most edge
weights - cheapest of these must be shortest path to that
node - update paths for new node and continue picking
cheapest path
70Correctness The Cloud Proof
Know shortest path for all inside cloud.
Known shortest path from source to that
nodeDist shortest path from source to that
node that lies wholly within Known set
Next shortest path from inside the known cloud
V
Better path to V? No!
The Known Cloud
W
Source
- How does Dijkstras decide which vertex to add to
the Known set next? - If path to V is shortest, path to W must be at
least as long - (or else we would have picked W as the next
vertex) - So the path through W to V cannot be any shorter!
71Correctness Inside the Cloud
- Prove by induction on of nodes in the cloud
- Initial cloud is just the source with shortest
path 0 - Assume Everything inside the cloud has the
correct shortest path - Inductive step Only when we prove the shortest
path to some node v (which is not in the cloud)
is correct, we add it to the cloud
When does Dijkstras algorithm not work?
Negative edge (even if not cycle)
72The Trouble with Negative Weight Cycles
Whats the shortest path from A to E? Problem?
Note that negative edges break Dijkstra, even
without cycles. See book for solution.
73Dijkstras vs BFS
- At each step
- Pick vertex from queue
- Add it to visited vertices
- Update queue with neighbors
- Breadth-first Search
- At each step
- Pick closest unknown vertex
- Add it to finished vertices
- Update distances
- Dijkstras Algorithm
Some Similarities
Both have set of completed nodes Both have set
of nodes in fringe BFS those in
queue Dijkstra non-infinite distance
74Single-Source Shortest Path
- Given a graph G (V, E) and a single
distinguished vertex s, find the shortest
weighted path from s to every other vertex in G. - All-Pairs Shortest Path
- Find the shortest paths between all pairs of
vertices in the graph. - How?
75Analysis
- Total running time for Dijkstras
- O(V log V E log V) (heaps)
- What if we want to find the shortest path from
each point to ALL other points?
76Dynamic Programming
- Algorithmic technique that systematically records
the answers to sub-problems in a table and
re-uses those recorded results (rather than
re-computing them). - Simple Example Calculating the Nth Fibonacci
number. Fib(N) Fib(N-1) Fib(N-2)
77Floyd-Warshall
In section 10.3.4 in Weiss
Can keep track of path with an extra
matrixpathij k if updating shortest path
Add in paths that go thru k
- for (int k 1 k lt V k)
- for (int i 1 i lt V i)
- for (int j 1 j lt V j)
- if ( ( Mik Mkj ) lt Mij ) Mij
Mik Mkj
For iter k will not be improving any paths that
either start or finish at k(and these are the
parts of the matrix you are examining to see if
you found a new minimum, so the inner 2 loops
could be executed in parallel.)
Invariant After the kth iteration, the matrix
includes the shortest paths for all pairs of
vertices (i,j) containing only vertices 1..k as
intermediate vertices
78Works with neg cost edges, but not neg cost cycles
2
b
a
-2
Initial state of the matrix
1
-4
3
c
1
d
e
a b c d e
a 0 2 - -4 -
b - 0 -2 1 3
c - - 0 - 1
d - - - 0 4
e - - - - 0
4
Kano changesKbMac Mab Mbc 2
-2 inf to 0Mae Mab Mbe 2 3
inf to 5KcMae Mac Mce 0 1
5 to 1Mbe Mbc Mce -2 1 3
to -1KdMae Mad Mde -4 4 1
to 0Keno changes
Mij min(Mij, Mik Mkj)
792
b
a
-2
Floyd-Warshall - for All-pairs shortest path
1
-4
3
c
1
d
e
4
a b c d e
a 0 2 0 -4 0
b - 0 -2 1 -1
c - - 0 - 1
d - - - 0 4
e - - - - 0
Final Matrix Contents
80CSE 326 Data StructuresSpanning Trees
81A Hidden Tree
Start
End
82Spanning Tree in a Graph
Spanning tree - Connects all the vertices - No
cycles
Vertex router Edge link between routers
83Undirected Graph
- G (V,E)
- V is a set of vertices (or nodes)
- E is a set of unordered pairs of vertices
2
1
V 1,2,3,4,5,6,7 E 1,2,1,6,1,5,2,7,
2,3, 3,4,4,7,4,5,5,6
3
7
4
2 and 3 are adjacent 2 is incident to edge 2,3
6
5
84Spanning Tree Problem
- Input An undirected graph G (V,E). G is
connected. - Output T contained in E such that
- (V,T) is a connected graph
- (V,T) has no cycles
85Spanning Tree Algorithm
ST(i vertex) mark i for each j adjacent
to i do if j is unmarked then
Add i,j to T ST(j) endST
Main T empty set ST(1) endMain
86Example of Depth First Search
ST(1)
2
1
3
7
4
6
5
87Example Step 16
ST(1)
2
1
3
7
4
6
5
1,2 2,7 7,5 5,4 4,3 5,6
88Minimum Spanning Trees
- Given an undirected graph G(V,E), find a graph
G(V, E) such that - E is a subset of E
- E V - 1
- G is connected
- is minimal
- Applications wiring a house, power grids,
Internet connections
G is a minimum spanning tree.
89Minimum Spanning Tree Problem
- Input Undirected Graph G (V,E) and a cost
function C from E to the reals. C(e) is the cost
of edge e. - Output A spanning tree T with minimum total
cost. That is T that minimizes
90Best Spanning Tree
- Each edge has the probability that it wont fail
- Find the spanning tree that is least likely to
fail
2
1
.80
.75
3
.95
7
.50
.84
1.0
.95
4
.89
6
.80
.85
5
91Example of a Spanning Tree
2
1
.80
.75
3
.95
7
.50
.84
1.0
.95
4
.89
6
.80
.85
5
Probability of success .85 x .95 x .89 x .95 x
1.0 x .84
.5735
92Minimum Spanning Tree Problem
- Input Undirected Graph G (V,E) and a cost
function C from E to the reals. C(e) is the cost
of edge e. - Output A spanning tree T with minimum total
cost. That is T that minimizes
93Reducing Best to Minimum
Let P(e) be the probability that an edge doesnt
fail. Define
Minimizing
is equivalent to maximizing
because
94Example of Reduction
2
2
.097
1
1
.80
.75
.125
3
3
.95
.022
7
7
.50
.301
.076
.84
1.0
.000
.95
.022
4
4
.89
.051
6
6
.80
.097
.85
.071
5
5
Best Spanning Tree Problem
Minimum Spanning Tree Problem
95Find the MST
96Find the MST
97Two Different Approaches
Prims Algorithm Looks familiar!
Kruskalss Algorithm Completely different!
One node, grow greedily
Forest of MSTs, Union them together. I wonder how
to union
98Prims algorithm
- Idea Grow a tree by adding an edge from the
known vertices to the unknown vertices. Pick
the edge with the smallest weight.
G
v
known
99Prims Algorithm for MST
- A node-based greedy algorithm
- Builds MST by greedily adding nodes
- Select a node to be the root
- mark it as known
- Update cost of all its neighbors
- While there are unknown nodes left in the graph
- Select an unknown node b with the smallest cost
from some known node a - Mark b as known
- Add (a, b) to MST
- Update cost of all nodes adjacent to b
Note cost from some a, not from root
100Find MST using Prims
Start with V1
Your Turn
2
v2
v1
3
1
10
4
2
7
v4
v3
v5
8
5
V Kwn Distance path
v1
v2
v3
v4
v5
v6
v7
4
6
1
v6
v7
Order Declared Known
V1
Total Cost
101Prims Algorithm Analysis
- Running time
- Same as Dijkstras O(E log V)
- Correctness
- Proof is similar to Dijkstras
-
102Kruskals MST Algorithm
- Idea Grow a forest out of edges that do not
create a cycle. Pick an edge with the smallest
weight.
G(V,E)
v
103Kruskals Algorithm for MST
- An edge-based greedy algorithm
- Builds MST by greedily adding edges
- Initialize with
- empty MST
- all vertices marked unconnected
- all edges unmarked
- While there are still unmarked edges
- Pick the lowest cost edge (u,v) and mark it
- If u and v are not already connected, add (u,v)
to the MST and mark u and v as connected to each
other
Maze construction used random edge
order.Otherwise the same!
Doesnt it sound familiar?
104Example of Kruskal 1
1
3
2
1
3
3
3
7
3
0
4
1
6
4
2
2
5
7,4 2,1 7,5 5,6 5,4 1,6 2,7 2,3
3,4 1,5 0 1 1 2 2
3 3 3 3 4
105Data Structures for Kruskal
- Sorted edge list
- Disjoint Union / Find
- Union(a,b) - union the disjoint sets named by a
and b - Find(a) returns the name of the set containing a
7,4 2,1 7,5 5,6 5,4 1,6 2,7 2,3
3,4 1,5 0 1 1 2 2
3 3 3 3 4
106Example of DU/F 1
1
Find(5) 7 Find(4) 7
1
3
2
3
1
3
3
3
7
3
0
4
1
7
6
4
2
2
5
7,4 2,1 7,5 5,6 5,4 1,6 2,7 2,3
3,4 1,5 0 1 1 2 2
3 3 3 3 4
107Example of DU/F 2
1
Find(1) 1 Find(6) 7
1
3
2
3
1
3
3
3
7
3
0
4
1
7
6
4
2
2
5
7,4 2,1 7,5 5,6 5,4 1,6 2,7 2,3
3,4 1,5 0 1 1 2 2
3 3 3 3 4
108Example of DU/F 3
Union(1,7)
1
3
2
3
1
3
3
3
7
3
0
4
7
1
6
4
2
2
5
7,4 2,1 7,5 5,6 5,4 1,6 2,7 2,3
3,4 1,5 0 1 1 2 2
3 3 3 3 4
109Kruskals Algorithm with DU / F
Sort the edges by increasing cost Initialize A
to be empty for each edge i,j chosen in
increasing order do u Find(i) v
Find(j) if not(u v) then add
i,j to A Union(u,v)
110Kruskal code
- void Graphkruskal()
- int edgesAccepted 0
- DisjSet s(NUM_VERTICES)
- while (edgesAccepted lt NUM_VERTICES 1)
- e smallest weight edge not deleted yet
- // edge e (u, v)
- uset s.find(u)
- vset s.find(v)
- if (uset ! vset)
- edgesAccepted
- s.unionSets(uset, vset)
-
-
E heap ops
2E finds
V unions
111Find MST using Kruskals
Total Cost
- Now find the MST using Prims method.
- Under what conditions will these methods give the
same result?