Question

In: Computer Science

problem should be done in C++ read in the adjacency matrix for a graph (with 5...

problem should be done in C++

read in the adjacency matrix for a graph (with 5 vertices) from a file, "adjacency.txt".  An example file is here:  adjacency.txt

This example file contains the following matrix:

1 2 4 1 0
2 0 1 1 3
4 1 2 1 0
1 1 1 0 1
0 3 0 1 3

Store the information in a 2D array.
to return the degree of a vertex in a generic graph.

For example, the following : 

cout << "vertex 3's degree:  " << vertexDegree(G,3) << endl;
cout << "vertex 4's degree:  " << vertexDegree(G,4) << endl;

should output:

vertex 3's degree: 4
vertex 4's degree: 10

thanks!

Solutions

Expert Solution

Program Screen Shot:

Sample Output:

Program Code to Copy:

#include <iostream>
#include <fstream> // for file IO
#define ROWS 5
#define COLS 5
//
using namespace std;
//
// function to determine the degree by
// summing the elements in row number v-1
// where v is the vertex id
int vertexDegree(int M[ROWS][COLS],int v){
int sum = 0;
int i = v-1; // obtain index from vertex`
for(int j=0;j<COLS;j++){
sum = sum + M[i][j]; // cumulative sum
}
return sum; // the degree
}
//
//
int main(){
//
int v1,v2,v3,v4,v5; // vertices
int G[ROWS][COLS]; // 2D matrix

// file operations
string inFileName = "adjacency.txt";
ifstream inFile;
inFile.open(inFileName); // open file

// read file until eof, reading integers per row
int i=0;
while (inFile>>v1>>v2>>v3>>v4>>v5){
G[i][0] = v1;
G[i][1] = v2;
G[i][2] = v3;
G[i][3] = v4;
G[i][4] = v5;
i++;
}
// close file
inFile.close();

// output invoking the vertexDegree function
cout << "vertex 3's degree: " << vertexDegree(G,3) << endl;
cout << "vertex 4's degree: " << vertexDegree(G,4) << endl;
//
return 0;
}

------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~


Related Solutions

Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list...
Answer True or False 1. For graph representation, adjacency Matrix is more efficiency than adjacency list in term of searching for edge. 2. Topological sort runs in O(|V| + |E|) where |V| is the number of vertices, and |E| is the number of edges in the input graph. 3. If vertex u can reach vertex v, and vertex v can reach vertex u, then vertices u and v are in the same Strongly-connected component (SCC). 4. The Bellman-Ford algorithm will...
One simple representation for a graph is to use an adjacency matrix: each of the N...
One simple representation for a graph is to use an adjacency matrix: each of the N nodes is given a unique number in the range 0 to N-1 to identify it. A large two dimensional array A with N rows and N columns is created so that A[x][y] stores the cost of travelling directly from node x to node y. if A[x][y] is zero, then there is no direct connection from x to y. A[x][y] does not need to equal...
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by...
Python3 question. Suppose that a graph represent as adjacency matrix, how could it perform DFS by using stack. Graph is a undirect and unweighted graph.
maximumST is a function that should take an n × n vector (representing the adjacency matrix...
maximumST is a function that should take an n × n vector (representing the adjacency matrix of a graph) as input and return the value of the maximum spanning tree of the graph defined by the n × n vector. Code: #include <vector> #include <cmath> #include <iostream> #include <cstdio> using namespace std; double maximumST( vector< vector<double> > adjacencyMatrix ){   vector<int> row(4,-1);   vector< vector<int> > matrix(5,row);   cerr << matrix.size() << endl;   cerr << matrix[0].size() << endl;   for( int i = 0;...
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive...
Given the following adjacency matrix, A, for nodes a, b, c, and d, find the transitive closure of A. Is the result an equivalence relation, and why or why not? A = ⌈ 1 0 1 0 ⌉ | 0 1 1 0 | | 1 0 0 1 | ⌊ 1 1 0 0 ⌋
C++ - Almost done, I have a problem with read access violation error. This is the...
C++ - Almost done, I have a problem with read access violation error. This is the link to my assignment first part: https://pastebin.com/yvcvdqLY second part: https://pastebin.com/vY7MK1Bf, My header file: https://pastebin.com/pcJnctgu My .cpp file: https://pastebin.com/9jAfP9u8 My source file: https://pastebin.com/kFsidY2k
C++ program that converts a directed graph data from a user into a corresponding adjacency list...
C++ program that converts a directed graph data from a user into a corresponding adjacency list format. First it will read an input graph data from a user first, after it will convert it to the adjacency matrix format. Assume that the maximum number of vertices in the input graph is less than or equal to 50. The Sample Input 1 3 6 0 1 1 0 1 2 2 1 2 0 0 2 Output 1 0->1->2 1->0->2 2->0->1...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data...
in C++ In the main function, declare a two-dimensional matrix array 5 by 5, read data from a file named “data.txt”. Suppose the data in the file are integers between 0 and 9. Write your main function to print out the left bottom below the diagonal of the matrix and keep the triangle shape. Note the numbers on the diagonal are not included in the output. An example is given as follows. (25 points) Suppose the original matrix is 1...
how do I create a graph class to store a adjacency list representation of a graph?...
how do I create a graph class to store a adjacency list representation of a graph? I need some help and dont conpletely understand how to start. if you could provide a code with some steps id appreciate it.
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR...
Assignment: Square Matrix ("Caesar Block")Cipher C++ ASSIGNMENTS *REMEMBER IT SHOULD NOT ONLY READ ONE LINE OR A SINGLE WORD remember, read, understand, and use the waterpump.cpp program will process an arbitrary amount of input text, however long. Arrange a stream in a two dimensional array of characters by filling up the array a line at a time, from leftmost column to rightmost column. DO NOT USE A GETLINE: this program should be able to input an entire library, not a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT