DAA Termworkkk
DAA Termworkkk
PROBLEM STATEMENT:
Given a (directed/undirected) graph, design an algorithm and implement it using a program to
find if a path exists between two given vertices or not. (Hint: use DFS)
OBJECTIVE:
To design and implement an algorithm using DFS (Depth First Search) to determine
whether a path exists between two given vertices in a directed or undirected graph.
ALGORITHM:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return graph;
}
void addEdge(struct Graph* graph, int src, int dest, bool isDirected) {
struct Node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
if (!isDirected) {
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
}
bool DFS(struct Graph* graph, int src, int dest) {
graph->visited[src] = true;
if (src == dest)
return true;
struct Node* temp = graph->adjLists[src];
while (temp != NULL) {
int connectedVertex = temp->vertex;
if (!graph->visited[connectedVertex]) {
if (DFS(graph, connectedVertex, dest))
return true;
}
temp = temp->next;
}
return false;
}
int main() {
int vertices, edges;
int isDirected;
printf("Enter number of vertices: ");
scanf("%d", &vertices);
printf("Is the graph directed? (1 for Yes, 0 for No): ");
scanf("%d", &isDirected);
OBJECTIVE:
To design and implement a program that solves the Fractional Knapsack Problem using a
greedy algorithm. Given weights and values of items along with a maximum weight
capacity of a knapsack, determine the maximum total value that can be carried, allowing
fractions of items to be included.
ALGORITHM:
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Item {
int value, weight;
};
int compare(const void* a, const void* b) {
double r1 = (double)((struct Item*)a)->value / ((struct Item*)a)->weight;
double r2 = (double)((struct Item*)b)->value / ((struct Item*)b)->weight;
return (r2 > r1) - (r2 < r1);
}
double fractionalKnapsack(int capacity, struct Item items[], int n) {
qsort(items, n, sizeof(struct Item), compare);
double totalValue = 0.0;
return 0;
}
14.PROBLEM STATEMENT:
You are given a set of jobs, each with a specific deadline and associated profit. The objective
is to determine the maximum profit achievable by scheduling these jobs within their
respective deadlines, ensuring that each job is completed before its deadline. Importantly,
each job takes exactly one unit of time to complete, and only one job can be scheduled at a
time. (Job Sequencing problem)
OBJECTIVE:
To schedule a set of jobs, each with a deadline and a profit, such that the total profit is
maximized. Each job takes exactly one unit of time, and only one job can be scheduled at
any given time. A job must be completed on or before its deadline.
ALGORITHM:
CODE:
#include <stdio.h>
#include <stdlib.h>
struct Job {
char id[5];
int deadline;
int profit;
};
int compare(const void* a, const void* b) {
return ((struct Job*)b)->profit - ((struct Job*)a)->profit;
}.
3
int min(int a, int b) {
return (a < b) ? a : b;
}
int main() {
int n;
printf("Enter number of jobs: ");
scanf("%d", &n);
int maxDeadline = 0;
for (int i = 0; i < n; i++) {
if (jobs[i].deadline > maxDeadline)
maxDeadline = jobs[i].deadline;
}
int slots[maxDeadline];
for (int i = 0; i < maxDeadline; i++)
slots[i] = -1;
int totalProfit = 0;
printf("\nScheduled Jobs: ");
for (int i = 0; i < n; i++) {
for (int j = min(jobs[i].deadline, maxDeadline-) 1; j >= 0; j--) {
if (slots[j] == -1) {
slots[j] = i;
totalProfit += jobs[i].profit;
printf("%s ", jobs[i].id);
break;
}
}
}
15.PROBLEM STATEMENT:
You are given a connected, undirected graph with weighted edges. The objective is to find a
Minimum Spanning Tree (MST) of the graph by Kruskal’s algorithm. An MST is a subset
of the edges that connects all the vertices together, without any cycles, and with the minimum
possible total edge weight. (Graph input as adjacency matrix)
OBJECTIVE:
To find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph
using Kruskal’s algorithm. An MST includes all vertices with the minimum total edge
weight and no cycles.
ALGORITHM:
CODE:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct Edge {
int src, dest, weight;
};
int compare(const void* a, const void* b) {
return ((struct Edge*)a)->weight - ((struct Edge*)b)->weight;
}
int find(int parent[], int i) {
if (parent[i] != i)
parent[i] = find(parent, parent[i]);
return parent[i];
}
void unionSet(int parent[], int rank[], int x, int y) {
int xroot = find(parent, x);
int yroot = find(parent, y);
int main() {
int V;
int adj[MAX][MAX];
struct Edge edges[MAX * MAX];
int edgeCount = 0;
if (setU != setV) {
printf("%d - %d (Weight: %d)\n", u, v, edges[i].weight);
totalWeight += edges[i].weight;
unionSet(parent, rank, setU, setV);
edgesInMST++;
}
}
printf("Total weight of MST: %d\n", totalWeight);
return 0;
}
16.PROBLEM STATEMENT:
You are given a connected, undirected graph with weighted edges. The objective is to find a
Minimum Spanning Tree (MST) of the graph by Prim’s algorithm. An MST is a subset of
the edges that connects all the vertices together, without any cycles, and with the minimum
possible total edge weight. (Graph input as adjacency matrix)
OBJECTIVE:
To find the Minimum Spanning Tree (MST) of a connected, undirected, weighted graph
using Prim’s algorithm. The MST connects all the vertices with the minimum total edge
weight and no cycles, using a greedy approach that grows the MST one vertex at a time.
ALGORITHM:
CODE:
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#define MAX 100
17.PROBLEM STATEMENT:
You are given a connected, weighted, and directed graph. The objective is to find the shortest
path from a specified source vertex to all other vertices in the graph. Write a program to solve
the shortest path problem using Dijkstra's Algorithm. Your program should compute and
print the shortest distances from the source vertex to all other vertices.
OBJECTIVE:
To find the shortest path from a given source vertex to all other vertices in a connected,
weighted, directed graph using Dijkstra’s algorithm. The goal is to minimize the total
edge weight from the source to each vertex.
ALGORITHM:
CODE:
#include <stdio.h>
#include <limits.h>
#include <stdbool.h>
#define MAX 100
#define INF 99999
return 0;
}
18.PROBLEM STATEMENT:
You are given a connected, directed graph with weighted edges. The objective is to find the
shortest path from a specified source vertex to all other vertices in the graph. Write a program
to solve the single-source shortest path problem using Bellman-Ford Algorithm. Your
program should compute and print the shortest distances from the source vertex to all other
vertices and indicate whether the graph contains a negative-weight cycle.
OBJECTIVE:
To find the shortest path from a specified source vertex to all other vertices in a
connected, directed, weighted graph using the Bellman-Ford Algorithm. The algorithm
also detects if the graph contains a negative-weight cycle.
ALGORITHM:
CODE:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define INF 99999
struct Edge {
int src, dest, weight;
};
void bellmanFord(struct Edge edges[], int V, int E, int src) {
int dist[V];
for (int i = 0; i < V; i++)
dist[i] = INF;
dist[src] = 0;
for (int i = 1; i <= V - 1; i++) {
for (int j = 0; j < E; j++) {
int u = edges[j].src;
int v = edges[j].dest;
int w = edges[j].weight;
OBJECTIVE:
To find the shortest paths between all pairs of vertices in a connected, directed, weighted
graph using the Floyd-Warshall Algorithm. The algorithm computes the shortest paths
between every pair of vertices by considering each vertex as an intermediate vertex, and
progressively improving the shortest path estimates.
ALGORITHM:
CODE:
#include <stdio.h>
#define MAX 100
#define INF 99999
floydWarshall(graph, V);
return 0;
}
20.PROBLEM STATEMENT:
You are given a knapsack that can hold a total weight of W. You have a set of n items, each
with a weight wi and a value vi. Your goal is to determine the maximum total value you can
carry in your knapsack without exceeding its weight capacity. This is a classic example of the
0/1 Knapsack Problem, where each item can either be included or excluded from the
knapsack. Write a program to solve the 0/1 Knapsack Problem using Dynamic
Programming. Your program should compute and print the maximum total value that can be
obtained without exceeding the knapsack's weight capacity.
OBJECTIVE:
To find the maximum value that can be carried in a knapsack with a given weight capacity,
using a set of items, each with a weight and a value. The goal is to maximize the total value
without exceeding the knapsack's weight capacity, using the 0/1 Knapsack Problem
approach. We will solve this using Dynamic Programming.
ALGORITHM:
CODE:
#include <stdio.h>
#define MAX_ITEMS 100
#define MAX_WEIGHT 1000
int main() {
int n, W;
int weights[MAX_ITEMS], values[MAX_ITEMS];
return 0;
}
21.PROBLEM STATEMENT:
The 8 Queens Problem is a classic puzzle in chess and computer science. The objective is to
place eight queens on an 8×8 chessboard such that no two queens threaten each other. In
chess, a queen can attack another queen if they share the same row, column, or diagonal. The
challenge is to find all possible arrangements where no two queens can attack each other.
Write a program to solve the 8 Queens Problem using Backtracking. Your program should
compute and print all possible configurations where eight queens can be placed on the
chessboard without threatening each other.
OBJECTIVE:
The 8 Queens Problem is to place 8 queens on an 8x8 chessboard such that no two queens
threaten each other. A queen can attack another queen if they share the same row, column, or
diagonal. The goal is to find all possible configurations where no two queens can attack each
other using Backtracking.
ALGORITHM:
CODE:
#include <stdio.h>
#include <math.h>
#define SIZE 8
int board[SIZE];
int solutionCount = 0;
int isSafe(int row, int col) {
for (int i = 0; i < row; i++) {
if (board[i] == col || abs(board[i] - col) == abs(i - row))
return 0;
}
return 1;
}
void solve(int row) {
if (row == SIZE) {
solutionCount++;
printf("\nSolution %d:\n", solutionCount);
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (board[i] == j)
printf("Q ");
else
printf(". ");
}
printf("\n");
}
return;
}
for (int col = 0; col < SIZE; col++) {
if (isSafe(row, col)) {
board[row] = col;
solve(row + 1);
}
}
}
int main() {
solve(0);
printf("\nTotal number of solutions: %d\n", solutionCount);
return 0;
}