SlideShare a Scribd company logo
1 of 92
2
Paths and cycles
• A path is a sequence of nodes
v1, v2, …, vN such that (vi,vi+1)E for 0<i<N
• The length of the path is N-1.
• Simple path: all vi are distinct, 0<i<N
• A cycle is a path such that v1=vN
• An acyclic graph has no cycles
3
Cycles
PIT
BOS
JFK
DTW
LAX
SFO
4
More useful definitions
• In a directed graph:
• The indegree of a node v is the number of distinct edges (w,v)E.
• The outdegree of a node v is the number of distinct edges (v,w)E.
• A node with indegree 0 is a root.
5
Trees are graphs
• A dag is a directed acyclic graph.
• A tree is a connected acyclic undirected graph.
• A forest is an acyclic undirected graph (not necessarily connected),
i.e., each connected component is a tree.
Tree
• Connected graph that has no cycles
• n vertex connected graph with n-1
edges
Spanning Tree
• Subgraph that includes all vertices of
the original graph
• Subgraph is a tree
• If original graph has n vertices, the
spanning tree has n vertices and n-1
edges
Minimum Cost Spanning Tree
• Weighted, undirected graph G
• Spanning tree T of G
• Cost(T) = sum of edge weights
• MST = Spanning tree T of G such
that no other spanning tree T' of G
has cost(T') < cost(T)
Minimum Cost Spanning Tree
Graph has 11 nodes, 13 edges
Any spanning tree will have 10 edges
2
3
8
101
4
5
9
11
6
7
4
8
6
6
7
5
2
4
4 5
3
8
2
A Spanning Tree
Spanning tree cost = 51
Is there a cheaper spanning tree?
2
3
8
101
4
5
9
11
6
7
4
8
6
6
7
5
2
4
4 5
3
8
2
Minimum Cost Spanning Tree
Spanning tree cost = 41
Is there any cheaper spanning tree?
2
3
8
101
4
5
9
11
6
7
4
8
6
6
7
5
2
4
4 5
3
8
2
12
Edsger 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.”
13
Dijkstra’s Algorithm for
Single Source Shortest Path
• Classic algorithm for solving shortest path in weighted graphs (with
only positive edge weights)
• Similar to breadth-first search, but uses a priority queue instead of a
FIFO queue:
• Always select (expand) the vertex that has a lowest-cost path to the start
vertex
• a kind of “greedy” algorithm
• Correctly handles the case where the lowest-cost (shortest) path to a
vertex is not the one with fewest edges
14
void BFS(Node startNode) {
Queue s = new Queue;
for v in Nodes do
v.visited = false;
startNode.dist = 0;
s.enqueue(startNode);
while (!s.empty()) {
x = s.dequeue();
for y in x.children() do
if (x.dist+1<y.dist) {
y.dist = x.dist+1;
s.enqueue(y);
}
}
}
void shortestPath(Node startNode) {
Heap s = new Heap;
for v in Nodes do
v.dist = ;
s.insert(v);
startNode.dist = 0;
s.decreaseKey(startNode);
startNode.previous = null;
while (!s.empty()) {
x = s.deleteMin();
for y in x.children() do
if (x.dist+c(x,y) < y.dist) {
y.dist = x.dist+c(x,y);
s.decreaseKey(y);
y.previous = x;
}
}
}
15
Dijkstra’s Algorithm:
Correctness Proof
Let Known be the set of nodes that were extracted from the heap
(through deleteMin)
• For every node x, x.dist = the cost of the shortest path from startNode
to x going only through nodes in Known
• In particular, if x in Known then x.dist = the shortest path cost
• Once a node x is in Known, it will never be reinserted into the heap
16
Dijkstra’s Algorithm:
Correctness Proof
startNode
Known
x.dist
17
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
18
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8



9

next
19
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9


9
15
next
20
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9


119
13
next
21
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9

11
119
13
next
22
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9

11
119
13
next
23
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9
14
11
119
13
next
24
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9
14
11
119
13
next
25
Dijkstra’s Algorithm in Action
A
C
B
D
F H
G
E
2 2 3
2
1
1
4
10
8
1
1
9
4
2
7
0
8
9
14
11
119
13
Done
26
Data Structures
for Dijkstra’s Algorithm
Select the unknown node with the lowest cost
findMin/deleteMin
y’s cost = min(y’s old cost, …)
decreaseKey
|V| times:
|E| times:
runtime: O((|V|+|E|) log |V|)
O(log |V|)
O(log |V|)
27
Spanning tree: a subset of the edges from a connected
graph such that:
 touches all vertices in the graph (spans the graph)
 forms a tree (is connected and contains no cycles)
Minimum spanning tree: the spanning tree with the least
total edge cost.
Spanning Tree
4 7
1 5
9
2
28
Applications of Minimal Spanning Trees
• Communication networks
• VLSI design
• Transportation systems
Minimum Spanning Trees
Text
Read Weiss, §9.5
Prim’s Algorithm
Weiss §9.5.1
Similar to Dijkstra’s Algorithm
Kruskal’s Algorithm
Weiss §9.5.2
Focuses on edges, rather than nodes
Definition
• A Minimum Spanning Tree (MST) is a subgraph of an undirected graph
such that the subgraph spans (includes) all nodes, is connected, is
acyclic, and has minimum total edge weight
Real Life Application of a MST
A cable TV company is laying cable in a new neighborhood. If it is
constrained to bury the cable only along certain paths, then there
would be a graph representing which points are connected by those
paths. Some of those paths might be more expensive, because they
are longer, or require the cable to be buried deeper; these paths
would be represented by edges with larger weights. A minimum
spanning tree would be the network with the lowest total cost.
33
Problem: Laying Telephone Wire
Central office
34
Wiring: Naïve Approach
Central office
Expensive!
35
Wiring: Better Approach
Central office
Minimize the total length of wire connecting the customers
36
Minimum Spanning Tree (MST)(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
Algorithm Characteristics
• Both Prim’s and Kruskal’s Algorithms work with
undirected graphs
• Both work with weighted and unweighted graphs but
are more interesting when edges are weighted
• Both are greedy algorithms that produce optimal
solutions
MST
• A minimum spanning tree connects all nodes in a given graph
• A MST must be a connected and undirected graph
• A MST can have weighted edges
• Multiple MSTs can exist within a given undirected graph
More about Multiple MSTs
• Multiple MSTs can be generated depending on which algorithm is
used
• If you wish to have an MST start at a specific node
• However, if there are weighted edges and all weighted edges are
unique, only one MST will exist
Borůvka’s Algorithm
• The first MST Algorithm was created by Otakar Borůvka in 1926
• The algorithm was used to create efficient connections between the
electricity network in the Czech Republic
• No longer used since Prim’s and Kruskal’s algorithms were discovered
Prim’s Algorithm
• Initially discovered in 1930 by Vojtěch Jarník, then rediscovered in
1957 by Robert C. Prim
• Similar to Dijkstra’s Algorithm regarding a connected graph
• Starts off by picking any node within the graph and growing from
there
Prim’s Algorithm Cont.
• Label the starting node, A, with a 0 and all others with infinite
• Starting from A, update all the connected nodes’ labels to A with their
weighted edges if it less than the labeled value
• Find the next smallest label and update the corresponding connecting
nodes
• Repeat until all the nodes have been visited
Prim’s Algorithm Example
Prim’s Algorithm Example
Kruskal’s Algorithm
• Created in 1957 by Joseph Kruskal
• Finds the MST by taking the smallest weight in the graph and connecting the two
nodes and repeating until all nodes are connected to just one tree
• This is done by creating a priority queue using the weights as keys
• Each node starts off as it’s own tree
• While the queue is not empty, if the edge retrieved connects two trees, connect
them, if not, discard it
• Once the queue is empty, you are left with the minimum spanning tree
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Kruskal’s Algorithm Example
Prim’s Algorithm
• Similar to Dijkstra’s Algorithm except that dv records edge weights,
not path lengths
Walk-Through
Initialize array
K dv pv
A F  
B F  
C F  
D F  
E F  
F F  
G F  
H F  
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Start with any node, say D
K dv pv
A
B
C
D T 0 
E
F
G
H
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A
B
C 3 D
D T 0 
E 25 D
F 18 D
G 2 D
H
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C 3 D
D T 0 
E 25 D
F 18 D
G T 2 D
H
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A
B
C 3 D
D T 0 
E 7 G
F 18 D
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B
C T 3 D
D T 0 
E 7 G
F 18 D
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A
B 4 C
C T 3 D
D T 0 
E 7 G
F 3 C
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A
B 4 C
C T 3 D
D T 0 
E 7 G
F T 3 C
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E 2 F
F T 3 C
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H 3 G
2
Table entries unchanged
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A 10 F
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Update distances of
adjacent, unselected nodes
K dv pv
A T 4 H
B 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Table entries unchanged
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
Select node with minimum
distance
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
4
A
H
B
F
E
D
C
G
2
3
4
3
3
Cost of Minimum Spanning
Tree =  dv = 21
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0 
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
Kruskal’s Algorithm
Work with edges, rather than nodes
Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not
generate a cycle
Walk-Through
Consider an undirected, weight graph
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10
Sort the edges by increasing edge weight
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5 
(D,F) 6
(A,B) 8
(A,F) 10
Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1 
(D,G) 2 
(E,G) 3 
(C,D) 3 
(G,H) 3 
(C,F) 3 
(B,C) 4 
5
1
A
H
B
F
E
D
C
G
2
3
3
3
edge dv
(B,E) 4 
(B,F) 4 
(B,H) 4 
(A,H) 5 
(D,F) 6
(A,B) 8
(A,F) 10
Done
Total Cost =  dv = 21
4
}not
considered

More Related Content

What's hot

Spectral clustering Tutorial
Spectral clustering TutorialSpectral clustering Tutorial
Spectral clustering TutorialZitao Liu
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slidesHJ DS
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplicationAustin Benson
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's AlgorithmArijitDhali
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentationSubid Biswas
 
Shortest route and mst
Shortest route and mstShortest route and mst
Shortest route and mstAlona Salva
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraguest1f4fb3
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning treeSTEFFY D
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101zhendy94
 
Parallel Algorithms for Geometric Graph Problems (at Stanford)
Parallel Algorithms for Geometric Graph Problems (at Stanford)Parallel Algorithms for Geometric Graph Problems (at Stanford)
Parallel Algorithms for Geometric Graph Problems (at Stanford)Grigory Yaroslavtsev
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explainedPIYUSH Dubey
 
K means and dbscan
K means and dbscanK means and dbscan
K means and dbscanYan Xu
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Treezhaokatherine
 
Hierarchical clustering techniques
Hierarchical clustering techniquesHierarchical clustering techniques
Hierarchical clustering techniquesMd Syed Ahamad
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Amrinder Arora
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)Akshay soni
 

What's hot (20)

Spectral clustering Tutorial
Spectral clustering TutorialSpectral clustering Tutorial
Spectral clustering Tutorial
 
Dijkstra c
Dijkstra cDijkstra c
Dijkstra c
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
A framework for practical fast matrix multiplication
A framework for practical fast matrix multiplication�A framework for practical fast matrix multiplication�
A framework for practical fast matrix multiplication
 
Dijkstra's Algorithm
Dijkstra's AlgorithmDijkstra's Algorithm
Dijkstra's Algorithm
 
Dijkstra's algorithm presentation
Dijkstra's algorithm presentationDijkstra's algorithm presentation
Dijkstra's algorithm presentation
 
Shortest route and mst
Shortest route and mstShortest route and mst
Shortest route and mst
 
KRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitraKRUSKAL'S algorithm from chaitra
KRUSKAL'S algorithm from chaitra
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Spanning trees
Spanning treesSpanning trees
Spanning trees
 
Ijciras1101
Ijciras1101Ijciras1101
Ijciras1101
 
d
dd
d
 
Parallel Algorithms for Geometric Graph Problems (at Stanford)
Parallel Algorithms for Geometric Graph Problems (at Stanford)Parallel Algorithms for Geometric Graph Problems (at Stanford)
Parallel Algorithms for Geometric Graph Problems (at Stanford)
 
minimum spanning tree
minimum spanning tree minimum spanning tree
minimum spanning tree
 
Algorithms explained
Algorithms explainedAlgorithms explained
Algorithms explained
 
K means and dbscan
K means and dbscanK means and dbscan
K means and dbscan
 
Minimum Spanning Tree
Minimum Spanning TreeMinimum Spanning Tree
Minimum Spanning Tree
 
Hierarchical clustering techniques
Hierarchical clustering techniquesHierarchical clustering techniques
Hierarchical clustering techniques
 
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
Proof of O(log *n) time complexity of Union find (Presentation by Wei Li, Zeh...
 
Biconnected components (13024116056)
Biconnected components (13024116056)Biconnected components (13024116056)
Biconnected components (13024116056)
 

Similar to Graph Paths and Spanning Trees

APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...KUSHDHIRRA2111026030
 
Clustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesClustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesData-Centric_Alliance
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph AlgorithmsAshwin Shiv
 
Minimal spanning tree class 15
Minimal spanning tree class 15Minimal spanning tree class 15
Minimal spanning tree class 15Kumar
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfzefergaming
 
Prim's and Kruskal's Algorithm
Prim's and Kruskal's AlgorithmPrim's and Kruskal's Algorithm
Prim's and Kruskal's AlgorithmOmTanna1
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9chidabdu
 
Single source Shortest path algorithm with example
Single source Shortest path algorithm with exampleSingle source Shortest path algorithm with example
Single source Shortest path algorithm with exampleVINITACHAUHAN21
 
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01Hemant Jha
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Mohanlal Sukhadia University (MLSU)
 

Similar to Graph Paths and Spanning Trees (20)

APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
APznzaZLM_MVouyxM4cxHPJR5BC-TAxTWqhQJ2EywQQuXStxJTDoGkHdsKEQGd4Vo7BS3Q1npCOMV...
 
Clustering of graphs and search of assemblages
Clustering of graphs and search of assemblagesClustering of graphs and search of assemblages
Clustering of graphs and search of assemblages
 
OR II - M3.pptx
OR II - M3.pptxOR II - M3.pptx
OR II - M3.pptx
 
Graph Algorithms
Graph AlgorithmsGraph Algorithms
Graph Algorithms
 
09 placement
09 placement09 placement
09 placement
 
Minimal spanning tree class 15
Minimal spanning tree class 15Minimal spanning tree class 15
Minimal spanning tree class 15
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Shortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdfShortest path by using suitable algorithm.pdf
Shortest path by using suitable algorithm.pdf
 
8_MST_pptx.pptx
8_MST_pptx.pptx8_MST_pptx.pptx
8_MST_pptx.pptx
 
Chap10 slides
Chap10 slidesChap10 slides
Chap10 slides
 
Prim's and Kruskal's Algorithm
Prim's and Kruskal's AlgorithmPrim's and Kruskal's Algorithm
Prim's and Kruskal's Algorithm
 
Algorithm chapter 9
Algorithm chapter 9Algorithm chapter 9
Algorithm chapter 9
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
17 prims-kruskals (1)
17 prims-kruskals (1)17 prims-kruskals (1)
17 prims-kruskals (1)
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Single source Shortest path algorithm with example
Single source Shortest path algorithm with exampleSingle source Shortest path algorithm with example
Single source Shortest path algorithm with example
 
mst.pdf
mst.pdfmst.pdf
mst.pdf
 
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01
Vlsiphysicaldesignautomationonpartitioning 120219012744-phpapp01
 
Data structure and algorithm
Data structure and algorithmData structure and algorithm
Data structure and algorithm
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 

Recently uploaded

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

Graph Paths and Spanning Trees

  • 1.
  • 2. 2 Paths and cycles • A path is a sequence of nodes v1, v2, …, vN such that (vi,vi+1)E for 0<i<N • The length of the path is N-1. • Simple path: all vi are distinct, 0<i<N • A cycle is a path such that v1=vN • An acyclic graph has no cycles
  • 4. 4 More useful definitions • In a directed graph: • The indegree of a node v is the number of distinct edges (w,v)E. • The outdegree of a node v is the number of distinct edges (v,w)E. • A node with indegree 0 is a root.
  • 5. 5 Trees are graphs • A dag is a directed acyclic graph. • A tree is a connected acyclic undirected graph. • A forest is an acyclic undirected graph (not necessarily connected), i.e., each connected component is a tree.
  • 6. Tree • Connected graph that has no cycles • n vertex connected graph with n-1 edges
  • 7. Spanning Tree • Subgraph that includes all vertices of the original graph • Subgraph is a tree • If original graph has n vertices, the spanning tree has n vertices and n-1 edges
  • 8. Minimum Cost Spanning Tree • Weighted, undirected graph G • Spanning tree T of G • Cost(T) = sum of edge weights • MST = Spanning tree T of G such that no other spanning tree T' of G has cost(T') < cost(T)
  • 9. Minimum Cost Spanning Tree Graph has 11 nodes, 13 edges Any spanning tree will have 10 edges 2 3 8 101 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2
  • 10. A Spanning Tree Spanning tree cost = 51 Is there a cheaper spanning tree? 2 3 8 101 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2
  • 11. Minimum Cost Spanning Tree Spanning tree cost = 41 Is there any cheaper spanning tree? 2 3 8 101 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3 8 2
  • 12. 12 Edsger 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.”
  • 13. 13 Dijkstra’s Algorithm for Single Source Shortest Path • Classic algorithm for solving shortest path in weighted graphs (with only positive edge weights) • Similar to breadth-first search, but uses a priority queue instead of a FIFO queue: • Always select (expand) the vertex that has a lowest-cost path to the start vertex • a kind of “greedy” algorithm • Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges
  • 14. 14 void BFS(Node startNode) { Queue s = new Queue; for v in Nodes do v.visited = false; startNode.dist = 0; s.enqueue(startNode); while (!s.empty()) { x = s.dequeue(); for y in x.children() do if (x.dist+1<y.dist) { y.dist = x.dist+1; s.enqueue(y); } } } void shortestPath(Node startNode) { Heap s = new Heap; for v in Nodes do v.dist = ; s.insert(v); startNode.dist = 0; s.decreaseKey(startNode); startNode.previous = null; while (!s.empty()) { x = s.deleteMin(); for y in x.children() do if (x.dist+c(x,y) < y.dist) { y.dist = x.dist+c(x,y); s.decreaseKey(y); y.previous = x; } } }
  • 15. 15 Dijkstra’s Algorithm: Correctness Proof Let Known be the set of nodes that were extracted from the heap (through deleteMin) • For every node x, x.dist = the cost of the shortest path from startNode to x going only through nodes in Known • In particular, if x in Known then x.dist = the shortest path cost • Once a node x is in Known, it will never be reinserted into the heap
  • 17. 17 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7
  • 18. 18 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8    9  next
  • 19. 19 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9   9 15 next
  • 20. 20 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9   119 13 next
  • 21. 21 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9  11 119 13 next
  • 22. 22 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9  11 119 13 next
  • 23. 23 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9 14 11 119 13 next
  • 24. 24 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9 14 11 119 13 next
  • 25. 25 Dijkstra’s Algorithm in Action A C B D F H G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7 0 8 9 14 11 119 13 Done
  • 26. 26 Data Structures for Dijkstra’s Algorithm Select the unknown node with the lowest cost findMin/deleteMin y’s cost = min(y’s old cost, …) decreaseKey |V| times: |E| times: runtime: O((|V|+|E|) log |V|) O(log |V|) O(log |V|)
  • 27. 27 Spanning tree: a subset of the edges from a connected graph such that:  touches all vertices in the graph (spans the graph)  forms a tree (is connected and contains no cycles) Minimum spanning tree: the spanning tree with the least total edge cost. Spanning Tree 4 7 1 5 9 2
  • 28. 28 Applications of Minimal Spanning Trees • Communication networks • VLSI design • Transportation systems
  • 29. Minimum Spanning Trees Text Read Weiss, §9.5 Prim’s Algorithm Weiss §9.5.1 Similar to Dijkstra’s Algorithm Kruskal’s Algorithm Weiss §9.5.2 Focuses on edges, rather than nodes
  • 30. Definition • A Minimum Spanning Tree (MST) is a subgraph of an undirected graph such that the subgraph spans (includes) all nodes, is connected, is acyclic, and has minimum total edge weight
  • 31.
  • 32. Real Life Application of a MST A cable TV company is laying cable in a new neighborhood. If it is constrained to bury the cable only along certain paths, then there would be a graph representing which points are connected by those paths. Some of those paths might be more expensive, because they are longer, or require the cable to be buried deeper; these paths would be represented by edges with larger weights. A minimum spanning tree would be the network with the lowest total cost.
  • 33. 33 Problem: Laying Telephone Wire Central office
  • 35. 35 Wiring: Better Approach Central office Minimize the total length of wire connecting the customers
  • 36. 36 Minimum Spanning Tree (MST)(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
  • 37. Algorithm Characteristics • Both Prim’s and Kruskal’s Algorithms work with undirected graphs • Both work with weighted and unweighted graphs but are more interesting when edges are weighted • Both are greedy algorithms that produce optimal solutions
  • 38. MST • A minimum spanning tree connects all nodes in a given graph • A MST must be a connected and undirected graph • A MST can have weighted edges • Multiple MSTs can exist within a given undirected graph
  • 39. More about Multiple MSTs • Multiple MSTs can be generated depending on which algorithm is used • If you wish to have an MST start at a specific node • However, if there are weighted edges and all weighted edges are unique, only one MST will exist
  • 40. Borůvka’s Algorithm • The first MST Algorithm was created by Otakar Borůvka in 1926 • The algorithm was used to create efficient connections between the electricity network in the Czech Republic • No longer used since Prim’s and Kruskal’s algorithms were discovered
  • 41. Prim’s Algorithm • Initially discovered in 1930 by Vojtěch Jarník, then rediscovered in 1957 by Robert C. Prim • Similar to Dijkstra’s Algorithm regarding a connected graph • Starts off by picking any node within the graph and growing from there
  • 42. Prim’s Algorithm Cont. • Label the starting node, A, with a 0 and all others with infinite • Starting from A, update all the connected nodes’ labels to A with their weighted edges if it less than the labeled value • Find the next smallest label and update the corresponding connecting nodes • Repeat until all the nodes have been visited
  • 45. Kruskal’s Algorithm • Created in 1957 by Joseph Kruskal • Finds the MST by taking the smallest weight in the graph and connecting the two nodes and repeating until all nodes are connected to just one tree • This is done by creating a priority queue using the weights as keys • Each node starts off as it’s own tree • While the queue is not empty, if the edge retrieved connects two trees, connect them, if not, discard it • Once the queue is empty, you are left with the minimum spanning tree
  • 60. Prim’s Algorithm • Similar to Dijkstra’s Algorithm except that dv records edge weights, not path lengths
  • 61. Walk-Through Initialize array K dv pv A F   B F   C F   D F   E F   F F   G F   H F   4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 2
  • 62. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Start with any node, say D K dv pv A B C D T 0  E F G H 2
  • 63. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0  E 25 D F 18 D G 2 D H 2
  • 64. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C 3 D D T 0  E 25 D F 18 D G T 2 D H 2
  • 65. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B C 3 D D T 0  E 7 G F 18 D G T 2 D H 3 G 2
  • 66. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B C T 3 D D T 0  E 7 G F 18 D G T 2 D H 3 G 2
  • 67. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A B 4 C C T 3 D D T 0  E 7 G F 3 C G T 2 D H 3 G 2
  • 68. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A B 4 C C T 3 D D T 0  E 7 G F T 3 C G T 2 D H 3 G 2
  • 69. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0  E 2 F F T 3 C G T 2 D H 3 G 2
  • 70. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H 3 G 2
  • 71. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H 3 G 2 Table entries unchanged
  • 72. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A 10 F B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2
  • 73. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2
  • 74. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2
  • 75. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Update distances of adjacent, unselected nodes K dv pv A T 4 H B 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 Table entries unchanged
  • 76. 4 25 A H B F E D C G 7 2 10 18 3 4 3 7 8 9 3 10 Select node with minimum distance K dv pv A T 4 H B T 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2
  • 77. 4 A H B F E D C G 2 3 4 3 3 Cost of Minimum Spanning Tree =  dv = 21 K dv pv A T 4 H B T 4 C C T 3 D D T 0  E T 2 F F T 3 C G T 2 D H T 3 G 2 Done
  • 78. Kruskal’s Algorithm Work with edges, rather than nodes Two steps: – Sort edges by increasing edge weight – Select the first |V| – 1 edges that do not generate a cycle
  • 79. Walk-Through Consider an undirected, weight graph 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10
  • 80. Sort the edges by increasing edge weight edge dv (D,E) 1 (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 81. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2 (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 82. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3 (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 83. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3 (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10 Accepting edge (E,G) would create a cycle
  • 84. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3 (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 85. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3 (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 86. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4 5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 87. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4 (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 88. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4 (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 89. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4 (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 90. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5 (D,F) 6 (A,B) 8 (A,F) 10
  • 91. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 3 2 4 6 3 4 3 4 8 4 3 10 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5  (D,F) 6 (A,B) 8 (A,F) 10
  • 92. Select first |V|–1 edges which do not generate a cycle edge dv (D,E) 1  (D,G) 2  (E,G) 3  (C,D) 3  (G,H) 3  (C,F) 3  (B,C) 4  5 1 A H B F E D C G 2 3 3 3 edge dv (B,E) 4  (B,F) 4  (B,H) 4  (A,H) 5  (D,F) 6 (A,B) 8 (A,F) 10 Done Total Cost =  dv = 21 4 }not considered