Module 2 - Quick Notes
Module 2 - Quick Notes
Definition: A BST where the height difference between the left and right subtrees of any node
(called the Balance Factor) is at most 1 (i.e., BF ∈ {-1, 0, 1}).
Balance Factor (BF): BF(node) = Height(LeftSubtree) - Height(RightSubtree) .
Advantage: Guarantees O(log n) time complexity for search, insertion, and deletion by preventing
the tree from becoming skewed.
Rotations: Used to rebalance the tree after an insertion or deletion causes a BF to become -2 or
+2.
LL Rotation (Single Left): Fixes imbalance caused by insertion into the left subtree of the left
child.
RR Rotation (Single Right): Fixes imbalance caused by insertion into the right subtree of the
right child.
LR Rotation (Left-Right Double): Fixes imbalance caused by insertion into the right subtree
of the left child. (Perform RR on child, then LL on parent).
RL Rotation (Right-Left Double): Fixes imbalance caused by insertion into the left subtree of
the right child. (Perform LL on child, then RR on parent).
Key Idea: Identify the unbalanced node, determine the case (LL, RR, LR, RL) based on the
insertion path, and perform the corresponding rotation(s).
Purpose: Manages a collection of disjoint sets (elements belong to exactly one set).
Operations:
MAKE-SET(x) : Creates a new set containing only element x .
FIND-SET(x) : Returns the representative (unique ID/root) of the set containing x . Used to
check if two elements are in the same set.
UNION(x, y) : Merges the two sets containing x and y . Checks FIND-SET(x) and FIND-
SET(y) ; if different, makes one representative the parent of the other.
Application (Connected Components):
1. MAKE-SET for each vertex.
2. For each edge (u, v) in the graph:
If FIND-SET(u) != FIND-SET(v) then UNION(u, v) .
3. After processing all edges, vertices with the same representative are in the same connected
component.
(a) Depth First Search (DFS): Explores as deeply as possible along each branch before
backtracking. Uses a stack (often implicit via recursion).
Algorithm DFS(G, u) :
1. Mark u as visited.
2. Process u .
3. For each neighbor v of u :
4. If v is not visited:
5. Call DFS(G, v) .
Complexity: O(V + E) with adjacency lists.
Applications: Cycle detection, topological sorting, finding SCCs.
(b) Breadth First Search (BFS): Explores level by level. Uses a queue.
Algorithm BFS(G, s) :
1. Mark all vertices unvisited.
2. Create a queue Q .
3. Mark s as visited and enqueue s .
4. While Q is not empty:
5. u = Dequeue(Q) .
6. Process u .
7. For each neighbor v of u :
8. If v is not visited:
9. Mark v as visited and Enqueue v .
Complexity: O(V + E) with adjacency lists.
Applications: Shortest path in unweighted graphs, finding levels.
Definition: In a directed graph, a maximal subset of vertices C such that for every pair u, v in C ,
there's a path from u to v and a path from v to u .
Key Idea (Kosaraju's Algorithm):
1. Perform DFS on the original graph G , keeping track of vertex finish times (or push to stack
upon finishing).
2. Compute the transpose graph G^T (reverse all edge directions).
3. Perform DFS on G^T , processing vertices in decreasing order of their finish times from step 1
(or pop from stack). Each tree formed in this second DFS is an SCC.
5. Topological Sorting:
Definition: A linear ordering of vertices in a Directed Acyclic Graph (DAG) such that for every
directed edge (u, v) , vertex u appears before vertex v in the ordering.
Requirement: Graph must be a DAG.
Key Idea (Kahn's / In-degree Algorithm):
1. Compute the in-degree (number of incoming edges) for all vertices.
2. Initialize a queue with all vertices having an in-degree of 0.
3. While the queue is not empty:
Dequeue a vertex u . Add u to the topological order result.
For each neighbor v of u :
Decrement the in-degree of v .
If the in-degree of v becomes 0, enqueue v .
If the final result contains fewer than |V| vertices, the graph has a cycle.
(Remember: This is a condensed overview. Refer to detailed notes for complete algorithms and
proofs.)
Okay, here are sample questions and answers for Module 2, covering the topics you listed.
AVL Trees
10 (BF=0)
2. Insert 20:
10 (BF=-1)
\
20 (BF=0)
3. Insert 30:
Node 10 is unbalanced (BF=-2). Its right child (20) has BF=-1. This is an RR case.
Perform RR rotation (Single Right rotation around 10):
20 becomes the new root.
10 becomes the left child of 20.
20 (BF=0)
/ \
10(BF=0) 30(BF=0)
Disjoint Sets
5. Q: Describe the MAKE-SET, FIND-SET, and UNION operations for disjoint sets.
A:
MAKE-SET(x): Creates a new set whose only member (and representative/root) is x . The
parent of x is typically set to itself (or NULL) initially.
FIND-SET(x): Returns the representative (root) of the set containing x . This is done by
following parent pointers until a node that points to itself (or has a NULL parent) is reached.
UNION(x, y): Merges the two dynamic sets that contain x and y into a new set that is the
union of these two sets. It first finds the representatives of the sets containing x and y . If they
are different, one representative is made the parent of the other. (Optimizations like union-by-
rank or union-by-size can be used).
6. Q: How can disjoint set operations be used to find connected components in an undirected graph?
A:
1. Initialization: For each vertex v in the graph, perform MAKE-SET(v) . Initially, each vertex is
in its own component (set).
2. Process Edges: For each edge (u, v) in the graph:
If FIND-SET(u) is not equal to FIND-SET(v) (meaning u and v are currently in different
components/sets), then perform UNION(u, v) . This merges the components of u and v .
3. Result: After processing all edges, all vertices that belong to the same set (i.e., have the
same representative returned by FIND-SET ) are in the same connected component. The
number of distinct sets remaining is the number of connected components.
Graph Traversal
7. Q: Give the algorithm for Depth First Search (DFS) and state its time complexity. List two
applications.
A:
Algorithm DFS(G, u) :
1. Mark vertex u as VISITED.
2. Process u (e.g., print it, add to a list).
3. For each neighbor v of u :
4. If v is NOT VISITED:
5. Recursively call DFS(G, v) .
(To ensure all components are visited in a disconnected graph, iterate through all vertices,
and if a vertex is unvisited, start a DFS from it.)
Time Complexity: O(V + E), where V is the number of vertices and E is the number of edges
(using adjacency list representation).
Applications:
1. Cycle Detection: If DFS encounters an already visited vertex that is an ancestor in the
DFS tree (a back edge).
2. Topological Sorting for DAGs.
(Other applications: Finding strongly connected components, path finding).
8. Q: Write the algorithm for Breadth First Search (BFS). What is its time complexity?
A:
Algorithm BFS(G, s) : (s is the starting vertex)
1. Initialize all vertices as NOT VISITED.
2. Create an empty queue Q .
3. Mark s as VISITED and enqueue s into Q .
4. While Q is not empty:
5. u = Dequeue(Q) .
6. Process u .
7. For each neighbor v of u :
8. If v is NOT VISITED:
9. Mark v as VISITED and enqueue v into Q .
Time Complexity: O(V + E) (using adjacency list representation).
Strongly Connected Components (SCC)
Topological Sorting