Skip to content

Commit 1759640

Browse files
committed
Add README
1 parent 56b7fa0 commit 1759640

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Minimum Spanning Tree Algorithms
2+
3+
## Background
4+
5+
Minimum Spanning Tree (MST) algorithms are used to find the minimum spanning tree of a weighted, connected graph. A
6+
spanning tree of a graph is a connected, acyclic subgraph that includes all the vertices of the original graph. An MST
7+
is a spanning tree with the minimum possible total edge weight.
8+
9+
## Prim's Algorithm and Kruskal's Algorithm
10+
11+
We will discuss more implementation-specific details and complexity analysis in the respective folders. In short,
12+
1. [Prim's Algorithm](prim) is a greedy algorithm that finds the minimum spanning tree of a graph by starting from an
13+
arbitrary node (vertex) and adding the edge with the minimum weight that connects the current tree to a new node, adding
14+
the node to the current tree, until all nodes are included in the tree.
15+
<<<<<<< HEAD
16+
2. [Kruskal's Algorithm](kruskal) is a greedy algorithm that finds the minimum spanning tree of a graph by sorting the
17+
edges by weight and adding the edge with the minimum weight that does not form a cycle into the current tree.
18+
19+
## Notes
20+
21+
### Difference in use of Priority Queue in Prim's and Kruskal's Algorithm
22+
Prim's Algorithm uses a priority queue to keep track of the minimum weight edge that connects the current tree to an
23+
unexplored node, which could possibly be updated each time a node is popped from the queue.
24+
25+
Kruskal's Algorithm uses a priority queue to sort all the edges by weight and the elements will not be updated at any
26+
point in time.
27+
28+
See the individual READMEs for more details.
29+
30+
### Difference between Minimum Spanning Tree and Shortest Path
31+
It is important to note that a Minimum Spanning Tree of a graph does not represent the shortest path between all the
32+
nodes. See below for an example:
33+
34+
The below graph is a weighted, connected graph with 5 nodes and 6 edges:
35+
![original graph img](../../../../../docs/assets/images/originalGraph.jpg)
36+
37+
The following is the Minimum Spanning Tree of the above graph:
38+
![MST img](../../../../../docs/assets/images/MST.jpg)
39+
40+
Taking node A and D into consideration, the shortest path between them is A -> D, with a total weight of 4.
41+
![SPOriginal img](../../../../../docs/assets/images/SPOriginal.jpg)
42+
43+
However, the shortest path between A and D in the Minimum Spanning Tree is A -> C -> D, with a total weight of 5, which
44+
is not the shortest path in the original graph.
45+
![SPMST img](../../../../../docs/assets/images/SPMST.jpg)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Kruskal's Algorithm
2+
3+
## Background
4+
Kruskal's Algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a connected, weighted graph.
5+
It works by sorting all the edges in the graph by their weight in non-decreasing order and then adding the smallest edge
6+
to the MST, provided it does not form a cycle with the already included edges. This is repeated until all vertices are
7+
included in the MST.
8+
9+
## Implementation Details
10+
Similar to Prim's Algorithm, Kruskal's Algorithm uses a priority queue (binary heap). However, instead of comparing
11+
the minimum edge weight to each vertex, all the weights of the individual edges are compared instead. Note that we do
12+
not need any decrease key operations as all edges are considered independently and will not be updated at any point in
13+
time.
14+
15+
A [disjoint set](/dataStructures/disjointSet/weightedUnion) data structure is used to keep track of the connectivity of
16+
vertices and detect cycles.
17+
18+
## Complexity Analysis
19+
20+
**Time Complexity:**
21+
Sorting the edges by weight: O(E log E) = O(E log V), where V and E is the number of vertices and edges respectively.
22+
Union-Find operations: O(E α(V)), where α is the inverse Ackermann function.
23+
Overall complexity: O(E log V)
24+
25+
**Space Complexity:**
26+
O(V + E) for the storage of vertices in the disjoint set and edges in the priority queue.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Prim's Algorithm
2+
3+
## Background
4+
5+
Prim's Algorithm is a greedy algorithm that finds the minimum spanning tree of a graph by starting from an
6+
arbitrary node (vertex) and adding the edge, with the minimum weight that connects the current tree to an unexplored
7+
node, and the unexplored node to the current tree, until all nodes are included in the tree.
8+
9+
### Implementation Details
10+
11+
A priority queue (binary heap) is utilised to keep track of the minimum weight edge that connects the current tree to an
12+
unexplored node. In an ideal scenario, the minimum weight edge to each node in the priority queue should be updated each
13+
time a lighter edge is found to maintain a single unique node in the priority queue. This means that a decrease key
14+
operation is required. However, we know that the decrease key operation of a binary heap implementation of a priority
15+
queue will take O(V) time, which will result in a larger time complexity for the entire algorithm compared to using only
16+
O(log V) operations for each edge.
17+
18+
Hence, in our implementation, to avoid the use of a decrease key operation, we will simply insert duplicate nodes with
19+
their new minimum weight edge, which will take O(log E) = O(log V) given an upper bound of E = V^2, into the queue,
20+
while leaving the old node in the queue. Additionally, we will track if a node has already been added into the MST to
21+
avoid adding duplicate nodes.
22+
23+
Note that a priority queue is an abstract data type that can be implemented using different data structures. In this
24+
implementation, the default Java `PriorityQueue` is used, which is a binary heap. By implementing the priority queue
25+
with an AVL tree, a decrease key operation that has a time complexity of O(log V) can also be achieved.
26+
27+
## Complexity Analysis
28+
29+
**Time Complexity:**
30+
- O(V^2 log V) for the basic version with an adjacency matrix, where V is the number of vertices.
31+
- O(E log V) with a binary heap and adjacency list, where V and E is the number of vertices and edges
32+
respectively.
33+
34+
**Space Complexity:**
35+
- O(V^2) for the adjacency matrix representation.
36+
- O(V + E) for the adjacency list representation.
37+
38+
## Notes
39+
40+
### Difference between Prim's Algorithm and Dijkstra's Algorithm
41+
42+
| | Prim's Algorithm | Dijkstra's Algorithm |
43+
|-------------------------------------|---------------------------------------------------------------------------------|----------------------------------------------------------|
44+
| Purpose | Finds MST - minimum sum of edge weights that includes all vertices in the graph | Finds shortest path from a single source to all vertices |
45+
| Property Compared in Priority Queue | Minimum weight of incoming edge to a vertex | Minimum distance from source vertex to current vertex |
46+

0 commit comments

Comments
 (0)