Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an adjacency list and how do you code one?

Here is an SO post of an adjacency list. However I see no difference from a single-linked list? Also here is a wikipedia article which says that it is all the edges (of a graph, discrete math type) in a list which is pretty broad, if I have a graph, which is not a path graph. How do I code an adjacency list?

like image 587
user1001776 Avatar asked Oct 25 '25 02:10

user1001776


2 Answers

A simple example: Suppose you have a vertex type Vertex. They your graph consists of a set of vertices, which you can implement as:

std::unordered_set<Vertex> vertices;

Now for every pair of vertices between which there is an edge in your graph, you need to record that edge. In an adjacency list representation, you would make an edge type which could be a pair of vertices, and your adjacency list could simply be a list (or again a set) of such edges:

typedef std::pair<Vertex, Vertex> Edge;
std::list<Edge> edges_as_list;
std::unordered_set<Edge> edges_as_set;

(You will probably want to supply the last set with an undirected comparator for which (a,b) == (b,a) if you have an undirected graph.)

On the other hand, if you want to make an adjacency matrix representation, you would instead create an array of bools and indicate which vertices have edges between them:

bool edges_as_matrix[vertices.size()][vertices.size()]; // `vector` is better
// edges_as_matrix[i][j] = true if there's an edge

(For this you would need a way to enumerate the vertices. In an undirected graph, the adjacency matrix is symmetric; in a directed graph it need not be.)

The list representation is better if there are few edges, while the matrix representation is better if there are a lot of edges.

like image 144
Kerrek SB Avatar answered Oct 26 '25 18:10

Kerrek SB


struct Node {
    std::vector<std::shared_ptr<Node>> Neighbors;
};
struct AdjacencyList{
    std::vector<std::shared_ptr<Node>> Nodes;
};

AdjacencyList holds an array of all of the Nodes, and each Node has links to all the other Nodes in the list. Technically the AdjacencyList class isn't required, all that's required is a std::shared_ptr<Node> root;, but having an the AdjacencyList with a vector makes iterating over them, and many other things, much easier.

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

AdjacencyList holds pointers to A, B, C, D, E, and F.
A has pointers to B and C.
B has pointers to A and D and E.
C has pointers to A.
D has pointers to B and E. E has pointers to B and D.
F has pointers to no other nodes.

The AdjacencyList must contain pointers and not Node values, or else when it resizes, all the Neighbors pointers would be invalidated.

like image 43
Mooing Duck Avatar answered Oct 26 '25 17:10

Mooing Duck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!