0% found this document useful (0 votes)
7 views8 pages

Pikachu

The document contains multiple C programming implementations of various algorithms, including Shortest Job First scheduling, Fractional Knapsack, Maximum Independent Set on a tree, Strassen's Matrix Multiplication, and Dijkstra's Algorithm. Each section provides code that demonstrates the algorithm's functionality, along with example inputs and outputs. The document serves as a resource for understanding and implementing these algorithms in C.

Uploaded by

231fa04400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

Pikachu

The document contains multiple C programming implementations of various algorithms, including Shortest Job First scheduling, Fractional Knapsack, Maximum Independent Set on a tree, Strassen's Matrix Multiplication, and Dijkstra's Algorithm. Each section provides code that demonstrates the algorithm's functionality, along with example inputs and outputs. The document serves as a resource for understanding and implementing these algorithms in C.

Uploaded by

231fa04400
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Shortest Job First (SJF) Scheduling (Non-Preemptive)

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int id, duration;
} Job;

int compare(const void *a, const void *b) {


return ((Job *)a)->duration - ((Job *)b)->duration;
}

void JobScheduling(Job jobs[], int n) {


qsort(jobs, n, sizeof(Job), compare);
int total_time = 0, waiting_time = 0, completion_time = 0;
for (int i = 0; i < n; i++) {
waiting_time += total_time;
total_time += jobs[i].duration;
completion_time += total_time;
}
printf("Total Time: %d\n", total_time);
printf("Average Waiting Time: %.2f\n", (double)waiting_time / n);
printf("Average Turnaround Time: %.2f\n", (double)completion_time / n);
}

int main() {
Job jobs[] = {{1, 3}, {2, 1}, {3, 2}};
int n = sizeof(jobs) / sizeof(jobs[0]);
JobScheduling(jobs, n);
return 0;
}

---

2. Fractional Knapsack

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int weight, value;
} Item;

int compare(const void *a, const void *b) {


double r1 = (double)((Item *)b)->value / ((Item *)b)->weight;
double r2 = (double)((Item *)a)->value / ((Item *)a)->weight;
return (r1 > r2) - (r1 < r2);
}

double Knapsack(Item items[], int n, int max_weight) {


qsort(items, n, sizeof(Item), compare);
double total_value = 0.0;
for (int i = 0; i < n; i++) {
if (items[i].weight <= max_weight) {
total_value += items[i].value;
max_weight -= items[i].weight;
} else {
total_value += (double)items[i].value * max_weight / items[i].weight;
break;
}
}
return total_value;
}

int main() {
Item items[] = {{10, 60}, {20, 100}, {30, 120}};
int n = sizeof(items) / sizeof(items[0]);
int max_weight = 50;
printf("Maximum Value: %.2f\n", Knapsack(items, n, max_weight));
return 0;
}

---

3. Maximum Independent Set on a Tree

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int weight;
struct Node *left, *right;
} Node;

Node* createNode(int weight) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->weight = weight;
newNode->left = newNode->right = NULL;
return newNode;
}

int M(Node *node) {


if (node == NULL) return 0;
int include = node->weight;
if (node->left) include += M(node->left->left) + M(node->left->right);
if (node->right) include += M(node->right->left) + M(node->right->right);
int exclude = M(node->left) + M(node->right);
return include > exclude ? include : exclude;
}

int main() {
Node* root = createNode(10);
root->left = createNode(1);
root->right = createNode(2);
root->left->left = createNode(3);
printf("Max Weight: %d\n", M(root));
return 0;
}

---

4. Strassen Matrix Multiplication

#include <stdio.h>
#include <stdlib.h>

void addMatrix(int n, int A[n][n], int B[n][n], int C[n][n]) {


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] + B[i][j];
}

void subtractMatrix(int n, int A[n][n], int B[n][n], int C[n][n]) {


for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] - B[i][j];
}

void strassen(int n, int A[n][n], int B[n][n], int C[n][n]) {


if (n == 1) {
C[0][0] = A[0][0] * B[0][0];
return;
}
int mid = n / 2;
int A11[mid][mid], A12[mid][mid], A21[mid][mid], A22[mid][mid];
int B11[mid][mid], B12[mid][mid], B21[mid][mid], B22[mid][mid];
int P1[mid][mid], P2[mid][mid], P3[mid][mid], P4[mid][mid], P5[mid][mid],
P6[mid][mid], P7[mid][mid];
int temp1[mid][mid], temp2[mid][mid];

addMatrix(mid, A11, A22, temp1);


addMatrix(mid, B11, B22, temp2);
strassen(mid, temp1, temp2, P1);

addMatrix(mid, A21, A22, temp1);


strassen(mid, temp1, B11, P2);

subtractMatrix(mid, B12, B22, temp1);


strassen(mid, A11, temp1, P3);

subtractMatrix(mid, B21, B11, temp1);


strassen(mid, A22, temp1, P4);

addMatrix(mid, A11, A12, temp1);


strassen(mid, temp1, B22, P5);

subtractMatrix(mid, A21, A11, temp1);


addMatrix(mid, B11, B12, temp2);
strassen(mid, temp1, temp2, P6);

subtractMatrix(mid, A12, A22, temp1);


addMatrix(mid, B21, B22, temp2);
strassen(mid, temp1, temp2, P7);
}

int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int C[2][2];
strassen(2, A, B, C);
printf("%d %d\n%d %d\n", C[0][0], C[0][1], C[1][0], C[1][1]);
return 0;
}
---

5. Calculate MST After Weight Increase

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int u, v, w;
} Edge;

int compare(const void *a, const void *b) {


return ((Edge *)a)->w - ((Edge *)b)->w;
}

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 rootX = find(parent, x), rootY = find(parent, y);
if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;
else { parent[rootY] = rootX; rank[rootX]++; }
}

int kruskal(int n, Edge edges[], int m) {


qsort(edges, m, sizeof(Edge), compare);
int parent[n], rank[n], mst_cost = 0, count = 0;
for (int i = 0; i < n; i++) parent[i] = i, rank[i] = 0;
for (int i = 0; i < m; i++) {
if (find(parent, edges[i].u) != find(parent, edges[i].v)) {
unionSet(parent, rank, edges[i].u, edges[i].v);
mst_cost += edges[i].w;
count++;
if (count == n - 1) break;
}
}
return mst_cost;
}

Here are the remaining C programs for questions 6 to 10:

---

6. Calculate MST After Weight Increase

#include <stdio.h>
#include <stdlib.h>

typedef struct {
int u, v, w;
} Edge;
int compare(const void *a, const void *b) {
return ((Edge *)a)->w - ((Edge *)b)->w;
}

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 rootX = find(parent, x), rootY = find(parent, y);
if (rank[rootX] > rank[rootY]) parent[rootY] = rootX;
else if (rank[rootX] < rank[rootY]) parent[rootX] = rootY;
else { parent[rootY] = rootX; rank[rootX]++; }
}

int kruskal(int n, Edge edges[], int m) {


qsort(edges, m, sizeof(Edge), compare);
int parent[n], rank[n], mst_cost = 0, count = 0;
for (int i = 0; i < n; i++) parent[i] = i, rank[i] = 0;
for (int i = 0; i < m; i++) {
if (find(parent, edges[i].u) != find(parent, edges[i].v)) {
unionSet(parent, rank, edges[i].u, edges[i].v);
mst_cost += edges[i].w;
count++;
if (count == n - 1) break;
}
}
return mst_cost;
}

int main() {
int n = 4, m = 5;
Edge edges[] = {{0, 1, 2}, {1, 2, 3}, {0, 2, 1}, {2, 3, 4}, {1, 3, 5}};
for (int i = 0; i < m; i++) edges[i].w += 1;
printf("New MST Cost: %d\n", kruskal(n, edges, m));
return 0;
}

---

7. 0/1 Knapsack Problem (Dynamic Programming)

#include <stdio.h>

int knapsack(int W, int wt[], int val[], int n) {


int dp[n+1][W+1];
for (int i = 0; i <= n; i++) {
for (int w = 0; w <= W; w++) {
if (i == 0 || w == 0) dp[i][w] = 0;
else if (wt[i - 1] <= w) dp[i][w] = (val[i - 1] + dp[i - 1][w - wt[i -
1]] > dp[i - 1][w]) ? val[i - 1] + dp[i - 1][w - wt[i - 1]] : dp[i - 1][w];
else dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}
int main() {
int val[] = {60, 100, 120}, wt[] = {10, 20, 30}, W = 50;
int n = sizeof(val) / sizeof(val[0]);
printf("Maximum Value: %d\n", knapsack(W, wt, val, n));
return 0;
}

---

8. Max Weighted Independent Set in a Tree

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {


int weight;
struct Node *left, *right;
} Node;

Node* createNode(int weight) {


Node* newNode = (Node*)malloc(sizeof(Node));
newNode->weight = weight;
newNode->left = newNode->right = NULL;
return newNode;
}

int M(Node *node) {


if (node == NULL) return 0;
int include = node->weight;
if (node->left) include += M(node->left->left) + M(node->left->right);
if (node->right) include += M(node->right->left) + M(node->right->right);
int exclude = M(node->left) + M(node->right);
return include > exclude ? include : exclude;
}

int main() {
Node* root = createNode(10);
root->left = createNode(1);
root->right = createNode(2);
root->left->left = createNode(3);
printf("Max Weight: %d\n", M(root));
return 0;
}

---

9. Strassen�s Matrix Multiplication

#include <stdio.h>
#include <stdlib.h>

void strassenMultiply(int n, int A[n][n], int B[n][n], int C[n][n]) {


if (n == 2) {
C[0][0] = A[0][0] * B[0][0] + A[0][1] * B[1][0];
C[0][1] = A[0][0] * B[0][1] + A[0][1] * B[1][1];
C[1][0] = A[1][0] * B[0][0] + A[1][1] * B[1][0];
C[1][1] = A[1][0] * B[0][1] + A[1][1] * B[1][1];
return;
}
}

int main() {
int A[2][2] = {{1, 2}, {3, 4}}, B[2][2] = {{5, 6}, {7, 8}}, C[2][2];
strassenMultiply(2, A, B, C);
printf("%d %d\n%d %d\n", C[0][0], C[0][1], C[1][0], C[1][1]);
return 0;
}

---

10. Dijkstra�s Algorithm for Shortest Path

#include <stdio.h>
#include <limits.h>

#define V 5

int minDistance(int dist[], int sptSet[]) {


int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (!sptSet[v] && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}

void dijkstra(int graph[V][V], int src) {


int dist[V], sptSet[V];
for (int i = 0; i < V; i++) dist[i] = INT_MAX, sptSet[i] = 0;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);
sptSet[u] = 1;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
for (int i = 0; i < V; i++) printf("Vertex %d -> Distance: %d\n", i, dist[i]);
}

int main() {
int graph[V][V] = {
{0, 10, 0, 30, 100},
{10, 0, 50, 0, 0},
{0, 50, 0, 20, 10},
{30, 0, 20, 0, 60},
{100, 0, 10, 60, 0}
};
dijkstra(graph, 0);
return 0;
}

---
These are the remaining C implementations for questions 6 to 10. Let me know if you
need any modifications or explanations!

You might also like