Lesson 25 +35 XP

Graph Terminology, Representations & Traversals (BFS & DFS)

Graph Terminology, Representations & Traversals (BFS & DFS)

A Graph G = (V, E) consists of a finite set of vertices (nodes) V and edges E connecting pairs of vertices.

Graph RepresentationsGraph Representations

1. Essential Graph Terminology

  • Undirected vs Directed (Digraph): Edges {u, v} have no direction vs directed arcs (u, v) from tail u to head v.
  • Degree: Number of edges incident to a vertex (Handshaking Lemma: ∑ deg(v) = 2|E|). In digraphs, Indegree(v) + Outdegree(v).
  • Self-Loop & Parallel Edges: An edge (u, u) or multiple edges between the same pair. A Simple Graph has neither.
  • Complete Graph (Kn): Every distinct pair of vertices is connected; has (n(n-1) / 2) undirected edges.
  • Connected Graph & Tree: A path exists between every pair of vertices; a Tree is a connected acyclic graph (|E| = |V| - 1).
  • Regular & Weighted Graph: Every vertex has equal degree (Regular); edges carry numeric costs/distances (Weighted).

2. Graph Representations

RepresentationSpaceCheck Edge (u, v)Iterate NeighborsBest For
Adjacency MatrixO(V2)O(1)O(V)Dense graphs
Adjacency ListO(V + E)O(deg(u))O(deg(u))Sparse graphs

3. Graph Traversals (States: 1 Ready, 2 Waiting, 3 Processed)

  1. Depth-First Search (DFS-Stack / Recursion):
  • Marks start vertex Waiting, pushes to Stack; pops vertex u, marks Processed, and pushes all Ready neighbors.
  • Worked Order Example: A → B → D → F → E → C → G (dives deep before backtracking).
  • Applications: Cycle detection, Connected Components, Topological Sort (on DAGs-ordering vertices so every edge u → v has u before v).
  1. Breadth-First Search (BFS-Queue):
  • Marks start vertex Waiting, enqueues it; dequeues vertex u, marks Processed, and enqueues all Ready neighbors.
  • Worked Order Example: A → B → C → E → D → F → G (visits level-by-level).
  • Applications: Shortest path in unweighted graphs, level-order discovery, bipartite checking.
  • Time Complexity: O(V + E) with Adjacency List, O(V2) with Adjacency Matrix.

4. Topological Sort (Kahn's Algorithm on DAGs)

For a Directed Acyclic Graph (DAG), compute indegree[v] for all vertices, enqueue all vertices with indegree == 0, repeatedly dequeue u, output u, and decrement indegree[v] for each neighbor v (enqueuing v if its indegree hits 0).

More Graph Terminology

TermMeaning
Adjacent verticesu and v are adjacent if an edge (u, v) exists
Incident edgeAn edge is incident on the vertices it joins
PathA sequence of vertices, each adjacent to the next
Simple pathA path with no repeated vertex
CycleA path that starts and ends at the same vertex
Simple cycleA cycle with no repeated vertex other than the first/last
Self-loopAn edge from a vertex to itself
Connected graphA path exists between every pair of vertices

Worked Example: The A-F Graph

Edges: A-B, A-C, B-C, B-D, C-D, C-E, D-E, D-F, and a self-loop at F.

Adjacency matrix

TEXT
     A  B  C  D  E  F
A  [ 0  1  1  0  0  0 ]
B  [ 1  0  1  1  0  0 ]
C  [ 1  1  0  1  1  0 ]
D  [ 0  1  1  0  1  1 ]
E  [ 0  0  1  1  0  0 ]
F  [ 0  0  0  1  0  1 ]   ← the 1 on the diagonal is the self-loop

Adjacency list

TEXT
A → B → C
B → A → C → D
C → A → B → D → E
D → B → C → E → F
E → C → D
F → D → F

Limitations of the adjacency matrix: it always needs O(V2) memory even when the graph has few edges, listing a vertex's neighbours takes O(V), and adding a vertex means rebuilding the whole matrix. Adjacency lists use O(V + E) space and are preferred for sparse graphs.

BFS and DFS with STATUS (Lipschutz algorithms)

STATUS = 1 (ready), 2 (waiting-in the queue/stack), 3 (processed).

BFS

  1. Set STATUS = 1 for every node.

2. Put the start node

A in QUEUE; set its STATUS = 2.

  1. Repeat steps 4-5 until QUEUE is empty:
  2. Remove the front node N of QUEUE; process N; set STATUS[N] = 3.
  3. Add to the rear of QUEUE every neighbour of N with STATUS = 1, and set their STATUS = 2.
  4. Exit.

DFS - identical, but use a STACK: pop the top node N in step 4 and push its ready neighbours in step 5.

Trace from A on the A-F graph (neighbours taken in alphabetical order):

BFS stepProcessQUEUE afterDFS stepProcessSTACK after (top on right)
start-Astart-A
1AB C1AB C
2BC D2CB D E
3CD E3EB D
4DE F4DB F
5EF5FB
6F-6B-
  • BFS order: A B C D E F
  • DFS order (stack version): A C E D F B. The recursive DFS, which follows the first unvisited neighbour immediately, gives A B C D E F.

Both run in O(V + E) with adjacency lists and O(V2) with a matrix.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Explain Adjacency Matrix and Adjacency List representations of a Graph with an example. Which is better for sparse graphs? (5 Marks)
  • Write: 4-node graph example, O(V2) matrix vs O(V + E) linked list, and comparison table.
  • Q2. Differentiate between BFS and DFS. Write both algorithms using STATUS states (1 Ready, 2 Waiting, 3 Processed). (10 Marks)
  • Write: Queue vs Stack, level-order vs depth-first, shortest unweighted path vs cycle/topological sort, and full step-by-step algorithm + traversal trace.