Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of an adjacency list graph representation

I have just started with the graph theory. I can't figure out how to code adjacency list using linked lists. for example, if I have this graph (undirected):

A--------B
|       /|\
|      / | \  
|     /  |  \
|    /   |   \
|   /    |    \
|  /     |     \
| /      |      \
C        E-------D

How do I code it? I know how to do it using adjacency matrix, but how to code it using adjacency list and linked lists (c++)?

like image 837
Somebody Avatar asked Jan 03 '13 04:01

Somebody


People also ask

How do you implement an adjacency list on a graph?

The adjacency list representation of a graph is linked list representation. In this representation we have an array of lists The array size is V. Here V is the number of vertices. In other words, we can say that we have an array to store V number of different lists.

What is adjacency list representation of a graph?

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.

Which of the data structure is used in implementation of graph using adjacency list?

Adjacency Matrix: Adjacency matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted graphs.


2 Answers

An adjacency list is just a vector/array of lists. Each element in the graph is an element in the array, and any edge is added to the it's adjacency list. Thus it looks something like:

A -> {B, C}

B -> {A, C, D, E}

C -> {A, B}

D -> {B, E}

E -> {B, D}

So we start with something like std::vector<std::list<vertex>>. However, we can do better than this, because verticies are unique, hence we can utilize a map. Furthermore, a vertex can only appear in an edge list once, so we modify it to std::map<vertex, std::set<vertex>>.

So to start with, something like:

struct vertex
{
   //
};

class undirected_graph
{
private:
    std::map<vertex, std::set<vertex>> graph_container;
public:
    void add_vertex(const vertex& v) { //add a vertex to the map }
    void add_edge(const vertex& v, const vertex& u) { //look up vertex in map and add to the vertex adjacency list }
    //Other methods
    //...
 };
like image 70
Yuushi Avatar answered Sep 25 '22 08:09

Yuushi


An adjacency list would just be a set of objects representing the edges of the graph.

struct edge {
    node *nodes[2];

    edge( node *a, node *b ) {
        if ( a < b ) { // define canonical order of edges for undirected graph
            nodes[0] = a;
            nodes[1] = b;
        } else {
            nodes[0] = b;
            nodes[1] = a;
        }
    }
};

A linked list doesn't sound particularly practical; usually you would define an ordering of edges and put them in a std::set or std::map.

bool operator< ( edge const &lhs, edge const &rhs ) {
    if ( lhs.nodes[0] < rhs.nodes[0] ) return true;
    if ( rhs.nodes[0] < lhs.nodes[0] ) return false;
    return lhs.nodes[1] < rhs.nodes[1];
}

typedef std::set< edge > graph;

There are many ways to do this, it's hard to suggest anything more without knowing what you intend to do with the graph.

like image 31
Potatoswatter Avatar answered Sep 23 '22 08:09

Potatoswatter