Skip to content

Commit c601303

Browse files
committed
Remove PQ in Kruskal README and add properties of MST
1 parent 1759640 commit c601303

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed
129 KB
Loading
138 KB
Loading

src/main/java/algorithms/minimumSpanningTree/README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@ Minimum Spanning Tree (MST) algorithms are used to find the minimum spanning tre
66
spanning tree of a graph is a connected, acyclic subgraph that includes all the vertices of the original graph. An MST
77
is a spanning tree with the minimum possible total edge weight.
88

9+
### 4 Properties of MST
10+
1. An MST should not have any cycles
11+
2. If you cut an MST at any single edge, the two pieces will also be MSTs
12+
3. **Cycle Property:** For every cycle, the maximum weight edge is not in the MST
13+
14+
![MST Property 3](../../../../../docs/assets/images/MSTProperty3.png)
15+
16+
Image Source: CS2040S 22/23 Sem 2 Lecture Slides
17+
18+
4. **Cut Property:** For every partition of the nodes, the minimum weight edge across the cut is in the MST
19+
20+
![MST Property 4](../../../../../docs/assets/images/MSTProperty4.png)
21+
22+
Image Source: CS2040S 22/23 Sem 2 Lecture Slides
23+
24+
Note that the other edges across the partition may or may not be in the MST.
25+
926
## Prim's Algorithm and Kruskal's Algorithm
1027

1128
We will discuss more implementation-specific details and complexity analysis in the respective folders. In short,
@@ -18,15 +35,6 @@ edges by weight and adding the edge with the minimum weight that does not form a
1835

1936
## Notes
2037

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-
3038
### Difference between Minimum Spanning Tree and Shortest Path
3139
It is important to note that a Minimum Spanning Tree of a graph does not represent the shortest path between all the
3240
nodes. See below for an example:

src/main/java/algorithms/minimumSpanningTree/kruskal/README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ to the MST, provided it does not form a cycle with the already included edges. T
77
included in the MST.
88

99
## 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.
10+
Kruskal's Algorithm uses a simple `ArrayList` to sort the edges by weight.
1411

15-
A [disjoint set](/dataStructures/disjointSet/weightedUnion) data structure is used to keep track of the connectivity of
16-
vertices and detect cycles.
12+
A [`DisjointSet`](/dataStructures/disjointSet/weightedUnion) data structure is also used to keep track of the
13+
connectivity of vertices and detect cycles.
1714

1815
## Complexity Analysis
1916

src/main/java/algorithms/minimumSpanningTree/prim/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ node, and the unexplored node to the current tree, until all nodes are included
88

99
### Implementation Details
1010

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
11+
A `PriorityQueue` (binary heap) is utilised to keep track of the minimum weight edge that connects the current tree to
12+
an unexplored node. In an ideal scenario, the minimum weight edge to each node in the priority queue should be updated each
1313
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.
14+
operation is required.
15+
16+
**Decrease Key Operation:**
1717

18-
Hence, in our implementation, to avoid the use of a decrease key operation, we will simply insert duplicate nodes with
18+
However, we know that the decrease key operation of a binary heap implementation of a priority
19+
queue will take O(V) time, which will result in a larger time complexity for the entire algorithm compared to using only
20+
O(log V) operations for each edge. Hence, in our implementation, to avoid the use of a decrease key operation, we will simply insert duplicate nodes with
1921
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,
2022
while leaving the old node in the queue. Additionally, we will track if a node has already been added into the MST to
2123
avoid adding duplicate nodes.
2224

25+
**Priority Queue Implementation:**
26+
2327
Note that a priority queue is an abstract data type that can be implemented using different data structures. In this
2428
implementation, the default Java `PriorityQueue` is used, which is a binary heap. By implementing the priority queue
2529
with an AVL tree, a decrease key operation that has a time complexity of O(log V) can also be achieved.

0 commit comments

Comments
 (0)