0% found this document useful (0 votes)
7 views23 pages

ADA Lab Abhi

Ritiekfkwkfkwkc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views23 pages

ADA Lab Abhi

Ritiekfkwkfkwkc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

ABHISHEK KUMAR

A230522412

LAB-01
Aim : Implementation of various sorting techniques.
 Bubble Sort

Code:

#include <iostream>
using namespace std;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
bool swapped = false;
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = true; } }
if (!swapped)
break; }}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int 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]; }
bubbleSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
ABHISHEK KUMAR
A230522412

return 0; }

 Insertion Sort
Code:
#include <iostream>
using namespace std;
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1; }
arr[j + 1] = key; } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
ABHISHEK KUMAR
A230522412

cin >> arr[i]; }


insertionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0; }

 Selection Sort
Code:
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
int k = i;
for (int j = i+1; j < n; j++) {
if (arr[j] < arr[k])
k = j; }
int temp = arr[k];
arr[k] = arr[i];
arr[i] = temp; } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
ABHISHEK KUMAR
A230522412

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]; }
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0; }
ABHISHEK KUMAR
A230522412

LAB-02
Aim: Implementation of Binary Search.
Code:
#include <iostream>
using namespace std;
int binarySearch(int arr[], int size, int key) {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key)
return mid;
if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main() {
int size, key;
cout << "Enter the number of elements in the array: ";
cin >> size;
int arr[size];
cout << "Enter the elements in sorted order: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
cout << "Enter the element to search for: ";
cin >> key;
int result = binarySearch(arr, size, key);
if (result != -1)
ABHISHEK KUMAR
A230522412

cout << "Element found at index " << result << endl;
else
cout << "Element not found in the array" << endl;

return 0;
}
ABHISHEK KUMAR
A230522412

LAB-03
Aim: Implementation of Quick Sort and Merge Sort.
 Quick Sort
Code:
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp; }
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]); } }
swap(arr[i + 1], arr[high]);
return (i + 1); }
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high); } }
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl; }
int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
ABHISHEK KUMAR
A230522412

int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i]; }
quickSort(arr, 0, n - 1);
cout << "Sorted array: ";
printArray(arr, n);
return 0; }

 Merge Sort
Code:
#include <iostream>
using namespace std;
void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

int leftArr[n1], rightArr[n2];


for (int i = 0; i < n1; i++)
leftArr[i] = arr[left + i];
for (int i = 0; i < n2; i++)
rightArr[i] = arr[mid + 1 + i];
int i = 0;
int j = 0;
int k = left;
ABHISHEK KUMAR
A230522412

while (i < n1 && j < n2) {


if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++; }
k++; }
while (i < n1) {
arr[k] = leftArr[i];
i++;
k++; }
while (j < n2) {
arr[k] = rightArr[j];
j++;
k++; } }
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right); } }
int main() {
int size;
cout << "Enter the number of elements: ";
cin >> size; int arr[size];
cout << "Enter the elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i]; }
mergeSort(arr, 0, size - 1);
cout << "Sorted array: ";
for (int i = 0; i < size; i++) {
ABHISHEK KUMAR
A230522412

cout << arr[i] << " "; }


cout << endl;
return 0; }
ABHISHEK KUMAR
A230522412

LAB-04
Aim: V Strassen Multiplication
Code:
#include <iostream>
using namespace std;
void strassenMultiply(int A[2][2], int B[2][2], int C[2][2]) {
int P1 = (A[0][0] + A[1][1]) * (B[0][0] + B[1][1]);
int P2 = (A[1][0] + A[1][1]) * B[0][0];
int P3 = A[0][0] * (B[0][1] - B[1][1]);
int P4 = A[1][1] * (B[1][0] - B[0][0]);
int P5 = (A[0][0] + A[0][1]) * B[1][1];
int P6 = (A[1][0] - A[0][0]) * (B[0][0] + B[0][1]);
int P7 = (A[0][1] - A[1][1]) * (B[1][0] + B[1][1]);
C[0][0] = P1 + P4 - P5 + P7;
C[0][1] = P3 + P5;
C[1][0] = P2 + P4;
C[1][1] = P1 + P3 - P2 + P6; }
int main() {
int A[2][2], B[2][2], C[2][2];
cout << "Enter elements of 2x2 matrix A (row-wise):\n";
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> A[i][j];
cout << "Enter elements of 2x2 matrix B (row-wise):\n";
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> B[i][j];
strassenMultiply(A, B, C);
cout << "Resultant matrix C after Strassen's multiplication:\n";
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << C[i][j] << " "; }
ABHISHEK KUMAR
A230522412

cout << endl; }


return 0;
}
ABHISHEK KUMAR
A230522412

LAB-05
Aim: Find Shortest Paths to other vertices using Dijkstra’s Algorithm.
Code:
#include <iostream>
using namespace std;
#define INF 99999
int findMinVertex(int dist[], bool visited[], int V) {
int minVertex = -1;
for (int i = 0; i < V; i++) {
if (!visited[i] && (minVertex == -1 || dist[i] < dist[minVertex])) {
minVertex = i; } }
return minVertex; }
void dijkstra(int graph[10][10], int V, int src) {
int dist[V];
bool visited[V];
for (int i = 0; i < V; i++) {
dist[i] = INF;
visited[i] = false; }
dist[src] = 0;
for (int i = 0; i < V - 1; i++) {
int minVertex = findMinVertex(dist, visited, V);
visited[minVertex] = true;
for (int j = 0; j < V; j++) {
if (graph[minVertex][j] != 0 && !visited[j]) {
int newDist = dist[minVertex] + graph[minVertex][j];
if (newDist < dist[j]) {
dist[j] = newDist; } } } }
cout << "Vertex\tDistance from Source\n";
for (int i = 0; i < V; i++) {
cout << i << "\t" << dist[i] << endl; }
}
int main() {
ABHISHEK KUMAR
A230522412

int V;
cout << "Enter the number of vertices: ";
cin >> V;
int graph[10][10];
cout << "Enter the adjacency matrix (enter 0 for no edge):\n";
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
cin >> graph[i][j]; } }
int src;
cout << "Enter the source vertex: ";
cin >> src;
dijkstra(graph, V, src);
return 0;
}
ABHISHEK KUMAR
A230522412

LAB-06
Aim: Implementation of Bellman Ford.
Code:

#include <iostream>

#include <vector>

#include <limits.h>

using namespace std;

void bellmanFord(int vertices, int edges, vector<vector<int>>& graph, int start) {

vector<int> distance(vertices, INT_MAX);

distance[start] = 0;

for (int i = 0; i < vertices - 1; i++) {

for (int j = 0; j < edges; j++) {

int u = graph[j][0];

int v = graph[j][1];

int weight = graph[j][2];

if (distance[u] != INT_MAX && distance[u] + weight < distance[v]) {

distance[v] = distance[u] + weight; }} }

for (int j = 0; j < edges; j++) {

int u = graph[j][0];

int v = graph[j][1];

int weight = graph[j][2];

if (distance[u] != INT_MAX && distance[u] + weight < distance[v]) {

cout << "Graph contains negative weight cycle!" << endl;

return; }}

cout << "Vertex\tDistance from Source" << endl;

for (int i = 0; i < vertices; i++) {

if (distance[i] == INT_MAX) {

cout << i << "\t\tInfinity" << endl;

} else {

cout << i << "\t\t" << distance[i] << endl; }}}


ABHISHEK KUMAR
A230522412

int main() {

int vertices, edges, source;

cout << "Enter number of vertices: ";

cin >> vertices;

cout << "Enter number of edges: ";

cin >> edges;

vector<vector<int>> graph(edges, vector<int>(3));

for (int i = 0; i < edges; i++) {

cout << "Enter edges (source, destination, weight):" << endl;

cin >> graph[i][0] >> graph[i][1] >> graph[i][2]; }

cout << "Enter the source vertex: ";

cin >> source;

bellmanFord(vertices, edges, graph, source);

return 0;

}
ABHISHEK KUMAR
A230522412

LAB-07
Aim: Implementing Fractional knapsack and 0/1 knapsack problem using
Dynamic Programming.
 Fractional Knapsack Method
Code:

#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_ITEMS 100
int main() {

int n;
cout << "Enter the number of items: ";
cin >> n;

int values[MAX_ITEMS], weights[MAX_ITEMS];


cout << "Enter the value and weight of each item (value weight):\n";
for (int i = 0; i < n; i++) {
cin >> values[i] >> weights[i]; }
int W;
cout << "Enter the maximum weight capacity of the knapsack: ";
cin >> W;
double ratio[MAX_ITEMS];
for (int i = 0; i < n; i++) {
ratio[i] = (double)values[i] / weights[i]; }
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {

swap(ratio[i], ratio[j]);
swap(values[i], values[j]);
swap(weights[i], weights[j]); } }}
double maxValue = 0.0;
ABHISHEK KUMAR
A230522412

for (int i = 0; i < n; i++) {


if (weights[i] <= W) {
maxValue += values[i];
W -= weights[i];
} else {
maxValue += values[i] * ((double)W / weights[i]);
break; }}
cout << "The maximum value in the knapsack is: " << maxValue << endl;
return 0;}

 0/1 Knapsack using Dynamic Programming


Code:
#include <iostream>
using namespace std;
int knapsack(int W, int wt[], int val[], int n) {
int dp[n + 1][W + 1];
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) {
dp[i][w] = 0; }
else if (wt[i - 1] <= w) {
dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]); }
ABHISHEK KUMAR
A230522412

else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
int main() {
int n;
cout << "Enter the number of items: ";
cin >> n;
int val[n], wt[n];
int W;
cout << "Enter the values of the items:\n";
for (int i = 0; i < n; i++) {
cin >> val[i]; }
cout << "Enter the weights of the items:\n";
for (int i = 0; i < n; i++) {
cin >> wt[i]; }
cout << "Enter the maximum capacity of the knapsack: ";
cin >> W;
cout << "The maximum value that can be obtained is: " << knapsack(W, wt, val, n) << endl;
return 0;
}
ABHISHEK KUMAR
A230522412

LAB-08
Aim: Implement Longest Common Subsequence.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int LCS(string s1, string s2) {
int n = s1.length();
int m = s2.length();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1[i - 1] == s2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1] + 1; // If characters match
} else {
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
return dp[n][m];
}
int main() {
string s1, s2;
cout << "Enter the first string: ";
cin >> s1;
cout << "Enter the second string: ";
cin >> s2;
int lcsLength = LCS(s1, s2);
cout << "Length of LCS: " << lcsLength << endl;
return 0;
}
ABHISHEK KUMAR
A230522412
ABHISHEK KUMAR
A230522412

LAB-09
Aim: Implement Traveling Salesman Problem using Dynamic Programming.
Code:
#include <iostream>
#include <vector>
using namespace std;
const int INF = 999999;
int main() {
int n;
cout << "Enter the number of cities: ";
cin >> n;
int dist[10][10];
cout << "Enter the distance matrix:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> dist[i][j];
}
}
vector<vector<int>> dp(1 << n, vector<int>(n, INF));
dp[1][0] = 0;
for (int mask = 1; mask < (1 << n); mask++) {
for (int i = 0; i < n; i++) {
if (mask & (1 << i)) {
for (int j = 0; j < n; j++) {
if (mask & (1 << j)) {
dp[mask][i] = min(dp[mask][i], dp[mask ^ (1 << i)][j] + dist[j][i]);
}
}
}
}
}
int result = INF;
ABHISHEK KUMAR
A230522412

for (int i = 1; i < n; i++) {


result = min(result, dp[(1 << n) - 1][i] + dist[i][0]);
}
cout << "The minimum cost to visit all cities is: " << result << endl;
return 0;
}

You might also like