FINAL DAA MANUAL 2024 For Presentation
FINAL DAA MANUAL 2024 For Presentation
ARTIFICIAL INTELLIGENCE
AND
DATA SCIENCE
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
Implementation of Minimum Spanning Tree using Prim’s and Kruskal’s Algorithm 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.
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
1 BINARY SEARCH
3 KNAPSACK
DYNAMIC PROGRAMMING
BACKTRACKING METHOD
10 SUM OF SUBSETS
12 TELECOMMUNICATION NETWORK
DESIGN
14 SUDOKU SOLVER
AIM:
To find whether the given element is found in the array or not using the divide and conquer algorithm
PSEUDOCODE:
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
mid=(beg+end)/2;
if(item==a[mid])
{
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:
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
AIM:
To solve the knapsack problem using greedy algorithm
PSEUDOCODE:
#include<stdio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
AIM:
To find the minimum spanning tree using Prim’s algorithm
PSEUDOCODE:
T = ∅;
U = { 1 };
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 V 5
int G[V][V] = {
int main() {
int selected[V];
no_edge = 0;
selected[0] = true;
printf("Edge : Weight\n");
x = 0;
y = 0;
if (selected[i]) {
min = G[i][j];
x = i;
y = j;
selected[y] = true;
no_edge++;
return 0;
}
INPUT/OUTPUT:
AIM:
PSEUDOCODE:
A=∅
KRUSKAL(G):
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
#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.
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>
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:
RESULT:
EX.NO:
DATE: TRAVELING SALESMAN PROBLEM USING DYNAMIC
PROGRAMMING
AIM:
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;
}
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:
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:
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:
subset_countsubset_count+1;
if(starting_index < list.length)
subset_sum(list, sum - list[starting_index-1], starting_index, target_sum);
else
end
PROGRAM:
#include <stdio.h>
#include <stdbool.h>
printf("Subset: { ");
printf("}\n");
void sumOfSubsetsUtil(int weights[], int targetSum, int n, int subset[], int subsetSize, int
if (sum == targetSum) {
printSubset(subset, subsetSize);
return;
subset[subsetSize] = weights[i];
weights[i], i + 1);
}
void sumOfSubsets(int weights[], int targetSum, int n) {
int subset[n];
int main() {
int n, targetSum;
scanf("%d", &n);
int weights[n];
scanf("%d", &weights[i]);
scanf("%d", &targetSum);
return 0;
}
INPUT/OUTPUT:
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 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
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
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
curr_weight -= adj[curr_path[level-1]][i];
curr_bound = temp;
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:
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];
// Cost to reach the starting cell is the cost of the starting cell itself
dp[startX][startY] = grid[startX][startY];
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}
};
printf("Minimum cost to reach from (%d, %d) to (%d, %d) is: %d\n", startX, startY, goalX, goalY, minCost);
return 0;
}
INPUT/OUTPUT:
RESULT:
EX.NO:
Sudoku Solver
DATE:
Tasks:
PSEUDOCODE:
#include <stdio.h>
#include <stdbool.h>
#define N 9
// 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;
}
// 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:
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;
}
return 0;
}
INPUT/OUTPUT:
RESULT: