Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphs using Adjacency List in c++

I am trying to implement a graph in C++. I am representing a node in graph using a structure which contains two variables -
a) an integer to contain some information about the node.
b) a list to contain index of other vertex which are connected to it.
Following is the code.

// Graphs using adjacency list

#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;

// structure to represent a vertex(node) in a graph
typedef struct vertex{
    int info;
    list<int> adj;   // adjacency list of edges contains the indexes to vertex
} *vPtr;             

int main(){
    vPtr node = (vPtr)malloc(sizeof(struct vertex));
    node->info = 34;            // some arbitrary value
    (node->adj).push_back(2);   // trying to insert a value in the list
    return 0;
}

The code is compiling fine but I am getting a run time error while I am pushing back an element in the list. Is there any problem in my structure.
I am using code blocks and GNU GCC, C++ 98 compiler to compile my code.

like image 584
Nishant Avatar asked Jul 29 '13 16:07

Nishant


People also ask

How do I make a graph from adjacency list?

In Adjacency List, we use an array of a list to represent the graph. The list size is equal to the number of vertex(n). Adjlist[0] will have all the nodes which are connected to vertex 0. Adjlist[1] will have all the nodes which are connected to vertex 1 and so on.

How do you implement a graph using adjacency matrix in C?

Create a 2D array(say Adj[N+1][N+1]) of size NxN and initialise all value of this matrix to zero. For each edge in arr[][](say X and Y), Update value at Adj[X][Y] and Adj[Y][X] to 1, denotes that there is a edge between X and Y. Display the Adjacency Matrix after the above operation for all the pairs in arr[][].

What is adjacency list explain with example?

An adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex. For example, we have a graph below. An undirected graph.

What is adjacency list in graph theory?

In graph theory and computer science, an adjacency list is a collection of unordered lists used to represent a finite graph. Each unordered list within an adjacency list describes the set of neighbors of a particular vertex in the graph.


3 Answers

malloc is a C function - it shouldn't be used with C++ objects, which is very well explained here (short answer: in C++, when you are not dealing with POD types, std::list in your case, you must call the object's constructor to have the actual object ready for use, and malloc() does not do that).

You should used new instead. While malloc only allocates a block of memory of size vertex, new does that and also initializes std::list aswell by calling it's constructor (interesting to point out that when you call delete(), you are calling your object's desctructor aswell).

Here is a piece of code that works for your case, although I suggest you to start using more C++ features within C++ projects:

#include <iostream>
#include <list>
#include <cstdlib>
#include <new>

using namespace std;

// structure to represent a vertex(node) in a graph
typedef struct vertex{
    int info;
    list<int> adj;   // adjacency list of edges contains the indexes to vertex
} *vPtr;             

int main(){
    cout << "allocating memory for our vertex struct... \n";
    vPtr node = new vertex();
    node->info = 34;            // some arbitrary value
    (node->adj).push_back(2);   // trying to insert a value in the list
    cout << "cleaning allocated memory... \n";
    delete(node);

    return 0;
}
like image 169
Natan Streppel Avatar answered Oct 12 '22 23:10

Natan Streppel


Couple of things.

  1. Because you are using malloc no constructor is ever called, and as such the non primitive member adj is never constructed and is NULL.
  2. You are leaking memory since you never free/delete any of your dynamically allocated memory.

  3. If you are using C++ why are you using malloc instead of new and delete?

  4. You don't have to say struct vertex in the sizeof for C++.

To fix it you could do:

vPtr node = new struct vertex(); // also change to delete instead of free

or

// use current malloc line, change adj to be a pointer to a list and new it
// but this will cause additional problems for you since you really need to use a constructor for STL::list
node->adj = new list<int>;

Bottom line you really shouldn't be using malloc here.

like image 45
UpAndAdam Avatar answered Oct 13 '22 00:10

UpAndAdam


This is UpAndAdam's answer, written completely.

// Graphs using adjacency list
//
#include <iostream>
#include <list>
#include <cstdlib>
using namespace std;

// structure to represent a vertex(node) in a graph
typedef struct vertex{
    int info;
    list<int> *adj;   // adjacency list of edges contains the indexes to vertex
} *vPtr;             

int main(){
    vPtr node = (vPtr)malloc(sizeof(struct vertex));
    node->adj = new list<int>;
    node->info = 34;            // some arbitrary value
    (node->adj)->push_back(2);  // trying to insert a value in the list
    return 0;
}
like image 26
Jiminion Avatar answered Oct 13 '22 01:10

Jiminion