Program: (Floyd Warshal)
#include <stdio.h>
// Define the number of vertices
#define nV 4
// Define the infinite value for the graph
#define INF 999
// Function to print the matrix
void printMatrix(int matrix[][nV]) {
for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}
// Floyd Warshall Algorithm
void floydWarshall(int graph[][nV]) {
int matrix[nV][nV], i, j, k;
// Initialize the matrix with the given graph
for (i = 0; i < nV; i++)
for (j = 0; j < nV; j++)
matrix[i][j] = graph[i][j];
// Adding vertices individually
for (k = 0; k < nV; k++) {
for (i = 0; i < nV; i++) {
for (j = 0; j < nV; j++) {
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printMatrix(matrix);
}
int main() {
int graph[nV][nV] = {
{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}
};
floydWarshall(graph);
return 0;
}
Output:
0 3 7 5
2 0 6 4
3 1 0 5
5 3 2 0
Program: (Travelling Salesman Problem)
#include <stdio.h>
#include <stdlib.h>
// Define the number of vertices
#define nV 4
// Define the infinite value for the graph
#define INF 999
// Function to print the matrix
void printMatrix(int matrix[][nV]) {
for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if (matrix[i][j] == INF)
printf("%4s", "INF");
else
printf("%4d", matrix[i][j]);
}
printf("\n");
}
}
// Function to calculate the minimum cost
int minCost(int cost[][nV], int completed[nV]) {
int i, nc = 999, min = 999, kmin;
for (i = 0; i < nV; i++) {
if ((cost[0][i] != 0) && (completed[i] == 0)) {
if (cost[0][i] < min) {
min = cost[0][i];
kmin = cost[0][i];
nc = i;
}
}
}
if (min != INF)
cost[0][nc] += kmin;
return nc;
}
// Function to solve the TSP using dynamic programming
void tspDynamic(int cost[][nV]) {
int i, j, k, completed[nV];
int matrix[nV][nV], mincost = 0;
// Initialize the matrix with the given graph
for (i = 0; i < nV; i++)
for (j = 0; j < nV; j++)
matrix[i][j] = cost[i][j];
// Calculate the minimum cost for each subset
for (i = 1; i < nV; i++) {
completed[i - 1] = 0;
for (j = 0; j < nV; j++) {
if ((cost[0][j] != 0) && (completed[j] == 0)) {
if (cost[0][j] < matrix[0][j]) {
matrix[0][j] = cost[0][j];
}
}
}
}
// Calculate the minimum cost for the remaining vertices
for (i = 1; i < nV; i++) {
completed[i - 1] = 1;
for (j = 0; j < nV; j++) {
if ((cost[0][j] != 0) && (completed[j] == 0)) {
if (cost[0][j] < matrix[0][j]) {
matrix[0][j] = cost[0][j];
}
}
}
}
// Print the minimum cost matrix
printMatrix(matrix);
// Calculate the total minimum cost
for (i = 0; i < nV; i++) {
mincost += matrix[0][i];
}
printf("\nMinimum cost: %d\n", mincost);
}
int main() {
int cost[nV][nV] = {
{0, 7, 3, 12},
{7, 0, 2, 9},
{3, 2, 0, 5},
{12, 9, 5, 0}
};
tspDynamic(cost);
return 0;
}
Output:
0 7 3 12
7 0 2 9
3 2 0 5
12 9 5 0
Program: (Tree Traversals)
#include <stdio.h>
// Define the structure for a node
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to perform inorder traversal
void inorderTraversal(struct Node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->data);
inorderTraversal(root->right);
}
// Function to perform preorder traversal
void preorderTraversal(struct Node* root) {
if (root == NULL) return;
printf("%d ->", root->data);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// Function to perform postorder traversal
void postorderTraversal(struct Node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->data);
}
int main() {
struct Node* root = createNode(1);
root->left = createNode(12);
root->right = createNode(9);
root->left->left = createNode(5);
root->left->right = createNode(6);
printf("Inorder traversal: ");
inorderTraversal(root);
printf("\n");
printf("Preorder traversal: ");
preorderTraversal(root);
printf("\n");
printf("Postorder traversal: ");
postorderTraversal(root);
printf("\n");
return 0;
}
Output:
Inorder traversal: 5 ->12 ->6 ->1 ->9 ->
Preorder traversal: 1 ->12 ->5 ->6 ->9 ->
Postorder traversal: 5 ->6 ->12 ->9 ->1 ->
Program: (Graph Traversals)
#include <stdio.h>
#include <stdlib.h>
// Define the number of vertices
#define nV 7
// Define the structure for a node
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// Function to perform BFS traversal
void bfsTraversal(struct Node* root) {
if (root == NULL) return;
struct Node* queue[nV];
int front = 0, rear = 0;
int visited[nV];
for (int i = 0; i < nV; i++) {
visited[i] = 0;
}
queue[rear++] = root;
visited[root->data] = 1;
while (front < rear) {
struct Node* node = queue[front++];
printf("%d ->", node->data);
struct Node* temp = node->next;
while (temp != NULL) {
if (!visited[temp->data]) {
queue[rear++] = temp;
visited[temp->data] = 1;
}
temp = temp->next;
}
}
printf("\n");
}
// Function to perform DFS traversal
void dfsTraversal(struct Node* root) {
if (root == NULL) return;
struct Node* stack[nV];
int top = -1;
int visited[nV];
for (int i = 0; i < nV; i++) {
visited[i] = 0;
}
stack[++top] = root;
visited[root->data] = 1;
while (top >= 0) {
struct Node* node = stack[top--];
printf("%d ->", node->data);
struct Node* temp = node->next;
while (temp != NULL) {
if (!visited[temp->data]) {
stack[++top] = temp;
visited[temp->data] = 1;
}
temp = temp->next;
}
}
printf("\n");
}
int main() {
struct Node* root = createNode(3);
root->next = createNode(0);
root->next->next = createNode(4);
root->next->next->next = createNode(1);
root->next->next->next->next = createNode(2);
root->next->next->next->next->next = createNode(5);
root->next->next->next->next->next->next = createNode(6);
printf("BFS traversal: ");
bfsTraversal(root);
printf("DFS traversal: ");
dfsTraversal(root);
return 0;
}
Output:
BFS traversal: 3 ->0 ->4 ->1 ->2 ->5 ->6 ->
DFS traversal: 3 ->6 ->5 ->2 ->1 ->4 ->0 ->
Program (Multi-Stage Graph):
#include <stdio.h>
#include <limits.h>
#define NUM_STAGES 3
#define NUM_NODES 4
// Function to find the minimum cost path
int findMinCost(int graph[NUM_STAGES][NUM_NODES][NUM_NODES], int source, int
destination) {
int cost[NUM_STAGES][NUM_NODES];
int prev[NUM_STAGES][NUM_NODES];
int i, j, k;
// Initialize the cost and prev arrays
for (i = 0; i < NUM_STAGES; i++) {
for (j = 0; j < NUM_NODES; j++) {
cost[i][j] = INT_MAX;
prev[i][j] = -1;
}
}
// Set the cost for the source node in the first stage
cost[0][source] = 0;
// Iterate through the stages
for (i = 1; i < NUM_STAGES; i++) {
for (j = 0; j < NUM_NODES; j++) {
for (k = 0; k < NUM_NODES; k++) {
if (cost[i-1][k] != INT_MAX && cost[i-1][k] + graph[i-1][k][j] < cost[i][j]) {
cost[i][j] = cost[i-1][k] + graph[i-1][k][j];
prev[i][j] = k;
}
}
}
}
// Find the minimum cost path
int minCost = cost[NUM_STAGES-1][destination];
int path[NUM_STAGES];
int pathLength = 0;
// Reconstruct the minimum cost path
i = NUM_STAGES - 1;
j = destination;
while (i >= 0) {
path[pathLength++] = j;
j = prev[i][j];
i--;
}
// Print the minimum cost path
printf("Minimum cost path: ");
for (i = pathLength - 1; i >= 0; i--) {
printf("%d ", path[i]);
}
printf("\nMinimum cost: %d\n", minCost);
return minCost;
}
int main() {
int graph[NUM_STAGES-1][NUM_NODES][NUM_NODES] = {
{
{0, 2, 3, 4},
{1, 0, 1, 3},
{2, 1, 0, 2},
{3, 2, 1, 0}
},
{
{0, 1, 2, 3},
{2, 0, 1, 2},
{3, 1, 0, 1},
{4, 2, 1, 0}
}
};
int source = 0;
int destination = 3;
findMinCost(graph, source, destination);
return 0;
}
Output:
Minimum cost path: 0 0 3
Minimum cost: 3
Program: (N – queens)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int col;
char board[4][4];
int leftrow[4];
int upperDiagonal[7];
int lowerDiagonal[7];
int n;
} Solution;
void solve(int col, Solution *s, int n) {
if (col == n) {
printf("Arrangement %d:\n", s->col);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%c ", s->board[i][j]);
}
printf("\n");
}
printf("\n");
return;
}
for (int row = 0; row < n; row++) {
if (s->leftrow[row] == 0 && s->lowerDiagonal[row + col] == 0 && s->upperDiagonal[n
- 1 + col - row] == 0) {
s->board[row][col] = 'Q';
s->leftrow[row] = 1;
s->lowerDiagonal[row + col] = 1;
s->upperDiagonal[n - 1 + col - row] = 1;
solve(col + 1, s, n);
s->board[row][col] = '.';
s->leftrow[row] = 0;
s->lowerDiagonal[row + col] = 0;
s->upperDiagonal[n - 1 + col - row] = 0;
}
}
}
void solveNQueens(int n) {
Solution s;
s.n = n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
s.board[i][j] = '.';
}
}
for (int i = 0; i < n; i++) {
s.leftrow[i] = 0;
}
for (int i = 0; i < 2 * n - 1; i++) {
s.upperDiagonal[i] = 0;
s.lowerDiagonal[i] = 0;
}
s.col = 0;
solve(0, &s, n);
}
int main() {
int n = 4; // we are taking 4*4 grid and 4 queens
solveNQueens(n);
return 0;
}
Output:
Arrangement 0:
..Q.
Q...
...Q
.Q..
Arrangement 0:
.Q..
...Q
Q...
..Q.
Program: (Sum of Subsets)
#include <stdio.h>
#include <stdlib.h>
static int total_nodes;
void printValues(int A[], int size){
for (int i = 0; i < size; i++) {
printf("%*d", 5, A[i]);
}
printf("\n");
}
void subset_sum(int s[], int t[], int s_size, int t_size, int sum, int ite, int const target_sum){
total_nodes++;
if (target_sum == sum) {
printValues(t, t_size);
subset_sum(s, t, s_size, t_size - 1, sum - s[ite], ite + 1, target_sum);
return;
}
else {
for (int i = ite; i < s_size; i++) {
t[t_size] = s[i];
subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);
}
}
}
void generateSubsets(int s[], int size, int target_sum){
int* tuplet_vector = (int*)malloc(size * sizeof(int));
subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum);
free(tuplet_vector);
}
int main(){
int set[] = { 5, 6, 12 , 54, 2 , 20 , 15 };
int size = sizeof(set) / sizeof(set[0]);
printf("The set is ");
printValues(set , size);
generateSubsets(set, size, 25);
printf("Total Nodes generated %d\n", total_nodes);
return 0;
}
Output:
The set is 5 6 12 54 2 20 15
5 6 12 2
5 20
Total Nodes generated 127
Graph Coloring Problem Program:
#include <stdio.h>
#include <stdbool.h>
#define V 4
// Function to check if the colored
// graph is safe or not
bool isSafe(int v, bool graph[V][V],
int color[], int c)
{
for (int i = 0; i < V; i++)
if (graph[v][i] && c == color[i])
return false;
return true;
}
// Function to recursively solve the
// m-coloring problem
bool graphColoringUtil(bool graph[V][V], int m,
int color[], int v)
{
if (v == V)
return true;
for (int c = 1; c <= m; c++)
{
if (isSafe(v, graph, color, c))
{
color[v] = c;
if (graphColoringUtil(graph, m, color, v + 1) == true)
return true;
color[v] = 0;
}
}
return false;
}
// Main function to solve the m-coloring problem
void graphColoring(bool graph[V][V], int m)
{
int color[V];
for (int i = 0; i < V; i++)
color[i] = 0;
if (graphColoringUtil(graph, m, color, 0) == false)
{
printf("Solution does not exist");
return;
}
printf("Solution exists. The coloring of vertices is:\n");
for (int i = 0; i < V; i++)
printf("Vertex %d -> Color %d\n", i, color[i]);
}
// Sample input
int main()
{
// Graph represented as adjacency matrix
bool graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0}};
// Number of colors
int m = 3;
// Call the function to solve the m-coloring problem
graphColoring(graph, m);
return 0;
}
Output:
The coloring of vertices is:
Vertex 0 -> Color 1
Vertex 1 -> Color 2
Vertex 2 -> Color 3
Vertex 3 -> Color 2
Page Replacement Program : (FIFO)
#include <stdio.h>
#include <stdlib.h>
void fcfs(int frames, int pages[], int n) {
int pageFaults = 0;
int frame[frames];
int index = 0; // To keep track of the oldest frame
for (int i = 0; i < frames; i++) {
frame[i] = -1;
}
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}
if (!found) {
pageFaults++;
frame[index] = pages[i];
index = (index + 1) % frames; // Move to the next frame in a circular manner
}
}
printf("Page Faults in FCFS: %d\n", pageFaults);
}
int main() {
int frames = 3;
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
fcfs(frames, pages, n);
return 0;
}
Output:
Page Faults in FCFS: 9
Program: (Optimal Policy)
#include<stdio.h>
void optimal(int frames, int pages[], int n) {
int pageFaults = 0;
int frame[frames];
for (int i = 0; i < frames; i++) {
frame[i] = -1;
}
for (int i = 0; i < n; i++) {
int found = 0;
for (int j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
break;
}
}
if (!found) {
pageFaults++;
// Find the frame to be replaced
int replaceIndex = -1;
int farthest = i + 1;
for (int j = 0; j < frames; j++) {
int k;
for (k = i + 1; k < n; k++) {
if (frame[j] == pages[k]) {
if (k > farthest) {
farthest = k;
replaceIndex = j;
}
break;
}
}
if (k == n) {
replaceIndex = j;
break;
}
}
if (replaceIndex == -1) {
for (int j = 0; j < frames; j++) {
if (frame[j] == -1) {
replaceIndex = j;
break;
}
}
}
frame[replaceIndex] = pages[i];
}
}
printf("Page Faults in Optimal: %d\n", pageFaults);
}
int main() {
int frames = 3;
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
optimal(frames, pages, n);
return 0;
}
Output:
Page Faults in Optimal: 7
Program: (LRU)
#include<stdio.h>
void lru(int frames, int pages[], int n) {
int pageFaults = 0;
int frame[frames];
int time[frames];
// Initialize frames to -1 and time to 0
for (int i = 0; i < frames; i++) {
frame[i] = -1;
time[i] = 0;
}
for (int i = 0; i < n; i++) {
int found = 0;
// Check if the page is already in one of the frames
for (int j = 0; j < frames; j++) {
if (frame[j] == pages[i]) {
found = 1;
time[j] = i;
break;
}
}
if (!found) {
pageFaults++;
int lruIndex = 0;
int minTime = time[0];
for (int j = 1; j < frames; j++) {
if (time[j] < minTime) {
minTime = time[j];
lruIndex = j;
}
}
frame[lruIndex] = pages[i];
time[lruIndex] = i;
}
}
printf("Page Faults in LRU: %d\n", pageFaults);
}
int main() {
int frames = 3;
int pages[] = {1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5};
int n = sizeof(pages) / sizeof(pages[0]);
lru(frames, pages, n);
return 0;
}
Output:
Page Faults in LRU: 10
Producer Consumer Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 5
sem_t empty;
sem_t full;
pthread_mutex_t mutex;
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
void *producer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
item = rand() % 100; // Produce a random item
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
printf("Produced: %d\n", item);
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
return NULL;
}
void *consumer(void *arg) {
int item;
for (int i = 0; i < 10; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
printf("Consumed: %d\n", item);
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
return NULL;
}
int main() {
pthread_t prod, cons;
sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
Produced: 45
Consumed: 45
Produced: 66
Consumed: 66
Produced: 74
Consumed: 74
Produced: 51
Consumed: 51
Produced: 42
Consumed: 42
Produced: 33
Consumed: 33
Produced: 28
Consumed: 28
Produced: 39
Consumed: 39
Produced: 82
Consumed: 82
Produced: 12
Consumed: 12
Reader Writer Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
sem_t rw_mutex;
pthread_mutex_t mutex;
int read_count = 0;
void *reader(void *arg) {
int num = *(int *)arg;
pthread_mutex_lock(&mutex);
read_count++;
if (read_count == 1)
sem_wait(&rw_mutex);
pthread_mutex_unlock(&mutex);
printf("Reader %d is reading\n", num);
pthread_mutex_lock(&mutex);
read_count--;
if (read_count == 0)
sem_post(&rw_mutex);
pthread_mutex_unlock(&mutex);
return NULL;
}
void *writer(void *arg) {
int num = *(int *)arg;
sem_wait(&rw_mutex);
printf("Writer %d is writing\n", num);
sem_post(&rw_mutex);
return NULL;
}
int main() {
pthread_t r[5], w[5];
int ids[5] = {1, 2, 3, 4, 5};
sem_init(&rw_mutex, 0, 1);
pthread_mutex_init(&mutex, NULL);
for (int i = 0; i < 5; i++) {
pthread_create(&r[i], NULL, reader, &ids[i]);
pthread_create(&w[i], NULL, writer, &ids[i]);
}
for (int i = 0; i < 5; i++) {
pthread_join(r[i], NULL);
pthread_join(w[i], NULL);
}
sem_destroy(&rw_mutex);
pthread_mutex_destroy(&mutex);
return 0;
}
Output:
Reader 1 is reading
Reader 2 is reading
Reader 3 is reading
Reader 4 is reading
Reader 5 is reading
Writer 1 is writing
Writer 2 is writing
Writer 3 is writing
Writer 4 is writing
Writer 5 is writing
Dining Philosopher Program:
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_PHILOSOPHERS 5
sem_t forks[NUM_PHILOSOPHERS];
void *philosopher(void *arg) {
int id = *(int *)arg;
int left = id;
int right = (id + 1) % NUM_PHILOSOPHERS;
for (int i = 0; i < 3; i++) {
printf("Philosopher %d is thinking\n", id);
sem_wait(&forks[left]);
sem_wait(&forks[right]);
printf("Philosopher %d is eating\n", id);
sem_post(&forks[right]);
sem_post(&forks[left]);
}
return NULL;
}
int main() {
pthread_t philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS];
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_init(&forks[i], 0, 1);
ids[i] = i;
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_create(&philosophers[i], NULL, philosopher, &ids[i]);
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
pthread_join(philosophers[i], NULL);
}
for (int i = 0; i < NUM_PHILOSOPHERS; i++) {
sem_destroy(&forks[i]);
}
return 0;
}
Output:
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Philosopher 0 is thinking
Philosopher 1 is thinking
Philosopher 2 is thinking
Philosopher 3 is thinking
Philosopher 4 is thinking
Philosopher 0 is eating
Philosopher 1 is eating
Philosopher 2 is eating
Philosopher 3 is eating
Philosopher 4 is eating
Disk Scheduling FCFS Program:
#include <stdio.h>
#include <stdlib.h>
void FCFS(int requests[], int n, int head) {
int seek_count = 0;
int distance, cur_track;
printf("Seek Sequence is:\n");
for (int i = 0; i < n; i++) {
cur_track = requests[i];
distance = abs(cur_track - head);
printf("Dis: %d\n", distance);
seek_count += distance;
head = cur_track;
printf("%d ", cur_track);
}
printf("\nTotal number of seek operations = %d\n", seek_count);
}
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
FCFS(requests, n, head);
return 0;
}
Output:
Seek Sequence is:
72 160 33 130 14 6 180
Total number of seek operations = 632
Shortest Seek Time First Program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include<limits.h>
int findClosest(int requests[], int n, int head, bool processed[]) {
int min_distance = INT_MAX;
int index = -1;
for (int i = 0; i < n; i++) {
if (!processed[i]) {
int distance = abs(requests[i] - head);
if (distance < min_distance) {
min_distance = distance;
index = i;
}
}
}
return index;
}
void SSTF(int requests[], int n, int head) {
int seek_count = 0;
int distance, cur_track;
bool processed[n];
for (int i = 0; i < n; i++) {
processed[i] = false;
}
printf("Seek Sequence is:\n");
for (int i = 0; i < n; i++) {
int index = findClosest(requests, n, head, processed);
processed[index] = true;
cur_track = requests[index];
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
printf("%d ", cur_track);
}
printf("\nTotal number of seek operations = %d\n", seek_count);
}
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
SSTF(requests, n, head);
return 0;
}
Output:
Seek Sequence is:
33 14 6 72 130 160 180
Total number of seek operations = 218
Scan Program:
#include <stdio.h>
#include <stdlib.h>
void SCAN(int requests[], int n, int head, int direction, int disk_size) {
int seek_count = 0;
int distance, cur_track;
int size = n + 2;
int seek_sequence[size];
int index = 0;
if (direction == 1) { // move towards the larger end first
seek_sequence[index++] = disk_size - 1;
} else { // move towards the smaller end first
seek_sequence[index++] = 0;
}
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (requests[j] > requests[j + 1]) {
int temp = requests[j];
requests[j] = requests[j + 1];
requests[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
if ((direction == 1 && requests[i] >= head) || (direction == 0 && requests[i] <= head)) {
seek_sequence[index++] = requests[i];
}
}
seek_sequence[index++] = head;
if (direction == 1) {
for (int i = 0; i < n; i++) {
if (requests[i] < head) {
seek_sequence[index++] = requests[i];
}
}
} else {
for (int i = n - 1; i >= 0; i--) {
if (requests[i] > head) {
seek_sequence[index++] = requests[i];
}
}
}
if (direction == 0) { // add the other end if necessary
seek_sequence[index++] = disk_size - 1;
} else {
seek_sequence[index++] = 0;
}
printf("Seek Sequence is:\n");
for (int i = 0; i < index; i++) {
cur_track = seek_sequence[i];
distance = abs(cur_track - head);
seek_count += distance;
head = cur_track;
printf("%d ", cur_track);
}
printf("\nTotal number of seek operations = %d\n", seek_count);
}
int main() {
int requests[] = { 72, 160 , 33, 130, 14, 6, 180};
int n = sizeof(requests) / sizeof(requests[0]);
int head = 50;
int disk_size = 200;
int direction = 1; // 1 for high, 0 for low
SCAN(requests, n, head, direction, disk_size);
return 0;
}
Output:
Seek Sequence is:
199 72 130 160 180 50 6 14 33 0
Total number of seek operations = 618