Program 7-Single Source Shortest Path
Program 7-Single Source Shortest Path
Aim: Compare the performance of single source shortest paths using greedy method when the
graph is represented by adjacency matrix and adjacency lists
Program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <time.h>
return min_index;
int dist[V]; // dist[i] will hold the shortest distance from src to i
dist[i] = INT_MAX;
visited[i] = 0;
dist[src] = 0;
visited[u] = 1;
struct Node {
int dest;
int weight;
};
// Adjacency list
struct AdjList {
};
struct Graph {
};
newNode->dest = dest;
newNode->weight = weight;
newNode->next = NULL;
return newNode;
graph->array[i].head = NULL;
return graph;
void addEdge(struct Graph* graph, int src, int dest, int weight) {
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
int dist[V];
int visited[V];
dist[i] = INT_MAX;
visited[i] = 0;
dist[src] = 0;
visited[u] = 1;
while (temp) {
int v = temp->dest;
temp = temp->next;
}
}
int main()
int matrix[V][V] = {
};
addEdge(graph, 0, 1, 10);
addEdge(graph, 0, 2, 20);
addEdge(graph, 1, 3, 50);
addEdge(graph, 1, 4, 10);
addEdge(graph, 2, 3, 20);
addEdge(graph, 2, 4, 33);
addEdge(graph, 3, 4, 2);
addEdge(graph, 4, 3, 2);
dijkstraMatrix(matrix, 0);
start = clock();
dijkstraList(graph, 0);
end = clock();
return 0;
Output:
Program:
#include <stdio.h>
#include <stdlib.h>
struct Job {
};
int maxDeadline = 0;
return maxDeadline;
// Array to store the result (sequence of jobs) and keep track of free time slots
int result[maxDeadline];
int slot[maxDeadline];
slot[i] = 0;
// Find a free slot for this job (starting from the last possible slot)
if (slot[j] == 0) {
break;
}
}
if (slot[i] == 1) {
int main() {
{1, 2, 100}, {2, 1, 19}, {3, 2, 27}, {4, 1, 25}, {5, 3, 15}
};
jobSequencing(jobs, n);
return 0;
Output:
Program:
#include <stdio.h>
return (a > b) ? a : b;
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else {
int w = W;
w -= weights[i - 1];
return dp[n][W];
int main() {
int n, W;
scanf("%d", &n);
scanf("%d", &W);
scanf("%d", &weights[i]);
scanf("%d", &values[i]);
return 0;
Output:
10 20 30
60 100 120
______________________________________________________________________________
Program:
#include <stdio.h>
#include <stdbool.h>
printf("\n");
printf("\n");
return false;
if (board[i][j])
return false;
if (board[i][j])
return false;
return true;
if (row >= N)
return true;
board[row][col] = 1;
return true;
board[row][col] = 0;
}
// If queen cannot be placed in any column in this row, return false
return false;
void solveNQueens() {
if (!solveNQueensUtil(board, 0)) {
return;
printSolution(board);
int main()
solveNQueens();
return 0;
Q.......
....Q...
......Q.
..Q.....
.....Q..
.Q......
...Q....
.......Q
11. Aim : To use backtracking strategy to solve 0/1 knapsack problem
Program:
#include <stdio.h>
void knapsackBacktracking(int index, int weight, int profit, int capacity, int weights[], int profits[], int
n, int count) {
maxProfit = profit;
bestItems[i] = currentItems[i];
if (index == n) return;
int main() {
int n, capacity;
scanf("%d", &n);
scanf("%d", &capacity);
scanf("%d", &weights[i]);
scanf("%d", &profits[i]);
return 0;
Output:
3456
________________________________________________________________________
12. Aim : To implement Travelling Salesman problem using Branch and Bound approach
Program:
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
printf("%d\n", path[0]);
min = graph[u][i];
}
return min;
if (graph[u][i] != 0) {
second = first;
first = graph[u][i];
second = graph[u][i];
return second;
void tspBranchAndBound(int graph[N][N], int currBound, int currWeight, int level, int currPath[], bool
visited[], int *minCost, int finalPath[]) {
// Base case: If all cities are visited, return to the starting city
if (level == N) {
if (graph[currPath[level - 1]][currPath[0]] != 0) {
*minCost = currRes;
finalPath[i] = currPath[i];
finalPath[N] = currPath[0];
}
return;
if (level == 1) {
} else {
currPath[level] = i;
visited[i] = true;
// Backtracking
currBound = temp;
visited[i] = false;
}
// Function to set up and start the TSP solution
bool visited[N];
int currBound = 0;
visited[i] = false;
visited[0] = true;
currPath[0] = 0;
printPath(finalPath);
int main() {
int graph[N][N] = {
};
solveTSP(graph);
return 0;
Output:
Minimum cost: 80