Implement Chu Liu / Edmonds Algorithm to find the minimum directed spanning tree in a directed graph using adjacency lis

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

Implement Chu Liu / Edmonds Algorithm to find the minimum directed spanning tree in a directed graph using adjacency lis

Post by answerhappygod »

Implement Chu Liu / Edmonds Algorithm to findthe minimum directed spanning tree in a directedgraph using adjacency list representation inC++. I have created a Graph class for the project.How should I implement the Edmonds Algorithm?
*This is not Edmond-Karp Algorithm*
#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 to represent an adjacencylist
vector<vector<Pair>> adjList;
// Graph Constructor
Graph(vector<Edge> const &edges, int n)
{
// resize the vector to hold `n` elements of typevector<Edge>
adjList.resize(n);
// add edges to the directed graph
for (auto &edge: edges)
{
int src = edge.src;
int dest = edge.dest;
int weight = 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 to print all neighboring vertices of a givenvertex
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 the above diagram.
// Please note that the initialization vector in the belowformat will
// work fine in C++11, C++14, C++17 but will 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 representation of a graph
printGraph(graph, n);
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply