0

Basically i generated a adj_matrix and i want to make an adj_list from the adj_matrix...However I keep getting an error saying "no match for call..." i tried it without aPair i still get the same error i can't seem to figure out what my problem is. Can anyone tell me why list isn't working? the list is at the very end of the code

int **gen_random_graph(int n)
{
    srand(time(0));
    int **adj_matrix = new int*[n];
     for(int i = 0; i < n; i++)
     {
        for (int j = i; j < n; j++)   //generating a N x N matrix  based on the # of vertex input
        {
            adj_matrix[i] = new int[n];
        }
     }

    for(int u = 0; u < n; u++)
    {
        for (int v = u; v < n; v++)
        {
            bool edgeOrNot = rand() % 2;   //decide whether it has an edge or not
            adj_matrix[u][v] = adj_matrix[v][u] = edgeOrNot;
            if(adj_matrix[u][v] == true)
            {
                adj_matrix[v][u] = true;
                if(u == v)                            //We can't have i = j in an undirected graph so we set it to false
                {
                    adj_matrix[u][v] = -1;
                }
            }
            else                                        //if adj_matrix[u][v] is false set the symmetry to be false
            {
                adj_matrix[v][u] = adj_matrix[u][v] = -1;
            }
        }

    }
    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++)           //create the N x N with edges and sets the weight between the edge randomly
        {
            if(adj_matrix[i][j] == true)
            {
                    int weight = rand() % 10 + 1;
                    adj_matrix[i][j] = adj_matrix[j][i] = weight;
                    cout << " ( " << i << "," << j << " ) " << "weight: " << adj_matrix[i][j] << endl;
            }
        }
    }



 for(int i = 0; i < n; i++)
{
    vector<int> adj_list;
    for(int j = i; j < n; j++)
    {
        if(adj_matrix[i][j] > 0)
        {
           int weight = adj_matrix[i][j];
           adj_list.push_back(j);
           cout << adj_list[i] <<  " " << endl;
        }
    }
}
    print(n,adj_matrix);
    return (adj_matrix);
}
Darkflame
  • 79
  • 2
  • 11

1 Answers1

2

I see that adj_list is not callable, so your code there is broken. There are a couple simple solutions to that. Taking a look at these docs, you may simply either access listObj.front() and listObj.back() OR you may also just create an iterator using listObj.begin() and iterating over the two elements (which may be desirable if you ever decide to put more than two elements in the list). See this tutorial for a simple example on creating an iterator for a list, in the code snippet right above the summary.

Note, here, your list object which I called listObj for simplicity/abstraction would simply be adj_matrix[i][j] in that bottom loop. That should fix your syntax error.

Also, aside from the syntax of your code, I don't get why you're trying to push weights to a list, then you're printing out and returning the adjacency matrix. I also don't get why you would use lists of pair objects when it seems like you only want to push integer weights onto it. For that, you can use a simple vector of integers (i.e.: vector <int> adj_list;)... or even simpler, you could use a simple array of integers... rather than using a vector of lists of pairs.

EDIT: After running the code locally and taking a look at the values, I realized the issue a bug in the OP's output was simply that he was using "true" in C++ in place of an integer, which was creating a bug, as explained in this SO post. The OP also has a further design decision to make where adjacency lists are concerned. More on what an adjacency list is, conceptually, found on Wikipedia.

Community
  • 1
  • 1
user3773048
  • 5,839
  • 4
  • 18
  • 22
  • o that suppose to be (j, weight) i was just testing out if it accept 1 integer instead of 2 but didn't work out – Darkflame Apr 24 '16 at 20:27
  • Oh, so have you figured it out? It looks like it should be fairly straightforward. But let me know how it goes. – user3773048 Apr 24 '16 at 20:32
  • No i honestly can't figure it out it keep givnig me the same error when i use vector < int > it crashing this time tho – Darkflame Apr 24 '16 at 20:39
  • Ok, good to know it crashes. Let's see what's going on. You're using an int**, and you're allocating space for each of the entries by using new once outside of a loop, and once inside the inner loop of a double loop... Are you sure you're initializing your memory correctly? – user3773048 Apr 24 '16 at 20:46
  • Also, do you have gdb or valgrind? Those can be useful tools for debugging code, especially when it crashes and you want to see a backtrace, or if you don't feel like using print statements to see the values of your data inside of loops etc. – user3773048 Apr 24 '16 at 20:48
  • ummm no i dont have any of t hose im a beginner coder xD and i update the code so far it is working without crashing, however not the result i wanted, it giving me 8936353 as my j :/ – Darkflame Apr 24 '16 at 20:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110079/discussion-between-darkflame-and-user3773048). – Darkflame Apr 24 '16 at 20:52