Discrete Mathematics — every example graph drawn, every result shown in colour
Notes By Pr. El Hadiq Zouhair
1 / 16This 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 / 16From 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.
Green edges = the BFS shortest-path tree.
Adjacent layers always differ by one — that is exactly why BFS gives shortest unweighted distances.
3 / 16Depth-first search started from each unvisited vertex paints one entire connected component before moving on. Here two separate clusters get two colours.
DFS from any unvisited vertex paints one whole connected component.
4 / 16The 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.
Six conflicting sessions split perfectly between two rooms: every edge joins a blue vertex to a yellow one, so the conflict graph is bipartite.
A triangle is an odd cycle: after colouring two vertices, the third clashes — no two-room split exists.
The red edge joins two vertices the algorithm was forced to give the same colour ⇒ not bipartite.
6 / 16Travel-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.
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 / 16The 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.
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 / 16Connecting 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.
The "crown" timetable graph is 2-colourable (two exam slots suffice). A bad visiting order forces greedy into a third slot:
A good order (one whole side first) recovers the optimal two slots — same graph, fewer colours:
Same vertices, same edges — only the order changed. Greedy colouring is fast but not guaranteed minimal.
10 / 16Rule. A connected graph has an Eulerian circuit (closed) exactly when every vertex has even degree.
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 / 16Rule. 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.
The two odd vertices are C, D. One stroke does it: C → E → D → A → B → C → D.
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.
All four vertices have odd degree (N:3, E:3, S:3, W:3) ⇒ more than two odd vertices ⇒ no Eulerian path.
12 / 16A 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.
Visiting order: 1 → 2 → 3 → 4 → 5 → 6 → 1 — every city once, back to the start.
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.
| Eulerian | Hamiltonian | |
|---|---|---|
| Visits every… | edge exactly once | vertex exactly once |
| Easy test? | Yes — count odd-degree vertices | No — NP-complete (no simple rule) |
| Circuit exists when | connected & all degrees even | (only sufficient hints: Dirac, Ore) |
| Path exists when | 0 or 2 odd-degree vertices | — |
| Real-life | postman / snowplough routes | delivery tours, TSP, circuit boards |
| Algorithm | What the colours show | Green / red edges |
|---|---|---|
| BFS | distance layer from the source | green = shortest-path tree |
| DFS | connected component | — |
| Topological sort | prerequisite depth (level) | arrows = "must come before" |
| Bipartite | the two sides / a conflict | red = same-colour clash |
| Dijkstra | shortest-distance rank | green = shortest-path tree |
| Bellman–Ford | final distance | red = negative edge |
| MST | (single colour) | green = chosen tree edges |
| Greedy colouring | assigned colour / time slot | — |
Notes By Pr. El Hadiq Zouhair
15 / 16Pair these drawings with the step-by-step walkthrough to connect each picture to its trace.
Notes By Pr. El Hadiq Zouhair
16 / 16