-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab9.java
More file actions
142 lines (103 loc) · 3.29 KB
/
Lab9.java
File metadata and controls
142 lines (103 loc) · 3.29 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.util.*;
class Edge implements Comparable<Edge> {
int src, dest, weight;
public Edge(int src, int dest, int weight) {
this.src = src;
this.dest = dest;
this.weight = weight;
}
public int compareTo(Edge other) {
return this.weight - other.weight;
}
}
class Node {
Node parent;
int rank;
public Node() {
this.parent = this;
this.rank = 0;
}
}
class Graph {
int V; // Number of vertices
List<Edge> edges; // List of edges
public Graph(int V) {
this.V = V;
edges = new ArrayList<>();
}
public void addEdge(int src, int dest, int weight) {
edges.add(new Edge(src, dest, weight));
}
// Disjoint-set make set operation
private Node makeSet() {
return new Node();
}
// Find function with path compression
private Node find(Node node) {
if (node != node.parent) {
node.parent = find(node.parent); // Path compression
}
return node.parent;
}
// Union by rank
private void union(Node node1, Node node2) {
Node root1 = find(node1);
Node root2 = find(node2);
if (root1 != root2) {
if (root1.rank < root2.rank) {
root1.parent = root2;
} else if (root1.rank > root2.rank) {
root2.parent = root1;
} else {
root2.parent = root1;
root1.rank++;
}
}
}
// Kruskal's algorithm to find MST
public List<Edge> kruskalMST() {
List<Edge> mst = new ArrayList<>();
Node[] subsets = new Node[V];
// Create subset nodes for each vertex
for (int i = 0; i < V; i++) {
subsets[i] = makeSet();
}
Collections.sort(edges);
for (Edge edge : edges) {
Node rootSrc = find(subsets[edge.src]);
Node rootDest = find(subsets[edge.dest]);
// If including this edge doesn't form a cycle, add it to the MST
if (rootSrc != rootDest) {
mst.add(edge);
union(rootSrc, rootDest);
}
}
return mst;
}
public int calculateMSTCost(List<Edge> mst) {
int totalWeight = 0;
for (Edge edge : mst) {
totalWeight += edge.weight;
}
return totalWeight;
}
}
public class Lab9 {
public static void main(String[] args) {
int V = 4; // Number of vertices
Graph graph = new Graph(V);
// Add edges: (source, destination, weight)
graph.addEdge(0, 1, 10);
graph.addEdge(0, 2, 6);
graph.addEdge(0, 3, 5);
graph.addEdge(1, 3, 15);
graph.addEdge(2, 3, 4);
List<Edge> mst = graph.kruskalMST();
System.out.println("Edges in the MST:");
for (Edge edge : mst) {
System.out.println("Edge: " + edge.src + " - " + edge.dest + " (weight: " + edge.weight + ")");
}
int mstCost = graph.calculateMSTCost(mst);
System.out.println("Total cost of MST: " + mstCost);
}
}