-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp7.cpp
More file actions
114 lines (95 loc) · 3.2 KB
/
Copy pathp7.cpp
File metadata and controls
114 lines (95 loc) · 3.2 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
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
#define MAX 10 // Maximum number of cities
// Structure for edges
struct Edge {
int start, end, weight;
};
class Graph {
int adj[MAX][MAX]; // Adjacency matrix
string city[MAX]; // City names
int numCities; // Number of cities
vector<Edge> mst; // Minimum Spanning Tree (MST)
int totalCost; // Total cost of MST
public:
Graph(); // Constructor
void prims(int start); // Prim's algorithm to find MST
void showMST(); // Display the MST
};
// Constructor: Initializes the graph
Graph::Graph() {
totalCost = 0;
cout << "Enter number of cities (1-" << MAX << "): ";
cin >> numCities;
numCities = min(numCities, MAX);
// Get city names
for (int i = 0; i < numCities; i++) {
cout << "Enter city " << i + 1 << ": ";
cin >> city[i];
}
// Initialize adjacency matrix with "infinity" (no direct connections)
for (int i = 0; i < numCities; i++) {
for (int j = 0; j < numCities; j++) {
adj[i][j] = INT_MAX; // Initialize with large value
}
}
// Get city connections (edges)
int connections;
cout << "Enter number of city connections: ";
cin >> connections;
// Show city codes
cout << "\nCity Codes: \n";
for (int i = 0; i < numCities; i++) {
cout << i << " " << city[i] << endl;
}
// Read edges
for (int i = 0; i < connections; i++) {
int x, y, cost;
cout << "Enter connection (city1 city2 cost): ";
cin >> x >> y >> cost;
adj[x][y] = cost; // Undirected graph
adj[y][x] = cost; // Symmetric
}
}
// Prim's Algorithm to find MST
void Graph::prims(int start) {
bool visited[MAX] = {false}; // Array to track visited cities
visited[start] = true; // Mark the starting city as visited
while (mst.size() < numCities - 1) { // While there are still edges to add
Edge minEdge = {0, 0, INT_MAX}; // Initialize minEdge with maximum cost
// Find the smallest edge from any visited city
for (int i = 0; i < numCities; i++) {
if (visited[i]) { // If city 'i' is visited
for (int j = 0; j < numCities; j++) {
if (!visited[j] && adj[i][j] < minEdge.weight) { // If city 'j' is unvisited and edge weight is less
minEdge = {i, j, adj[i][j]}; // Update minEdge
}
}
}
}
// Add the edge to MST
mst.push_back(minEdge);
totalCost += minEdge.weight;
visited[minEdge.end] = true; // Mark the destination city as visited
}
}
// Display the Minimum Spanning Tree (MST)
void Graph::showMST() {
cout << "\nMost efficient network (Minimum Spanning Tree):\n";
for (Edge e : mst) {
cout << city[e.start] << " - " << city[e.end] << " (Cost: " << e.weight << ")\n";
}
cout << "Total network cost: " << totalCost << endl;
}
// Main function
int main() {
Graph g;
int start;
cout << "\nEnter starting city code: ";
cin >> start;
g.prims(start);
g.showMST();
return 0;
}