Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Enquire Book a call

Coding · Graphs

Kinds of graphs

Lesson · 8 min read · Graphs · Introduction to graphs

Directed or not, weighted or not, cyclic or acyclic, dense or sparse: the properties that decide which algorithm a problem needs.

Why the kind matters

The labels below are not mutually exclusive: a graph can be directed, weighted and acyclic at once. They matter because each one changes which algorithm is correct. Breadth-first search gives shortest paths only when edges are unweighted; Dijkstra's algorithm fails with negative weights; a topological order exists only without cycles; a two-colouring exists only if the graph is bipartite. The first thing to settle in an interview is which of these the problem's graph is.

Undirected and directed

An undirected graph models a symmetric relation: two people who have met, two cells that touch, two cities joined by a road that runs both ways. A directed graph models one that is not: a dependency, a link from one web page to another, a one-way street. Problems signal direction with words like "depends on", "must come before", "points to", "follows" or "from … to".

Weighted

In a weighted graph every edge carries a number: a distance, a time, a cost, a capacity, a probability. An unweighted graph is the special case where every edge counts as 1.

73249512ABCDE
The cheapest route from A to D is A → C → B → D, cost 9, although A → B → D uses fewer edges.

Weights can be negative (a rebate, a gain in an exchange cycle), and that one fact rules out Dijkstra's algorithm. A later module explains why and gives the replacement.

Cyclic and acyclic, and the DAG

A graph with at least one cycle is cyclic; one with none is acyclic. A directed acyclic graph, or DAG, is the shape of every schedule and every build: tasks are nodes, and an edge u → v says u must finish before v starts.

rawpxclncalfeatfitrep
A research pipeline: raw data is cleaned; features need the cleaned data, prices (px) and a calendar (cal); the fit and the report follow.

A DAG can look like a tree, but it is not one: the features node has three parents. What it shares with a tree is that you can never come back to where you started, which is exactly what makes an order of execution possible.

Trees are graphs

A tree is a connected undirected graph with no cycle. Three facts about trees are used constantly: a tree on n nodes has exactly n − 1 edges; there is exactly one path between any two nodes; and removing any edge disconnects it. Any two of "connected", "acyclic" and "n − 1 edges" imply the third, which gives a quick test for whether an edge list describes a tree.

123456
Six nodes, five edges, no cycle: a tree.

Bipartite

A graph is bipartite if its nodes split into two groups with every edge running between the groups and none inside a group. Assignments are bipartite by nature: analysts and desks, students and projects, jobs and machines.

A1A2A3D1D2D3analystsdesks
Every edge joins an analyst to a desk that would take them.

A graph is bipartite exactly when it has no cycle of odd length, and a two-colouring by breadth-first or depth-first search tests that in linear time. A later module does that, and another finds the largest set of pairings with no analyst or desk used twice.

Dense, sparse, and graphs you never build

An undirected graph on n nodes has at most n(n − 1)/2 edges. One near that limit is dense; one with a number of edges close to n is sparse. Road networks, social networks and grids are sparse, and the choice of how to store a graph, covered in the next module, turns on this.

Many interview graphs are never stored at all. In a grid each cell is a node and its neighbours are computed from its coordinates; in a word puzzle each word is a node and its neighbours are the words one letter away. The algorithms are the same; only the function that lists neighbours changes.

0123012x
A grid is a graph nobody writes down: cell (1, 1) has neighbours (0, 1), (2, 1), (1, 0) and (1, 2).

Before writing code, say out loud what the nodes are, what the edges are, whether they are directed, and whether they carry weights. Most of the interview is decided there.