Algorithm Analysis Module 2 Important Topics
Algorithm Analysis Module 2 Important Topics
Topics
For more notes visit
https://rtpnotes.vercel.app
Algorithm-Analysis-Module-2-Important-Topics
1. AVL Trees
What is an AVL Tree?
Calculating Balancing Factor
Balancing AVL Tree
Left Rotation
Right Rotation
Left-RIght Rotation (LR Rotation)
Right-Left Rotation
Deletion in AVL Trees
Different cases in deletion
Case 1: Delete Leaf Node Example
Case 2: Delete a node with one child
Case 3: Delete an internal node (Having both left and right subtree)
Height of an AVL Tree
Problem
2. Breadth First Search (BFS)
Algorithm BFS(G,S)
Analysis
Example
Applications
3. Depth First Search
Algorithm
Algorithm DFS(g)
Algorithm DFSVISIT(G,u)
Analysis
Example
Applications
4. Strongly connected components
What are Connected Components?
What are strongly connected components?
Example
Strongly Connected Components - Complexity
Applications
5. Topological sorting
Topological sorting algorithm
Example
Topological sorting complexity
1. AVL Trees
What is an AVL Tree?
We want to balance this graph, The method that we will use is Rotation.
Left Rotation
Right Rotation
Here first we did a Left Rotation, and then a Right rotation, This is called LR Rotation
Right-Left Rotation
Straightening the graph, We have to Bring 2 to the right, so we do
Right Rotate(3)
Unbalanced becomes the right child, Here its 3
2 will be the parent
Delete 65
Swap 65 and 50
Delete 65
Case 3: Delete an internal node (Having both left and right subtree)
Delete 40
Let N (h) denotes the minimum number of nodes in an AVL Tree of height h
if h = 0, N (0) = 1
h = 1, N (1) = 2
N (h) = N (h − 1) + N (h − 2) + 1
Problem
Log 7 = 2
Here we basically take all the vertices, set their color to white, set their distance to infinity
and their predecessor to nil
We mark color gray, distance as 0 and predecessor as nil for the Source node
We initialize the queue as null
We start by enqueueing the source vertex to the queue
While the queue is not empty, we need to dequeue the queue and store it it variable u
Now We will take each of u's adjacent vertices one by one
If their color is white, change it to gray, update distance and predecessor and
Enqueue that node
After all vertices are processed, Change the color of vertex to black, indicating the vertex
is completely processed
What the colors mean
White for unvisited vertices.
Gray for vertices that are currently in the queue and are being processed.
Black for vertices that have been fully processed.
Analysis
Example
Dequeueing A
Checking for unvisited neighbours
B and C
Adding to Queue, mark as visited
Dequeuing B
Neighbours, D and E
Dequeueing C
Neighbours, D, already visited
Dequeuing D
Neighbours, E,D and A, already visited
Dequeueing E
Neighbours, none
Applications
Algorithm DFS_VISIT(G,u)
1. time = time + 1
2. u.d = time
3. u.colour = GREY # Marking as visited
4. for each vertex v in Adj(u) do
1. if v.colour == white then
1. π[v] = u
2. DFS_VISIT(G,V) # Visiting the adjacent vertices
Analysis
Example
All nodes are unvisited
Suppose A is the starting Node
Starting Traversal
Starting with A
Neighbours of A are, B, D and E
Applications
Scheduling Problems
Cycle detection in graphs
Solving puzzles with only one solution, like mazes
Example
Find strongly connected components of the digraph using algorithm showing each step
Pass 1
All nodes are unvisited
Stack is empty
Starting DFS Traversal
Starting from 1, mark as visited
Its unvisited neighbour is 4
4's neighbour is 7
7 has no neighbour
Push 7 to the stack
Going back, 4 has no other neighbour
Push 4 to stack
Going back, 1 has no other neighbour
Push 1 to stack
Popping 2
Already visited
Popping 9
Visiting 9
Neighbour 6
Neighbour 3
9-6-3 is the connected component
Popping 3
Already visited
Popping 6
Already visited
Popping 1
Unvisited
Visiting 1
Visiting neighbour 7
Visiting neighbour 4
8-2-5
9-6-3
1-7-4
Applications
Social networks
5. Topological sorting
Topological sorting is applicable for Direct Acyclic Graph (DAG)
The graphs should not have a cycle
Example