0% found this document useful (0 votes)
10 views8 pages

Print Program 11 and 12 Daa PDF

Uploaded by

Mihir
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)
10 views8 pages

Print Program 11 and 12 Daa PDF

Uploaded by

Mihir
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/ 8

Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)

Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2

Program-11
Objective:WAP in C to Implement Floyd Warshal algorithm.

#include <stdio.h>
#include <stdlib.h>

void floydWarshall(int **graph, int n)


{
int i, j, k;
for (k = 0; k < n; k++)
{
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (graph[i][j] > graph[i][k] + graph[k][j])
graph[i][j] = graph[i][k] + graph[k][j];
}
}
}
}

int main(void)
{
int n, i, j;

Page No- 21
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2


printf("Enter the number of vertices: ");
scanf("%d", &n);
int *graph = (int *)malloc((long unsigned) n * sizeof(int *));
for (i = 0; i < n; i++)
{
graph[i] = (int *)malloc((long unsigned) n * sizeof(int));
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (i == j)
graph[i][j] = 0;
else
graph[i][j] = 100;
}
}
printf("Enter the edges: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%d", &graph[i][j]);
}

Page No- 22
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2


}
printf("The original graph is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
floydWarshall(graph, n);
printf("The shortest path matrix is:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
return 0;
}

Page No- 23
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2

Page No- 24
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2

Program-12
Objective:WAP in C to Implement Travelling Salesman problem.

#include <stdio.h>
int matrix[25][25], visited_cities[10], limit, cost = 0;

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;

Page No- 25
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2


}
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);

Page No- 26
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2


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;
}

Page No- 27
Name:Mohd Azeem 3rd/Vth sem (Odd Sem 2024-25)
Roll no:2200271530072 DAA Lab(BCS-553)

Branch/Sec on: CSE-AIML-2

Page No- 28

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy