Daaaa
Daaaa
Daaaa
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 9.
Object: Write a program to implement Counting sort.
#include <iostream>
#include <vector>
// Update the count array to store the cumulative count of each element
for (int i = 1; i <= max_element; ++i) {
count[i] += count[i - 1];
}
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
}
int main() {
std::vector<int> arr = {4, 2, 2, 8, 3, 3, 1};
std::cout << "Original array:";
for (int num : arr) {
std::cout << " " << num;
}
std::cout << std::endl;
countingSort(arr);
return 0;
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
OUTPUT:-
Program 10.
Object: Write a program to implement radix sort.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> arr = {170, 45, 75, 90, 802, 24, 2, 66};
std::cout << "Original array:";
for (int num : arr) {
std::cout << " " << num;
}
std::cout << std::endl;
radixSort(arr);
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
return 0;
}
OUTPUT:-
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\"
; if ($?) { g++ redix.cpp -o redix } ; if ($?) { .\redix }
Original array: 170 45 75 90 802 24 2 66
Sorted array: 2 24 45 66 75 90 170 802
PS C:\Users\mukul\OneDrive\Desktop\New folder>
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 11.
Object: Write a program to implement BUCKET sort.
#include <iostream>
#include <vector>
#include <algorithm>
// Create buckets
std::vector<std::vector<double>> buckets(n);
int main() {
int n;
std::cout << "Enter the number of elements: ";
std::cin >> n;
std::vector<double> arr(n);
std::cout << "Enter the elements (between 0 and 1): ";
for (int i = 0; i < n; i++) {
std::cin >> arr[i];
}
bucketSort(arr);
return 0;
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
OUTPUT:-
Program 12.
Object: Write a program to Implement Longest Common Subsequence.
Code:
#include <iostream>
#include <cstring> // for memset
using namespace std;
int i = n, j = m;
while (i > 0 && j > 0) {
if (a[i - 1] == b[j - 1]) {
lcs[index - 1] = a[i - 1];
i--;
j--;
index--;
} else if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
cout << "Total length of LCS is: " << dp[n][m] << endl;
cout << "LCS of " << a << " and " << b << " is " << lcs << endl;
}
int main() {
string a, b;
cout << "Enter string 1: " << endl;
cin >> a;
cout << "Enter string 2: " << endl;
cin >> b;
int n = a.length();
int m = b.length();
lcs(a, b, n, m);
return 0;
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
lcs.cpp - lcs } ; if ($?) { .\lcs }
Enter string 1:
abcd
Enter string 2:
defgabc
Total length of LCS is: 3
LCS of abcd and defgabc is abc
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 13.
Object: Write a program to implement matrix chain multiplication.
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
int main() {
vector<int> dimensions = {10, 30, 5, 60};
int n = dimensions.size();
cout << "Minimum number of scalar multiplications needed: " << matrixChainOrder(dimensions, n)
<< endl;
return 0;
}
OUTPUT :-
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\"
; if ($?) { g++ BUCKET.cpp -o BUCKET } ; if ($?) { .\BUCKET }
Minimum number of scalar multiplications needed: 4500
PS C:\Users\mukul\OneDrive\Desktop\New folder>
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 14.
Object: WAP to Implement Travelling Salesman Problem
Code:
// CPP program to implement traveling salesman
// problem using naive approach.
#include <bits/stdc++.h>
using namespace std;
#define V 4
// update minimum
min_path = min(min_path, current_pathweight);
} while (
next_permutation(vertex.begin(), vertex.end()));
return min_path;
}
// Driver Code
int main()
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
OUTPUT:
Program 15.
Object: WAP to Implement 01 knapsack problem using Dynamic programming.
Code:
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5;
if (wt[n] > W)
{
dp[n][W] = knapsack(W, wt, val, n - 1);
return dp[n][W];
}
else
{
dp[n][W] = max(knapsack(W, wt, val, n - 1), val[n] + knapsack(W - wt[n], wt, val, n - 1));
return dp[n][W];
}
}
int main()
{
memset(dp, -1, sizeof(dp));
cout << "Enter number of elements " << endl;
int n, W;
cin >> n;
int w[n], v[n];
cout << "Weight of every elements " << endl;
for (int i = 0; i < n; i++)
{
cin >> w[i];
}
cout << "Value of every elements respective to weights: " << endl;
for (int i = 0; i < n; i++)
{
cin >> v[i];
}
cout << "Total Weights: " << endl;
cin >> W;
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
knap.cpp -o knap } ; if ($?) { .\knap }
Program 16.
Object: Write a program to implement Fractional k - napsack.
#include <iostream>
#include <vector>
#include <algorithm>
return totalValue;
}
int main() {
int capacity, n;
cout << "Enter the capacity of the knapsack: ";
cin >> capacity;
vector<Item> items;
cout << "Enter weight and value for each item:" << endl;
for (int i = 0; i < n; ++i) {
int weight, value;
cin >> weight >> value;
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
items.push_back(Item(weight, value));
}
return 0;
}
OUTPUT:-
Enter the capacity of the knapsack: 12
Enter the number of items: 5
Enter weight and value for each item:
1
2
5
6
8
8
9
4
5
10
Maximum value in the knapsack: 19
PS C:\Users\mukul\OneDrive\Desktop\New folder>
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 17.
Object: Write a program to implement BIN tracking.
#include <iostream>
#include <vector>
int main() {
vector<int> items = {4, 8, 1, 4, 2, 5};
int binCapacity = 10;
cout << "Bin capacity: " << binCapacity << endl;
firstFit(items, binCapacity);
return 0;
}
OUTPUT:-
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\"
; if ($?) { g++ BIN.CPP -o BIN } ; if ($?) { .\BIN }
Bin capacity: 10
Number of bins used: 3
Items packed into bins:
Bin 1: 9
Bin 2: 10
Bin 3: 5
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 18.
Object: Write a program to implement DFS.
Code:
#include <bits/stdc++.h>
using namespace std;
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
dfs.cpp -o dfs } ; if ($?) { .\dfs }
56
01
02
13
14
24
34
DFS of Given Graph is as follows:
01342
PS C:\Users\mukul\OneDrive\Desktop\New folder>
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 19.
Object: Write a program to implement BFS.
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int V, E;
cin >> V >> E;
vector<int> adj[V];
for (int i = 0; i < E; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
}
vector<int> ans = bfsOfGraph(V, adj);
cout << "BFS of Given Graph is as follows" << endl;
for (int i = 0; i < ans.size(); i++)
{
cout << ans[i] << " ";
}
cout << endl;
return 0;
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
bfs.cpp -o bfs } ; if ($?) { .\bfs }
56
01
02
13
14
24
34
BFS of Given Graph is as follows
01234
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 20.
Object: Write a program to implement Prim's algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of weights of edges of the Minimum Spanning Tree.
void spanningTree(int V, vector<vector<int>> adj[])
{
int n = V;
int parent[n];
int keys[n];
bool mstSet[n];
for (int i = 0; i < n; i++)
{
parent[i] = -1;
keys[i] = INT_MAX;
mstSet[i] = false;
}
keys[0] = 0;
parent[0] = -1;
for (int i = 0; i < n - 1; i++)
{
int mini = INT_MAX, u;
Program 21.
Object: Write a program to implement Kruskal's Algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int u;
int v;
int wt;
node(int first, int second, int weight)
{
u = first;
v = second;
wt = weight;
}
};
bool comp(node a, node b)
{
return a.wt < b.wt;
}
int findPar(int u, vector<int> &parent)
{
if (u == parent[u])
return u;
return parent[u] = findPar(parent[u], parent);
}
void unionn(int u, int v, vector<int> &parent, vector<int> &rank)
{
u = findPar(u, parent);
v = findPar(v, parent);
if (rank[u] < rank[v])
{
parent[u] = v;
}
else if (rank[v] < rank[u])
{
parent[v] = u;
}
else
{
parent[v] = u;
rank[u]++;
}
}
void kruskals(vector<node> edges, int N)
{
sort(edges.begin(), edges.end(), comp);
vector<int> parent(N);
for (int i = 0; i < N; i++)
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
parent[i] = i;
vector<int> rank(N, 0);
int cost = 0;
vector<pair<pair<int, int>, int>> mst;
for (auto it : edges)
{if (findPar(it.v, parent) != findPar(it.u, parent)){cost += it.wt;mst.push_back({{it.u,
it.v},it.wt});unionn(it.u, it.v, parent, rank);}}
cout << "Edge \tWeight\n";
for (auto it : mst)
cout << it.first.first << " - " << it.first.second << " \t" << it.second << " \n";
cout << "Total weight of MST" << endl;
cout << cost << endl;
}
int main()
{
int N, m;
cin >> N >> m;
vector<node> edges;
for (int i = 0; i < m; i++)
{
int u, v, wt;
cin >> u >> v >> wt;
edges.push_back(node(u, v, wt));
}
kruskals(edges, N);
return 0;
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
kruskal.cpp -o kruskal } ; if ($?) { .\kruskal }
57
012
036
123
138
145
247
349
Edge Weight
0-1 2
1-2 3
1-4 5
0-3 6
Total weight of MST
16
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 22.
Object: Write a program to implement Dijkstra algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;
vector<int> dijkstra(int n, vector<vector<int>> adj[], int src)
{
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> distTo(n, INT_MAX);
distTo[src] = 0;
pq.push(make_pair(0, src));
while (!pq.empty())
{
int dist = pq.top().first;
int prev = pq.top().second;
pq.pop();
for (auto it : adj[prev])
{
int next = it[0];
int nextDist = it[1];
int V, E;
cin >> V >> E;
vector<vector<int>> adj[V];
int i = 0;
while (i++ < E)
{
int u, v, w;
cin >> u >> v >> w;
vector<int> t1, t2;
t1.push_back(v);
t1.push_back(w);
adj[u].push_back(t1);
t2.push_back(u);
t2.push_back(w);
adj[v].push_back(t2);
}
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
int S;
cin >> S;
vector<int> res = dijkstra(V, adj, S);
cout << "Distance from source to each node: " << endl;
for (int i = 0; i < V; i++)
cout << "0->" << res[i] << " ";
cout << endl;
return 0;
}
OUTPUT:
Program 23.
Object: Write a program to implement Bellman ford algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int u, v, wt;
node(int f, int s, int wait)
{
u = f;
v = s;
wt = wait;
}
};
void bellmanFord()
{
int N, m;
cin >> N >> m;
vector<node> edges;
for (int i = 0; i < m; i++)
{
int u, v, wt;
cin >> u >> v >> wt;
edges.push_back(node(u, v, wt));
}
int src;
cin >> src;
int inf = 1e9;
vector<int> dist(N, inf);
dist[src] = 0;
for (int i = 0; i <= N - 1; i++)
{
for (auto it : edges)
{
if (dist[it.u] + it.wt < dist[it.v])
{
dist[it.v] = dist[it.u] + it.wt;
}
}
}
int fl = 0;
for (auto it : edges)
{
if (dist[it.u] + it.wt < dist[it.v])
{
cout << "NegativeCycle" << endl;
fl = 1;
break;
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
}
}
if (!fl)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < N; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
}
int main()
{
bellmanFord();
return 0;
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
21
019
0
Vertex Distance from Source
0 0
1 9
PS C:\Users\mukul\OneDrive\Desktop\New folder>
COLLEGE OF TECHNOLOGY AND ENGINEERING
NAME :- Mukul Bharat SUBJECT :- CS 362 (PCC): DESIGN & ANALYSIS OF ALGORITHMS
Program 24.
Object: Write a program to implement Ford Fulkerson algorithm.
Code:
#include <bits/stdc++.h>
using namespace std;
#define V 6
bool bfs(int rGraph[V][V], int s, int t, int parent[])
{
bool visited[V];
memset(visited, 0, sizeof(visited));
queue<int> q;
q.push(s);
visited[s] = true;
parent[s] = -1;
while (!q.empty())
{
int u = q.front();
q.pop();
{
u = parent[v];
path_flow = min(path_flow, rGraph[u][v]);
}
for (v = t; v != s; v = parent[v])
{
u = parent[v];
rGraph[u][v] -= path_flow;
rGraph[v][u] += path_flow;
}
return 0;
}
OUTPUT:
PS C:\Users\mukul\OneDrive\Desktop\New folder> cd "c:\Users\mukul\OneDrive\Desktop\New folder\" ; if ($?) { g++
ford.cpp -o ford } ; if ($?) { .\ford }