Question

Problem 3 (25 points) Write a program to find minimum weight cycle in an undirected weighted graph. The input is the Adjacency Matrix A of the graph. If there is an edge between vertex i to vertex j, and weight of this edge is u, then ?3] Aj, i-w. If there is no edge between i and J, A 11.j-Aj, i--1.

In C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution C+ Code: //header file //for cin and cout using namespace std; //define the infinity value # define INFVALUE 0x3f3f3

//function declaration veid insertEdge (int sv, int dv, int w); veid deleteEdge (int sv, int dv, int w); intcomputeMinimumPat

2.era els.begin ( ) ); int sv t.second; list< pair<int, int> >::iterator it; far (ít =A[sv] .beg i n ( ) ; it !=A[3v].end ( )

for (int r F 0r<te r++) //existing Edge data edgeDetail e= ed [r]; //call deleteEdge ) function deleteEge e.s, ey, e.weight);

Sample Output: I.ll Result Sg++ -o main .cpp $main Minimum cycle value: 6Copyable Code:

//header file
#include<bits/stdc++.h>
//for cin and cout
using namespace std;
//define the infinity value
# define INFVALUE 0x3f3f3f3f
//structure for edge data
struct edgeDetail
{
//declare required variable
int sv;
int dv;
int weight;
};
//class for undirected udGraph
class udGraph
{
int vertex;
//declare the adjacent in list
list <pair <int, int>>*A;
//for stores the data of every edge
vector <edgeDetail> ed;
public:
//constructor for udGraph
udGraph(int vertex)
{
this->vertex = vertex;
A = new list <pair<int, int>>[vertex];
}
//function declaration
void insertEdge(int sv, int dv, int w);
void deleteEdge(int sv, int dv, int w);
int computeMinimumPath(int sv, int dv);
void deleteEdge(int sv, int dv);
int computeMinimumCycle();

};
//function definition for insert edge into given graph
void udGraph :: insertEdge(int sv, int dv, int w1)
{
//push back
A[sv].push_back(make_pair(dv, w1));
A[dv].push_back(make_pair(sv, w1));
//insert edge to given edge list
edgeDetail e1 {sv, dv, w1};
ed.push_back (e1);
}
//function definition for delete edge from given graph
void udGraph :: deleteEdge(int sv, int dv, int w1)
{
//for delete edge
A[sv].remove(make_pair(dv, w1));
A[dv].remove(make_pair(sv, w1));
}
//function definition for compute minimum path
//using Dijikstra’s shortest path algorithm
int udGraph::computeMinimumPath(int sv, int dv)
{
set<pair<int, int>> s;
//generate a vector for distances
//initializes the infinity for every distance
vector<int> d(vertex, INFVALUE);
//insert the source itself
s.insert(make_pair(0, sv));
d[sv] = 0;
while (!s.empty())
{
pair<int, int> t = *(s.begin());
s.erase(s.begin());
int sv = t.second;
list< pair<int, int> >::iterator it;
for (it = A[sv].begin(); it != A[sv].end(); ++it)
{
int dv = (*it).first;
int weight = (*it).second;
//If there is minimum path to dv by su, then
if (d[dv] > d[sv] + weight)
{
///check below condition
if (d[dv] != INFVALUE)
s.erase(s.find(make_pair(d[dv], dv)));
//altering the distance of dv
d[dv] = d[sv] + weight;
s.insert(make_pair(d[dv], dv));
}
}
}
//return shortest path
return d[dv] ;
}
//function definition for find minimum weighted cycle
int udGraph :: computeMinimumCycle()
{
//for minimum cycle
int mc = INT_MAX;
//size
int te = ed.size();
for (int r = 0 ; r < te ; r++)
{
//existing Edge data
edgeDetail e = ed[r];
//call deleteEdge() function
deleteEdge(e.sv, e.dv, e.weight);
//compute minimum distance between 2 vertices
int md = computeMinimumPath(e.sv, e.dv);
//compute minimum cycle value
mc = min(mc, md + e.weight );
//call insertEdge()
insertEdge(e.sv, e.dv, e.weight );
}
return mc ;
}
//main function
int main()
{
int totalVertex = 9;
//create object for given undirected graph class udGraph
udGraph adj(totalVertex);
//create graph
adj.insertEdge(0, 1, 1);
adj.insertEdge(0, 2, 4);
adj.insertEdge(0, 3, 3);
adj.insertEdge(1, 3, 2);
adj.insertEdge(2, 3, 5);
cout << "Minimum cycle value: "<<adj.computeMinimumCycle() << endl;
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C++ Problem 3 (25 points) Write a program to find minimum weight cycle in an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program to process a weighted undirected graph as follows.   (a) Read in the number...

    Write a program to process a weighted undirected graph as follows.   (a) Read in the number of vertices V and the number of edges E of the graph followed by its E edges, each in the form u, v, w where 1 <= u, v <= V & w > 0 representing an edge uv with weight w.     (b) Set up and print the adjacency matrix representation of the Graph.     (c) Determine whether the graph is connected.    ...

  • Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create...

    Creating Graphs First, write Java code in GraphTest.java to read a data file (graph.in) and create a graph using the input data. The input file must have the following format: 6 049 0 27 2 3 1 25 2 1 5 6 352 4 5 1 Here, the number on the first line represents the number of vertices in the graph. The letter on the second line indicates whether this is a directed graph ('D') or an undirected graph ('U')....

  • C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected...

    C++ programing question22 Minimum spanning tree Time limit: 1 second Problem Description For a connected undirected graph G = (V, E), edge e corresponds to a weight w, a minimum weight spaning tree can be found on the graph. Into trees. Input file format At the beginning, there will be a positive integer T, which means that there will be T input data. The first line of each input has two positive integers n,m, representing n points and m edges...

  • Write a program that specifies a simple undirected graph by its “adjacency matrix”. Recall that that...

    Write a program that specifies a simple undirected graph by its “adjacency matrix”. Recall that that the adjacency matrix A is such that A(i, j) = 1 if nodes i and j are adjacent and A(i, j) = 0 otherwise. Let αk(i ,j) be the number of paths of length k between nodes i and j. For instance, the number of paths of length-1 between nodes i and j in a simple undirected graph is 1 if they are adjacent...

  • IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and...

    IN C LANGUAGE Write a program that counts the leaves of a given binary tree. Hint: You will make a small (and smart) update to the add function we discussed in class. a) 25 points Create the following binary trees by creating the nodes (malloc) and setting the related pointers (leftChild, right Child). Make content random. 50 40 60 100 70 45 30 120 47 b) 25 points Display the trees on screen the way we did in slide 9...

  • Solve all parts please 5. In the following problems, recall that the adjacency matrix (or incidence matrix) for a simp...

    Solve all parts please 5. In the following problems, recall that the adjacency matrix (or incidence matrix) for a simple graph with n vertices is an n x n matrix with entries that are all 0 or 1. The entries on the diagonal are all 0, and the entry in the ih row and jth column is 1 if there is an edge between vertex i and vertex j and is 0 if there is not an edge between vertex...

  • Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An...

    Design and implement Dijkstra’s algorithm to compute all-pair shortest paths in any given graph using An adjacency matrix using a one-dimensional array for storing only the elements of the lower triangle in the adjacency matrix.[Program in C language] The input to program must be connected, undirected, and weighted graphs. The programs must be able to find shortest paths on two types of connected, undirected, and weighted graphs: complete graph (a graph with a link between every pair of nodes) and...

  • Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, ...

    Help !! I need help with Depth-First Search using an undirected graph. Write a program, IN JAVA, to implement the depth-first search algorithm using the pseudocode given. Write a driver program, which reads input file mediumG.txt as an undirected graph and runs the depth-first search algorithm to find paths to all the other vertices considering 0 as the source. This driver program should display the paths in the following manner: 0 to ‘v’: list of all the vertices traversed to...

  • Please write the complete code in C. Write a C function that prints the minimum spanning...

    Please write the complete code in C. Write a C function that prints the minimum spanning tree of a graph. At the end, print the weight of the spanning tree. A suggested report format is shown in the following example. Source Vertex To Vertex Weight A B 2 A C 4 B D 3 D E 1 Total weight of spanning tree: 10 Your main program will read a graph from DataIn file to an adjacency table before calling the...

  • 3, (30 points) Given a directed graph G - N. E), each edge eEhas weight We, 3, (30 points) Given a directed graph...

    3, (30 points) Given a directed graph G - N. E), each edge eEhas weight We, 3, (30 points) Given a directed graph G (V, E), each edgee which can be positive or negative. The zero weight cycle problem is that whether exists a simple cycle (each vertex passes at most once) to make the sum of the weights of each edge in G is exactly equal to 0. Prove that the problem is NP complete. 3, (30 points) Given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT