Daa Tanu File
Daa Tanu File
Daa Tanu File
Lab File
if (index == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", index);
}
return 0;
}
void main()
{
int heap[10], num, i, j, c, rootElement, tempVar;
}
Experiment no: -3
int i, j, k;
i = 0;
j = 0;
k = p;
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
printf("Sorted array: \n");
printArray(arr, size);
}
Experiment no: -4
int main() {
int data[] = {20, 12, 10, 15, 2};
int size = sizeof(data) / sizeof(data[0]);
selectionSort(data, size);
printf("Sorted array in Acsending Order:\n");
printArray(data, size);
}
Experiment no: -5
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
Experiment no: -6
int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
return 0;
}
Experiment no: -8
int tsp(int c)
{
int count, nearest_city = 999;
int minimum = 999, temp;
for(count = 0; count < limit; count++)
{
if((matrix[c][count] != 0) && (visited_cities[count] == 0))
{
if(matrix[c][count] < minimum)
{
minimum = matrix[count][0] + matrix[c][count];
}
temp = matrix[c][count];
nearest_city = count;
}
}
if(minimum != 999)
{
cost = cost + temp;
}
return nearest_city;
}
void minimum_cost(int city)
{
int nearest_city;
visited_cities[city] = 1;
printf("%d ", city + 1);
nearest_city = tsp(city);
if(nearest_city == 999)
{
nearest_city = 0;
printf("%d", nearest_city + 1);
cost = cost + matrix[city][nearest_city];
return;
}
minimum_cost(nearest_city);
}
int main()
{
int i, j;
printf("Enter Total Number of Cities:\t");
scanf("%d", &limit);
printf("\nEnter Cost Matrix\n");
for(i = 0; i < limit; i++)
{
printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);
for(j = 0; j < limit; j++)
{
scanf("%d", &matrix[i][j]);
}
visited_cities[i] = 0;
}
printf("\nEntered Cost Matrix\n");
for(i = 0; i < limit; i++)
{
printf("\n");
for(j = 0; j < limit; j++)
{
printf("%d ", matrix[i][j]);
}
}
printf("\n\nPath:\t");
minimum_cost(0);
printf("\n\nMinimum Cost: \t");
printf("%d\n", cost);
return 0;
}
Experiment no: -9
return parent[component]
= findParent(parent, parent[component]);
}
void unionSet(int u, int v, int parent[], int rank[], int n)
{
u = findParent(parent, u);
v = findParent(parent, v);
int parent[n];
int rank[n];
printf(
"Following are the edges in the constructed MST\n");
for (int i = 0; i < n; i++) {
int v1 = findParent(parent, edge[i][0]);
int v2 = findParent(parent, edge[i][1]);
int wt = edge[i][2];
if (v1 != v2) {
unionSet(v1, v2, parent, rank, n);
minCost += wt;
printf("%d -- %d == %d\n", edge[i][0],
edge[i][1], wt);
}
}
kruskalAlgo(5, edge);
return 0;
}
Experiment no: -10
return true;
}
bool solveNQUtil(int board[N][N], int col)
{
if (col >= N)
return true;
for (int i = 0; i < N; i++) {
if (isSafe(board, i, col)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1))
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[N][N] = { { 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 },
{ 0, 0, 0, 0 } };
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
int main()
{
solveNQ();
return 0;
}
Experiment no: -11
Program:
Dynamic Programming method:
#include<stdio.h>
int max(int a, int b) { return (a > b)? a : b; }
int knapSack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n+1][W+1];
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i==0 || w==0)
K[i][w] = 0;
else if (wt[i-1] <= w)
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w]);
else
K[i][w] = K[i-1][w];
}
}
return K[n][W];
}
int main()
{
int i, n, val[20], wt[20], W;
printf("Enter number of items:");
scanf("%d", &n);
Greedy method:
#include <stdio.h>
#include <stdlib.h>
struct Item {
int weight;
int value;
double ratio; // value-to-weight ratio
};
int compare(const void *a, const void *b) {
double ratioA = ((struct Item *)a)->ratio;
double ratioB = ((struct Item *)b)->ratio;
return (ratioB > ratioA) ? 1 : -1;
}
double knapsackGreedy(struct Item items[], int n, int capacity) {
for (int i = 0; i < n; i++) {
items[i].ratio = (double)items[i].value / items[i].weight;
}
qsort(items, n, sizeof(struct Item), compare);
int currentWeight = 0;
double totalValue = 0.0;
return totalValue;
}
int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
return 0;
}
Experiment no: -12
Aim: From a given vertex in a weighted connected graph, find shortest paths to
other vertices using Dijkstra’s algorithm.
Program:
#include<stdio.h>
#include<conio.h>
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[100]){
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n){
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w;
flag[u]=1;
count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
Experiment no: -13
Aim: Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm.
Program:
#include <stdio.h>
#include <limits.h>
#define V 5
min = key[v];
min_index = v;
return min_index;
printf("Edge Weight\n");
for (int i = 1; i < V; i++) {
int parent[V];
int key[V];
int mstSet[V];
key[i] = INT_MAX;
mstSet[i] = 0;
mstSet[u] = 1;
parent[v] = u;
key[v] = graph[u][v];
printMST(parent, graph);
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}
};
primMST(graph);
return 0;
}
Experiment no: -14
Program:
#include <stdio.h>
#include <stdbool.h>
int graph[MAX_VERTICES][MAX_VERTICES];
int n;
void printSolution(int path[]) {
printf("Hamiltonian Cycle: ");
for (int i = 0; i < n; i++) {
printf("%d ", path[i]);
}
printf("%d\n", path[0]);
}
return true;
}
return false;
}
void hamiltonianCycle() {
int path[MAX_VERTICES];
for (int i = 0; i < n; i++) {
path[i] = -1;
}
path[0] = 0;
if (!hamiltonianCycleUtil(path, 1)) {
printf("Hamiltonian Cycle does not exist.\n");
}
}
int main() {
printf("Enter the number of vertices in the graph: ");
scanf("%d", &n);
hamiltonianCycle();
return 0;
}
OUTPUT
1)
2)
3)
4)
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)