Professional Documents
Culture Documents
Time complexity:
21
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
return result;
}
return result;
}
22
vector<vector<int>> A12(newSize, vector<int>(newSize, 0));
vector<vector<int>> A21(newSize, vector<int>(newSize, 0));
vector<vector<int>> A22(newSize, vector<int>(newSize, 0));
// Populate submatrices
for (int i = 0; i < newSize; ++i) {
for (int j = 0; j < newSize; ++j) {
A11[i][j] = A[i][j];
A12[i][j] = A[i][j + newSize];
A21[i][j] = A[i + newSize][j];
A22[i][j] = A[i + newSize][j + newSize];
B11[i][j] = B[i][j];
B12[i][j] = B[i][j + newSize];
B21[i][j] = B[i + newSize][j];
B22[i][j] = B[i + newSize][j + newSize];
}
}
// newSize is already halved during the recursive calls in the Strassen algorithm
23
for (int i = 0; i < newSize; ++i) {
for (int j = 0; j < newSize; ++j) {
result[i][j] = C11[i][j];
result[i][j + newSize] = C12[i][j];
result[i + newSize][j] = C21[i][j];
result[i + newSize][j + newSize] = C22[i][j];
}
}
return result;
}
int main() {
// Example usage
vector<vector<int>> A = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
vector<vector<int>> B = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
return 0;
}
24
OUTPUT:
25
Week 3
Program 5: Program to find the shortest path using Floyd’s algorithm.
Description:
The Floyd Warshall Algorithm is an all pair shortest path algorithm
unlike Dijkstra and Bellman Ford which are single source shortest path algorithms. This
algorithm works for both the directed and undirected weighted graphs. But, it does not work
for the graphs with negative cycles (where the sum of the edges in a cycle is negative). It
follows Dynamic Programming approach to check every possible path going via every
possible node in order to calculate shortest distance between every pair of nodes.
ALGORITHM:
Step 1 − Construct an adjacency matrix A with all the costs of edges present in the graph. If
there is no path between two vertices, mark the value as ∞.
Step 2 − Derive another adjacency matrix A1 from A keeping the first row and first column
of the original adjacency matrix intact in A1. And for the remaining values, say A1[i,j],
if A[i,j]>A[i,k]+A[k,j] then replace A1[i,j] with A[i,k]+A[k,j]. Otherwise, do not change the
values. Here, in this step, k = 1 (first vertex acting as pivot).
Step 3 − Repeat Step 2 for all the vertices in the graph by changing the k value for every
pivot vertex until the final matrix is achieved.
Step 4 − The final adjacency matrix obtained is the final solution with all the shortest paths.
Time Complexity :
Best Case: O(V3)
• In the best-case scenario, the algorithm completes all iterations of the nested loops
without any relaxation steps needed.
• With V vertices, each loop iterates V times, resulting in a time complexity
of O(V3) for the best case.
Average Case: O(V3)
• The average-case time complexity of the Floyd-Warshall algorithm is also O(V3).
• This complexity holds true across various graph structures and densities, as the
algorithm’s performance primarily depends on the number of vertices and the number
of iterations needed to compute shortest paths between all pairs of vertices.
Worst Case: O(V3)
• Conversely, in the worst-case scenario, the algorithm performs relaxation steps for
each pair of vertices during all iterations of the nested loops.
• With V vertices, each loop iterates V times, resulting in a time complexity
of O(V3) for the worst case as well.
26
SOURCE CODE:
#include<bits/stdc++.h>
# define INF 10000000
using namespace std;
27
OUTPUT:
28
Program 6: Program to solve the knapsack problem using greedy method.
Description:
The basic idea of the greedy approach is to calculate the ratio profit/weight for each item and
sort the item on the basis of this ratio. Then take the item with the highest ratio and add them
as much as we can (can be the whole element or a fraction of it).
This will always give the maximum profit because, in each step it adds an element such that
this is the maximum possible profit for that much weight.
ALGORITHM:
• Calculate the ratio (profit/weight) for each item.
• Sort all the items in decreasing order of the ratio.
• Initialize res = 0, curr_cap = given_cap.
• Do the following for every item i in the sorted order:
• If the weight of the current item is less than or equal to the remaining capacity then
add the value of that item into the result
• Else add the current item as much as we can and break out of the loop.
• Return res.
Time Complexity:
• The greedy algorithm sorts the items based on their value-to-weight ratio and selects
items greedily until the knapsack is full.
• Sorting the items takes O(n log n) time, where n is the number of items.
• After sorting, selecting items and calculating the fractional values can be done in
linear time, O(n).
• Therefore, the overall time complexity of the greedy approach is O(n log n) due to
sorting.
Greedy Algorithm of Knapsack : O(n log n)
29
SOURCE CODE:
#include<bits/stdc++.h>
using namespace std;
30
OUTPUT:
31