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

Algorithm Analysis Module 3 Important Topics

Uploaded by

erinpaul12112002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Algorithm Analysis Module 3 Important Topics

Uploaded by

erinpaul12112002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

Algorithm-Analysis-Module-3-Important-

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

1. Divide and conquer-control abstraction


By control abstraction we mean a procedure whose flow of control is clear but whose
primary operations are specified by other procedures whose precise meaning is left
undefined

What is divide and conquer


Control abstraction

it involves breaking down a program into smaller, manageable parts while abstracting away the
intricate details of how those parts work internally.

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

1. We need to compute the product of 2 nxn matrices A and B


2. Assume n is power of 2
3. Partition A and B into 4 square matriices, each of size n/2 x n/2
4. AB can be computed using the formula

C11 = A11 x B11 + A12 x B21


C12 = A11 x B12 + A12 x B22
C21 = A21 x B11 + A22 x B21
C22 = A21 x B12 + A22 x B22

Divide and conquer Matrix multiplication - Complexity


There are a total of 8 matrix multiplications here

8 recursive calls on n/2 matrix


Addition of matrices take O(n^2) time

Theres no difference in the complexity, so divide and conquer here is of no use

Strassens Matrix Multiplication Algorithm

1. A and B are matrices with dimension nxn


2. If n is not power of 2, add rows and columns of 0s to make the dimensions the power of 2
3. Partition A and B into 4 square matrices n/2 x n/2

1. a,b,c and d are sub matrices of A, size m/2 x n/2


2. e,f,g,h are sub matrices of B, size n/2 x n/2
4. Compute the 7 matrices P1 to P7
1.

2. '

Trick to learn Strassen Multiplication formula

1. a Flaming Hawk (P1): a(f−h)


2. Hungry Animal and Berries (P2): h(a+b)
3. Eager Cheetah and Deer (P3): e(c+d)
4. Daring Guard and Elephant (P4): d(g−e)
5. All Dragons Eat Honey (P5): (a+d)(e+h)
6. Bravery Doesn't Go Hungry (P6): (b−d)(g+h)
7. All Cats Eats Fish (P7): (a−c)(e+f)

Story Based on the above

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.

Example of Strassens multiplication algorithm

We get the following values for a to h

Applying the equations..


Strassens matrix multiplication complexity

As we saw before, there are 7 matrix multiplications and 10 matrix addition/subtractions


Addition/subtraction takes O(n^2) time
Multiplication takes T(n/2)

O(n^2.81) is better than O(n^3)

Recurrence relation
3. Two way merge sort
What is merge sort?

Sorting using merge sort

Consider the below Example,


We have an Array 38,27,43,3,9,82,10 and our goal here is to sort it

Divide Operation

First we have an unsorted array


38,27,43,3,9,82,10
We divide the unsorted array to almost 2 pieces
Here the two pieces are
38,27,43,3
9,82,10

We Further divide the array into equal pieces


38,27
43,3
9,82
10
We divide it again and get individual pieces
38
27
43
3
9
82
10

Merge Operation

During merge operation, The Steps we did earlier are reversed


Merge 1
Merging 38 and 27
Sorting, we get 27,38
Merging 43, and 3
3,43
Merging 9,82
9,82

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

Merge sort algorithm


Merge Algorithm

Algorithm

Applying the algorithm

x = first arrays starting index


y = second arrays starting index
i = final arrays starting index
1. Compare x and y
1. Here x is 5 and y is 2
2. y < x
3. Add y (2) to final array
1. Update i and y

Compare x and y again


x<y
Add 5 to final array
update i and x by 1 position

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

Second array is finished completely


We can place the remaining things from x into the array
- Added 10 and 12, Updating positions

Time complexity of merge sort


4. Greedy approach
The Control Abstraction of Greedy Strategy
The argument is array A with n inputs
Solution is set to null
Forloop iterates n times
Select function is used to select one item from a
The item is assigned to x
Check if x is feasible
If feasible add it to solution, otherwise discard x

5. Fractional knapsack problem


What is Fractional Knapsack Problem?

This problem can be solved using greedy strategy

We have a knapsack or bag of capacity m


n is the number of objects
Wi is the weight of object i
Pi is the profit of object i
Xi - Fraction of ith object placed in the knapsack
PiXi - Profit earned from ith object
The objective is to obtain an optimal solution of the knapsack that maximises the total
profit earned
Total weight of all chosen objects should not be more than m

Fractional knapsack problem - Algorithm


We arrange the objects in descending order of profit/weight

1. Initially x values are set to 0


2. Set knapsack capacity to U
3. if current object weight > Balance capacity in knapsack
1. Break
4. Else
1. set x value as 1
2. Subtract the capacity with the current weight
5. There are 2 cases
1. When i<n (When loop is broken)
1. Assign the remaining weight to x by U/w[i]
2. When i = n+1 (When loop is not broken)
1. Nothing

Fractional knapsack problem - Time Complexity

For loop will execute maximum n times


Time complexiy = O(n)
Fractional knapsack - Problem -1

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}

Now we to sort the Profit/Weight in descending order

We will get {10,7,6,5}


Arrange the corresponding values of i, profit and weight

Now place these data in a table, in the descending order


Here
i is item number
Pi Profit
Wi Weight
Xi is the fraction
U is the knapsack balance capacity

i Pi Wi Xi U = U-Wi
2 100 10
1 280 40
3 120 20
4 120 24

Initially our knapsack balance capacity is the total capacity m = 60

Initially, set all fraction to 0

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

No more capacity, capacity is 0, so stopping here

Solution

Total Profit = Sum of (profit x Fraction)


= 100 x 1 + 280 x 1 + 120 x 1/2 = 440
Solution vector X = {1,1,1/2,0} (Obtained from Xi)

Fractional knapsack - Problem - 2


Sorting in descending order...

We will get the table as


We will get the solution as..
6. Minimum cost spanning tree, Kruskals Algorithm
What is a spanning tree?

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)

Minimum spanning tree

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

Next smallest weight is 2 (from 3 to 4)


Next smallest is 3 (from 4 to 2)

Next smallest is 4 (from 1 to 3)


Kruskal’s Algorithm

Builds tree edge by edge


Edges are considered in the increasing order of cost
If the selected edge forms a cycle, discard
The selection process continues until there are n-1 edges
7. Dijikstras algorithm
Algorithm

During initialise single source


Setting distance of all vertices to infinite
Setting parent of all the vertices to null
Setting distance of Source to 0
During Relax(u,v,w)
We are updating the newer distance if older distance is larger and updating the
parent
During the main algorithm
We call initalize single source
Setting Q to be the set of all vertices in G
While Q is not empty
u is set to the minimum distance in Q
Removing u from Q
For each vertex v in adjacent(u), call the RELAX function
Example of Dijkstra’s algorithm

Set all vertices to infinity distance and null previous

Set distance of S to 0
The minimum distance vertex now is S
So, remove it, marking it as blue

Get the neighbour nodes of S


A, D and B
Distance from A = 4 < Infinity
Setting Distance of A = 4, previous node = S
Distance from D = 7 < infinity
Setting Distance of D = 7, previous node = S
Distance from B = 3 < infinity
- Setting Distance of B = 3, previous node = S

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

The node with minimum distance is C with 5


Removing node C
Neighbours of C are
E and D
Distance of E = 5+1 = 6 < Infinity
Setting distance = 6 and previous = C
Distance of D = 7, same as previous value, not changing
The minimum distance among E and D is E with distance 6
Removing E

Neighbours of E are G and T


Distance of G = 6 + 2 = 8 < infinity
Setting distance and previous to 8, E
Distance of T = 6+4 < infinity
Setting distance and previous = 10,E
Among these nodes, D is the minimum with 7
Removing Node D
Neighbours of D are
T and F
Distance of T is 7 + 3 = 10, Which is same as previous distance
Distance of F is 7+5 = 12 < infinity
Setting distance of F to 12, and previous to D
Among the nodes, G has the least distance with 8
Removing G

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

Djikstras Algorithm - Analysis

You might also like