IITP · Semester ISummaries · Quizzes · Assignments

Lecture summary

DAA 22 Nov

22 Nov 20261h 25m3 parts

Source: IITP-big-files\Sem-1\ECS5101-DAA\Lectures\2026-11-22 0000 DAA 22 Nov\summary\notes.md

Overview

This class covered fundamental graph algorithms, focusing on traversal and the introduction of minimum spanning trees. It began with a review of graph representations (adjacency matrix and list) and their trade-offs. The core of the lecture detailed Breadth-First Search (BFS) and Depth-First Search (DFS) traversal algorithms with step-by-step examples. The session then transitioned to a real-world cabling problem, motivating the concept of a Minimum Spanning Tree (MST). Kruskal's greedy algorithm for MST was explained and demonstrated, and Prim's algorithm was introduced. This class fits into the course as a bridge from basic graph operations to more advanced, optimization-focused graph algorithms.

Learning outcomes

  • Represent graphs using adjacency matrices and adjacency lists and select the appropriate representation based on graph density.
  • Execute and trace the BFS and DFS traversal algorithms on a given graph from a specified start vertex.
  • Explain the selection criteria and real-world use cases for BFS versus DFS.
  • Define a Minimum Spanning Tree (MST) and state its key properties.
  • Apply Kruskal's algorithm to find the MST of a weighted, undirected graph.
  • Describe the basic approach of Prim's algorithm and how it differs from Kruskal's.

Core concepts

Graph Representations

The two primary ways to store a graph in memory. The adjacency matrix is a sequential, VxV matrix where a 1 indicates an edge. The adjacency list is a link-based representation where each vertex has a list of its neighbors. The choice matters for algorithm efficiency: matrices are preferred for dense graphs, while lists are better for sparse graphs.

Graph Traversal (BFS & DFS)

Systematic methods to visit all vertices in a graph. BFS uses a queue (FCFS) to explore all neighbors at the current depth before moving deeper. DFS uses a stack (LIFO) to explore as far down a branch as possible before backtracking. They are foundational operations for exploring graph structure and are prerequisites for more complex algorithms.

Minimum Spanning Tree (MST)

A subgraph of a connected, undirected graph that is a tree (acyclic), includes all vertices, has exactly V-1 edges, and minimizes the total sum of edge weights. It solves optimization problems like minimizing cable or piping costs in a network. Multiple MSTs can exist, but all share the same minimum total cost.

Greedy MST Algorithms

Algorithms that build an MST by making a series of locally optimal choices. Kruskal's algorithm sorts all edges by weight and adds them if they don't create a cycle. Prim's algorithm starts from a source and grows a single tree by always adding the cheapest edge connecting the tree to a new vertex. Both guarantee an optimal MST.

Formulas and maths

  • V - 1: The exact number of edges in a spanning tree for a graph with V vertices. Used to verify a candidate spanning tree.
  • sum(edge_weights): The cost of a spanning tree, calculated by summing the weights of all its edges. The goal for an MST is to minimize this sum.

Worked examples

BFS Example 1 (Numerical)

Start node: 5. 1. Process 5 → neighbors 1 and 2 added to queue. 2. Process 1 → neighbors 0, 4, and 2 (2 already in queue, add 0 and 4). 3. Process 2 → no new neighbors. 4. Process 0 → neighbors 7, 5, 3 (5 processed, add 3 and 7). 5. Process 4 → neighbor 3 (already in queue). 6. Process 3 → neighbor 2 (processed). 7. Process 7 → neighbor 6 (add 6). 8. Process 6 → no new neighbors. All vertices reach status 3 (processed).

BFS Example 2 (Alphabetical)

Start node: A. 1. Process A → neighbors B, D, E added to queue. 2. Process B → neighbors E and C (E in queue, add C). 3. Process D → neighbors A and G (A processed, add G). 4. Process E → neighbors B, A, G (all already in queue/processed). 5. Process C → neighbors B and F (add F). 6. Process G → neighbors H and D (D processed, add H). 7. Process F → no new neighbors. 8. Process H → neighbor I (add I). 9. Process I → completion.

DFS Example (Numerical)

Start node: 5. 1. Push 5, pop and process it. 2. Push neighbors of 5 (1, 2). 3. Pop 1, process it, push its neighbors (0, 4, 2; 2 already in stack, push 0 and 4). 4. Pop 0, process it, push its neighbors (7, 3, 5; 5 processed, push 3 and 7). 5. Pop 3, process it, neighbor 2 already in stack. 6. Pop 7, process it, push neighbor 6. 7. Pop 6, process it. 8. Pop 4, process it. 9. Pop 2, process it. Order of processing: 5, 1, 0, 3, 7, 6, 4, 2.

Kruskal's Algorithm Example

Edges sorted: 1, 2, 4, 6, 7, 7, 8, 9, ... Edges added sequentially if no cycle formed. Edges with weights 7 (HI), 10 (EF), 11 (BH), and 14 (DF) were rejected due to cycle formation. Final MST total weight: 45.

Prim's Algorithm Example

Starting from source s with edge weights 9 and 8. Choose edge weight 8 first. From the growing tree {s, ...}, repeatedly select the minimum-weight edge connecting the tree to a new vertex (e.g., weights 2, 1, 6).

Questions asked in class

Q1: When should a programmer select BFS vs. DFS? A1: Use DFS if heuristics suggest the target is deep; use BFS if it's on neighboring nodes. BFS is preferred for MST algorithms; DFS is preferred for evaluating algebraic expression trees.

Q2: What are real-life use cases for BFS and DFS? A2: BFS: Planning multi-city journeys, MST applications (cabling). DFS: Solving algebraic expression trees, delaying calculations due to operator precedence.

Q3: Why was edge 'ig' (weight 6) selected over 'hi' (weight 7) in Kruskal's example? A3: Edges are selected in strict ascending weight order from the sorted list; lower weights are always preferred first.

Q4: What is the exam format? A4: Approximately 50 questions (MCQ, multiple select, true/false, matching) in a 2-hour window. Rough work on paper is allowed.

Q5: What is the syllabus coverage for the exam? A5: From Divide and Conquer (e.g., Merge Sort) onwards, not earlier basic concepts or the Master Theorem.

Q6: When to choose Prim's vs. Kruskal's? A6: Prim's requires a given source vertex. Kruskal's does not; it refines edges based on global minimum weight.

Key takeaways

  • An adjacency matrix is optimal for dense graphs, while an adjacency list is optimal for sparse graphs.
  • BFS traversal uses a queue (FCFS) and explores level by level.
  • DFS traversal uses a stack (LIFO) and explores depth-first along branches.
  • Vertex states during traversal are: Ready (1), Waiting (in queue/stack, 2), Processed (3).
  • A spanning tree is a connected, acyclic subgraph containing all vertices and exactly V-1 edges.
  • A Minimum Spanning Tree (MST) is a spanning tree with the minimum possible total edge weight.
  • Kruskal's algorithm builds an MST by greedily adding the smallest edge that doesn't create a cycle.
  • Prim's algorithm builds an MST by greedily adding the smallest edge connecting the current tree to a new vertex.
  • MST algorithms apply to undirected graphs; shortest path algorithms (Dijkstra, Bellman-Ford) apply to directed graphs.
  • Multiple MSTs can exist for a graph, but all have the same total minimum cost.
  • The Maximum Spanning Tree problem is a known variant.
  • Assignment weight in the final grade is low (~4-5%); significant weight is on quizzes and exams.

Exam-style questions

  1. Given an adjacency list and a start vertex, trace the complete BFS traversal, indicating the queue state at each step. Outline: Initialize queue with start vertex. While queue not empty, dequeue front vertex, mark as processed, and enqueue all its unvisited (ready state) neighbors. Show queue contents and processing order.

  2. Apply Kruskal's algorithm to a weighted graph. Show the sorted edge list, indicate which edges are added/rejected and why, and state the final MST cost. Outline: List all edges sorted by weight. Iterate, adding edges to the MST set if they do not create a cycle (using concepts like connected components). Reject edges that form cycles. Stop when V-1 edges are selected. Sum weights of selected edges.

  3. Compare and contrast Prim's and Kruskal's algorithms for finding an MST. Outline: Prim's requires a source, grows a single connected tree, uses a priority queue for edges from the tree. Kruskal's has no source, can build disconnected components that merge, starts by sorting all edges. Both are greedy and produce optimal MSTs.

Glossary

  • Adjacency Matrix - A VxV matrix representation of a graph where entry (i,j) indicates an edge from vertex i to j.
  • Adjacency List - A representation where each vertex has a linked list of its adjacent vertices.
  • Breadth-First Search (BFS) - A graph traversal algorithm that uses a queue to visit vertices in level order.
  • Depth-First Search (DFS) - A graph traversal algorithm that uses a stack (implicitly via recursion) to visit vertices depth-wise.
  • Minimum Spanning Tree (MST) - A spanning tree with the minimum total edge weight.
  • Spanning Tree - A connected, acyclic subgraph containing all vertices of the original graph.
  • Kruskal's Algorithm - A greedy MST algorithm that builds a forest by adding edges in sorted weight order, avoiding cycles.
  • Prim's Algorithm - A greedy MST algorithm that grows a single tree from a source by adding the minimum connecting edge.

Follow-ups

  • Detailed implementation of Kruskal's algorithm operations: Make-Set, Find-Set, and Union.
  • More worked examples for Prim's algorithm.
  • Coverage of Bellman-Ford, Dijkstra, and Floyd-Warshall algorithms for shortest path problems.
  • Clarification on maximum spanning tree concept.
  • Quiz 2 syllabus will cover material up to this class (22 Nov).
  • End-semester exam syllabus starts from Divide and Conquer through the final class.

Quiz cross-check (added 2026-12-01, from DAA Quiz 2)

Four facts that the quiz tests but this class did not state in words. They follow from the material above and are the exam-ready form of it.

  • BFS gives shortest paths in an unweighted graph. Vertices leave the queue in order of edge-distance from the source, so the first time a vertex is reached, its level is the minimum number of edges needed to reach it. With weights, BFS is no longer valid — that is Dijkstra's job.
  • Mark visited on enqueue, not on dequeue. A vertex moves Ready → Waiting at the moment it is put in the queue and → Processed when it is taken out. Marking on enqueue is what stops the same vertex being queued twice through different neighbours, and it is what makes the "first discovery = shortest path" argument work.
  • DFS applications include topological sorting of a DAG — order the vertices by decreasing finishing time (equivalently, push each vertex onto the front of a list as its DFS call finishes). DFS also does cycle detection and connected-component labelling. Expression trees are only one application; topological sort is the one the exam asks for.
  • Binary search worst case = ⌊log₂ n⌋ + 1 comparisons (the height of its decision tree). For n = 127 = 2⁷ − 1 that is 7. This is the concrete version of the "order log n" stated earlier in the course.
  • An undirected graph's adjacency matrix is symmetric: A[i][j] = A[j][i], because the edge {i, j} is recorded in both directions. Only the directed case (built in the 14 Nov class) is asymmetric.