Page 1 of 1

Write C++ codes to implement Edmonds/Chu-Liu algorithm to find minimum directed spanning tree in a weighted directed gra

Posted: Tue Jul 12, 2022 8:20 am
by answerhappygod
Write C++ codes to implementEdmonds/Chu-Liu algorithm to find minimum directedspanning tree in a weighted directed graph by usingadjacency list.
THIS IS NOT EDMONDS KARP ALGORITHM
I have write Graph.h for my graph. Here is the code, how shouldI continue?
#include <iostream>
#include <vector>
using namespace std;
// Data structure to store a graph edge
struct Edge {
int src, dest, weight;
};
typedef pair<int, int> Pair;
// A class to represent a graph object
class Graph
{
public:
// a vector of vectors of Pairs torepresent an adjacency list
vector<vector<Pair>>adjList;
// Graph Constructor
Graph(vector<Edge> const&edges, int n)
{
// resize thevector to hold `n` elements of type vector<Edge>
adjList.resize(n);
// add edges tothe directed graph
for (auto&edge: edges)
{
intsrc = edge.src;
intdest = edge.dest;
intweight = edge.weight;
//insert at the end
adjList[src].push_back(make_pair(dest,weight));
//uncomment the following code for undirected graph
//adjList[dest].push_back(make_pair(src, weight));
}
}
};
// Function to print adjacency list representation of agraph
void printGraph(Graph const &graph, int n)
{
for (int i = 0; i < n; i++)
{
// Function toprint all neighboring vertices of a given vertex
for (Pair v:graph.adjList) {
cout<< "(" << i << ", " << v.first << "," << v.second << ") ";
}
cout <<endl;
}
}
// Graph Implementation using STL
int main()
{
// vector of graph edges as per theabove diagram.
// Please note that the initializationvector in the below format will
// work fine in C++11, C++14, C++17 butwill fail in C++98.
vector<Edge> edges =
{
// (x, y, w)—> edge from `x` to `y` having weight `w`
{0, 1, 6}, {1,2, 7}, {2, 0, 5}, {2, 1, 4}, {3, 2, 10}, {5, 4, 1}, {4, 5, 3}
};
// total number of nodes in the graph(labelled from 0 to 5)
int n = 6;
// construct graph
Graph graph(edges, n);
// print adjacency list representationof a graph
printGraph(graph, n);
return 0;
}