0% found this document useful (0 votes)
2 views5 pages

Daa Tutorial

The document contains two algorithms: the Travelling Salesman Problem (TSP) and Graph Coloring. The TSP algorithm calculates the minimum cost of visiting all cities and returning to the start, while the Graph Coloring algorithm assigns colors to graph vertices such that no two adjacent vertices share the same color. Both algorithms utilize recursive functions and backtracking techniques to find solutions.

Uploaded by

kavyaa
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)
2 views5 pages

Daa Tutorial

The document contains two algorithms: the Travelling Salesman Problem (TSP) and Graph Coloring. The TSP algorithm calculates the minimum cost of visiting all cities and returning to the start, while the Graph Coloring algorithm assigns colors to graph vertices such that no two adjacent vertices share the same color. Both algorithms utilize recursive functions and backtracking techniques to find solutions.

Uploaded by

kavyaa
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/ 5

Travelling Salesman Problem

#include <stdio.h>
#include <conio.h>
#define MAX 10
int visited[MAX];
int cost[MAX][MAX];
int n;
int minCost = INT_MAX;
void tsp(int pos, int count, int pathCost, int start)
{
int i;
if (count == n && cost[pos][start])
{
int totalCost = pathCost + cost[pos][start];
if (totalCost < minCost)
minCost = totalCost;
return;
}
for (i = 0; i < n; i++)
{
if (!visited[i] && cost[pos][i])
{
visited[i] = 1;
tsp(i, count + 1, pathCost + cost[pos][i], start);
visited[i] = 0;
}
}
}
int main()
{
int i, j;
printf("Enter number of cities: ");
scanf("%d", &n);
printf("Enter cost matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &cost[i][j]);
}
}
for (i = 0; i < n; i++)
{
visited[i] = 0;
}
visited[0] = 1; // Starting from city 0
tsp(0, 1, 0, 0);
printf("Minimum tour cost: %d\n", minCost);
return 0;
}
Graph Coloring
#include <stdio.h>
#define MAX 10
int graph[MAX][MAX];
int colors[MAX];
int n;
int m;
int isSafe(int vertex, int c)
{
int i;
for (i = 0; i < n; i++) {
if (graph[vertex][i] == 1 && colors[i] == c)
{
return 0;
}
}
return 1;
}
int graphColoring(int vertex)
{
int c;
if (vertex == n)
{
return 1;
}
for (c = 1; c <= m; c++)
{
if (isSafe(vertex, c))
{
colors[vertex] = c;
if (graphColoring(vertex + 1))
{
return 1;
}
colors[vertex] = 0; // Backtrack
}
}
return 0;
void printColors()
{
int i;
printf("Assigned colors to vertices:\n");
for (i = 0; i < n; i++) {
printf("Vertex %d ---> Color %d\n", i, colors[i]);
}
}
int main()
{
int i, j;
printf("Enter number of vertices: ");
scanf("%d", &n);
printf("Enter the adjacency matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &graph[i][j]);
}
}
printf("Enter the number of colors: ");
scanf("%d", &m);
for (i = 0; i < n; i++)
{
colors[i] = 0;
}
if (graphColoring(0))
{
printColors();
} else
{
printf("No solution exists with %d colors.\n", m);
}
return 0;
}

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