Daa Practical File Ak
Daa Practical File Ak
PRACTICAL - 1
int pivot=arr[high];
//Index of smaller element and Indicate
//the right position of pivot found so far
int i=(low-1);
for(int j=low;j<=high;j++)
{
//If current element is smaller than the pivot
if(arr[j]<pivot)
{
//Increment index of smaller element
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[high]);
return (i+1);
}
int pi=partition(arr,low,high);
//Recursion Call
//smaller element than pivot goes left and
//higher element goes right quickSort(arr,low,pi-
1);
quickSort(arr,pi+1,high);
}
}
// Function call
quickSort(arr,0,n-1);
//Print the sorted array
cout<<"Sorted Array\n";
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
PRACTICAL - 2
// UTILITY FUNCTIONS
// Function to print an array
vprintArray(int A[], int
{
for (int i = 0; i < size; i++)
cout << A[i] << " "; cout
<< endl;
}
// Driver
code int
main() {
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
OUTPUT
PRACTICAL - 3
Write a C++ program to find all the ShortestPath using FloydWarshall
Algorithm
#include <iostream> using
namespace std;
#define INF 1000000000 // Infinity
int main()
{
int V = 4;
// V represent number of vertices
int dist[V][V] = {{0, 2, INF, 4},
{6, 0, 5, 3},
{INF, 10, 0, 1},
{7, 9, 8, 0}};
// Represent the graph using adjacency matrix
return
0; }
OUTPUT
PRACTICAL - 4
// Constructor
Item(int value, int weight)
: value(value), weight(weight)
{
}
};
// Driver Code
int main()
{
// Weight of knapsack
int N = 60;
// Function Call
cout << "Maximum profit earned = "
<< fractionalKnapsack(arr, N, size);
return 0;
}
OUTPUT
PRACTICAL - 5
#include <limits.h>
#include <stdio.h>
return min_index;
}
dijkstra(graph, 0);
return 0;
}
OUTPUT
PRACTICAL - 6
Write a C++ program to find Minimum Spanning Tree using Prim’s
Algorithm.
// A C++ program for Prim's Minimum
// Spanning Tree (MST) algorithm. The program is
// for adjacency matrix representation of the graph
#include <bits/stdc++.h>
using namespace std;
return min_index;
}
// Driver's
code int
main() {
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
return 0;
}
OUTPUT
PRACTICAL - 7
if (i == n) {
// return if we have reached at the end of the array
return;
}
// Driver code
int main()
{
// Test case 1 int
set[] = { 1, 2, 1 };
int sum = 3;
int n = sizeof(set) /
sizeof(set[0]); vector<int>
subset; cout << "Output 1:" <<
endl;
PrintSubsetSum(0, n, set, sum,
subset); cout << endl; flag = 0; //
Test case 2
int set2[] = { 3, 34, 4, 12, 5, 2 };
int sum2 = 30;
int n2 = sizeof(set) / sizeof(set[0]);
vector<int> subset2; cout
<< "Output 2:" << endl;
PrintSubsetSum(0, n2, set2, sum2, subset2);
if (!flag) {
cout << "There is no such subset";
}
return 0;
}
OUTPUT
PRACTICAL – 8
Write a C++ program to solve TravellingSales problem using the Dynamic
Programming Approach.
#include <iostream>
return 0;
}
OUTPUT
PRACTICAL – 9
#include <bits/stdc++.h>
#define N 4 using
namespace std;
return true;
}
if (solveNQUtil(board, 0) == false) {
cout << "Solution does not exist";
return false;
}
printSolution(board);
return true;
}
OUTPUT
PRACTICAL – 10
Write a C++ program to solve the Knapsack Problem using Branch & Bound.
if (u.weight >= W)
return 0;
return profit_bound;
}
return maxProfit;
}
return 0; }
OUTPUT
PRACTICAL – 11
// DFS function
void dfs1(int x)
{
vis1[x] = true;
for (auto i :
gr1[x]) if (!
vis1[i])
dfs1(i);
}
// DFS function
void dfs2(int x)
{
vis2[x] = true;
for (auto i :
gr2[x]) if (!
vis2[i])
dfs2(i);
}
bool Is_Connected(int n)
{
// Call for correct direction
memset(vis1, false, sizeof vis1);
dfs1(1);
// Call for reverse direction
memset(vis2, false, sizeof vis2);
dfs2(1);
// If graph is connected
return true;
}
// Driver
code int
main() {
int n = 4;
// Add edges
Add_edge(1, 2);
Add_edge(1, 3);
Add_edge(2, 3);
Add_edge(3, 4);
// Function call if
(Is_Connected(n))
cout << "Yes"; else
cout << "No";
return 0;
}
OUTPUT