Question

In: Computer Science

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

The Sample Input 2

3
0

Output 3

0
1
2

Solutions

Expert Solution

Please find Code and related screenshots

CODE

#include <iostream>
using namespace std;

int main() {
//Since we know max number of vertices is 50
   int arr[50][50];
  
   //initializing the array, every element is zero as no edges are present
   for (int i = 0; i < 50; i++) {
       for (int j = 0; j < 50; j++) {
           arr[i][j] = 0;
       }
   }
  
   int n;
   cin >> n;
  
   int edges;
   cin >> edges;
  
   for(int i = 0; i < edges; i++) {
       int a,b;
       cin >> a >> b;
      

//Marking Edge between a and b
       arr[a][b] = 1;
   }
  
   //Itteratinf through all the vertices and cheking for edge
   //If Edge is there we simply print
   for (int i = 0; i < n; i++) {
       cout << i;
       for (int j = 0; j < n; j++) {
           if(arr[i][j]== 1)
               cout << "->" << j;
       }
      
       cout << endl;
   }
  
  
   return 0;
}

OUTPUT

'

Please comment for any further assistance


Related Solutions

Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
In this program you will read a file specifying a directed graph G as a list...
In this program you will read a file specifying a directed graph G as a list of edges. Each edge u →v is listed on one line as u v. The input file simply lists the edges in arbitrary order as pairs of vertices, with each edge on a separate line. The vertices are numbered in order from 1 to the total number of vertices. The program outputs the out-degree sequence for GSCC in increasing order. For example for the...
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...
Write a Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
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.
2. Create a C++ program that converts the number of American dollars entered by the user...
2. Create a C++ program that converts the number of American dollars entered by the user into one of the following foreign currencies: Euro, British pound, German mark, or Swiss franc. Allow the user to select the foreign currency from a menu. Store the exchange rates in a four-element double array named rates. Notice that the menu choice is always one number more than the subscript of its corresponding rate. For example, menu choice 1's rate is stored in the...
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
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...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT