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 . The set contains vertices (also called nodes): the objects, such as towns or accounts. The set contains edges: the connections between them. An edge can be undirected, such as a two-way road , or directed, such as a follow link that does not imply . 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, and . Each ordered pair names an arrow from its first vertex to its second. Vertex still belongs to the graph even though it has no outgoing arrow.
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, and 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 indicates an edge from to | Checking a particular connection quickly |
For vertex order , the diagram's adjacency matrix has rows , , , and . 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 vertices, an adjacency matrix has 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 have two-way roads – of length , – of length , and – of length . Write an adjacency list.
- Use both directions. ; ; .
- Check symmetry. The – road appears under both and with the same length.
- Keep the meaning. These weights are distances, not probabilities. A later path algorithm could compare the direct – length with the route through of length .
Application 2: one-way follows
Accounts have follows , , and . Who does follow, and which follows point back to ?
- List outgoing neighbours. , , .
- Answer by direction. follows nobody in this graph. Only has an edge pointing to .
- Avoid an undirected reading. does not add .
The same three account names would give different reachability answers if the arrows were removed.
Resources
| Source | Title | Why read it |
|---|---|---|
| MIT OpenCourseWare | Introduction to Algorithms: lecture notes | See 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