Skip to content

Commit 5fdd5ca

Browse files
committed
PRIMS MST added
1 parent cbddf63 commit 5fdd5ca

File tree

6 files changed

+100
-164
lines changed

6 files changed

+100
-164
lines changed

.idea/workspace.xml

+38-26
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/com/thealgorithm/graph/Edge.java

+9
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public static <V> Edge<V, V> createSimpleEdgeUnWeighted(V v1, V v2) {
1616
return new Edge<>(Vertex.create(v1), Vertex.create(v2), 0D);
1717
}
1818

19+
public static <V> Edge<V, V> createEdge(V v1, V v2, double weight) {
20+
return new Edge<>(Vertex.create(v1), Vertex.create(v2), weight);
21+
}
22+
1923
@Override
2024
public boolean equals(Object o) {
2125
if (this == o) return true;
@@ -29,4 +33,9 @@ public boolean equals(Object o) {
2933
public int hashCode() {
3034
return Objects.hash(getVertex1(), getVertex2(), getWeight());
3135
}
36+
37+
@Override
38+
public String toString() {
39+
return "<" + vertex1.getKey() + "--" + vertex2.getKey() + "|" + weight + '>';
40+
}
3241
}

src/main/java/com/thealgorithm/graph/Graph.java

+6
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,10 @@ public void addEdge(Edge<K, V> edge) {
3232
}
3333
}
3434
}
35+
36+
void clear() {
37+
vertexSet.clear();
38+
edgeSet.clear();
39+
isDirected = false;
40+
}
3541
}

src/main/java/com/thealgorithm/graph/PrimsMST.java

-137
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithm.graph;
2+
3+
import java.util.Comparator;
4+
import java.util.HashSet;
5+
import java.util.PriorityQueue;
6+
import java.util.Set;
7+
8+
/**
9+
* @author: Subham Santra
10+
*/
11+
public class PrimsMinimumSpanningTree {
12+
13+
public <K, V> Set<Edge<K, V>> findMST(final Graph<K, V> graph) {
14+
Set<Vertex<K, V>> visited = new HashSet<>();
15+
PriorityQueue<Edge<K, V>> edgePQ =
16+
new PriorityQueue<>(Comparator.comparingDouble(Edge::getWeight));
17+
Set<Edge<K, V>> minimumSpanningTree = new HashSet<>();
18+
19+
edgePQ.addAll(graph.getEdgeSet());
20+
21+
while ((!edgePQ.isEmpty()) && (visited.size() < graph.getVertexSet().size())) {
22+
Edge<K, V> currentMinimumEdge = edgePQ.poll();
23+
visited.add(currentMinimumEdge.getVertex1());
24+
visited.add(currentMinimumEdge.getVertex2());
25+
minimumSpanningTree.add(currentMinimumEdge);
26+
}
27+
28+
return minimumSpanningTree;
29+
}
30+
31+
// Test code
32+
public static void main(String[] args) {
33+
Graph<Integer, Integer> graph = new Graph<>(false);
34+
35+
graph.addEdge(Edge.createEdge(1, 2, 4));
36+
graph.addEdge(Edge.createEdge(1, 3, 1));
37+
graph.addEdge(Edge.createEdge(1, 5, 3));
38+
graph.addEdge(Edge.createEdge(5, 4, 1));
39+
graph.addEdge(Edge.createEdge(5, 3, 1));
40+
graph.addEdge(Edge.createEdge(2, 4, 2));
41+
42+
System.out.println(new PrimsMinimumSpanningTree().findMST(graph));
43+
}
44+
}

src/main/java/com/thealgorithm/lld/ParkingLotLLD.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ ParkingSpot parkThis(Vehicle vehicle) {
105105
class ParkingLot {
106106
List<Level> parkingLevels;
107107

108-
void park(Vehicle vehicle) {}
108+
void park(final Vehicle vehicle) {
109+
110+
}
109111
}
110112

111113
public class ParkingLotLLD {}

0 commit comments

Comments
 (0)