You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/java/algorithms/minimumSpanningTree/prim/README.md
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,18 +8,22 @@ node, and the unexplored node to the current tree, until all nodes are included
8
8
9
9
### Implementation Details
10
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
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
13
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.
14
+
operation is required.
15
+
16
+
**Decrease Key Operation:**
17
17
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
19
21
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
22
while leaving the old node in the queue. Additionally, we will track if a node has already been added into the MST to
21
23
avoid adding duplicate nodes.
22
24
25
+
**Priority Queue Implementation:**
26
+
23
27
Note that a priority queue is an abstract data type that can be implemented using different data structures. In this
24
28
implementation, the default Java `PriorityQueue` is used, which is a binary heap. By implementing the priority queue
25
29
with an AVL tree, a decrease key operation that has a time complexity of O(log V) can also be achieved.
0 commit comments