Chapter 10

Graph representationsCS

This chapter stores graphs and compares representation costs. Graph-theoretic properties belong to Engineering Mathematics; BFS, DFS, shortest paths and minimum spanning trees belong to Algorithms.

10.1 Matrices, adjacency lists and edge listsCS

Let G=(V,E)G=(V,E) be a graph, nv=∣V∣n_v=|V| its vertex count and ne=∣E∣n_e=|E| edge count. A directed adjacency matrix stores an entry at row uu, column vv for edge u→vu\to v. An undirected matrix is symmetric. A weighted matrix must distinguish “no edge” from a genuine zero-weight edge, for example with a separate marker.

A matrix tests edge existence in constant time. An unsorted list takes O(deg⁡+(u)+1)O(\deg^+(u)+1) to test u→vu\to v; storing incoming lists separately supports indegree-related access. For an undirected edge deletion, update both adjacency records. Representation choices depend on density and operations, not just memory.

10.2 Incidence storage and traversal interfacesCS

An incidence matrix has one row per vertex and one column per edge. For a directed edge, use −1-1 at its tail and +1+1 at its head; each non-loop column sums to zero. For an undirected non-loop edge, both endpoint entries are 1. Storage is Θ(nvne)\Theta(n_vn_e) entries, usually larger than adjacency lists for sparse graphs.

Traversal algorithms consume a neighbour iterator. Scanning every adjacency list once touches nv+nen_v+n_e scale data; scanning every row of a matrix touches nv2n_v^2 entries. The iterator's order affects traversal order but not the graph itself. Inserting edges at the head of a list reverses their insertion order. For a directed graph, outgoing lists alone do not directly enumerate incoming neighbours; reverse edges to obtain the transpose representation.

Last-minute recall

  • Distinguish absence from a zero edge weight.
  • Undirected adjacency lists normally store each edge twice.
  • Matrices give constant-time edge lookup.
  • Lists make sparse neighbour enumeration efficient.
  • Preserve isolated vertices when storing an edge list.
  • State the directed incidence sign convention.
  • Representation order can change traversal order.

Revise offline: the whole Programming and Data Structures handbook as one PDF

55 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections