Lab 1-1
Lab 1-1
paths from 0 to
other vertices using Dijkstra’s algorithm and output its time
complexity.
#include <stdio.h>
#define INFINITY 9999
#define MAX 10
int main() {
int G[MAX][MAX], i, j, n, u;
if (u < 0 || u >= n) {
printf("Invalid starting node.\n");
return 1;
}
dijkstra(G, n, u);
return 0;
}
// Initializing arrays
for(i = 0; i < n; i++) {
distance[i] = cost[startnode][i];
pred[i] = startnode;
visited[i] = 0;
}
distance[startnode] = 0;
visited[startnode] = 1;
count = 1;
// Dijkstra's algorithm
while(count < n - 1) {
mindistance = INFINITY;
visited[nextnode] = 1;
j = i;
do {
j = pred[j];
printf(" <- %d", j);
} while(j != startnode);
}
}
}
Output
Enter the number of vertices (max 10): 4
Distance of node 1 = 10
Path = 1 <- 0
Distance of node 2 = 15
Path = 2 <- 0
Distance of node 3 = 35
Path = 3 <- 1 <- 0
#include <stdio.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 main() {
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the number of vertices: ");
scanf("%d", &n);
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;
}
return 0;
}
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;
}
Output
Implementation of Kruskal's algorithm
Minimum cost = 50
#include <stdio.h>
#include <stdlib.h>
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
inorder(root);
preorder(root);
postorder(root);
levelOrder(root);
return 0;
}
Output
Inorder Traversal: 4 2 5 1 6 3 7
Preorder Traversal: 1 2 4 5 3 6 7
Postorder Traversal: 4 5 2 6 7 3 1
Level Order Traversal: 1 2 3 4 5 6 7
Time Complexity for Inorder, Preorder, Postorder Traversals: O(n)
Time Complexity for Level Order Traversal: O(n)
12))Find a subset of a given set S = { sl, s2, ……,sn } of n positive
integers whose
sum is equal to a given positive integer d and output its time
complexity.
For example, if S= {1, 2, 5, 6, 8} and d = 9 there are two solutions {1,
2, 6} and
{1,8}. A suitable message is to be displayed if the given problem
instance
doesn't have a solution
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
if (i == 0) {
if (dp[i][j]) {
printf("%d ", set[i]);
}
return;
}
if (d >= MAX) {
printf("Desired sum exceeds maximum allowed value.\n");
return;
}
int main() {
int set[MAX], n, d;
int i;
if (d < 0) {
printf("The target sum must be a positive integer.\n");
return 1;
}
return 0;
}
Output
#include <stdio.h>
#include <limits.h>
#define MAX 20
#define INF 1000000
int dist[MAX][MAX];
int dp[1 << MAX][MAX];
int n;
if (dp[mask][pos] != -1) {
return dp[mask][pos];
}
int main() {
printf("Enter the number of cities: ");
scanf("%d", &n);
int result = tsp(1, 0); // Start from the first city with only the first city visited
printf("Minimum cost: %d\n", result);
return 0;
}
Output
Enter the number of cities: 4
Enter the distance matrix: 0 1 2 9
1 0 6 4
2 6 0 3
9 4 3 0
Minimum cost: 13
14))Find Minimum Cost Spanning Tree of a given undirected graph using Prim’s
algorithm and output its time complexity.
#include <stdio.h>
#include <limits.h>
int main() {
int graph[MAX][MAX], n, i, j;
return 0;
}
#include <stdio.h>
#include <limits.h>
// Floyd-Warshall Algorithm
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (dist[i][k] != INF && dist[k][j] != INF &&
dist[i][k] + dist[k][j] < dist[i][j]) {
dist[i][j] = dist[i][k] + dist[k][j];
}
}
}
}
int main() {
int graph[MAX][MAX], n, i, j;
return 0;
}
Output
Enter the number of vertices: 3
Enter the adjacency matrix:
014
102
420
Shortest distances between every pair of vertices:
0 1 3
1 0 2
3 2 0
16)))Implement N Queen's problem using Back Tracking and output its time
complexity
#include <stdio.h>
#include <stdbool.h>
#define MAX 20
int board[MAX][MAX];
int N;
return true;
}
return false;
}
int main() {
printf("Enter the number of queens (N): ");
scanf("%d", &N);
if (solveNQueens(0)) {
printf("Solution to %d-Queens problem:\n", N);
printBoard();
} else {
printf("No solution exists for %d-Queens problem.\n", N);
}
return 0;
}
Output