In: Computer Science
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
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