Discord

INAIO Guide Stage 1 12Graph Theory and Algorithms

12.1 Graph representations

Use a graph to capture relationships, then describe its vertices, edges, directions, weights, and storage.

Graphs are useful when a problem is mainly about connections. A road map asks which towns can be reached and by which route. A follower network asks which way information can travel. A task plan asks whether one job depends on another. Drawing each situation as a graph keeps the relationships that matter and lets us use the same search ideas across very different problems.

This is foundational discrete mathematics. You only need sets and simple tables to begin. MIT OpenCourseWare's Introduction to Algorithms lecture notes continue from representations into search.

A basic idea: the names for objects and connections

A graph is written G=(V,E)G=(V,E). The set VV contains vertices (also called nodes): the objects, such as towns or accounts. The set EE contains edges: the connections between them. An edge can be undirected, such as a two-way road {A,B}\{A,B\}, or directed, such as a follow link A→BA\to B that does not imply B→AB\to A. In a weighted graph, an edge carries a number such as road length or travel time; in an unweighted graph, edges have no such extra value and can be counted as individual moves. Decide what a vertex and an edge mean in the real problem before assigning these names.

In the directed diagram below, V={A,B,C,D}V=\{A,B,C,D\} and E={(A,B),(A,C),(B,D),(C,D)}E=\{(A,B),(A,C),(B,D),(C,D)\}. Each ordered pair names an arrow from its first vertex to its second. Vertex DD still belongs to the graph even though it has no outgoing arrow.

Directed graph A points to B and C, and B and C point to D. An adjacency list beside it records A: B,C; B: D; C: D; D: none.Directed graph A points to B and C, and B and C point to D. An adjacency list beside it records A: B,C; B: D; C: D; D: none.
The drawing and adjacency list encode the same four-vertex graph.

A quick tour of representations

A drawing is easy to inspect, but an algorithm needs a precise way to store the same graph. In a directed graph, (A,B)(A,B) and (B,A)(B,A) are different edges. The following layouts all preserve that direction when used correctly:

Storage What it records Useful when
Edge list A list of edges, perhaps with weights Reading or sorting individual edges
Adjacency list For each vertex, its outgoing neighbours Exploring a graph with relatively few edges
Adjacency matrix Entry MijM_{ij} indicates an edge from ii to jj Checking a particular connection quickly

For vertex order A,B,C,DA,B,C,D, the diagram's adjacency matrix has rows A:(0,1,1,0)A:(0,1,1,0), B:(0,0,0,1)B:(0,0,0,1), C:(0,0,0,1)C:(0,0,0,1), and D:(0,0,0,0)D:(0,0,0,0). In an undirected graph, the matrix is symmetric because every edge works both ways. In a weighted graph, a matrix entry can store a weight, but decide how to represent no edge separately; zero may itself be a valid weight.

With nn vertices, an adjacency matrix has n2n^2 entries even when there are few edges. An adjacency list stores the neighbours that actually exist. The choice changes storage and convenience, not the graph itself.

Application 1: a two-way road map

Towns P,Q,RP,Q,R have two-way roads PP–QQ of length 55, QQ–RR of length 22, and PP–RR of length 99. Write an adjacency list.

  1. Use both directions. P: (Q,5),(R,9)P:\ (Q,5),(R,9); Q: (P,5),(R,2)Q:\ (P,5),(R,2); R: (P,9),(Q,2)R:\ (P,9),(Q,2).
  2. Check symmetry. The PP–QQ road appears under both PP and QQ with the same length.
  3. Keep the meaning. These weights are distances, not probabilities. A later path algorithm could compare the direct PP–RR length 99 with the route through QQ of length 77.

Application 2: one-way follows

Accounts A,B,CA,B,C have follows A→BA\to B, A→CA\to C, and C→AC\to A. Who does BB follow, and which follows point back to AA?

  1. List outgoing neighbours. A:[B,C]A:[B,C], B:[]B:[], C:[A]C:[A].
  2. Answer by direction. BB follows nobody in this graph. Only CC has an edge pointing to AA.
  3. Avoid an undirected reading. A→BA\to B does not add B→AB\to A.

The same three account names would give different reachability answers if the arrows were removed.

Resources

SourceTitleWhy read it
MIT OpenCourseWareIntroduction to Algorithms: lecture notesSee the graph representation and search lectures.

Finished this module? Mark it done.

Comments

Stuck on something, spotted a mistake, or found a resource that helped you? Tell us below.

Loading comments