Question: #include #include #include / / for pair using namespace std; struct Edge { int dest; int weight; } ; class Graph { private: int V;

#include
#include
#include // for pair
using namespace std;
struct Edge {
int dest;
int weight;
};
class Graph {
private:
int V;
vector> adjList;
public:
Graph(int vertices) : V(vertices){
adjList.resize(V);
}
// Function to add an edge between two vertices with a weight.
void addEdge(int src, int dest, int weight){
Edge edge;
edge.dest = dest;
edge.weight = weight;
adjList[src].push_back(edge);
edge.dest = src;
adjList[dest].push_back(edge);
}
void display(){
for (int v =0; v < V; ++v){
cout << "Vertex "<< v <<" connects to:
";
for (const Edge& edge : adjList[v]){
cout <<" Vertex "<< edge.dest <<" with weight "<< edge.weight <<"
";
}
cout <<"
";
}
}
};
int main(){
// Create a graph with 4 vertices
Graph cityNetwork(4);
cityNetwork.addEdge(0,1,5);
cityNetwork.addEdge(0,2,10);
cityNetwork.addEdge(1,2,3);
cityNetwork.addEdge(2,3,7);
cityNetwork.display();
return 0;
} How could you implement Prims algorith to find the minimum spanning tree to this?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!