Dfs TSP
Dfs TSP
Step 1 : Start
Step 2 : reach[v]=1;
Step 3 : i=1; while (i<=n), go to Step 4, else go to Step 8.
Step 4 : if (a[v][i] && !reach[i]), go to Step 5, else go to Step 7.
Step 5 : Display (“%d->%d”,v,i)
Step 6 : DFS(i);
Step 7 : i=i+1; Go to Step 3.
Step 8 : Stop.
Step 1 : Start
Step 2 : Input the number of vertices.
Step 3 : i=1; while (i<=n), go to Step 4, else go to Step 9.
Step 4 : reach[i]=0;
Step 5 : j=1; while (j<=n), go to Step 6, else go to Step 8.
Step 6 : a[i][j]=0;
Step 7 : j=j+1; Go to Step 5.
Step 8 : i=i+1; Go to Step 3.
Step 9 : Input the graph in adjacency matrix form
Step 10 : DFS(1).
Step 11 : i=1; while (i<=n), go to Step 12, else go to Step 15.
Step 12 : if (reach[i]), go to Step 13, else go to Step 14.
Step 13 : count=count+1;
Step 14 : i=i+1; Go to Step 11.
Step 15 : if (count==n), go to Step 16, else go to Step 17.
Step 16 : Display “Graph is Connected”. Go to Step 18.
Step 17 : Display “Graph is not Connected”.
Step 18 : return 0;
Step 19 : Stop.
Program:
#include<stdio.h>
int a[20][20];
int reach[20];
int n;
void DFS(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i]&&!reach[i])
{
printf("\n%d->%d",v,i);
DFS(i);
}
}
int main()
{
int i,j,count=0;
printf("\nEnter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\nEnter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
DFS(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\nGraph is connected");
else
printf("\nGraph is not connected");
return 0;
}
Output :
Enter the number of vertices: 5
01111
10000
10000
10000
10000
1->2
1->3
1->4
1->5
Graph is Connected.
Remarks :
In this program we have learnt how to implement the Depth First Search (DFS)
algorithm in C using a stack data structure. We have also learnt how the DFS
algorithm works and how we use recursion in order to visit all of the nodes. The DFS
algorithm visits the deepest nodes first and then moves on to visit the other nodes.
The time complexity of DFS algorithm is O(V+E), where V stands for Vertices and E
stands for Edges.
Question : Write a program to implement the Travelling Salesman Problem.
Step 1 : Start
Step 2 : int count,temp,nearest_city=999,minimum=999;
Step 3 : count=0; while (count<limit), go to Step 4, else go to Step 10.
Step 4 : if ((matrix[c][count]!=0) && (visited_cities[count]==0), go to Step 5,
else go to Step 9.
Step 5 : if (matrix[c][count]<minimum), go to Step 6, else go to Step 7.
Step 6 : minimum=matrix[count][0]+matrix[c][count];
Step 7 : temp=matrix[c][count];
Step 8 : nearest_city=count;
Step 9 : count=count+1; Go to Step 3.
Step 10 : if (minimum!=999), go to Step 11, else go to Step 12.
Step 11 : cost=cost+temp;
Step 12 : return nearest_city;
Step 13 : Stop.
Step 1 : Start
Step 2 : visited_cities[city]=1;
Step 3 : nearest_city=tsp(city);
Step 4 : if (nearest_city=999), go to Step 5, else go to Step 9.
Step 5 : nearest_city=0;
Step 6 : Display the nearest city.
Step 7 : cost=cost+matrix[city][nearest_city];
Step 8 : return;
Step 9 : minimum_cost(nearest_city);
Step 10 : Stop.
Step 1 : Start
Step 2 : Inpu the number of cities.
Step 3 : i=0; while (i<limit), go to Step 4, else go to Step 9.
Step 4 : j=0; while (j<limit), go to Step 5, else go to Step 7.
Step 5 : scanf(“%d”,&matrix[i][j]);
Step 6 : j=j+1; Go to Step 4.
Step 7 : visited_cities[i]=0;
Step 8 : i=i+1; Go to Step 3.
Step 9 : Display the cost matrix.
Step 10 : Display the path as minimum_cost(0);
Step 11 : Display cost.
Step 12 : return 0;
Step 13 : Stop.
Program:
#include <stdio.h>
int matrix[25][25];
int visited_cities[10];
int 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;
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 the number of cities\n");
scanf("%d",&limit);
printf("Enter the cost matrix\n");
for(i=0;i<limit;i++)
{
printf("Enter elements in Row [%d] \n",i+1);
for(j=0;j<limit;j++)
scanf("%d",&matrix[i][j]);
visited_cities[i]=0;
}
printf("Entered cost matrix \n");
for(i=0;i<limit;i++)
{
for(j=0;j<limit;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
printf("Path\n");
minimum_cost(0);
printf("\nMinimum Cost\n");
printf("%d",cost);
return 0;
}
Output :
Remarks :
In this program we have learnt how to implement the Travelling Salesman Problem
using recursion and Dynamic Programming. We have learnt how to use recusrion to
calculate the smallest path the salesman has to take in order to travel all the cities
and return to his starting point.