You are on page 1of 4

Let G = (V, E) be our graph where V is the set of vertices and E is the set of edges where each

edge is represented as a tuple of vertices. How we want to represent a graph largely depends on
the operations we intend to support.

Adjacency list:
An array of linked lists is used. Size of the array is equal to number of vertices. Let the array be
array[]. An entry array[i] represents the linked list of vertices adjacent to the ith vertex. This
representation can also be used to represent a weighted graph. The weights of edges can be stored
in nodes of linked lists. Following is adjacency list representation of the above graph.

An array A of length n where each entry A[i] contains a pointer to a linked list of all the out-
neighbors of vertex i. In an undirected graph with edge {u, v} the edge will appear in the adjacency
list for both u and v.
An adjacency list is a list of lists. Each list corresponds to a vertex u and contains a list of edges
(u, v) that originate from u. Thus, an adjacency list takes up Θ(V + E) space.
Adjacency matrix:
An adjacency matrix is a 2-D array of size |V| X |V| of bits where element (i, j) is 1 if and only if
the edge (vi, vj) is in E. Thus an adjacency matrix takes up Θ (V2) storage (note that the constant
factor here is small since each entry in the matrix is just a bit).

Edge List:
 The edge list structure simply stores the vertices and the edges into unsorted sequences.
 Easy to implement.
 Finding the edges incident on a given vertex is inefficient since it requires examining the
entire edge sequence
 Total space: O(n + m)

Edge lists

One simple way to represent a graph is just a list, or array, of |E| edges, which we call an edge list.
To represent an edge, we just have an array of two vertex numbers, or an array of objects containing
the vertex numbers of the vertices that the edges are incident on. If edges have weights, add either
a third element to the array or more information to the object, giving the edge's weight. Since each
edge contains just two or three numbers, the total space for an edge list is Θ(E). For example, here's
how we represent an edge list in JavaScript for the social network graph:
[ [0,1], [0,6], [0,8], [1,4], [1,6], [1,9], [2,4], [2,6], [3,4], [3,5], [3,8], [4,5], [4,9], [7,8], [7,9] ]
Edge lists are simple, but if we want to find whether the graph contains a particular edge, we have
to search through the edge list. If the edges appear in the edge list in no particular order, that's a
linear search through |E| edges. Question to think about: How can you organize an edge list to
make searching for a particular edge take O(lgE) time? The answer is a little tricky.

The worst case storage of an adjacency list is when the graph is dense, i.e. E = Θ(V2). This gives
us the same space complexity as the adjacency matrix representation. The Θ(V + E) space
complexity for the general case is usually more desirable, however. Furthermore, adjacency lists
give you the set of adjacent vertices to a given vertex quicker than an adjacency matrix
O(neighbors) for the former vs O(V ) for the latter. In the algorithms we’ve seen in class, finding
the neighbors of a vertex has been essential.