Graph data structures

Graph Data Structures
There are two main ways of representing graphs in computer memory. For simplicity, we will only consider simple graphs (i.e. no multi-graphs or self-loops).

Adjacency Matrix
The first method of storing graphs is through the means of an adjacency matrix. This simply means we have an $$V*V$$ matrix where each cell contains a 0 if there is no edge running from one vertex to another, otherwise we will have either a 1 or a positive value (for weighted graphs). So for example, if we had a graph that had an edge from vertex 2 to vertex 5, then the cell of [2,5] would contain a 1. If the edge was weighted with say 3, we would mark the cell of [2,5] with 3. Note that this notation supports both undirected and directed graphs. Note that for particularly sparse graphs, there is a considerable amount of wasted space ($$O(|V|^2)$$). So an adjacency matrix isn't practical for sparse or large graphs in general since the space requirements grow quadratically. However, what we gain from this representation is speed and simplicity when determining whether there is an edge going from vertex $$n$$ to vertex $$m$$ since we just merely have to lookup the appropriate cells (a constant time operation). See also Adjacency Matrix for a neat algebraic property of this data structure.

For implementation, let's write some basic code to convert edges and place them into a simplistic adjacency matrix. For our purposes, let's assume the graph is weighted and directed, and the input comes in form of a string vector/array, each string contains three elements ("A B W"), which means there is an edge going from A to B with a weight of W (where $$A,B < NUM\_EDGES$$). After we have inserted the edges into the graph representation, we will print out all the adjacent vertices with their corresponding weights.

Adjacency List
The second method of storing graphs is through a similar means to an adjacency matrix, but is more space efficient. This data structure is called an adjacency list. An adjacency list basically has $$V$$ linked lists, with each corresponding linked list containing the elements that are adjacent to a particular vertex. So given the example we used earlier, we would have a linked list in cell 2 that contains a single element of 5. If vertex 2 was also adjacent to vertex 6 then the linked list in cell 2 would contain two elements, 5 and 6 and so forth. The space requirements grow in proportion to the number of edges plus the number of vertices, i.e. $$O(|V| |E|)$$. If the graph was weighted, then we would need to create an abstraction of each node of the linked list. Instead of merely having a numerical value representing which vertices it was adjacent to, each node needs to have another field containing the weight of the edge.

Using the same restrictions and objectives as the adjacency matrix implementation, the adjacency list implementation follows:

Incidency List
This structure is more flexible than adjacency list, but requires little more source code to implement it, and to use it. Each vertex has list of pointers to edges which it is incident to. Adventage of this is that two vertices that are adjecent to this edge share the same instance of edge, so when you want to manipulate with edge data, for example flow or cost, you only change data in one object. You also modify neighbour function do make your graph directed, or mixed. Imagine how easy would be to change orientation of edge with this data strucutre.