0% found this document useful (0 votes)
20 views9 pages

21BCE9125 DAA Assignment-8

The document discusses three algorithms: (1) solving the traveling salesperson problem using brute force and dynamic programming, (2) solving graph coloring problems using backtracking, (3) finding a Hamiltonian circuit in a graph using backtracking.
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)
20 views9 pages

21BCE9125 DAA Assignment-8

The document discusses three algorithms: (1) solving the traveling salesperson problem using brute force and dynamic programming, (2) solving graph coloring problems using backtracking, (3) finding a Hamiltonian circuit in a graph using backtracking.
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/ 9

Design and Analysis of Algorithms

ASSIGNMENT - 8

SLOT: L31+L32

SUBMITTED BY

NAME: Kedarisetty Venkata Krishna Sai Suhas

REG NO: 21BCE9125

SUBMITTED TO

Prof. TANIKELLA DIVYA NAGA PAVANI


1. Write a program for solving a traveling salesperson's problem
(TSP) using
Aim:
The aim of this problem is to find the shortest possible route that visits each city
exactly once and returns to the origin city.
Theory:
The Traveling Salesperson's Problem (TSP) is a classic optimization problem. It
involves finding the shortest possible route that visits every city exactly once and
returns to the original city. It's an NP-hard problem, meaning there's no known
polynomial-time solution for all instances. Brute force and dynamic
programming are two approaches to solving it.
(a) Brute Force Approach:
In the brute force approach, you generate all possible permutations of the cities
and calculate the total distance for each permutation. Finally, you select the
permutation with the minimum total distance.
Code:
import java.util.ArrayList;

public class TSPBruteForce {

static int[][] graph;

static int calculateDistance(ArrayList<Integer> path) {


int distance = 0;
int n = path.size();
for (int i = 0; i < n - 1; i++) {
distance += graph[path.get(i)][path.get(i + 1)];
}
distance += graph[path.get(n - 1)][path.get(0)];
return distance;
}

static void swap(ArrayList<Integer> list, int i, int j) {


int temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
static void permute(ArrayList<Integer> path, int l, int r,
ArrayList<Integer> minPath, int[] minDistance) {
if (l == r) {
int distance = calculateDistance(path);
if (distance < minDistance[0]) {
minDistance[0] = distance;
minPath.clear();
minPath.addAll(path);
}
} else {
for (int i = l; i <= r; i++) {
swap(path, l, i);
permute(path, l + 1, r, minPath, minDistance);
swap(path, l, i); // backtrack
}
}
}

static ArrayList<Integer> tspBruteForce(int start) {


int n = graph.length;
ArrayList<Integer> cities = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (i != start)
cities.add(i);
}

ArrayList<Integer> minPath = new ArrayList<>();


int[] minDistance = {Integer.MAX_VALUE};

permute(cities, 0, cities.size() - 1, minPath, minDistance);


minPath.add(0, start);
minPath.add(start);

return minPath;
}

public static void main(String[] args) {


graph = new int[][]{{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}};
int start = 0;
ArrayList<Integer> shortestPath = tspBruteForce(start);
System.out.println("Shortest Path: " + shortestPath);
System.out.println("Total Distance: " +
calculateDistance(shortestPath));
}
}
Output:

(b) Dynamic Programming Algorithm:


Dynamic programming is a more efficient approach compared to brute force. It
uses memoization to store solutions to subproblems and avoids redundant
calculations.2. Write a program for solving graph colouring problems
using backtracking.
Code:
public class TSPDynamicProgramming {

static int[][] graph;


static int n;
static int tspDynamicProgramming(int start, int mask, int[][] dp) {
if (mask == (1 << n) - 1)
return graph[start][0];

if (dp[start][mask] != -1)
return dp[start][mask];

int minDistance = Integer.MAX_VALUE;


for (int city = 0; city < n; city++) {
if ((mask & (1 << city)) == 0) {
int newMask = mask | (1 << city);
int distance = graph[start][city] +
tspDynamicProgramming(city, newMask, dp);
minDistance = Math.min(minDistance, distance);
}
}
return dp[start][mask] = minDistance;
}

public static void main(String[] args) {


graph = new int[][]{{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}};
n = graph.length;
int start = 0;
int[][] dp = new int[n][1 << n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < (1 << n); j++) {
dp[i][j] = -1;
}
}
int shortestDistance = tspDynamicProgramming(start, 1, dp);
System.out.println("Shortest Distance: " + shortestDistance);
}
}

Output:
2. Write a program for solving graph colouring problems using
backtracking.
Aim:
The aim of this problem is to assign colors to vertices of a graph in such a way
that no two adjacent vertices have the same color using the minimum number
of colors.
Theory:
Graph coloring is a fundamental problem in graph theory. It involves assigning
colors to the vertices of a graph in such a way that no two adjacent vertices share
the same color. Backtracking is commonly used to solve this problem.
Code:
import java.util.Arrays;

public class GraphColoring {

static boolean isSafe(int v, int graph[][], int colors[], int c) {


for (int i = 0; i < graph.length; i++) {
if (graph[v][i] == 1 && c == colors[i])
return false;
}
return true;
}

static boolean graphColoringUtil(int graph[][], int m, int colors[], int


v) {
if (v == graph.length)
return true;

for (int c = 1; c <= m; c++) {


if (isSafe(v, graph, colors, c)) {
colors[v] = c;
if (graphColoringUtil(graph, m, colors, v + 1))
return true;
colors[v] = 0;
}
}
return false;
}

static boolean graphColoring(int graph[][], int m) {


int[] colors = new int[graph.length];
Arrays.fill(colors, 0);
if (!graphColoringUtil(graph, m, colors, 0)) {
System.out.println("No solution exists.");
return false;
}
System.out.println("Solution exists. Vertex colors:");
for (int i = 0; i < graph.length; i++) {
System.out.println("Vertex " + i + " : Color " + colors[i]);
}
return true;
}

public static void main(String[] args) {


int graph[][] = {{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}};
int m = 3;
graphColoring(graph, m);
}
}

Output:
3. Write a program to find the Hamiltonian Circuit in a given graph.
Aim:
The aim of this problem is to find a closed loop in a graph that visits every vertex
exactly once.

Theory:
A Hamiltonian Circuit is a closed loop that visits every vertex exactly once in a
graph. Finding a Hamiltonian Circuit is NP-complete, meaning that there's no
known efficient algorithm to solve it for all graphs. Backtracking is often used to
find Hamiltonian Circuits in graphs.

Code:
import java.util.ArrayList;

public class HamiltonianCircuit {

static boolean isSafe(int v, int graph[][], ArrayList<Integer> path, int


pos) {
if (graph[path.get(pos - 1)][v] == 0)
return false;

for (int i = 0; i < pos; i++)


if (path.get(i) == v)
return false;

return true;
}

static boolean hamiltonianUtil(int graph[][], ArrayList<Integer> path, int


pos) {
if (pos == graph.length) {
if (graph[path.get(pos - 1)][path.get(0)] == 1)
return true;
else
return false;
}

for (int v = 1; v < graph.length; v++) {


if (isSafe(v, graph, path, pos)) {
path.add(v);
if (hamiltonianUtil(graph, path, pos + 1))
return true;
path.remove(pos);
}
}
return false;
}

static void hamiltonianCircuit(int graph[][]) {


ArrayList<Integer> path = new ArrayList<>();
path.add(0);
if (hamiltonianUtil(graph, path, 1)) {
System.out.print("Hamiltonian Circuit found: ");
for (int vertex : path)
System.out.print(vertex + " ");
System.out.println(path.get(0));
} else {
System.out.println("No Hamiltonian Circuit exists.");
}
}

public static void main(String[] args) {


int graph[][] = {
{0, 1, 0, 1, 0},
{1, 0, 1, 1, 1},
{0, 1, 0, 0, 1},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 0}
};
hamiltonianCircuit(graph);
}
}

Output:

You might also like