Professional Documents
Culture Documents
Q1:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Function to find the vertex with minimum distance value, from the set of vertices not yet
included in shortest path tree
int minDistance(int dist[], int sptSet[]) {
int min = INT_MAX, min_index;
return min_index;
}
// Function to print the final shortest path from source vertex to all other vertices
void printSolution(int dist[]) {
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Function that implements Dijkstra's single source shortest path algorithm for a graph
represented using adjacency matrix representation
void dijkstra(int graph[V][V], int src) {
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
int sptSet[V]; // sptSet[i] will be 1 if vertex i is included in shortest path tree or shortest distance
from src to i is finalized
int main() {
int graph[V][V] = { {0, 7, 9, 0, 0, 14},
{7, 0, 10, 15, 0, 0},
{9, 10, 0, 11, 0, 2},
{0, 15, 11, 0, 6, 0},
{0, 0, 0, 6, 0, 9},
{14, 0, 2, 0, 9, 0} };
return 0;
}
Q2:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Function to print the final shortest path from source vertex to all other vertices
void printSolution(int dist[]) {
printf("Vertex \t\t Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t\t %d\n", i, dist[i]);
}
// Function that implements Bellman-Ford algorithm for a graph represented using adjacency
matrix representation
void bellmanFord(int graph[V][V], int src) {
int dist[V]; // The output array. dist[i] will hold the shortest distance from src to i
// Relax all edges V-1 times. A simple shortest path from src to any other vertex can have at
most V-1 edges
for (int i = 0; i < V - 1; i++) {
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
if (graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
}
}
// Check for negative-weight cycles. If we get a shorter path, then there is a cycle
for (int u = 0; u < V; u++) {
for (int v = 0; v < V; v++) {
if (graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) {
printf("Graph contains negative-weight cycle\n");
return;
}
}
}
int main() {
return 0;
}
Q3:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
// Function to find the shortest path with exactly k edges from source to destination
int shortestPathWithKEdges(int graph[V][V], int src, int dest, int k) {
int dp[V][V][k+1]; // 3D array to store the shortest distance between each pair of vertices with k
edges on the path
// Initialize the 3D array with infinity for all pairs of vertices with k edges
for (int e = 0; e <= k; e++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
dp[i][j][e] = INT_MAX;
}
}
}
// Initialize the 3D array with the weight of the direct edges between the vertices
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if (graph[i][j] != 0) {
dp[i][j][1] = graph[i][j];
}
}
}
// Compute the shortest distance between each pair of vertices with exactly k edges on the
path
for (int e = 2; e <= k; e++) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
for (int x = 0; x < V; x++) {
if (graph[i][x] != 0 && dp[x][j][e-1] != INT_MAX) {
dp[i][j][e] = (dp[i][j][e] < dp[i][x][1] + dp[x][j][e-1]) ? dp[i][j][e] : (dp[i][x][1] + dp[x][j]
[e-1]);
}
}
}
}
}
// Return the shortest distance between the source and destination vertices with exactly k
edges on the path
return dp[src][dest][k];
}
int main() {
// The example graph from the assignment
int graph[V][V] = { {0, 10, 3, 0},
{0, 0, 0, 7},
{0, 0, 0, 6},
{0, 0, 0, 0} };
int shortestPath = shortestPathWithKEdges(graph, src, dest, k); // Find the weight of the
shortest path
if (shortestPath == INT_MAX) {
printf("There is no path with exactly %d edges from vertex %d to vertex %d\n", k, src, dest);
} else {
printf("The weight of the shortest path with exactly %d edges from vertex %d to vertex %d is
%d\n", k, src, dest, shortestPath);
}
return 0;
}
WEEK 8: Solution
Q1:
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
visited[start_city] = true;
int edges[MAX_CITIES];
int num_edges = 0;
num_edges--;
edges[min_cost_dest] = edges[num_edges];
}
return min_cost;
}
int main() {
int num_cities;
printf("Enter the number of cities: ");
scanf("%d", &num_cities);
int graph[MAX_CITIES][MAX_CITIES];
printf("Enter the adjacency matrix:\n");
return 0;
}
Q2:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
Q3:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Edge {
int src, dest, weight;
};
struct Subset {
int parent;
int rank;
};
if (src_parent != dest_parent) {
max_spanning_weight += edges[i].weight;
unionSets(subsets, src_parent, dest_parent);
}
}
free(subsets);
return max_spanning_weight;
}
int main() {
int num_vertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &num_vertices);
int edge_index = 0;
WEEK 9: Solutions
Q1:
#include <stdio.h>
#include <stdbool.h>
int main() {
int num_vertices;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &num_vertices);
int graph[MAX_VERTICES][MAX_VERTICES];
printf("Enter the adjacency matrix:\n");
floyd_warshall(graph, num_vertices);
return 0;
}
Q2:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
struct Item {
int weight;
int value;
double ratio;
};
if (capacity == 0)
break;
}
printf("item-weight\n");
for (int i = 0; i < num_items; i++) {
if (items[i].weight <= capacity) {
printf("%d-%d\n", i + 1, items[i].weight);
} else {
printf("%d-%.1lf\n", i + 1, (double)(items[i].value * capacity) / items[i].weight);
}
if (capacity == 0)
break;
}
}
int main() {
int num_items;
printf("Enter the number of items: ");
scanf("%d", &num_items);
int capacity;
printf("Enter the maximum capacity of the knapsack: ");
scanf("%d", &capacity);
return 0;
}
Q3:
#include <stdio.h>
#include <stdlib.h>
while (n > 1) {
// Take the two smallest elements
int smallest1 = arr[0];
int smallest2 = arr[1];
return total_cost;
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the array elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
return 0;
}
Q1:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int index;
int startTime;
int finishTime;
} Activity;
int count = 1;
int lastSelected = 0;
int main() {
int n;
printf("Enter the number of activities: ");
scanf("%d", &n);
int startTimes[n];
printf("Enter the starting times of the activities: ");
for (int i = 0; i < n; i++) {
scanf("%d", &startTimes[i]);
}
int finishTimes[n];
printf("Enter the finishing times of the activities: ");
for (int i = 0; i < n; i++) {
scanf("%d", &finishTimes[i]);
}
return 0;
}
Q2:
#include <stdio.h>
int main() {
int n;
printf("Enter the total number of tasks: ");
scanf("%d", &n);
int timeTaken[n];
printf("Enter the time taken for each task: ");
for (int i = 0; i < n; i++) {
scanf("%d", &timeTaken[i]);
}
int deadlines[n];
printf("Enter the deadlines for each task: ");
for (int i = 0; i < n; i++) {
scanf("%d", &deadlines[i]);
}
return 0;
}
Q3:
#include <stdio.h>
#include <stdlib.h>
count = 0;
for (int i = 0; i < n; i++) {
if (elements[i] == candidate) {
count++;
}
}
printf("Majority element: %s\n", (count > n / 2) ? "yes" : "no");
printf("Median: ");
if (n % 2 == 1) {
printf("%d\n", elements[n / 2]);
} else {
int median1 = elements[n / 2 - 1];
int median2 = elements[n / 2];
printf("%d\n", (median1 + median2) / 2);
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int elements[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &elements[i]);
}
findMajorityAndMedian(n, elements);
return 0;
}
Q1:
#include <stdio.h>
#include <limits.h>
return dp[1][n];
}
int main() {
int n;
printf("Enter the number of matrices: ");
scanf("%d", &n);
int dimensions[MAX_SIZE][2];
printf("Enter the dimensions of the matrices:\n");
for (int i = 0; i < n; i++) {
scanf("%d %d", &dimensions[i][0], &dimensions[i][1]);
}
return 0;
}
Q2:
#include <stdio.h>
dp[0] = 1;
return dp[N];
}
int main() {
int numCoins;
printf("Enter the number of coins: ");
scanf("%d", &numCoins);
int coinValues[MAX_SIZE];
printf("Enter the values of the coins: ");
for (int i = 0; i < numCoins; i++) {
scanf("%d", &coinValues[i]);
}
int N;
printf("Enter the value of N: ");
scanf("%d", &N);
return 0;
}
Q3:
#include <stdio.h>
#include <stdbool.h>
if (totalSum % 2 != 0) {
return false;
}
return dp[n][targetSum];
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int set[MAX_SIZE];
printf("Enter the elements of the set: ");
for (int i = 0; i < n; i++) {
scanf("%d", &set[i]);
}
if (isPossible) {
printf("yes\n");
} else {
printf("no\n");
}
return 0;
}