Unit 6 CLO 4 6 lecture hours

Graph Theory

A graph is dots joined by lines. That is the whole concept — the entire unit is vocabulary for the dots, the lines, and a handful of procedures for finding routes through them. Real applications: road networks, social networks, circuit boards, internet routing.

The two-part vocabulary

Vertices (V) = the dots.   Edges (E) = the lines.

A graph is written G = (V, E). Learn the words vertex / node and edge / link / arc — exams mix them up on purpose.

1. Anatomy of a Graph and Terminology

A B C D E deg A = 3 deg B = 2 deg C = 2 deg D = 1 deg E = 2 edge — a line joining two vertices A and B are ADJACENT A and C ADJACENT A and D are NOT adjacent E has a LOOP an edge from a vertex back to itself it contributes 2 to deg(E) the two A–B lines are PARALLEL EDGES A path: A → B → C a cycle: A–B–C–A Length counts EDGES, not vertices. So A–B–C has length 2. Count the edges touching A: one to B, one to C, one more to B. Degree 3.
Count the edges touching A: one to B, one to C, one to B again (the parallel edge) — degree 3.
TermMeaning
Vertex / nodeA single point in the graph
Edge / linkA line joining two vertices
AdjacentTwo vertices are adjacent if an edge joins them
Degree, deg(v)Number of edges touching v. A loop counts twice
LoopAn edge from a vertex back to itself
Parallel edgesTwo or more edges joining the same pair of vertices
PathA sequence of vertices where each consecutive pair is joined by an edge
CycleA path that starts and ends at the same vertex, with no other repetition
WalkA path where vertices may repeat
LengthNumber of edges in the path (not vertices)
Simple graphNo loops and no parallel edges
Complete graph KₙEvery pair of vertices is joined — each vertex has degree n−1
SubgraphA graph made from some vertices and some edges of the original
Order |V|How many vertices
Size |E|How many edges
Length = edges, not vertices

A path from A to C via B, written A–B–C, has length 2 (two edges: A–B and B–C), even though it visits three vertices. Students lose marks on this every single year. Count the lines, not the dots.

Connected vs disconnected

  • Connected — you can get from any vertex to any other by following edges
  • Disconnected — some vertices are stranded in a separate piece
  • Component — a maximal connected piece of a disconnected graph

Complete graph Kₙ in one line

Every vertex is joined to every other, so:

deg(v) = n − 1 for all v
|E| = n(n−1)/2

K₃ is a triangle, K₄ is a triangle with a dot in the middle, K₅ is the pentagon-and-star.

2. Directed and Undirected Graphs

Undirected

Edges are two-way. A road you can drive both ways. Drawn with a plain line.

{A, B} = {B, A}

Used for: friendships, mutual connections, physical cables.

Directed (digraph)

Edges are one-way. Drawn with an arrowhead.

(A, B) ≠ (B, A)

Used for: web links, "follows", one-way streets, task dependencies.

UNDIRECTED A B C A—B and B—A are the same edge drawn as a plain line, no arrow DIRECTED A B C A→B and B→A are DIFFERENT edges note C→A points back the other way
In the directed version the bottom edge was reversed. Same vertices, different graph.
Degree in a directed graph

Split it in two:

  • Out-degree d⁺(v) = arrows leaving v (how many things v points to)
  • In-degree d⁻(v) = arrows entering v (how many things point to v)

For every vertex: in-degree = out-degree, summed over the whole graph. This is the directed version of the handshaking lemma.

3. Degree of a Vertex & the Handshaking Lemma

Sum of all degrees Σ deg(v) = 2 |E|   for an undirected graph
WHY DOUBLE? EVERY EDGE HAS TWO ENDS. u v w +1 to u +1 to v +1 to v +1 to w 2 edges, 4 degree-units. |E| = 2, so 2|E| = 4 ✓
Counting degrees counts each edge once from each end, hence the factor of 2. A loop adds 2 to a single vertex.
Example — using the handshaking lemma

Question: A graph has 6 vertices with degrees 3, 2, 4, 3, 1, 2. How many edges?

Σ deg = 3+2+4+3+1+2 = 15
|E| = 15/2 = 7.5

7.5 is not a whole number — so the graph as stated is impossible. That is the point of the trick: the sum of degrees must be even.

No such graph exists. The degree sum of any undirected graph is always even.
Trick — the parity check

Before anything else in a graph question, add up the degrees.

  • Total is even → plausible, carry on. Then |E| = sum/2.
  • Total is odd → impossible. Say so. This is a free mark and it takes 10 seconds.

Also useful: every undirected graph with n vertices needs at least n − 1 edges to be connected. If a "connected" graph has fewer than n − 1 edges, it is not connected.

Consequences you should be able to quote • There is an even number of vertices of odd degree in any graph (handshake applied twice)
• A graph with n vertices has at most n(n−1)/2 edges (that is Kₙ)
• A regular graph has all vertices of the same degree; d must be even if n is odd

4. Adjacency Matrix

An adjacency matrix stores the graph as a table of 0s and 1s: rows are vertices as source, columns are vertices as target.

Definition A = [aᵢⱼ] where aᵢⱼ = 1 if there is an edge from vᵢ to vⱼ, and 0 if there is not.
THE GRAPH v₁ v₂ v₃ v₄ edges: v₁v₂, v₁v₃, v₃v₄, v₁v₄ degrees: 3, 1, 2, 2 → sum 8 → |E| = 4 ✓ ADJACENCY MATRIX A v₁v₂v₃v₄ v₁v₂v₃v₄ 0111 1000 1001 1010 row 1 = "what v₁ connects to" = 3 ones → deg 3 ✓ diagonal is all 0 → no loops symmetric → undirected graph
The row sums give the degrees, and the matrix is symmetric because the graph is undirected.
PropertyWhat it looks likeWhy
Undirected graphMatrix is symmetric (aᵢⱼ = aⱼᵢ)If u joins v, then v joins u
No loopsDiagonal entries are 0A loop would need aᵢᵢ = 1
Degree of vᵢSum of row i (undirected)Row i lists everything vᵢ touches
Directed graphNot necessarily symmetricaᵢⱼ = 1 does not force aⱼᵢ = 1
Out-degreeSum of row i (directed)Row = leaving vᵢ
In-degreeSum of column i (directed)Column = arriving at vᵢ
Loop in an undirected graph

A loop makes the matrix not symmetric unless you put a 2 in the diagonal cell. That is the standard convention: aᵢᵢ = 2 for a loop, because a loop contributes 2 to the degree.

5. Incidence Matrix

The incidence matrix is different: it relates vertices to edges, not vertices to vertices. The columns are the edges.

Definition M = [mᵢⱼ] where mᵢⱼ = 1 if vertex vᵢ is an ENDPOINT of edge eⱼ, else 0.
SAME GRAPH v₁ v₂ v₃ v₄ e₁ e₂ e₃ e₄ INCIDENCE MATRIX M e₁e₂e₃e₄ v₁v₂v₃v₄ 1101 1000 0110 0011 Column 1 (e₁ = v₁v₂) has 1s in rows 1 and 2 ✓ Every column sums to 2 (undirected) Row sums are still the degrees: 3,1,2,2 ✓ Number of columns = |E|, not |V|
Adjacency matrix: n × n. Incidence matrix: n × m. Different shapes, different job.
Adjacency matrixIncidence matrix
Sizen × n (vertices × vertices)n × m (vertices × edges)
Entries mean"is vᵢ joined to vⱼ?""is vᵢ an end of eⱼ?"
Row sum = degree?Yes (undirected)Yes
Column sumdegree (undirected) / in-degree (directed)always 2 in an undirected graph
Symmetric?Yes if undirectedNo
Trick — "adjacent" vs "incidence" vs "incident"

Students mix these up. Fix it with a memory line:

  • Adjacent = vertex ↔ vertex. Both are dots. "Two dots are neighbours."
  • Incident = vertex ↔ edge. A dot touches a line. "The dot is on the line."
  • Adjacency matrix = vertex ↔ vertex. Incidence matrix = vertex ↔ edge.

Mnemonic: "A for Adjacent (A for All-points), I for Incidence (I touches the line in between points)."

Example — building both matrices

Graph on {a, b, c} with edges: ab, bc.

Adjacency (a is row 1):

A = | 0 1 0 |
     | 1 0 1 |
     | 0 1 0 |

Check: row 1 sums to 1 → deg(a) = 1 ✓. Symmetric ✓. Total edges = (1+2+1)/2 = 2 ✓.

Incidence (columns = ab, bc):

M = | 1 0 |
     | 1 1 |
     | 0 1 |

Check: each column sums to 2 ✓. Row sums 1, 2, 1 = the degrees ✓.

Adjacency 3×3, Incidence 3×2. The graph is a path a–b–c.

6. Dijkstra's Shortest Path Algorithm

Dijkstra finds the cheapest route from one starting vertex to every other vertex, when every edge has a non-negative weight (cost, distance, time).

The rule At every step, pick the unvisited vertex with the SMALLEST known distance. "The nearest confirmed place is final. The far ones can still improve."
EXAMPLE GRAPH — find shortest paths from S 4 4 8 6 9 2 7 5 S A B C D E F Weighted graph. All weights positive, so Dijkstra applies.
S is the source. The numbers on the lines are the edge weights (costs).
Dijkstra worked through — all six steps

Setup. Start at S. Write ∞ for everything you cannot reach yet.

Step 0: S=0, A=∞, B=∞, C=∞, D=∞, E=∞, F=∞   visited: {S}

Step 1 — from S. Smallest unvisited is S (dist 0). Relax its two edges:

A = 0 + 4 = 4    B = 0 + 4 = 4    C, D, E, F = ∞
visited: {S}   permanent labels: S=0, A=4, B=4

Step 2 — pick the smallest. A and B both have 4; take A.

From A: C = 4 + 8 = 12   (was ∞, improves)
Labels: S=0, A=4, B=4, C=12, D=∞, E=∞, F=∞    visited: {S, A}

Step 3 — smallest unvisited is B (4).

From B: C = 4 + 6 = 10   (improves on 12!)   E = 4 + 2 = 6
Labels: S=0, A=4, B=4, C=10, D=∞, E=6, F=∞    visited: {S, A, B}

Step 4 — smallest unvisited is E (6).

From E: F = 6 + 5 = 11
Labels: S=0, A=4, B=4, C=10, D=∞, E=6, F=11    visited: {S, A, B, E}

Step 5 — smallest unvisited is C (10).

From C: D = 10 + 9 = 19
Labels: S=0, A=4, B=4, C=10, D=19, E=6, F=11    visited: {S, A, B, E, C}

Step 6 — smallest unvisited is F (11).

From F: D = 11 + 7 = 18   (improves on 19!)
Final: S=0, A=4, B=4, C=10, D=18, E=6, F=11
Shortest distances from S: A=4, B=4, C=10, D=18, E=6, F=11
Shortest path to D: S → B → E → F → D   = 4 + 2 + 5 + 7 = 18
The permanent / tentative table

The most reliable way to run Dijkstra on paper is a table with two columns per vertex: permanent (final, written in ink) and tentative (best guess so far, in pencil).

  • Pick the smallest tentative → move it to permanent.
  • Permanent labels never change again. That is what makes Dijkstra correct.
  • Tentative = min(itself, current + edge weight).
  • Never "un-permanent" a vertex. If your table says otherwise, you have made an error.
Where Dijkstra breaks

Dijkstra needs non-negative weights. With a negative edge it can give the wrong answer, and you need Bellman–Ford instead. In practice: distances, costs and times are always positive, so Dijkstra is safe.

Do not confuse these two

Dijkstra finds the cheapest path in a weighted graph — think money or kilometres.

BFS (breadth-first search) finds the path with the fewest edges in an unweighted graph — think "fewest hops" in an unweighted friend network.

They give the same answer when all edges have equal weight. If the question gives no numbers on the edges, you want BFS.

7. Graph Colouring

Colour the vertices so that no two adjacent vertices share a colour. The point is that fewer colours means fewer resources — fewer channels, fewer time slots, fewer frequency bands.

Chromatic number χ(G) The minimum number of colours needed. The question "what is χ(G)?" is a standard exam item.
THE REAL PROBLEM — 4-COLOUR MAP No two regions sharing a border may be the same colour. The four-colour theorem says 4 colours ALWAYS suffice. EQUIVALENT GRAPH A B C D E 5 vertices, 3 colours used, so χ(G) = 3
Map-colouring is just graph-colouring in disguise: regions become vertices, shared borders become edges.

Standard results worth memorising

GraphChromatic numberReason
Empty graph (no edges)1Everything can be one colour
Tree / forest2Bipartite — alternate two colours
Cycle Cₙ, n even2Alternate around, closes cleanly
Cycle Cₙ, n odd3Alternating fails at the last vertex
Bipartite graph2No odd cycles present
Complete graph KₙnEvery vertex touches every other
Any planar graph≤ 4Four-colour theorem
Example — colouring a cycle

Try to 2-colour a 5-cycle v₁–v₂–v₃–v₄–v₅–v₁:

v₁ = RED → v₂ = BLUE → v₃ = RED → v₄ = BLUE → v₅ = RED
but v₅ is adjacent to v₁, and both are RED. ✗ CONFLICT

So 2 colours fail. Try 3:

v₁=R, v₂=B, v₃=R, v₄=B, v₅=G  →  v₅ vs v₁ is G vs R ✓ all fine
χ = 3 for any odd cycle, χ = 2 for any even cycle.
Trick — the bipartite check (the most useful colouring idea)
  1. Try to divide the vertices into two groups X and Y so that every edge goes from X to Y. (Think chessboard colours, or "boys and girls".)
  2. Succeeded? The graph is bipartite and χ = 2. Two colours always work, and 2 is the minimum unless there are no edges at all.
  3. Failed? There is an odd cycle inside, so χ ≥ 3.

Super-fast odd-cycle hunt: if you can trace a closed loop with an odd number of edges (3, 5, 7…), the graph is not bipartite. This catches most exam questions in seconds.

Self-check

Open to see the answers

1. A graph has degrees 2, 3, 3, 4. How many edges? Is it possible?

Sum = 2+3+3+4 = 12 → |E| = 12/2 = 6 edges.
Valid, since the sum is even. Also note: 2 odd-degree vertices (the two 3s) — an even number, as required.

2. Can a simple graph have vertices of degree 1, 1, 1? What about 1, 1, 1, 1?

degrees 1,1,1: sum = 3, odd → impossible.
degrees 1,1,1,1: sum = 4, |E| = 2 → possible: two separate edges (two disjoint pairs).

3. Build the adjacency matrix for V = {1,2,3,4}, E = {(1,2), (2,3), (3,4), (4,1)}.

1 adjacent to 2 and 4 → 0 1 0 1
2 adjacent to 1 and 3 → 1 0 1 0
3 adjacent to 2 and 4 → 0 1 0 1
4 adjacent to 1 and 3 → 1 0 1 0
Matrix is symmetric ✓ (undirected). Row sums all = 2 → every vertex has degree 2 ✓. Total edges = 8/2 = 4 ✓. This is the 4-cycle C₄, so χ = 2.

4. Why does Dijkstra give the wrong answer if edge weights can be negative?

Because it permanently fixes a vertex's distance as soon as that vertex is the nearest. If a negative edge exists, a later route through a far-away vertex could come back and make that fixed distance smaller — and the algorithm has already committed to the old value. The proof of correctness relies entirely on all weights being non-negative. Use Bellman–Ford instead when negative weights are possible.