### Data structures used to represent graphs

• Overview: Data structures used to represent graphs

• Commonly used data structures for graphs:

• Common practice in graph representation

• Mapping

 Nodes in the graph are mapped to integers 0, 1, 2, ..., (n−1)

Example:

• Storing nodes:

• Nodes are represented:

 the index of an one-dimensional array

• Storing edges:

 Edges are stored as a linked list

• Example:

• Graph:

• Representation:

• Java classes:

 ``` /* ======================================= List element that represents an edge ======================================= */ class Edge { int NodeID; // The neighbor node Node next; // Next Edge in the list } /* ======================================= The nodes ======================================= */ Edge[] node; // Node[i] contains a list of neighbor for i ```

• Storing nodes:

• Nodes are represented:

 the index of a two-dimensional array (= matrix)

• Storing edges:

 Edges are represented as a value in the matrix

• Example:

• Graph:

• Representation:

 ``` 0 1 2 3 4 5 6 7 8 +- -+ | 0 1 0 1 0 0 0 0 1 | // 0 | 1 0 0 0 0 0 0 1 0 | // 1 | 0 0 0 1 0 1 0 1 0 | // 2 | 1 0 1 0 1 0 0 0 0 | // 3 M = | 0 0 0 1 0 0 0 0 1 | // 4 | 0 0 1 0 0 0 1 0 0 | // 5 | 0 0 0 0 0 1 0 0 0 | // 6 | 0 1 1 0 0 0 0 0 0 | // 7 | 1 0 0 0 1 0 0 0 0 | // 8 +- -+ ```

• Java variables:

 ``` /* ======================================= The edges ======================================= */ int[][] M; // M[i][j] > 0 <==> edge (i,j) ```

• Data structure selection

• The selection of a data structure has consequences to the running times of algorithms

• Many algorithms solve problems by traversing all edges in the graph

• Assume that a graph has:

 n nodes m edges

• Running time to visit all edges:

• O(m + n)

Because:

We must:

 Visit each array element: n steps Visit all "edges" (the blue squares): m steps

• O(n2)

Because:

 ``` 0 1 2 3 4 5 6 7 8 +- -+ | 0 1 0 1 0 0 0 0 1 | // 0 | 1 0 0 0 0 0 0 1 0 | // 1 | 0 0 0 1 0 1 0 1 0 | // 2 | 1 0 1 0 1 0 0 0 0 | // 3 M = | 0 0 0 1 0 0 0 0 1 | // 4 | 0 0 1 0 0 0 1 0 0 | // 5 | 0 0 0 0 0 1 0 0 0 | // 6 | 0 1 1 0 0 0 0 0 0 | // 7 | 1 0 0 0 1 0 0 0 0 | // 8 +- -+ ```

We must visit all elements in an n × n matrix

• Note:

 When a graph has a large number of edges, we will have m ~= n × n Then no matter which representation you choose, the running time will be O(n × n)

• Ease of programming- Choice of data structure:

 Adjacency matrix usually result in simpler algorithms because you deal with one data structure (matrix) The Adjacency list is a composite structure with an array and a list (or 2 lists)