0% found this document useful (0 votes)
25 views

Advanced Algorithmda2

The document discusses four algorithms - N-Queens problem using backtracking, travelling salesman problem using branch and bound, knapsack problem using dynamic programming, and Floyd-Warshall algorithm. For each algorithm, it provides the code implementation and sample output.
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)
25 views

Advanced Algorithmda2

The document discusses four algorithms - N-Queens problem using backtracking, travelling salesman problem using branch and bound, knapsack problem using dynamic programming, and Floyd-Warshall algorithm. For each algorithm, it provides the code implementation and sample output.
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/ 10

ADVANCED ALGORITHM

LAB DA-2
22MIC0009
MAHALAKSHMI BALAN

1)N-Queens Problem Using Backtracking


CODE:
#include <stdio.h>

#define N 4

void printBoard(int board[N][N])

for (int i = 0; i < N; i++) {

for (int j = 0; j < N; j++)

if (board[i][j])

printf("Q ");

else

printf(". ");

printf("\n");

int isSafe(int board[N][N], int row, int col)

int i, j;

for (i = 0; i < col; i++)

if (board[row][i])
return 0;

for (i = row, j = col; i >= 0 && j >= 0; i--, j--)

if (board[i][j])

return 0;

for (i = row, j = col; j >= 0 && i < N; i++, j--)

if (board[i][j])

return 0;

return 1;

int solveNQ(int board[N][N], int col)

if (col >= N)

return 1;

for (int i = 0; i < N; i++) {

if (isSafe(board, i, col)) {

board[i][col] = 1;

if (solveNQ(board, col + 1))

return 1;

board[i][col] = 0; // BACKTRACK

return 0;

}
int solve()

int board[N][N] = { { 0, 0, 0, 0 },

{ 0, 0, 0, 0 },

{ 0, 0, 0, 0 },

{ 0, 0, 0, 0 } };

if (solveNQ(board, 0) == 0) {

printf("No Possible Solution exist\n");

return 0;

printBoard(board);

return 1;

int main()

solve();

return 0;

OUTPUT:
2)Travelling salesman problem using Branch and Bound
CODE:
#include <stdio.h>

int visited[10], n, cost = 0;

void travellingsalesman(int c, int tsp_g[10][10]) {

int k, adj_vertex = 999;

int min = 999;

visited[c] = 1;

printf("%d ", c + 1);

for (k = 0; k < n; k++) {

if ((tsp_g[c][k] != 0) && (visited[k] == 0)) {

if (tsp_g[c][k] < min) {

min = tsp_g[c][k];

adj_vertex = k;

if (min != 999) {

cost = cost + min;

if (adj_vertex == 999) {

adj_vertex = 0;

printf("%d", adj_vertex + 1);

cost = cost + tsp_g[c][adj_vertex];

return;

travellingsalesman(adj_vertex, tsp_g);
}

int main() {

int i, j;

printf("Enter the number of vertices: ");

scanf("%d", &n);

int tsp_g[10][10];

printf("Enter the adjacency matrix:\n");

for (i = 0; i < n; i++) {

for (j = 0; j < n; j++) {

scanf("%d", &tsp_g[i][j]);

for (i = 0; i < n; i++) {

visited[i] = 0;

printf("\n");

printf("Shortest Path: ");

travellingsalesman(0, tsp_g);

printf("\nMinimum Cost: ");

printf("%d\n", cost);

return 0;

OUTPUT:
3)Knapsack Problem using Dynamic Programming
CODE:
#include <stdio.h>

int max(int X, int Y)

if (X > Y)

return X;

else

return Y;

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 n, W;

printf("Enter the number of items: ");

scanf("%d", &n);

int val[n], wt[n];

printf("Enter the values for each item: ");

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

scanf("%d", &val[i]);

printf("Enter the weights for each item: ");

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

scanf("%d", &wt[i]);

printf("Enter the knapsack capacity: ");


scanf("%d", &W);

printf("%d\n", knapSack(W, wt, val, n));

return 0;

OUTPUT:

4)Floyd-Warshall Algorithm
CODE:
#include <stdio.h>

#include <limits.h>

#define INF INT_MAX

void fillDistanceMatrix(int n, int A[n][n], int D[n][n]) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if (i == j)

D[i][j] = 0;

else if (A[i][j] == 0)

D[i][j] = INF;
else

D[i][j] = A[i][j];

void floydWarshall(int n, int A[n][n], int D[n][n]) {

fillDistanceMatrix(n, A, D);

for (int k = 0; k < n; k++) {

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if (D[i][k] < INF && D[k][j] < INF)

if (D[i][k] + D[k][j] < D[i][j])

D[i][j] = D[i][k] + D[k][j];

int main() {

int n;

printf("Enter the number of vertices: ");

scanf("%d", &n);

int A[n][n];

int D[n][n];

printf("Enter the adjacency matrix (%d x %d) with 'I' for infinity element-wise:\n", n, n);

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

printf("Enter value for A[%d][%d]: ", i, j);

char input[5];

scanf("%s", input);

if (input[0] == 'I') {

A[i][j] = INF;
} else {

sscanf(input, "%d", &A[i][j]);

floydWarshall(n, A, D);

printf("Shortest distances between all pairs of vertices:\n");

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {

if (D[i][j] == INF)

printf("%7s", "I");

else

printf("%7d", D[i][j]);

printf("\n");

return 0;

OUTPUT:

You might also like