0% found this document useful (0 votes)
4 views

Module 2 - Quick Notes

Module 2 covers key concepts in data structures including AVL Trees, Disjoint Sets, Graph Traversal Algorithms, Strongly Connected Components, and Topological Sorting. It provides definitions, advantages, algorithms, and applications for each topic, emphasizing operations like rotations in AVL trees, union-find techniques, and traversal methods like DFS and BFS. The module also outlines algorithms for finding strongly connected components and performing topological sorting in directed acyclic graphs.

Uploaded by

devikaminnu21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Module 2 - Quick Notes

Module 2 covers key concepts in data structures including AVL Trees, Disjoint Sets, Graph Traversal Algorithms, Strongly Connected Components, and Topological Sorting. It provides definitions, advantages, algorithms, and applications for each topic, emphasizing operations like rotations in AVL trees, union-find techniques, and traversal methods like DFS and BFS. The module also outlines algorithms for finding strongly connected components and performing topological sorting in directed acyclic graphs.

Uploaded by

devikaminnu21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Module 2 - Quick notes

Module 2: Quick Revision Notes

1. AVL Trees (Self-Balancing Binary Search Trees):

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).

2. Disjoint Sets (Union-Find Data Structure):

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.

3. Graph Traversal Algorithms:

(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.

4. Strongly Connected Components (SCCs):

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.

Module 2: Sample Questions and Answers

AVL Trees

1. Q: Define an AVL tree. What is its main advantage?


A:
Definition: An AVL tree is a self-balancing Binary Search Tree (BST) where for every node, the
heights of its left and right subtrees differ by at most 1. This height difference is called the
Balance Factor.
Advantage: The primary advantage of an AVL tree is that it guarantees a height of O(log n)
(where n is the number of nodes). This ensures that search, insertion, and deletion operations
can always be performed in O(log n) time in the worst case, preventing the tree from becoming
skewed like a regular BST.
2. Q: How is the Balance Factor (BF) of a node in an AVL tree calculated? What are the permissible
BF values?
A:
Calculation: Balance Factor (Node) = Height (Left Subtree of Node) - Height (Right
Subtree of Node) . The height of an empty subtree is often considered -1 or 0 (be consistent).
Permissible BF Values: For an AVL tree, the Balance Factor of every node must be -1, 0, or
+1.
BF = -1: Right subtree is one level taller.
BF = 0: Left and right subtrees have equal height.
BF = +1: Left subtree is one level taller.
3. Q: Explain LL rotation in an AVL tree. When is it applied?
A:
Explanation: LL (Single Left) rotation is performed when an imbalance is caused by an
insertion into the left subtree of the left child of an unbalanced node (the grandparent of the
inserted node, if you will).
Application: If node A becomes unbalanced with BF=+2, and its left child B has BF=+1 (or 0
if B is where insertion happened), an LL rotation is applied.
Steps:
1. The left child B becomes the new root of this subtree.
2. The original root A becomes the right child of B .
3. The original right child of B (if any) becomes the left child of A .
This rotation restores balance.
4. Q: Consider an initially empty AVL tree. Show the steps and rotations involved in inserting the keys:
10, 20, 30.
A:
1. Insert 10:

10 (BF=0)

2. Insert 20:

10 (BF=-1)
\
20 (BF=0)

3. Insert 30:

10 (BF=-2) <-- Unbalanced


\
20 (BF=-1)
\
30 (BF=0)

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)

The tree is now balanced.

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)

9. Q: Define a Strongly Connected Component (SCC) in a directed graph.


A: A Strongly Connected Component (SCC) of a directed graph G = (V, E) is a maximal set of
vertices C ⊆ V such that for every pair of vertices u and v in C, there is a path from u to v AND
a path from v to u . In other words, all vertices within an SCC are mutually reachable.
10. Q: Briefly outline the steps of Kosaraju's algorithm for finding SCCs.
A:
1. Step 1 (DFS on G): Perform a Depth First Search (DFS) on the original graph G. Keep track
of the finishing times of vertices (or, more simply, push vertices onto a stack as they finish their
DFS exploration).
2. Step 2 (Compute Transpose): Compute the transpose graph GT (reverse the direction of all
edges in G).
3. Step 3 (DFS on GT): Perform DFS on GT. Process vertices in the order of decreasing
finishing times from Step 1 (i.e., pop vertices from the stack and if unvisited, start a new DFS
from that vertex in GT). Each tree formed in this second DFS traversal on GT constitutes a
Strongly Connected Component.

Topological Sorting

11. Q: What is Topological Sorting? What type of graph is it applicable to?


A:
Definition: Topological sorting for a Directed Acyclic Graph (DAG) is a linear ordering of its
vertices such that for every directed edge (u, v) from vertex u to vertex v , u comes before
v in the ordering.
Applicable Graph: It is only applicable to Directed Acyclic Graphs (DAGs). If a graph
contains a cycle, it's impossible to produce a topological sort.
12. Q: Describe an algorithm to find a topological sort of a DAG (e.g., Kahn's algorithm).
A: (Kahn's Algorithm using in-degrees)
1. Compute In-degrees: Calculate the in-degree (number of incoming edges) for every vertex in
the DAG.
2. Initialize Queue: Create a queue and enqueue all vertices with an in-degree of 0.
3. Process Queue:
Initialize an empty list L (for the topological order).
Initialize count_visited_nodes = 0 .
While the queue is not empty:
Dequeue a vertex u from the queue.
Add u to the list L .
Increment count_visited_nodes .
For each neighbor v of u :
Decrement the in-degree of v .
If the in-degree of v becomes 0, enqueue v .
4. Check for Cycle: If count_visited_nodes is not equal to the total number of vertices in the
graph, then the graph has a cycle (and thus no topological sort exists). Otherwise, the list L
contains the topological sort.

You might also like