-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq17.cpp
176 lines (147 loc) · 4.03 KB
/
q17.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
using namespace std;
const int INF = 1e9;
int graph[100][100], parent[100], ranks[100], n, m;
struct Edge {
int u, v, weight;
};
int dist[100], visited[100];
void initialize() {
for (int i = 0; i < n; i++) {
parent[i] = i;
ranks[i] = 0;
}
}
int find(int u) {
if (u != parent[u]) {
parent[u] = find(parent[u]);
}
return parent[u];
}
void union_sets(int u, int v) {
int root_u = find(u);
int root_v = find(v);
if (root_u != root_v) {
if (ranks[root_u] > ranks[root_v]) {
parent[root_v] = root_u;
} else if (ranks[root_u] < ranks[root_v]) {
parent[root_u] = root_v;
} else {
parent[root_v] = root_u;
ranks[root_u]++;
}
}
}
void insertionSort(Edge edges[], int edgeCount) {
for (int i = 1; i < edgeCount; i++) {
Edge key = edges[i];
int j = i - 1;
while (j >= 0 && edges[j].weight > key.weight) {
edges[j + 1] = edges[j];
j = j - 1;
}
edges[j + 1] = key;
}
}
void dijkstra(int source) {
for (int i = 0; i < n; i++) {
dist[i] = INF;
visited[i] = 0;
}
dist[source] = 0;
for (int count = 0; count < n - 1; count++) {
int u = -1;
for (int i = 0; i < n; i++) {
if (!visited[i] && (u == -1 || dist[i] < dist[u])) {
u = i;
}
}
visited[u] = 1;
for (int v = 0; v < n; v++) {
if (graph[u][v] != INF && !visited[v] && dist[u] + graph[u][v] < dist[v]) {
dist[v] = dist[u] + graph[u][v];
}
}
}
cout << "Shortest distances from source node " << source << " are:" << endl;
for (int i = 0; i < n; i++) {
if (dist[i] == INF) {
cout << "Node " << i << ": INF" << endl;
} else {
cout << "Node " << i << ": " << dist[i] << endl;
}
}
}
void kruskal() {
Edge edges[100];
int edgeCount = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (graph[i][j] != INF) {
edges[edgeCount++] = {i, j, graph[i][j]};
}
}
}
insertionSort(edges, edgeCount);
int mstWeight = 0;
int mstEdges = 0;
initialize();
cout << "Edges in the Minimum Spanning Tree (MST):" << endl;
for (int i = 0; i < edgeCount && mstEdges < n - 1; i++) {
int u = edges[i].u;
int v = edges[i].v;
if (find(u) != find(v)) {
union_sets(u, v);
cout << u << " - " << v << " with weight " << edges[i].weight << endl;
mstWeight += edges[i].weight;
mstEdges++;
}
}
cout << "Total Weight of MST: " << mstWeight << endl;
}
int main() {
cout << "Enter the number of nodes: ";
cin >> n;
cout << "Enter the number of edges: ";
cin >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j) graph[i][j] = INF;
else graph[i][j] = 0;
}
}
cout << "Enter the edges (u v weight):" << endl;
for (int i = 0; i < m; i++) {
int u, v, weight;
cin >> u >> v >> weight;
graph[u][v] = weight;
graph[v][u] = weight;
}
int choice;
while (true) {
cout << "1. Run Dijkstra's Algorithm\n";
cout << "2. Run Kruskal's Algorithm\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int source;
cout << "Enter the source node for Dijkstra: ";
cin >> source;
dijkstra(source);
break;
}
case 2: {
kruskal();
break;
}
case 3:
cout << "Exiting program." << endl;
return 0;
default:
cout << "Invalid choice! Please try again." << endl;
}
}
return 0;
}