I am struggling to convert a list of Edges to Adjacency Matrix in a certain required way.
Problem:
The Adjacency matrix should have number of edges connected to a vertex, so it should not have only 1s where two vertexes are connected. For better explanation here is sample input.
Input
4 7
1 2
2 3
3 2
2 4
4 1
4 2
3 2
Where 4 is number of vertexes and 7 is the number of edges and each line represents edges.
Output:
4
0 1 0 1
1 0 3 2
0 3 0 0
1 2 0 0
Where 4 is number of vertexes and each line represents how many times a vertex is connected.
In my code I only managed to convert to normal Adjacency matrix with 1s, but I cannot understand if I can solve this task only with arrays and how? Here is my code:
#include <iostream>
using namespace std;
int main()
{
int adjMatrix[100][100] = { };
int edgeList[100][100];
int row, col;
int N, M;
cin >> N;
cin >> M;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
adjMatrix[i][j] = 0;
}
}
for (int i = 1; i < 8; i++)
{
for (int j = 1; j <= 2; j++)
{
cin >> row;
cin >> col;
adjMatrix[row][col] = 1;
adjMatrix[col][row] = 1;
}
}
for (int i = 1; i < 8; i++)
{
for (int j = 1; j <= 2; j++)
{
if (adjMatrix[i][j] == adjMatrix[j][i])
{
adjMatrix[i][j]++;
}
}
}
cout << N << endl;
for (int i = 1; i <= N; i++)
{
for (int j = 1; j <= N; j++)
{
cout << adjMatrix[i][j] << " ";
}
cout << endl;
}
return 0;
}