Algo
Algo
#include <iostream>
using namespace std;
int main()
{
cout << "Arriyaan Ali Syed\n";
int arr[100], size;
int item, loc;
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter the elements of the array: " << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "Enter item to search: " << endl;
cin >> item;
for (loc = 0; loc <= size; loc++)
{
if (arr[loc] == item)
{
cout << "element found at position:" << loc + 1 << endl;
return 0;
}
}
cout << "element not found!";
return 0;
}
Output:
Program 02: Write a program to perform Binary Search.
Code:
#include <iostream>
using namespace std;
int main()
{
int size, n;
int arr[100];
cout << "Arriyaan Ali Syed\n";
cout << "Enter the size of the array: ";
cin >> size;
cout << "Enter the elements of the sorted array: " << endl;
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
cout << "Enter the element to search: ";
cin >> n;
int beg = 0, end = size - 1, mid;
while (beg <= end)
{
mid = (beg + end) / 2;
if (arr[mid] == n)
{
cout << "Element found at position: " << mid + 1 << endl;
return 0;
}
if (arr[mid] < n) {
beg = mid + 1;
}
else {
end = mid - 1;
}
}
cout << "Element not found!" << endl;
return 0;
}
Output:
Program 03: Write a program to perform Bubble Sort.
Code:
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Arriyaan Ali Syed\n";
int arr[n];
bubbleSort(arr, n);
return 0;
}
Output:
Program 04: Write a program to perform Insertion Sort.
Code:
#include <iostream>
using namespace std;
int main() {
int size, a[100];
cout << "Arriyaan Ali Syed\n";
insert(a, size);
return 0;
}
Output:
Program 05: Write a program to perform Merge Sort.
Code:
#include <iostream>
using namespace std;
return 0;
}
Output:
Program 06: Write a program to perform Quick Sort.
Code:
#include <iostream>
using namespace std;
int main() {
cout << "Arriyaan Ali Syed\n";
int n;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
quickSort(arr, 0, n - 1);
return 0;
}
Output:
Program 07: Write a program to perform Heap Sort.
Code:
#include <iostream>
using namespace std;
if (largest != i) {
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
}
int main() {
int n;
cout << "Arriyaan Ali Syed\n";
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
heapSort(arr, n);
return 0;
}
Output:
Program 08: Write a program to perform Counting Sort.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cout << "Arriyaan Ali Syed\n";
cout << "Enter the number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
countingSort(arr);
return 0;
}
Output:
Program 09: Write a program to perform Radix Sort.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cout << "Arriyaan Ali Syed\n";
cout << "Enter the number of elements: ";
cin >> n;
vector<int> arr(n);
cout << "Enter the elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
radixSort(arr);
return 0;
}
Output:
Program 10: Write a program to perform Bucket Sort.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
int index = 0;
for (int i = 0; i < n; i++) {
for (size_t j = 0; j < buckets[i].size(); j++) {
arr[index++] = buckets[i][j];
}
}
}
int main() {
int n;
cout << "Arriyaan Ali Syed\n";
cout << "Enter the number of elements: ";
cin >> n;
vector<float> arr(n);
cout << "Enter " << n << " floating-point numbers between 0 and 1: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
bucketSort(arr);
return 0;
}
Output:
Program 11: Write a program to perform Prim’s Algorithm.
Code:
#include <iostream>
#include <vector>
#include <limits.h>
key[0] = 0;
int main() {
int V;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of vertices: ";
cin >> V;
primMST(graph, V);
return 0;
}
Output:
Program 12: Write a program to perform Kruskal’s Algorithm.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
struct Edge {
int src, dest, weight;
};
struct Subset {
int parent, rank;
};
int e = 0, i = 0;
while (e < V - 1 && i < edges.size()) {
Edge nextEdge = edges[i++];
int x = find(subsets, nextEdge.src);
int y = find(subsets, nextEdge.dest);
if (x != y) {
result.push_back(nextEdge);
unionSets(subsets, x, y);
e++;
}
}
int main() {
int V, E;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of vertices and edges: ";
cin >> V >> E;
vector<Edge> edges(E);
cout << "Enter edges (src dest weight):\n";
for (int i = 0; i < E; i++)
cin >> edges[i].src >> edges[i].dest >> edges[i].weight;
kruskalMST(edges, V);
return 0;
}
Output:
Program 13: Write a program to perform 0/1 Knapsack Algorithm.
Code:
#include <iostream>
#include <vector>
return dp[n][W];
}
int main() {
int n, W;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of items: ";
cin >> n;
cout << "Enter knapsack capacity: ";
cin >> W;
return 0;
}
Output:
Program 14: Write a program to perform Fractional Knapsack Algorithm.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
struct Item {
int value, weight;
};
return totalValue;
}
int main() {
int n, W;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of items: ";
cin >> n;
cout << "Enter knapsack capacity: ";
cin >> W;
vector<Item> items(n);
cout << "Enter values and weights of items:\n";
for (int i = 0; i < n; i++)
cin >> items[i].value >> items[i].weight;
return 0;
}
Output:
Program 15: Write a program to perform LCS Algorithm.
Code:
#include <iostream>
#include <vector>
return dp[m][n];
}
int main() {
cout << "Arriyaan Ali Syed\n";
string X, Y;
cout << "Enter first string: ";
cin >> X;
cout << "Enter second string: ";
cin >> Y;
return 0;
}
Output:
Program 16: Write a program to perform BFS Algorithm.
Code:
#include <iostream>
#include <vector>
#include <queue>
class Graph {
int V;
vector<vector<int>> adj;
public:
Graph(int V) {
this->V = V;
adj.resize(V);
}
visited[start] = true;
q.push(start);
int main() {
cout << "Arriyaan Ali Syed\n";
int V, E;
cout << "Enter number of vertices and edges: ";
cin >> V >> E;
Graph g(V);
int start;
cout << "Enter starting vertex for BFS: ";
cin >> start;
g.BFS(start);
return 0;
}
Output:
Program 17: Write a program to perform DFS Algorithm.
Code:
#include <iostream>
#include <vector>
class Graph {
int V;
vector<vector<int>> adj;
public:
Graph(int V) {
this->V = V;
adj.resize(V);
}
Graph g(V);
int start;
cout << "Enter starting vertex for DFS: ";
cin >> start;
g.DFS(start);
return 0;
}
Output:
Program 18: Write a program to perform Dijkstra’s Algorithm.
Code:
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
class Graph {
int V;
vector<vector<pii>> adj;
public:
Graph(int V) {
this->V = V;
adj.resize(V);
}
pq.push({0, src});
dist[src] = 0;
while (!pq.empty()) {
int u = pq.top().second;
pq.pop();
cout << "Vertex Distance from Source " << src << endl;
for (int i = 0; i < V; i++)
cout << i << " \t\t " << dist[i] << endl;
}
};
int main() {
int V, E;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of vertices and edges: ";
cin >> V >> E;
Graph g(V);
int start;
cout << "Enter starting vertex for Dijkstra's algorithm: ";
cin >> start;
g.dijkstra(start);
return 0;
}
Output:
Program 19: Write a program to perform Bellman-Ford Algorithm.
Code:
#include <iostream>
#include <vector>
#include <climits>
struct Edge {
int src, dest, weight;
};
class Graph {
int V, E;
vector<Edge> edges;
public:
Graph(int V, int E) {
this->V = V;
this->E = E;
}
cout << "Vertex Distance from Source " << src << endl;
for (int i = 0; i < V; i++)
cout << i << " \t\t " << dist[i] << endl;
}
};
int main() {
int V, E;
cout << "Arriyaan Ali Syed\n";
cout << "Enter number of vertices and edges: ";
cin >> V >> E;
int start;
cout << "Enter starting vertex for Bellman-Ford algorithm: ";
cin >> start;
g.bellmanFord(start);
return 0;
}
Output:
Program 20: Write a program to perform Ford-Fulkerson Algorithm.
Code:
#include <iostream>
#include <vector>
#include <climits>
#include <queue>
// BFS to check if there is a path from source to sink in the residual graph
bool bfs(vector<vector<int>>& residualGraph, int source, int sink, vector<int>& parent) {
vector<bool> visited(V, false);
queue<int> q;
q.push(source);
visited[source] = true;
parent[source] = -1;
while (!q.empty()) {
int u = q.front();
q.pop();
// Ford-Fulkerson Algorithm
int fordFulkerson(vector<vector<int>>& graph, int source, int sink) {
vector<vector<int>> residualGraph = graph;
vector<int> parent(V);
int maxFlow = 0;
maxFlow += pathFlow;
}
return maxFlow;
}
int main() {
cout << "Arriyaan Ali Syed\n";
vector<vector<int>> graph = {
{0, 16, 13, 0, 0, 0},
{0, 0, 10, 12, 0, 0},
{0, 4, 0, 0, 14, 0},
{0, 0, 9, 0, 0, 20},
{0, 0, 0, 7, 0, 4},
{0, 0, 0, 0, 0, 0}
};
cout << "The maximum possible flow is: " << fordFulkerson(graph, source, sink) << endl;
return 0;
}
Output:
Program 21: Write a program to perform TSP Algorithm.
Code:
#include <iostream>
#include <vector>
#include <climits>
int main() {
cout << "Arriyaan Ali Syed\n";
for (int i = 0; i < N; i++)
for (int j = 0; j < (1 << N); j++)
dp[i][j] = -1; // Initialize DP table with -1
cout << "The minimum cost of visiting all cities is: " << tsp(0, 1) << endl;
return 0;
}
Output:
Program 22: Write a program to perform Floyd-Warshall Algorithm.
Code:
#include <iostream>
#include <vector>
#include <climits>
floydWarshall(graph);
return 0;
}
Output:
S.No. Name of Program Date of Date of Sign
Execution Submission