Skip to content

Commit

Permalink
Remove PQ in Kruskal README and add properties of MST
Browse files Browse the repository at this point in the history
  • Loading branch information
junnengsoo committed Apr 9, 2024
1 parent 1759640 commit c601303
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
Binary file added docs/assets/images/MSTProperty3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/images/MSTProperty4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 17 additions & 9 deletions src/main/java/algorithms/minimumSpanningTree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ Minimum Spanning Tree (MST) algorithms are used to find the minimum spanning tre
spanning tree of a graph is a connected, acyclic subgraph that includes all the vertices of the original graph. An MST
is a spanning tree with the minimum possible total edge weight.

### 4 Properties of MST
1. An MST should not have any cycles
2. If you cut an MST at any single edge, the two pieces will also be MSTs
3. **Cycle Property:** For every cycle, the maximum weight edge is not in the MST

![MST Property 3](../../../../../docs/assets/images/MSTProperty3.png)

Image Source: CS2040S 22/23 Sem 2 Lecture Slides

4. **Cut Property:** For every partition of the nodes, the minimum weight edge across the cut is in the MST

![MST Property 4](../../../../../docs/assets/images/MSTProperty4.png)

Image Source: CS2040S 22/23 Sem 2 Lecture Slides

Note that the other edges across the partition may or may not be in the MST.

## Prim's Algorithm and Kruskal's Algorithm

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

## Notes

### Difference in use of Priority Queue in Prim's and Kruskal's Algorithm
Prim's Algorithm uses a priority queue to keep track of the minimum weight edge that connects the current tree to an
unexplored node, which could possibly be updated each time a node is popped from the queue.

Kruskal's Algorithm uses a priority queue to sort all the edges by weight and the elements will not be updated at any
point in time.

See the individual READMEs for more details.

### Difference between Minimum Spanning Tree and Shortest Path
It is important to note that a Minimum Spanning Tree of a graph does not represent the shortest path between all the
nodes. See below for an example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ to the MST, provided it does not form a cycle with the already included edges. T
included in the MST.

## Implementation Details
Similar to Prim's Algorithm, Kruskal's Algorithm uses a priority queue (binary heap). However, instead of comparing
the minimum edge weight to each vertex, all the weights of the individual edges are compared instead. Note that we do
not need any decrease key operations as all edges are considered independently and will not be updated at any point in
time.
Kruskal's Algorithm uses a simple `ArrayList` to sort the edges by weight.

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

## Complexity Analysis

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/algorithms/minimumSpanningTree/prim/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ node, and the unexplored node to the current tree, until all nodes are included

### Implementation Details

A priority queue (binary heap) is utilised to keep track of the minimum weight edge that connects the current tree to an
unexplored node. In an ideal scenario, the minimum weight edge to each node in the priority queue should be updated each
A `PriorityQueue` (binary heap) is utilised to keep track of the minimum weight edge that connects the current tree to
an unexplored node. In an ideal scenario, the minimum weight edge to each node in the priority queue should be updated each
time a lighter edge is found to maintain a single unique node in the priority queue. This means that a decrease key
operation is required. However, we know that the decrease key operation of a binary heap implementation of a priority
queue will take O(V) time, which will result in a larger time complexity for the entire algorithm compared to using only
O(log V) operations for each edge.
operation is required.

**Decrease Key Operation:**

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

**Priority Queue Implementation:**

Note that a priority queue is an abstract data type that can be implemented using different data structures. In this
implementation, the default Java `PriorityQueue` is used, which is a binary heap. By implementing the priority queue
with an AVL tree, a decrease key operation that has a time complexity of O(log V) can also be achieved.
Expand Down

0 comments on commit c601303

Please sign in to comment.