-
-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathMinimumSpanningTree.java
More file actions
86 lines (60 loc) · 2.53 KB
/
Copy pathMinimumSpanningTree.java
File metadata and controls
86 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package util;
import java.util.*;
public class MinimumSpanningTree {
public List<GraphEdge> primMST(WeightedGraph graph) {
List<GraphEdge>[] vertices = graph.getVertices();
boolean[] inMST = new boolean[vertices.length];
PriorityQueue<GraphEdge> priorityQueue =
new PriorityQueue<>(Comparator.comparingInt(GraphEdge::getWeight));
List<GraphEdge> mstEdges = new ArrayList<>();
// Start from vertex 0
int startVertex = 0;
inMST[startVertex] = true;
// Add all edges from the start vertex to the priority queue
priorityQueue.addAll(vertices[startVertex]);
while (!priorityQueue.isEmpty()) {
GraphEdge currentEdge = priorityQueue.poll();
int destination = currentEdge.getDestination();
// Skip if the destination is already in the MST
if (inMST[destination]) continue;
// Else add the current edge to the MST
mstEdges.add(currentEdge);
inMST[destination] = true;
// Add all edges from the destination to the priority queue
priorityQueue.addAll(vertices[destination]);
}
return mstEdges;
}
// Find the minimum spanning tree using Kruskal's algorithm
public List<GraphEdge> kruskalMST(WeightedGraph graph) {
List<GraphEdge>[] vertices = graph.getVertices();
// Create a priority queue to store all the edges
PriorityQueue<GraphEdge> priorityQueue =
new PriorityQueue<>(Comparator.comparingInt(GraphEdge::getWeight));
// Add all the edges to the priority queue
for (List<GraphEdge> edges : vertices)
priorityQueue.addAll(edges);
// Create a list to store the MST edges
List<GraphEdge> mstEdges = new ArrayList<>();
// Create a set to store the vertices that are already in the MST
Set<Integer> inMST = new HashSet<>();
// Keep polling the edges from the priority queue until the queue is empty
while (!priorityQueue.isEmpty()) {
GraphEdge currentEdge = priorityQueue.poll();
int source = currentEdge.getSource();
int destination = currentEdge.getDestination();
// Skip if the edge forms a cycle
if (inMST.contains(source) && inMST.contains(destination))
continue;
// Else add the current edge to the MST
mstEdges.add(currentEdge);
inMST.add(source);
inMST.add(destination);
}
// Print the MST edges
System.out.println("Minimum Spanning Tree Edges:");
for (GraphEdge edge : mstEdges)
System.out.println(edge.getSource() + " - " + edge.getDestination() + " : " + edge.getWeight());
return mstEdges;
}
}