Graph Algorithms — Visual & Coloured

Discrete Mathematics — every example graph drawn, every result shown in colour

Notes By Pr. El Hadiq Zouhair

1 / 16

How to read these pictures

This is the visual companion to the graph-algorithms course. Each algorithm is shown on a concrete example graph that is drawn and coloured by its result:

A short legend sits above every drawing.

2 / 16

1. BFS — coloured by distance

From Ana, breadth-first search colours every person by how many introductions away they are. The tree of green edges is the shortest chain to each person.

distance 0 (Ana)distance 1distance 2
AnaBilalCarlaDiegoEvaFarah

Green edges = the BFS shortest-path tree.

Adjacent layers always differ by one — that is exactly why BFS gives shortest unweighted distances.

3 / 16

2. DFS — coloured by connected component

Depth-first search started from each unvisited vertex paints one entire connected component before moving on. Here two separate clusters get two colours.

component 1 {a,b,c,d,e}component 2 {x,y}
abdecxy

DFS from any unvisited vertex paints one whole connected component.

4 / 16

3. Topological sort — the prerequisite DAG, coloured by depth

The Discrete-Mathematics courses as a directed acyclic graph. Arrows point from a course to the one it unlocks; colour is the prerequisite depth (how many courses must come before it). Reading the colours left to right gives a valid study order.

Colour = prerequisite depth (level). Arrows point from a course to the course it unlocks. PL=Propositional Logic · LE=Logical Equivalences · PR=Predicate Logic · RI=Rules of Inference · ST=Sets · OS=Operations on Sets · RF=Relations & Functions · SS=Sequences & Series · PT=Proof Techniques · MI=Math Induction · CO=Counting · BI=Binomial Identities · GR=Graphs
PLLEPRRISTCOOSPTBIMIRFGRSS 5 / 16

4. Bipartite check — a clean two-colouring

Six conflicting sessions split perfectly between two rooms: every edge joins a blue vertex to a yellow one, so the conflict graph is bipartite.

Room A (colour 0)Room B (colour 1)
123456

…and when it fails

A triangle is an odd cycle: after colouring two vertices, the third clashes — no two-room split exists.

colour 0colour 1CONFLICT — needs a 3rd
uvw

The red edge joins two vertices the algorithm was forced to give the same colour ⇒ not bipartite.

6 / 16

5. Dijkstra — coloured by shortest distance

Travel-time map from S. Nodes are shaded by their final distance rank (nearest → farthest); the green edges form the shortest-path tree the algorithm settles.

nearestfarthest
723485263SABCDE

Node colour = rank of final distance from S (S=0, A=5, B=2, C=9, D=7, E=10). Green = shortest-path tree.

7 / 16

6. Bellman–Ford — the negative edge in red

The same shortest-path idea, but one edge has a negative weight (red). Dijkstra would mis-handle it; Bellman–Ford relaxes every edge repeatedly and still finds the correct distances.

678-459-32SABCD

Red arrow = the negative edge A→C = −4 (Dijkstra could not handle it). Final distances: S=0, A=6, B=4, C=2, D=1.

8 / 16

7. Minimum Spanning Tree — chosen edges in green

Connecting five towns at least cost. Every possible link is drawn with its price; the thick green links are the ones Kruskal/Prim keep — the cheapest network with no redundant loop.

Thick green = chosen MST edges · grey = available but unused (would form a cycle).
4312457T1T2T3T4T5 9 / 16

8. Greedy colouring — when the order matters

The "crown" timetable graph is 2-colourable (two exam slots suffice). A bad visiting order forces greedy into a third slot:

slot 1slot 2slot 3
a1a2a3b1b2b3

A good order (one whole side first) recovers the optimal two slots — same graph, fewer colours:

slot 1slot 2
a1a2a3b1b2b3

Same vertices, same edges — only the order changed. Greedy colouring is fast but not guaranteed minimal.

10 / 16

9. Eulerian graphs — use every edge exactly once

🌍 Real-life example — a snowplough or postman that must travel down every street once and return to the depot is looking for an Eulerian circuit.

Rule. A connected graph has an Eulerian circuit (closed) exactly when every vertex has even degree.

all vertices even degreeedge order in the circuit
651432ABCDE

Degrees A:2, B:2, C:4, D:2, E:2 — all even ✓. The orange numbers give one Eulerian circuit, edge by edge: A → C → E → D → C → B → A (start = end).

11 / 16

9a. Eulerian path — and when it is impossible

🌍 Real-life example — "draw the house without lifting the pen": an Eulerian path that need not return to its start.

Rule. A connected graph has an Eulerian path (open) exactly when it has exactly two odd-degree vertices — and the path must start at one and finish at the other.

odd-degree vertex (start / end)even-degree vertex)edge order
456312ABCDE

The two odd vertices are C, D. One stroke does it: C → E → D → A → B → C → D.

When it is impossible — the Seven Bridges of Königsberg

Königsberg's four land masses each touched an odd number of bridges. With four odd-degree vertices, no Euler path or circuit exists — Euler's 1736 proof that the famous walk is impossible, and the birth of graph theory.

land massodd degree
NESW

All four vertices have odd degree (N:3, E:3, S:3, W:3) ⇒ more than two odd vertices ⇒ no Eulerian path.

12 / 16

10. Hamiltonian graphs — visit every vertex exactly once

🌍 Real-life example — a delivery driver who must visit every city once and come back home wants a Hamiltonian cycle (the heart of the Travelling-Salesman Problem).

A Hamiltonian cycle passes through every vertex exactly once and returns to the start. Here the outer hexagon is one such cycle (green); the diagonals are extra roads it does not need.

the Hamiltonian cyclecities, coloured by visiting order
123456

Visiting order: 1 → 2 → 3 → 4 → 5 → 6 → 1 — every city once, back to the start.

When none exists

Vertex S below is a dead-end (degree 1). You can enter it but never leave to continue a tour, so no Hamiltonian cycle exists. Unlike the Eulerian case there is no simple degree rule — deciding Hamiltonicity is NP-complete in general.

on some cycledead-end (degree 1) — blocks any tour
PQRS 13 / 16

Eulerian vs Hamiltonian — the one-line difference

EulerianHamiltonian
Visits every…edge exactly oncevertex exactly once
Easy test?Yes — count odd-degree verticesNo — NP-complete (no simple rule)
Circuit exists whenconnected & all degrees even(only sufficient hints: Dirac, Ore)
Path exists when0 or 2 odd-degree vertices
Real-lifepostman / snowplough routesdelivery tours, TSP, circuit boards
14 / 16

Colour key — what each colour meant

AlgorithmWhat the colours showGreen / red edges
BFSdistance layer from the sourcegreen = shortest-path tree
DFSconnected component
Topological sortprerequisite depth (level)arrows = "must come before"
Bipartitethe two sides / a conflictred = same-colour clash
Dijkstrashortest-distance rankgreen = shortest-path tree
Bellman–Fordfinal distancered = negative edge
MST(single colour)green = chosen tree edges
Greedy colouringassigned colour / time slot

Notes By Pr. El Hadiq Zouhair

15 / 16
End

See the colours, understand the algorithm

Pair these drawings with the step-by-step walkthrough to connect each picture to its trace.

Notes By Pr. El Hadiq Zouhair

16 / 16