Question

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.

    (d) Find a minimum spanning tree for each component and print the minimum spanning forest in adjacency matrix representation (regardless it has just one or more than one components).

    You should document your program, analyze the complexity of your algorithms, and show the outputs from sample data sets in the following.

        graph one:

    20

    25

    19,1,3

    1,20,5

    1,2,7

    2,4,7

    4,5,10

    17,5,5

    18,5,20

    8,3,3

    7,8,2

    16,7,6

    7,10,5

    4,10,7

    6,11,6

    11,12,10

    9,13,12

    7,13,10

    13,14,8

    10,14,50

    14,11,100

    15,11,12

    6,4,5

    1,9,20

    8,4,15

    17,12,33

    15,18,5

   

    graph two

    10

    12

    1,9,3

   1,2,1.2

    2,,5,0.5

    2,3,0.8

    3,6,3.1

    3,10,1.5

    4,9,3.2

    4,5,1.5

    5,7,2

    5,8,5.1

    10,8,8.8

    6,7,5.5

   

    graph three

    10

    13

    1,4,2.3

    1,9,1.5

    1,5,2.4

    7,4,8.3

    5,4,3.1

    9,5,5.6

    7,9,0.8

    8,6,3.1

    8,2,8.2

    2,3,1.5

    2,10,6.3

    3,6,3.2

    3,10,5.6

   

    graph four

    15

    20

    1,3,1.2

    1,2,3.1

    2,3,2.5

    6,7,0.8

    6,9,1.2

    6,15,9.8

    7,9,0.8

    7,15,1.1

    7,12,3

    12,9,2.5

    15,12,3.1

    4,5,1.2

    4,8,3

    5,13,1.6

    13,8,6.1

    11,8,3.2

    11,10,1.2

    10,8,5.1

    10,14,2.1

    13,14,3.1

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

CODE:

#include <iostream>
using namespace std;
class Graph {
public:
int** adjacencyMatrix; //declare a 2 dimensional array
int vertexCount; //total no of vertex
public:
Graph(int vertexCount) { //initialize graph
this->vertexCount = vertexCount;
adjacencyMatrix = new int*[vertexCount];
for (int i = 0; i < vertexCount; i++) {
adjacencyMatrix[i] = new int[vertexCount];
for (int j = 0; j < vertexCount; j++)
adjacencyMatrix[i][j] = -1; //initialize weight of graph to -1
}
}
//function to add edge
void addEdge(int i, int j,int w) {
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount) {
adjacencyMatrix[i][j] = w; //add weight to edge
adjacencyMatrix[j][i] = w; //undirected graph weight will be same
}
}


bool isEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j > 0 && j < vertexCount && adjacencyMatrix[i][j]>=0)
{
return true;}
  
else
return false;
}
};

int main() {
   //code
int vt,e,w,i;
int u,v;
cout<<"Enter no of vertices\n";
cin>>vt;
cout<<"Enter no of edges\n";
cin>>e;

Graph g(vt); //create object of graph
//input edge with weight
cout<<"Enter "<<e<<" edges with weight in order (u,v,w)\n";
for(i=0;i<e;i++)
{
cin>>u>>v>>w;
g.addEdge(u,v,w); //add edge to graph
  
}

//print adjacency matrix
cout<<"Adjacency matrix of entered graph :\n";
for(i=0;i<vt;i++)
{
for(int j=0;j<vt;j++)
{
cout<<g.adjacencyMatrix[i][j]<<" ";
}
cout<<"\n";
}
   return 0;
}

OUTPUT:

Enter no of vertices Enter no of edges Enter 25 edges with weight in order (u,v,W) 19 1 3 1 20 5 4 5 10 17 5 5 18 5 20 16 7 6

Add a comment
Know the answer?
Add Answer to:
Write a program to process a weighted undirected graph as follows.   (a) Read in the number...
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
  • 7. Graphs u, u2, u3, u4, u5, u6} and the (a) Consider the undirected graph G...

    7. Graphs u, u2, u3, u4, u5, u6} and the (a) Consider the undirected graph G (V, E), with vertex set V set of edges E ((ul,u2), (u2,u3), (u3, u4), (u4, u5), (u5, u6). (u6, ul)} i. Draw a graphical representation of G. ii. Write the adjacency matrix of the graph G ii. Is the graph G isomorphic to any member of K, C, Wn or Q? Justify your answer. a. (1 Mark) (2 Marks) (2 Marks) b. Consider an...

  • 2. Let G = (V, E) be an undirected connected graph with n vertices and with...

    2. Let G = (V, E) be an undirected connected graph with n vertices and with an edge-weight function w : E → Z. An edge (u, v) ∈ E is sparkling if it is contained in some minimum spanning tree (MST) of G. The computational problem is to return the set of all sparkling edges in E. Describe an efficient algorithm for this computational problem. You do not need to use pseudocode. What is the asymptotic time complexity of...

  • 3. (10 points) Let G be an undirected graph with nodes vi,..Vn. The adja.- cency matriz represent...

    Please show work clearly. Thanks 3. (10 points) Let G be an undirected graph with nodes vi,..Vn. The adja.- cency matriz representation for G is the nx n matrix M given by: Mij-1 if there is an edge from v, to ty. and M,',-0 otherwise. A triangle is a set fvi, vjof 3 distinct vertices so that there is an edge from v, to vj, another from v to k and a third from vk to v. Give and analyze...

  • IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and...

    IN JAVA Given is a weighted undirected graph G = (V, E) with positive weights and a subset of its edges F E. ⊆ E. An F-containing spanning tree of G is a spanning tree that contains all edges from F (there might be other edges as well). Give an algorithm that finds the cost of the minimum-cost F-containing spanning tree of G and runs in time O(m log n) or O(n2). Input: The first line of the text file...

  • Question 1: Given an undirected connected graph so that every edge belongs to at least one...

    Question 1: Given an undirected connected graph so that every edge belongs to at least one simple cycle (a cycle is simple if be vertex appears more than once). Show that we can give a direction to every edge so that the graph will be strongly connected. Question 2: Given a graph G(V, E) a set I is an independent set if for every uv el, u #v, uv & E. A Matching is a collection of edges {ei} so...

  • Consider the java Graph class below which represents an undirected graph in an adjacency list. How...

    Consider the java Graph class below which represents an undirected graph in an adjacency list. How would you add a method to delete an edge from the graph? // Exercise 4.1.3 (Solution published at http://algs4.cs.princeton.edu/) package algs41; import stdlib.*; import algs13.Bag; /** * The <code>Graph</code> class represents an undirected graph of vertices * named 0 through V-1. * It supports the following operations: add an edge to the graph, * iterate over all of the neighbors adjacent to a vertex....

  • Let G be an undirected graph and let X be a subset of the vertices of...

    Let G be an undirected graph and let X be a subset of the vertices of G. A connecting tree on X is a tree composed out of the edges of G that contains all the vertices in X. One way to compute a connecting tree consists of two steps: (1) Compute a minimum spanning tree T over G. (2) Delete all the edges out of T not needed to connect vertices in X. Give an algorithm(Pseudo-code) to carry out...

  • 4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix...

    4&5 0 1 2 3 1. Draw the undirected graph that corresponds to this adjacency matrix 0 0 1 1 0 1 1 1 1 0 1 1 1 2 1 1 1 0 1 3 1 0 1 1 0 1 2. Given the following directed graph, how would you represent it with an adjacency list? 3. We've seen two ways to store graphs - adjacency matrices, and adjacency lists. For a directed graph like the one shown above,...

  • 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...

  • 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...

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