Loading lessons...
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 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
| Representation | Space | Check Edge (u, v) | Iterate Neighbors | Best For |
|---|---|---|---|---|
| Adjacency Matrix | O(V2) | O(1) | O(V) | Dense graphs |
| Adjacency List | O(V + E) | O(deg(u)) | O(deg(u)) | Sparse graphs |
3. Graph Traversals (States: 1 Ready, 2 Waiting, 3 Processed)
- Depth-First Search (DFS-Stack / Recursion):
- Marks start vertex
Waiting, pushes to Stack; pops vertex u, marksProcessed, and pushes allReadyneighbors. - 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).
- Breadth-First Search (BFS-Queue):
- Marks start vertex
Waiting, enqueues it; dequeues vertex u, marksProcessed, and enqueues allReadyneighbors. - 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
| Term | Meaning |
|---|---|
| Adjacent vertices | u and v are adjacent if an edge (u, v) exists |
| Incident edge | An edge is incident on the vertices it joins |
| Path | A sequence of vertices, each adjacent to the next |
| Simple path | A path with no repeated vertex |
| Cycle | A path that starts and ends at the same vertex |
| Simple cycle | A cycle with no repeated vertex other than the first/last |
| Self-loop | An edge from a vertex to itself |
| Connected graph | A 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
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-loopAdjacency list
A → B → C
B → A → C → D
C → A → B → D → E
D → B → C → E → F
E → C → D
F → D → FLimitations 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
- Set STATUS = 1 for every node.
2. Put the start node
A in QUEUE; set its STATUS = 2.
- Repeat steps 4-5 until QUEUE is empty:
- Remove the front node N of QUEUE; process N; set STATUS[N] = 3.
- Add to the rear of QUEUE every neighbour of N with STATUS = 1, and set their STATUS = 2.
- 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 step | Process | QUEUE after | DFS step | Process | STACK after (top on right) | |
|---|---|---|---|---|---|---|
| start | - | A | start | - | A | |
| 1 | A | B C | 1 | A | B C | |
| 2 | B | C D | 2 | C | B D E | |
| 3 | C | D E | 3 | E | B D | |
| 4 | D | E F | 4 | D | B F | |
| 5 | E | F | 5 | F | B | |
| 6 | F | - | 6 | B | - |
- 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.
📝 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
STATUSstates (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.