0% found this document useful (0 votes)
73 views75 pages

FINAL DAA MANUAL 2024 For Presentation

Uploaded by

pondymanoj2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views75 pages

FINAL DAA MANUAL 2024 For Presentation

Uploaded by

pondymanoj2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 75

DEPARTMENT OF

ARTIFICIAL INTELLIGENCE
AND
DATA SCIENCE

U23CSBC01 - DESIGN AND ANALYSIS OF ALGORITHMS


THEORY CUM PRACTICAL
PRACTICAL MANUAL

Year/Semester/Sec: II/III/A&B

Regulation : R-23
SYLLABUS
Department Computer Science and Programme: B.Tech.
Engineering
Semester III Course Category: PC End Semester Exam Type:
TE
Periods/Week Credit Maximum Marks
Course Code U23CSBC01 L T P C CA ESE TM
M
Course Name Design and Analysis of Algorithms 2 - 2 3 50 50 100

(Common to All Branches)


Prerequisite Programming (C or C++), Data Structures and Problem Solving Approaches.
BT
On completion of the course, the students will be able to
Mapping
(Highest
Level)
CO1 Analyze and improve the efficiency of algorithms and estimate the K2
Course performance of algorithm and Divide and Conquer.
Outcome
CO2 Determine the Greedy paradigms, Dynamic Programming and explain when K3
s an algorithmic
design situation calls for it.
CO3 Interpret the Backtracking paradigms, Branch and Bound, NP-Hard K3
paradigms and explain
when an algorithmic design situation calls for it.
CO4 Demonstrate programs using Divide and Conquer, Greedy paradigms. K3
CO5 Build the programs using Dynamic Programming, Backtracking and Branch K2
and Bound.
UNIT - I Introduction To Algorithm and Divide and Conquer Periods:10
Introduction – Algorithm – Pseudo code for expressing algorithms – Performance Analysis – Time complexity – Space CO1
complexity – Asymptotic Notation – Big oh notation – Omega notation – Theta notation and Little oh notation.
Divide and Conquer method: Binary search – Merge sort – Quick sort
UNIT - II Greedy Method and Dynamic Programming Periods:10
Greedy method: General method – applications– Knapsack problem – Minimum cost spanning trees –Single source
shortest path problem. CO2
Dynamic Programming: Applications – Multistage graphs – 0/1 knapsack problem, All pairs shortest path problem –
Traveling sales person problem
UNIT - III Backtracking and Branch and Bound Periods:10
Backtracking: General method. Applications – N – queen problem – Sum of subsets problem – Graph coloring –
Hamiltonian cycle – 0/1 Knapsack Problem. CO3
Branch and Bound: General method – Applications – Traveling sales person problem – 0/1 knapsack problem – LC
Branch and Bound solution –FIFO Branch and Bound solution
UNIT - IV Laboratory Exercises Periods:15
 Implementation of binary search using Divide-and-Conquer technique
CO4
 Implementation of Finding Maximum and Minimum using Divide-and-Conquer technique.

 Implementation of Knapsack using Greedy technique.

 Implementation of Minimum Spanning Tree using Prim’s and Kruskal’s Algorithm using Greedy technique.

 Implementation of Single-Source Shortest Paths algorithms using Greedy technique.

UNIT - V Periods:15
Laboratory Exercises
 Implementation of All Pairs Shortest Paths using Dynamic Programming technique.
CO5
 Implementation of Traveling Salesman algorithms using Dynamic Programming technique.

 Implementation of 8 Queens with the design of Backtracking.

 Implementation of sum of subsets with the design of Backtracking.

 Implementation of Traveling Salesman problems with Branch-and-Bound technique.


Tutorial Periods: - Practical Periods: 30 Total Periods:60
Lecture Periods:30

Text Books
1. Levitin Anany,” Introduction to the Design and Analysis of Algorithms”, Pearson Education India,1st Edition,2019.
2. E. Horowitz and S.Sahni, “Fundamentals of Algorithms”, Galgotia Publications, 2nd Edition, 2010.
3. T.H.Cormen, C.E.Leiserson, R.L.Rivest, and C.Stein, “Introduction to Algorithms”,PHI/Pearson Education,
3rdEdition,2009.

Reference Books
1. Anany Levitin, “Introduction to the Design and Analysis of Algorithms”, Pearson Education, Third Edition,
2012.
2. Aho Alfred V.,“Design & Analysis of Computer Algorithms”, Pearson Education India,2nd Edition,2018
3. Basu S. K.,” Design Methods and Analysis of Algorithms”, PHI Learning,3rd Edition, 2018.
4. E. Horowitz and S.Sahni, “Fundamentals of Algorithms”, 2nd Edition, Galgotia Publications, 2010.
5. T.H.Cormen, C.E.Leiserson, R.L.Rivest, and C.Stein, “Introduction to Algorithms, 3rd Edition, PHI/Pearson
Education, 2009.
Web References
1. https://www.tutorialspoint.com/design_and_analysis_of_algorithms/
2. https://www.javatpoint.com/daa-tutorial
3. https://www.guru99.com/design-analysis-algorithms-tutorial.html
4. https://www.geeksforgeeks.org/fundamentals-of-algorithms/
5. https://swayam.gov.in/nd1_noc20_cs71/preview

COs/POs/PSOs Mapping

Program
Program Outcomes
COs (POs) Specific
Outcomes
(PSOs)
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
1 - - - - - 2 3 2 2 - - 3 - - -
2 - - - - - 2 3 2 2 - - 3 - - -
3 - - - - - 3 3 2 2 - - 3 - - -
4 - - - - - 2 3 2 2 - - 3 - - -
5 - - - - - 2 3 2 2 - - 3 - - -
TABLE OF CONTENTS

EX.NO DATE TITLE OF THE EXERCISE PAGE MARKS SIGNATURE


NO

DIVIDE AND CONQUER METHOD (UNIT-IV)

1 BINARY SEARCH

2 FIND MAXIMUM AND MINIMUM

GREEDY METHOD (UNIT IV)

3 KNAPSACK

4 MINIMUM SPANNING TREE USING


PRIM’S ALGORITHM

5 MINIMUM SPANNING TREE USING


KRUSKAL’S ALGORITHM

6 SINGLE SOURCE SHORTEST PATHS

DYNAMIC PROGRAMMING

7 ALL PAIR SHORTEST PATHS


TABLE OF CONTENTS

EX.NO DATE TITLE OF THE EXERCISE PAGE MARKS SIGNATURE


NO

8 TRAVELING SALESMAN PROBLEM

BACKTRACKING METHOD

9 N QUEEN OR 8 QUEENS PROBLEM

10 SUM OF SUBSETS

BRANCH AND BOUND

11 TRAVELING SALESMAN PROBLEM

CONTENT BEYOND THE SYLLABUS

12 TELECOMMUNICATION NETWORK
DESIGN

13 GRID-BASED ROBOTIC PATH PLANNING

14 SUDOKU SOLVER

15 VEHICLE ROUTING PROBLEM


Divide-and-Conquer
Technique
Divide and Conquer Algorithm involves breaking a larger problem into smaller subproblems, solving them
independently, and then combining their solutions to solve the original problem. The basic idea is to
recursively divide the problem into smaller subproblems until they become simple enough to be solved
directly. Once the solutions to the subproblems are obtained, they are then combined to produce the overall
solution.
Working of Divide and Conquer Algorithm:
The divide and Conquer Algorithm can be divided into three steps: Divide, Conquer, and Merge.
1. Divide:
Break down the original problem into smaller subproblems.
Each subproblem should represent a part of the overall problem.
The goal is to divide the problem until no further division is possible.
2. Conquer:
Solve each of the smaller subproblems individually.
If a subproblem is small enough (often referred to as the “base case”), we solve it directly without further
recursion.
The goal is to find solutions for these subproblems independently.
3. Merge:
Combine the sub-problems to get the final solution of the whole problem.
Once the smaller subproblems are solved, we recursively combine their solutions to get the solution to a
larger problem.
The goal is to formulate a solution for the original problem by merging the results from the subproblems.
Example Problems:
1. Binary search
2. Merge Sort
3. Quick Sort
4. Finding the maximum and minimum elements in an array
EX.NO:
DATE : BINARY SEARCH

AIM:

To find whether the given element is found in the array or not using the divide and conquer algorithm

PSEUDOCODE:

Binary_Search(a, lower_bound, upper_bound, val)


/* 'a' is the given array, 'lower_bound' is the index of the first array element, 'upper_bound' is the index of the last
array element, 'val' is the value to search */
Step 1: set beg = lower_bound, end = upper_bound, pos = - 1
Step 2: repeat steps 3 and 4 while beg <=end
Step 3: set mid = (beg + end)/2
Step 4: if a[mid] = val
set pos = mid
print pos
go to step 6
else if a[mid] > val
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
Step 5: if pos = -1
print "value is not present in the array"
[end of if]
Step 6: exit
PROGRAM:
#include<stdio.h>
#include<conio.h>
int a[30];
void binsearch(int,int,int);
void main()
{

int n,j,i,beg,end,mid,item,t;
clrscr();
printf("\nenter the number of element:");
scanf("%d",&n);
printf("\nenter the array element:");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);

}
printf("\nsorted array:");
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
if(a[i]>=a[j])
{

t=a[i];
a[i]=a[j];
a[j]=t;
}
}
printf("\n%d",a[i]);
}
printf("\nenter the search element:");
scanf("%d",&item);
binsearch(1,n,item);
getch();
}
void binsearch(int beg,int end,int item)

{
int mid,add,sub;
if(end>0)
{

if(beg==end)
{
if(item==a[beg])
printf("\nelement %d is found in position %d",item,beg);
else

printf("\nelement %d is not found",item);


}
else
{

mid=(beg+end)/2;
if(item==a[mid])
{

printf("\nelement %d is found in position


%d",item,mid); add=mid+1;
sub=mid-1; while((add<=end)||
(sub>=beg))
{
if(item==a[add])
{
printf(" and %d",add);
add=add+1;
}
else if(item==a[sub])
{

printf(" and
%d",sub); sub=sub-1;

exit(0);
}
}

else if(item<a[mid])
binsearch(beg,mid-1,item);
else

binsearch(mid+1,end,item);
}
}
else
printf("\n%d is not found",item);
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
EX.NO:
DATE: FINDING MAXIMUM AND MINIMUM

AIM:

To find the maximum and minimum element in an array using divide and conquer method

PSEUDOCODE:

MinMax(A, n){
int min = A[0];
int max = A[0];
for(int i = 1; i < n; i++){
if(max < A[i])
max = A[i];
else if(min > A[i])
min = A[i];
}
return (min, max);
}
Step 1: Find the mid of the array.
Step 2: Find the maximum and minimum of the left subarray recursively.
Step 3: Find the maximum and minimum of the right subarray recursively.
Step 4: Compare the result of step 3 and step 4
Step 5: Return the minimum and maximum.
PROGRAM:

#include<stdio.h>
#include<stdio.h>
int max, min;
int a[100];
void maxmin(int i, int j)
{
int max1, min1, mid; if(i==j)
{
max = min = a[i];
}
else
{
if(i == j-1)
{
if(a[i] <a[j])
{
max = a[j];
min = a[i];
}
else
{
max = a[i];
min = a[j];
}
}
else

mid = (i+j)/2; maxmin(i, mid);


max1 = max; min1 = min; maxmin(mid+1, j); if(max <max1)
max = max1; if(min > min1) min = min1;
}}}
int main ()
{
int i, num;
printf ("\nEnter the total number of numbers : "); scanf ("%d",&num);
printf ("Enter the numbers : \n");
for (i=1;i<=num;i++)
scanf ("%d",&a[i]); max = a[0];
min = a[0]; maxmin(1, num);
printf ("Minimum element in an array : %d\n", min);
printf ("Maximum element in an array : %d\n", max); return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
Greedy Technique
A greedy algorithm is a type of optimization algorithm that makes locally optimal choices at each step to find a
globally optimal solution. It operates on the principle of “taking the best option now” without considering the long-
term consequences.
Steps for Creating a Greedy Algorithm
Define the problem: Clearly state the problem to be solved and the objective to be optimized.
Identify the greedy choice: Determine the locally optimal choice at each step based on the current state.
Make the greedy choice: Select the greedy choice and update the current state.
Repeat: Continue making greedy choices until a solution is reached.
Following the given steps, one can learn how to use greedy algorithms to find optimal solutions.
Greedy Algorithm Examples
Fractional Knapsack: Optimizes the value of items that can be fractionally included in a knapsack with limited
capacity.
Dijkstra’s algorithm: Finds the shortest path from a source vertex to all other vertices in a weighted graph.
Prim’s and Kruskal’s algorithm: Finds the minimum spanning tree of a weighted graph.
Huffman coding: Compresses data by assigning shorter codes to more frequent symbols.
EX.NO:
DATE: KNAPSACK

AIM:
To solve the knapsack problem using greedy algorithm

PSEUDOCODE:

Fractional Knapsack (Array W, Array V, int M)


1. for i <- 1 to size (V)
2. calculate cost[i] <- V[i] / W[i]
3. Sort-Descending (cost)
4. i ← 1
5. while (i <= size(V))
6. if W[i] <= M
7. M ← M – W[i]
8. total ← total + V[i];
9. if W[i] > M
10. i ← i+1
PROGRAM:

#include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;

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


x[i] = 0.0;

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


if (weight[i] > u)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}

if (i < n)
x[i] = u / weight[i];

tp = tp + (x[i] * profit[i]);

printf("\nThe result vector is:- ");


for (i = 0; i < n; i++)
printf("%f\t", x[i]);

printf("\nMaximum profit is:- %f", tp);

int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;

printf("\nEnter the no. of objects:- ");


scanf("%d", &num);

printf("\nEnter the wts and profits of each object:- ");


for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}

printf("\nEnter the capacity of knapsack:- ");


scanf("%f", &capacity);

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


scanf("%f", &capacity);

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


ratio[i] = profit[i] / weight[i];
}

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


for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}

knapsack(num, weight, profit, capacity);


return(0);
}
INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
EX.NO:
DATE: MINIMUM SPANNING TREE USING PRIM’S

AIM:
To find the minimum spanning tree using Prim’s algorithm

PSEUDOCODE:

T = ∅;
U = { 1 };

let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;


while (U ≠ V)

T = T ∪ {(u, v)}
U = U ∪ {v}
The steps for implementing Prim's algorithm are as follows:
1. Initialize the minimum spanning tree with a vertex chosen at random.
2. Find all the edges that connect the tree to new vertices, find the minimum and add it to the tree
3. Keep repeating step 2 until we get a minimum spanning tree
PROGRAM:

#include<stdio.h>

#include<stdbool.h>

#define INF 9999999

// number of vertices in graph

#define V 5

// create a 2d array of size 5x5

//for adjacency matrix to represent graph

int G[V][V] = {

{0, 9, 75, 0, 0},


{9, 0, 95, 19, 42},

{75, 95, 0, 51, 66},

{0, 19, 51, 0, 31},

{0, 42, 66, 31, 0}};

int main() {

int no_edge; // number of edge

// create a array to track selected vertex

// selected will become true otherwise false

int selected[V];

// set selected false initially

memset(selected, false, sizeof(selected));

// set number of edge to 0

no_edge = 0;

// the number of egde in minimum spanning tree will be

// always less than (V -1), where V is number of vertices in graph

// choose 0th vertex and make it true

selected[0] = true;

int x; // row number

int y; // col number


// print for edge and weight

printf("Edge : Weight\n");

while (no_edge < V - 1) {

int min = INF;

x = 0;

y = 0;

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

if (selected[i]) {

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


if (!selected[j] && G[i][j]) {

if (min > G[i][j]) {

min = G[i][j];

x = i;

y = j;

printf("%d - %d : %d\n", x, y, G[x][y]);

selected[y] = true;

no_edge++;

return 0;

}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
EX.NO:
DATE: MINIMUM SPANNING TREE USING KRUSKAL’S ALGORITHM

AIM:

To find the minimum spanning tree using Kruskal’s algorithm

PSEUDOCODE:

A=∅
KRUSKAL(G):

For each vertex v ∈ G.V:

For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
MAKE-SET(v)

A = A ∪ {(u, v)}
if FIND-SET(u) ≠ FIND-SET(v):

UNION(u, v)
return A
PROGRAM:

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

int i, j, k, a, b, u, v, n, ne = 1;
int min, mincost = 0, cost[9][9], parent[9];
int find(int);
int uni(int, int);
void main() {
printf("\n\tImplementation of Kruskal's Algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d", & n);
printf("\nEnter the cost adjacency matrix:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", & cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");
while (ne < n) {
for (i = 1, min = 999; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
u = find(u);
v = find(v);
if (uni(u, v)) {
printf("%d edge (%d,%d) =%d\n", ne++, a, b, min);
mincost += min;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\n\tMinimum cost = %d\n", mincost);
getch();
}
int find(int i) {
while (parent[i])
i = parent[i];
return i;
}
int uni(int i, int j) {
if (i != j) {
parent[j] = i;
return 1;
}
return 0;
}
INPUT/OUTPUT:

w
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
EX.NO:
DATE: SINGLE-SOURCE SHORTEST PATH

AIM:
To find the shortest path from a single source using Dijkstra’s Algorithm

PSEUDOCODE:

function dijkstra(G, S)
for each vertex V in G
distance[V] <- infinite
previous[V] <- NULL
If V != S, add V to Priority Queue Q
distance[S] <- 0

while Q IS NOT EMPTY


U <- Extract MIN from Q
for each unvisited neighbour V of U
tempDistance <- distance[U] + edge_weight(U, V)
if tempDistance < distance[V]
distance[V] <- tempDistance
previous[V] <- U
return distance[], previous[]
PROGRAM:

#include <stdio.h>
#include <conio.h>
#define infinity
9999
void dij(int n,int v,int cost[10][10],int dist[])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++) f
lag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
min=dist[w],u=w; flag[u]=1; count++;
for(w=1;w<=n;w++)
if((dist[u]+cost[u][w]<dist[w]) && !flag[w])
dist[w]=dist[u]+cost[u][w];
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]); if(cost[i]
[j]==0) cost[i][j]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
INPUT/OUTPUT:
Enter the number of nodes: 7
Enter the cost matrix
0 0 1 2 0 0 0
0 0 2 0 0 3 0
1 2 0 1 3 0 0
2 0 1 0 0 0 1
0 0 3 0 0 2 0
0 3 0 0 2 0 1
0 0 0 1 0 1 0
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
Dynamic Programming
Technique
Dynamic Programming (DP) is a method used in mathematics and computer science to solve complex problems
by breaking them down into simpler subproblems. By solving each subproblem only once and storing the results,
it avoids redundant computations, leading to more efficient solutions for a wide range of problems.

How Does Dynamic Programming (DP)Work?


Identify Subproblems: Divide the main problem into smaller, independent subproblems.
Store Solutions: Solve each subproblem and store the solution in a table or array.
Build Up Solutions: Use the stored solutions to build up the solution to the main problem.
Avoid Redundancy: By storing solutions, DP ensures that each subproblem is solved only once, reducing
computation time.
Common Algorithms that Use Dynamic Programming:
Longest Common Subsequence (LCS): Finds the longest common subsequence between two strings.
Shortest Path in a Graph: Finds the shortest path between two nodes in a graph.
Knapsack Problem: Determines the maximum value of items that can be placed in a knapsack with a given capacity.
Matrix Chain Multiplication: Optimizes the order of matrix multiplication to minimize the number of operations.
Fibonacci Sequence: Calculates the nth Fibonacci number.
EX.NO: ALL PAIRS SHORTEST PATHS USING DYNAMIC PROGRAMMING
DATE: TECHNIQUE.

AIM:
To find the shortest path from all the vertices using Floyd-Warshall Algorithm (Dynamic Programming
Approach)

PSEUDOCODE:

n = no of vertices
A = matrix of dimension n*n
for k = 1 to n
for i = 1 to n
for j = 1 to n
Ak[i, j] = min (Ak-1[i, j], Ak-1[i, k] + Ak-1[k, j])
return A
PROGRAM:
// Floyd-Warshall Algorithm in C

#include <stdio.h>

// defining the number of vertices


#define nV 4

#define INF 999

void printMatrix(int matrix[][nV]);

// Implementing floyd warshall algorithm


void floydWarshall(int graph[][nV]) {
int matrix[nV][nV], i, j, k;

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


for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];

// Adding vertices individually


for (k = 0; k < nV; k++) {
for (i = 0; i < nV; i++) {
for (j = 0; j < nV; j++) {
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printMatrix(matrix);
}

void printMatrix(int matrix[][nV]) {


for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
floydWarshall(graph);
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
EX.NO:
DATE: TRAVELING SALESMAN PROBLEM USING DYNAMIC
PROGRAMMING

AIM:

To solve travelling salesman problem using dynamic programming

PSEUDOCODE:

Algorithm: Traveling-Salesman-Problem
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
for all j є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i є S and i ≠ j}
Return minj C ({1, 2, 3, …, n}, j) + d(j, i)
PROGRAM:

#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;
}
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);
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;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
Backtracking Technique
Backtracking is a problem-solving algorithmic technique that involves finding a solution incrementally by
trying different options and undoing them if they lead to a dead end. It is commonly used in situations where
you need to explore multiple possibilities to solve a problem, like searching for a path in a maze or solving
puzzles like Sudoku. When a dead end is reached, the algorithm backtracks to the previous decision point and
explores a different path until a solution is found or all possibilities have been exhausted.

Applications of Backtracking
Creating smart bots to play Board Games such as Chess.
Solving mazes and puzzles such as N-Queen problem.
Network Routing and Congestion Control.
Decryption
Text Justification
EX.NO:
N-QUEEN PROBLEM OR 8 QUEEN
DATE:
PROBLEM

AIM:
To solve N-queen problem using backtracking algorithm

PSEUDOCODE:

Place (k, i)
{
For j ← 1 to k - 1
do if (x [j] = i)
or (Abs x [j]) - i) = (Abs (j - k))
then return false;
return true;
}

N - Queens (k, n)
{
For i ← 1 to n
do if Place (k, i) then
{
x [k] ← i;
if (k ==n) then
write (x [1....n));
else
N - Queens (k + 1, n);
}
}
PROGRAM:

#include<stdio.h>
#define BOARD_SIZE 5
void displayChess(int chBoard[BOARD_SIZE][BOARD_SIZE]) {
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++)
printf("%d ", chBoard[row][col]);
printf("\n");
}
}
int isQueenPlaceValid(int chBoard[BOARD_SIZE][BOARD_SIZE], int crntRow, int crntCol) {
// checking if queen is in the left or not
for (int i = 0; i < crntCol; i++)
if (chBoard[crntRow][i])
return 0;
for (int i = crntRow, j = crntCol; i >= 0 && j >= 0; i--, j--)
//checking if queen is in the left upper diagonal or not
if (chBoard[i][j])
return 0;
for (int i = crntRow, j = crntCol; j >= 0 && i < BOARD_SIZE; i++, j--)
//checking if queen is in the left lower diagonal or not
if (chBoard[i][j])
return 0;
return 1;
}
int solveProblem(int chBoard[BOARD_SIZE][BOARD_SIZE], int crntCol) {
//when N queens are placed successfully
if (crntCol >= BOARD_SIZE)
return 1;
// checking placement of queen is possible or not
for (int i = 0; i < BOARD_SIZE; i++) {
if (isQueenPlaceValid(chBoard, i, crntCol)) {
//if validate, place the queen at place (i, col)
chBoard[i][crntCol] = 1;
//Go for the other columns recursively
if (solveProblem(chBoard, crntCol + 1))
return 1;
//When no place is vacant remove that queen
chBoard[i][crntCol] = 0;
}
}
return 0;
}
int displaySolution() {
int chBoard[BOARD_SIZE][BOARD_SIZE];
for(int i = 0; i < BOARD_SIZE; i++)
for(int j = 0; j < BOARD_SIZE; j++)
//set all elements to 0
chBoard[i][j] = 0;
//starting from 0th column
if (solveProblem(chBoard, 0) == 0) {
printf("Solution does not exist");
return 0;
displayChess(chBoard);
return 1;
}
int main() {
displaySolution();
return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25
RESULT:
EX.NO:
DATE: SUM OF SUBSETS

AIM:
To find the subset of a set is such a way that the element of the given subset sum up to a given
number K

PSEUDOCODE:

function subset_sum(list[],sum, starting_index, target_sum)


begin
if( target_sum == sum )

subset_countsubset_count+1;
if(starting_index < list.length)
subset_sum(list, sum - list[starting_index-1], starting_index, target_sum);
else

for i=strating_index to list.length do

subset_sum(list, sum + list[i], i + 1, target_sum);


end loop
end if

end
PROGRAM:

#include <stdio.h>

#include <stdbool.h>

void printSubset(int subset[], int size) {

printf("Subset: { ");

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

printf("%d ", subset[i]);

printf("}\n");

void sumOfSubsetsUtil(int weights[], int targetSum, int n, int subset[], int subsetSize, int

sum, int index) {

if (sum == targetSum) {

printSubset(subset, subsetSize);

return;

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

if (sum + weights[i] <= targetSum) {

subset[subsetSize] = weights[i];

sumOfSubsetsUtil(weights, targetSum, n, subset, subsetSize + 1, sum +

weights[i], i + 1);

}
void sumOfSubsets(int weights[], int targetSum, int n) {

int subset[n];

sumOfSubsetsUtil(weights, targetSum, n, subset, 0, 0, 0);

int main() {

int n, targetSum;

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

scanf("%d", &n);

int weights[n];

printf("Enter the elements: ");

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

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

} printf("Enter the target sum: ");

scanf("%d", &targetSum);

sumOfSubsets(weights, targetSum, n);

return 0;

}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
Branch-and-Bound
Technique
Branch and bound algorithms are used to find the optimal solution for combinatory, discrete, and general mathematical
optimization problems.
A branch and bound algorithm provides an optimal solution to an NP-Hard problem by exploring the entire search space.
Through the exploration of the entire search space, a branch and bound algorithm identify possible candidates for solutions
step-by-step.
Basic Concepts of Branch and Bound:
▸ Generation of a state space tree:
As in the case of backtracking, B&B generates a state space tree to efficiently search the solution space of a given problem
instance.In B&B, all children of an E-node in a state space tree are produced before any live node gets converted in an E-
node. Thus, the E-node remains an E-node until i becomes a dead node.
Evaluation of a candidate solution:
Unlike backtracking, B&B needs additional factors evaluate a candidate solution:
A way to assign a bound on the best values of the given criterion functions to each node in a state space tree: It is produced
by the addition of further omponents to the partial solution given by that node.
The best values of a given criterion function obtained so far: It describes the upper bound for the maximization problem and
the lower bound for the minimization problem.
A feasible solution is defined by the problem states that satisfy all the given constraints.
An optimal solution is a feasible solution, which produces the best value of a given objective function.
Bounding function :
It optimizes the search for a solution vector in the solution space of a given problem instance. It is a heuristic function that
evaluates the lower and upper bounds on the possible solutions at each node. The bound values are used to search the partial
solutions leading to an optimal solution. If a node does not produce a solution better than the best solution obtained thus far,
then it is abandoned without further exploration.The algorithm then branches to another path to get a better solution. The
desired solution to the problem is the value of the best solution produced so far.
Applications of Branch and Bound:
Combinatorial Optimization: Branch and Bound is widely used in solving combinatorial optimization problems such as the
Traveling Salesman Problem (TSP), Knapsack Problem, and Job Scheduling.
Constraint Satisfaction Problems: Branch and Bound can efficiently handle constraint satisfaction problems by
systematically exploring the search space and pruning branches based on constraints.
Resource Allocation: It’s applied in scenarios like resource allocation where resources need to be distributed optimally
among competing demands.
EX.NO:
DATE: TRAVELLING SALESMAN PROBLEM USING BRANCH AND BOUND

AIM:
To solve the traveling salesman problem using branch and bound algorithm

PSEUDOCODE:

function findMin(adj, i, n):


min = ∞
for k = 0 to n-1:
if adj[i][k] < min and i != k:
min = adj[i][k]
return min

function calculateBound(adj, path, level, n):


bound = 0
for i = 0 to level-1:
bound += adj[path[i]][path[i+1]]
for i = level to n-1:
bound += findMin(adj, path[i], n)
return bound

function copyToFinal(curr_path, n, final_path):


for i = 0 to n-1:
final_path[i] = curr_path[i]
final_path[n] = curr_path[0]

function TSPRec(adj, curr_bound, curr_weight, level, curr_path, visited, final_path, final_res, n):
if level == n:
if adj[curr_path[level-1]][curr_path[0]] != 0:
curr_res = curr_weight + adj[curr_path[level-1]][curr_path[0]]
if curr_res < final_res:
final_res = curr_res
copyToFinal(curr_path, n, final_path)
return

for i = 0 to n-1:
if adj[curr_path[level-1]][i] != 0 and not visited[i]:
temp = curr_bound
curr_weight += adj[curr_path[level-1]][i]

if level == 1:
curr_bound -= (findMin(adj, curr_path[level-1], n) + findMin(adj, i, n)) / 2
else:
curr_bound -= (findMin(adj, curr_path[level-1], n) + findMin(adj, i, n)) / 2
if curr_bound + curr_weight < final_res:
curr_path[level] = i
visited[i] = true
TSPRec(adj, curr_bound, curr_weight, level + 1, curr_path, visited, final_path, final_res, n)

curr_weight -= adj[curr_path[level-1]][i]
curr_bound = temp
for j = 0 to n-1:
visited[j] = false
for j = 0 to level-1:
visited[curr_path[j]] = true

function TSP(adj, n):


curr_path = new array[n+1]
visited = new array[n]
final_path = new array[n+1]
final_res = ∞
curr_bound = 0
for i = 0 to n-1:
visited[i] = false
curr_path[i] = -1

for i = 0 to n-1:
curr_bound += (findMin(adj, i, n) + findMin(adj, i, n))
curr_bound = curr_bound / 2

visited[0] = true
curr_path[0] = 0

TSPRec(adj, curr_bound, 0, 1, curr_path, visited, final_path, final_res, n)

print "Minimum cost: " + final_res


print "Path: "
for i = 0 to n:
print final_path[i] + " "
PROGRAM:

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

#define N 4 // Number of cities

// Function to find the minimum cost edge connected to the node


int findMin(int adj[N][N], int i, int n) {
int min = INT_MAX;
for (int k = 0; k < n; k++) {
if (adj[i][k] < min && i != k)
min = adj[i][k];
}
return min;
}

// Function to calculate the lower bound of the path


int calculateBound(int adj[N][N], int path[], int level, int n) {
int bound = 0;
for (int i = 0; i < level; i++) {
bound += adj[path[i]][path[i+1]];
}
for (int i = level; i < n; i++) {
bound += findMin(adj, path[i], n);
}
return bound;
}

// Function to copy temporary solution to the final solution


void copyToFinal(int curr_path[], int n, int final_path[]) {
for (int i = 0; i < n; i++) {
final_path[i] = curr_path[i];
}
final_path[n] = curr_path[0];
}

// Recursive function to solve TSP using Branch and Bound


void TSPRec(int adj[N][N], int curr_bound, int curr_weight, int level, int curr_path[], int visited[], int final_path[], int
*final_res, int n) {
if (level == n) {
if (adj[curr_path[level-1]][curr_path[0]] != 0) {
int curr_res = curr_weight + adj[curr_path[level-1]][curr_path[0]];
if (curr_res < *final_res) {
copyToFinal(curr_path, n, final_path);
*final_res = curr_res;
}
}
return;
}

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


if (adj[curr_path[level-1]][i] != 0 && visited[i] == 0) {
int temp = curr_bound;
curr_weight += adj[curr_path[level-1]][i];
if (level == 1) {
curr_bound -= ((findMin(adj, curr_path[level-1], n) + findMin(adj, i, n)) / 2);
} else {
curr_bound -= ((findMin(adj, curr_path[level-1], n) + findMin(adj, i, n)) / 2);
}

if (curr_bound + curr_weight < *final_res) {


curr_path[level] = i;
visited[i] = 1;

TSPRec(adj, curr_bound, curr_weight, level+1, curr_path, visited, final_path, final_res, n);


}

curr_weight -= adj[curr_path[level-1]][i];
curr_bound = temp;

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


visited[j] = 0;
}
for (int j = 0; j <= level-1; j++) {
visited[curr_path[j]] = 1;
}
}
}
}

// Function to solve TSP using Branch and Bound


void TSP(int adj[N][N], int n) {
int curr_path[N+1];
int visited[N];
int final_path[N+1];
int final_res = INT_MAX;

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


visited[i] = 0;
curr_path[i] = -1;
}

int curr_bound = 0;
for (int i = 0; i < n; i++) {
curr_bound += (findMin(adj, i, n) + findMin(adj, i, n));
}
curr_bound = (curr_bound & 1) ? curr_bound/2 + 1 : curr_bound/2;

visited[0] = 1;
curr_path[0] = 0;
TSPRec(adj, curr_bound, 0, 1, curr_path, visited, final_path, &final_res, n);
printf("Minimum cost: %d\n", final_res);
printf("Path: ");
for (int i = 0; i <= n; i++) {
printf("%d ", final_path[i]);
}
printf("\n");
}
int main()
{
int adj[N][N] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};

TSP(adj, N);

return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
Content Beyond the Syllabus
EX.NO: Telecommunication Network Design
DATE:

Description
Consider a real time scenario, where the network designers need to connect five cities with the
least cost in terms of laying down optical fiber cables. The cities and the costs (in units) of laying cables between
them are represented in a weighted graph.
Here is the adjacency matrix for the graph:
A B C D

A 0 2 3 0

B 2 0 4 1

C 3 4 0 5

D 0 1 5 0

E 0 0 6 7
PSEUDOCODE:

#include <stdio.h>
#define MAX 30
typedef struct edge {
int u, v, w;
} edge;
typedef struct edge_list {
edge data[MAX];
int n;
} edge_list;
edge_list elist;
int Graph[MAX][MAX], n;
edge_list spanlist;
void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();
// Applying Krushkal Algo
void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}
}
sort();
for (i = 0; i < n; i++)
belongs[i] = i;
spanlist.n = 0;
for (i = 0; i < elist.n; i++) {
cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);
if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}}
int find(int belongs[], int vertexno) {
return (belongs[vertexno]);
}
void applyUnion(int belongs[], int c1, int c2) {
int i;
for (i = 0; i < n; i++)
if (belongs[i] == c2)
belongs[i] = c1;}

void sort() {
int i, j;
edge temp;
for (i = 1; i < elist.n; i++)
for (j = 0; j < elist.n - 1; j++)
if (elist.data[j].w > elist.data[j + 1].w) {
temp = elist.data[j];
elist.data[j] = elist.data[j + 1];
elist.data[j + 1] = temp;
}
}
void print() {
int i, cost = 0;
for (i = 0; i < spanlist.n; i++) {
printf("\n%d - %d : %d", spanlist.data[i].u, spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}
printf("\nSpanning tree cost: %d", cost);
}
int main() {
int i, j, total_cost,n;
n = 6;
Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;

Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 4;
Graph[4][4] = 4;

Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
kruskalAlgo();

print();
}

INPUT/OUTPUT:
CRITERIA MAX.MARKS MARKS OBTAINED
AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:

EX.NO:
DATE:
Grid-Based Robotic Path Planning Using
Dynamic Programming

AIM:
Consider a grid-world where an agent needs to find the shortest path from a start position to a goal
position. The agent can move in four directions (up, down, left, right) and each movement has a cost
associated with it.

PSEUDOCODE:
#include <stdio.h>
#include <limits.h>
#define ROWS 5
#define COLS 5
#define INF INT_MAX
int min(int a, int b) {
return (a < b) ? a : b;
}
// Function to find the shortest path from start to goal in a grid
int shortestPath(int grid[ROWS][COLS], int startX, int startY, int goalX, int goalY) {
// DP table to store minimum cost to reach each cell
int dp[ROWS][COLS];

// Initialize DP table with infinity


for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
dp[i][j] = INF;
}
}

// Cost to reach the starting cell is the cost of the starting cell itself
dp[startX][startY] = grid[startX][startY];

// Fill the DP table


for (int i = startX; i <= goalX; ++i) {
for (int j = startY; j <= goalY; ++j) {
if (i > startX || j > startY) {
if (i > startX) {
dp[i][j] = min(dp[i][j], dp[i-1][j] + grid[i][j]);
}
if (j > startY) {
dp[i][j] = min(dp[i][j], dp[i][j-1] + grid[i][j]);
}
}
}
}

// Return the minimum cost to reach the goal cell


return dp[goalX][goalY];
}

int main() {
int grid[ROWS][COLS] = {
{3, 2, 8, 6, 4},
{6, 7, 3, 9, 2},
{5, 1, 5, 2, 7},
{8, 4, 1, 3, 4},
{4, 3, 2, 1, 9}
};

int startX = 0, startY = 0;


int goalX = ROWS - 1, goalY = COLS - 1;
int minCost = shortestPath(grid, startX, startY, goalX, goalY);

printf("Minimum cost to reach from (%d, %d) to (%d, %d) is: %d\n", startX, startY, goalX, goalY, minCost);

return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
EX.NO:
Sudoku Solver
DATE:

Objective: Solve a given 9x9 Sudoku puzzle using backtracking.

Tasks:

1. Implement a Sudoku solver using backtracking.


2. Read an input Sudoku puzzle from a file or user input.
3. Display the solved Sudoku grid.

PSEUDOCODE:
#include <stdio.h>
#include <stdbool.h>

#define N 9

// Function to print the Sudoku grid


void printGrid(int grid[N][N]) {
for (int row = 0; row < N; row++) {
for (int col = 0; col < N; col++) {
printf("%d ", grid[row][col]);
}
printf("\n");
}
}

// Function to check if it is safe to place a number in the given cell


bool isSafe(int grid[N][N], int row, int col, int num) {
// Check if the number is not already present in the current row
for (int x = 0; x < N; x++) {
if (grid[row][x] == num) {
return false;
}
}

// Check if the number is not already present in the current column


for (int x = 0; x < N; x++) {
if (grid[x][col] == num) {
return false;
}
}

// Check if the number is not already present in the current 3x3 box
int startRow = row - row % 3, startCol = col - col % 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (grid[i + startRow][j + startCol] == num) {
return false;
}
}
}

return true;
}

// Function to find an empty cell in the grid


bool findEmptyCell(int grid[N][N], int *row, int *col) {
for (*row = 0; *row < N; (*row)++) {
for (*col = 0; *col < N; (*col)++) {
if (grid[*row][*col] == 0) {
return true;
}
}
}
return false;
}

// Function to solve the Sudoku puzzle using backtracking


bool solveSudoku(int grid[N][N]) {
int row, col;

// If there is no empty cell, we are done


if (!findEmptyCell(grid, &row, &col)) {
return true;
}

// Try numbers from 1 to 9 in the current empty cell


for (int num = 1; num <= 9; num++) {
if (isSafe(grid, row, col, num)) {
grid[row][col] = num;

// Recursively try to solve the rest of the grid


if (solveSudoku(grid)) {
return true;
}

// If the current number does not lead to a solution, undo the move
grid[row][col] = 0;
}
}

// Trigger backtracking
return false;
}

// Driver code
int main() {
int grid[N][N] = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};

if (solveSudoku(grid) == true) {
printGrid(grid);
} else {
printf("No solution exists\n");
}

return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:
EX.NO:
DATE: Vehicle Routing Problem

Problem Description:
 We have a set of customers with known demands.
 A fleet of vehicles, each with a maximum capacity.
 A single depot where all vehicles start and end.
 The goal is to minimize the total distance traveled while servicing all customers without exceeding
vehicle capacities.
This example will use a simplified problem with a small number of customers and vehicles.

PSEUDOCODE:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define N 4 // Number of customers + 1 depot
#define K 2 // Number of vehicles
#define INF INT_MAX
// Structure to represent a vehicle
typedef struct {
int capacity;
int load;
int position;
} Vehicle;
// Structure to represent the problem instance
typedef struct {
int distances[N][N];
int demands[N];
Vehicle vehicles[K];
int best_cost;
int best_route[N];
} VRPInstance;
// Function to initialize the VRP instance
void initializeVRP(VRPInstance *vrp) {
// Example distances (symmetric matrix)
int distances[N][N] = {
{0, 10, 15, 20},
{10, 0, 35, 25},
{15, 35, 0, 30},
{20, 25, 30, 0}
};
// Example demands
int demands[N] = {0, 10, 15, 10}; // Depot has 0 demand
// Example vehicle capacities
for (int i = 0; i < K; i++) {
vrp->vehicles[i].capacity = 20;
vrp->vehicles[i].load = 0;
vrp->vehicles[i].position = 0; // All start at the depot
}
// Copy the data to the VRP instance
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
vrp->distances[i][j] = distances[i][j];
}
vrp->demands[i] = demands[i];
}
vrp->best_cost = INF;
}
// Function to calculate the cost of the current route
int calculateCost(VRPInstance *vrp, int route[N]) {
int cost = 0;
for (int i = 0; i < N - 1; i++) {
cost += vrp->distances[route[i]][route[i + 1]];
}
cost += vrp->distances[route[N - 1]][route[0]]; // Return to depot
return cost;
}

// Recursive function to implement the Branch and Bound algorithm


void branchAndBound(VRPInstance *vrp, int route[N], int visited[N], int level) {
if (level == N) {
int cost = calculateCost(vrp, route);
if (cost < vrp->best_cost) {
vrp->best_cost = cost;
for (int i = 0; i < N; i++) {
vrp->best_route[i] = route[i];
}
}
return;
}

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


if (!visited[i]) {
visited[i] = 1;
route[level] = i;
branchAndBound(vrp, route, visited, level + 1);
visited[i] = 0;
}
}
}
int main() {
VRPInstance vrp;
initializeVRP(&vrp);

int route[N] = {0}; // Start from the depot


int visited[N] = {0};
visited[0] = 1; // Depot is visited

branchAndBound(&vrp, route, visited, 1);

printf("Best cost: %d\n", vrp.best_cost);


printf("Best route: ");
for (int i = 0; i < N; i++) {
printf("%d ", vrp.best_route[i]);
}
printf("\n");

return 0;
}
INPUT/OUTPUT:

CRITERIA MAX.MARKS MARKS OBTAINED


AIM & ALGORITHM 5
EXECUTION & OUTPUT 10
VIVA 10
TOTAL 25

RESULT:

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