#Include #Include
#Include #Include
#include <stdio.h>
#include <stdio.h>
int main()
{
int a[100];
int i, max, min, n;
printf("Enter the number of elements in the array :");
scanf("%d", &n);
printf("Enter %d elements in the array :\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
max = a[0];
min = a[0];
// Traverse the array to find the maximum and minimum elements
for (i = 1; i < n; i++)
{
if (a[i] > max)
{
max = a[i];
}
if (a[i] < min)
{
min = a[i];
}
}
// Print the maximum and minimum elements
printf("Maximum element is : %d\n", max);
printf("Minimum element is : %d\n\n", min);
return 0;
}
Develop a C program to demonstrate stack operations using an array.
#include <stdio.h>
int stack[100], choice, n, top, x, i;
void push(void);
void pop(void);
void display(void);
int main()
{
top = -1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d", &n);
printf("\n 1.PUSH\t 2.POP\t 3.DISPLAY\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d", &choice);
switch (choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t END ");
break;
}
default:
{
printf("\n Enter a Valid Choice(1/2/3/4)");
}
}
} while (choice != 4);
return 0;
}
void push()
{
if (top >= n - 1)
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d", &x);
top++;
stack[top] = x;
}
}
void pop()
{
if (top <= -1)
{
printf("\n\t Stack is Empty");
}
else
{
printf("\n\t The popped elements is %d", stack[top]);
top--;
}
}
void display()
{
if (top >= 0)
{
printf("\n The elements in STACK \n");
for (i = top; i >= 0; i--)
printf("\n%d", stack[i]);
}
else
{
printf("\n The STACK is empty");
}
}
Develop a C program on a graph to perform the Depth First Search (DFS) and the Breadth
First Search (BFS).
BFS
#include <stdio.h>
int top = -1, q[20], arr[20][20], visited[20] = {0};
int front = -1, rear = -1;
void bfs(int s, int n);
int main()
{
int i, j, n, ch, s;
printf("Enter the Number of Vertices");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("Enter 1 if %d has a node with %d else 0:", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("Enter starting vertex");
scanf("%d", &s);
bfs(s, n);
return 0;
}
void add(int item)
{
if (rear == 19)
printf("QUEUE FULL");
else
{
if (rear == -1)
{
q[++rear] = item;
front++;
}
else
q[++rear] = item;
}
}
int delete()
{
int k;
if ((front > rear) || (front == -1))
return (0);
else
{
k = q[front++];
return (k);
}
}
void bfs(int s, int n)
{
int i, p;
add(s);
visited[s] = 1;
p = delete ();
if (p != 0)
printf("%d", p);
while (p != 0)
{
for (i = 1; i <= n; i++)
{
if ((arr[p][i] != 0) && (visited[i] == 0))
{
add(i);
visited[i] = 1;
}
}
p = delete ();
if (p != 0)
printf(" %d ", p);
}
for (i = 1; i <= n; i++)
{
if (visited[i] == 0)
bfs(i, n);
}
}
DFS
#include <stdio.h>
int top = -1, stack[20], arr[20][20], visited[20] = {0};
void dfs(int s, int n);
int main()
{
int i, j, n, s;
printf("Enter the Number of Vertices");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("Enter 1 if %d has a node with %d else 0:", i, j);
scanf("%d", &arr[i][j]);
}
}
printf("Enter starting vertex");
scanf("%d", &s);
dfs(s, n);
return 0;
}
void push(int item)
{
if (top == 19)
printf("Stack overflow ");
else
stack[++top] = item;
}
int pop()
{
int k;
if (top == -1)
return (0);
else
{
k = stack[top--];
return (k);
}
}
void dfs(int s, int n)
{
int k, i;
push(s);
visited[s] = 1;
k = pop();
if (k != 0)
printf(" %d ", k);
while (k != 0)
{
for (i = 1; i <= n; i++)
{
if ((arr[k][i] != 0) && (visited[i] == 0))
{
push(i);
visited[i] = 1;
}
k = pop();
if (k != 0)
printf(" %d \n", k);
}
}
for (i = 1; i <= n; i++)
{
if (visited[i] == 0)
dfs(i, n);
}
}
Develop a C program to find the shortest path in a graph using Floyd Warshall’s
Algorithm.
#include <stdio.h>
// defining the number of vertices
#define nV 4
#define INF 999
void printGraph(int matrix[][nV]);
// Implementing floyd warshall algorithm
void floydWarshall(int graph[][nV])
{
int matrix[nV][nV], i, j, k;
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];
}
}
}
printGraph(matrix);
}
void printGraph(int matrix[][nV])
{
printf("\n Final Shortest Path Graph Matrix:\n");
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");
}
}
int main()
{
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
floydWarshall(graph);
}