CmSc 250 Intro to Algorithms
Graph Algorithms - Minimum Spanning Tree
- Spanning trees
Number of nodes in the spanning tree: |V|
Number of edges in the spanning tree: |V|-1
If the graph is unweighted any spanning tree will have same number of edges. That is why it is not called "minimum" - all spanning trees will be 'minimum' trees with number of edges equal to |V|-1.
If the graph is weighted then we are interested in finding a tree with a minimum total sum of the edge weights.
Algorithm to find a spanning tree (similar to finding the shortest path in unweighted graphs)
Data structures needed:
Example: the "prisoners" problem
Complexity: O(|E| + |V|)
We store in the queue
each vertex only once (O(|V|)).
In step 2b. we examine the outgoing edges for a given vertex,
thus the sum of all examined edges in the while loop will be equal to
the the number of all edges -
(O(|E|)).
If we use matrix representation the complexity is O(|V|2),
because we need to read an entire row in the matrix of length |V| in order
to find the adjecent vertices for a given vertex.
Problem: For a given weighted graph, find a spanning tree with the minimal sum of the weights.
The algorithm is similar to finding the shortest paths
in a weighted graphs.
The difference is that we record in the
table the length of the current edge,
not the length of the path .
Data structures needed:
Algorithm:
At the end of the algorithm, the tree would be represented in the table with its edges
{(Ti,3 , vi ) | i = 1, 2, -|V|}
Complexity: O(| E |log(| V |))
All edges have to be examined, and in the worst case each edge might
cause updating of the priority queue.
If adjacency matrix is used, the complexity would increase to O(|V|2)
Example: The "conspirators" problem
Kruskal's algorithm works with tree forests and the set of edges.
Algorithm:
Implementation
The algorithm is implemented using operations on
disjoint sets.
All the vertices are grouped into sets corresponding
to the currently built trees.
Since each vertex appears in one tree only,
the sets are disjoint.
Two set operations are used:
Each vertex is associated with the disjoint set where it belongs.
To find out whether two vertices are in the same tree,
we need to find out if their disjoint sets are the same.
When we link the trees, the combine the two disjoint
sets to form one set
- corresponding to the new tree
Example: The "conspirators" problem
Complexity of Kruskal's algorithm:
A detailed analysis will show O(|V|) + O(|E|log(|E|)) + O(|E|log(|V|)).Disregarding the lower term O(|V|) we get O(|E|(log(|V|) + log(|E|)).
At the worst case |E| = O(|V|2). Hence log(|E|) = O(log(|V|2)) =
O(2log(|V|)) = O(log(|V|).
Thus we get complexity O(|E|log(|V|)).
On the other hand, |V| = O(|E|), hence we can reduce the complexity expression to O(|E|log(|E|)).
For sparse trees Kruskal's algorithm is better - since it is guided by the edges.
For dense trees Prim's algorithm is better - the process is limited by the number of the vertices in the graph.
Learning Goals
Exam-like questions