GF2: Data Structures for Graphs & Digraphs

San Skulrattanakulchai

November 2, 2018

Adjacency matrices

Example problem

Adjacency lists

Efficient implementation

Example problem, again

High-level code for adjacency list representation

Believe it or not?

Notes

  1. For some application, each vertex (edge) may have some associated information. We can keep such information with the node representing it.
  2. This technique of storing extra information at the node can be useful in general even when the application itself does not require it. For example, storing the indegrees and/or outdegrees of every vertex may help in processing the graph.
  3. Implementation of adjacency lists by singly-linked lists is appropriate only for static graphs, ones that do not change during the run of the algorithm. For dynamic graphs whose vertices and/or edges may be added and/or deleted, the lists need to be doubly-linked for efficiency. Moreover, for undirected graphs, each node \(w\) appearing on the adjacency list of \(v\) must have a field MATE[w] that points to the node \(v\) appearing on the adjacacy list of \(w\), and vice versa. This is required for implementing edge deletion in \(O(1)\) time.
  4. Multigraphs and multidigraphs may be represented using adjacency lists by either making each edge correspond to each node on the lists, or lumping all parallel edges together as one node. Which method is appropriate depends on the application.