-2

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;
}
Dominique Fortin
  • 2,212
  • 15
  • 20
Kallo
  • 17
  • 1
  • 5
  • All pastebin has to do is shuffle it's links, garbage collect, or go out of business and this question can no longer help anyone. All material required to interpret a question should be in the question so that the question remains useful after linked content rots. – user4581301 Mar 19 '17 at 14:49
  • I deliberately didnt put the code here because when i paste it, it is not formatted. I am new to the website and maybe I don't know how to format it, but It is a bad ux to have to format a formatted code after pasting. :) – Kallo Mar 19 '17 at 16:05
  • 1
    Take advantage of the `{}` button atop the text input box to format the code or in most editors block select your code and hit tab before copying. – user4581301 Mar 19 '17 at 16:52

1 Answers1

1

Problem: Want count of connections, not simply that connection exists.

Solution: Use ++

adjMatrix[row][col]++;
adjMatrix[col][row]++;

It is worth noting that

This loop

for (int i = 0; i < 100; i++)
{
    for (int j = 0; j < 100; j++)
    {
        adjMatrix[i][j] = 0;
    }
}

Is rendered redundant by

int adjMatrix[100][100] = { };

because the = {} default initializes the array, setting all elements to 0.

In addition, what if N is greater than the 100 used to define the array dimensions? adjMatrix needs to be dynamic. Read up on and use a std::vector

The following loop conditions are incorrect

    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;
        }
    }

14 lines will ALWAYS be read from the file regardless of the size of the file. The outer loop will always run 7 times (1..7) and the inner loop always twice (1..2).

M defines the number of rows in the file. Use that to control your loop.

This fixed number of lines is particularly bad if there are less than 14 lines in the file because the cin >> are not being tested for validity. The code could be loading the matrix with garbage and you would never know. Instead use something like

if (cin >> row >> col)
{
    do stuff
}
else
{
    handle error
} 

A program must validate all input because you can't trust those slimy users. One minute they're making typos and the next trying to hack into the Pentagon.

This loop accomplishes nothing useful, but you knew that already. No thought went into this at all. It is exceptionally hard to write code without thinking. This is why I held off on answering the question so long. Not thinking is also a disincentive to others offering assistance.

for (int i = 1; i < 8; i++)
{
    for (int j = 1; j <= 2; j++)
    {
        if (adjMatrix[i][j] == adjMatrix[j][i])
        {
            adjMatrix[i][j]++;
        }

    }
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thanks for the reply. :) I first did adjMatrix[row][col]++; adjMatrix[col][row]++; and it results in a false output: 0 1 0 1 1 0 10 2 0 10 0 0 1 2 0 0 Row 2 should have 3 instead of 10, same goes for the next row. That is why I decided to try StackOveflow for help. :) – Kallo Mar 19 '17 at 18:14