Algorithm Analysis Module 3 Important Topics
Algorithm Analysis Module 3 Important Topics
Topics
For more notes visit
https://rtpnotes.vercel.app
Algorithm-Analysis-Module-3-Important-Topics
1. Divide and conquer-control abstraction
What is divide and conquer
Control abstraction
Divide and conquer control abstraction
Divide and Conquer - Time complexity
Recurrence relation for Divide and Conquer time complexity
2. Strassen Matrix Multiplication
Strassen’s Algorithm for Matrix Multiplication-Analysis
Divide and conquer matrix multiplication
Divide and conquer Matrix multiplication - Complexity
Strassens Matrix Multiplication Algorithm
Example of Strassens multiplication algorithm
Strassens matrix multiplication complexity
Recurrence relation
3. Two way merge sort
What is merge sort?
Sorting using merge sort
- Divide Operation
- Merge Operation
Merge sort algorithm
Merge Algorithm
Algorithm
Applying the algorithm
Time complexity of merge sort
4. Greedy approach
The Control Abstraction of Greedy Strategy
5. Fractional knapsack problem
What is Fractional Knapsack Problem?
Fractional knapsack problem - Algorithm
Fractional knapsack problem - Time Complexity
Fractional knapsack - Problem -1
-i=1
-i=2
-i=3
-i=4
- Solution
Fractional knapsack - Problem - 2
6. Minimum cost spanning tree, Kruskals Algorithm
What is a spanning tree?
Minimum spanning tree
Examples
Kruskal’s Algorithm
7. Dijikstras algorithm
Algorithm
Example of Dijkstra’s algorithm
Djikstras Algorithm - Analysis
it involves breaking down a program into smaller, manageable parts while abstracting away the
intricate details of how those parts work internally.
2. '
Once upon a time, in an enchanted forest, a Flaming Hawk soared high above the
treetops.
It spotted a Hungry Animal munching on Berries.
It was an Eager Cheetah chasing after a Deer.
Nearby, was a Daring Guard protecting the forest from a Elephant known for causing
trouble.
In this magical world, there was a golden rule: All Dragons Eat Honey. These mystical
creatures thrived on a sweet diet of honey. They often said, Bravery Doesn’t Go Hungry.
Those who dared to take risks and face challenges head-on always found themselves
well-fed and content, much like the cheetah who cleverly balanced its diet between berries
and other forest offerings.
And lastly, everyone knew the simple yet profound truth: All Cats Eats Fish. The cats,
with their sharp instincts, would always find their way to the freshest fish, symbolizing
resourcefulness and the rewards of patience.
Recurrence relation
3. Two way merge sort
What is merge sort?
Divide Operation
Merge Operation
10
10
Merge 2
Merging 27,38 and 3,43
2,27,38,43
Merging 9,82,10
9,10,82
Merge 3
3,9,10,27,38,43,82
Algorithm
Comparing x and y
-y<x
- Add 7 to the array
- Update y and i by 1 position
x and y compared
-x<y
- 8 added, x and i updated
x and y compared
-y<x
- 10 added
- y and i position updated
Here, Given
Maximum capacity m = 60
Number of items n = 4
i = {1, 2, 3, 4}
Profit values are = {280,100,120,120}
Weight values are = {40,10,20,24}
Profit/Weight values are = {7, 10, 6, 5}
i Pi Wi Xi U = U-Wi
2 100 10
1 280 40
3 120 20
4 120 24
i Pi Wi Xi U = U-Wi
2 100 10 0
1 280 40 0
3 120 20 0
4 120 24 0
i=1
U = 60
Wi = 10
Completely place the weight
set Xi = 1
Balance knapsack capacity is U - Wi = 60 - 10 = 50
i Pi Wi Xi U = U-Wi
2 100 10 1 50
1 280 40 0
3 120 20 0
4 120 24 0
i=2
U = 50
Wi = 40
Completely Place the weight
set Xi = 1
U = U - Wi = 50 - 40 = 10
i Pi Wi Xi U = U-Wi
2 100 10 1 50
1 280 40 1 10
3 120 20 0
4 120 24 0
i=3
U = 10
Wi = 20
We cant completely place the weight, Weight is 20, but the capacity is only 10
We can only place a fraction, its calculated by
U/Wi = 10/20 = 1/2
Xi = 1/2
U = U-Wi = 10 - 10 = 0
i Pi Wi Xi U = U-Wi
2 100 10 1 50
1 280 40 1 10
3 120 20 1/2 0
4 120 24 0
i=4
Solution
Its basically a subset of a graph, which has all vertices covered with minimum number of
edges
Here we can see, we got 3 spanning tree from a graph, and all vertices are covered
Some points
Spanning tree has n-1 edges, where n is the number of nodes
Adding one edge to spanning tree will create a loop
Removing one edge from spanning tree will make the graph disconnected
A Spanning tree doesnt have any loops
All spanning tree has same number of edges and vertices
A graph can have more than one spanning tree
Total number of spanning tree possible for complete graph with n vertices = n^(n-2)
A minimum spanning tree is a spanning tree that has minimum weight than all other
spanning trees of the same graph
Examples
The cost is the weights added up together
We have a condition, 0 should be a leaf node
So we start with 0
Set distance of S to 0
The minimum distance vertex now is S
So, remove it, marking it as blue
Among the nodes, the node with minimum distance is Node B with 3.
Removing Node B
Neighbours of B
D
Cost from D = 3 + 4 = 7, Which is equal to previous distance 7, not updating
Bottom row denotes the previous node
Among the nodes, A is the node with minimum distance 7
Removing Node A
Neighbour of A = C
Distance from S = 1 +4 = 5 < infinity
Setting Distance A = 1, and previous = A
Neighbours of G are T
Distance of T = 8 + 3 =11, Which is more than existing distance, no change required
Smallest Node is T with 10
Removing T
Neighbour of T is F
Distance of F is 10 + 5 = 15 which is more than existing distance, so not changing
Last node is F, Removing the node
F has no neighbouring nodes
Calculating path and distance from the table
To get path from S to E
Previous of E = C
Previous of C = A
Previous of A = S
We get the path = S-A-C-E