Assign 5
Assign 5
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int items[MAX];
} Queue;
void initQueue(Queue* q) {
q->front = 0;
q->rear = -1;
int isQueueEmpty(Queue* q) {
q->items[++q->rear] = value;
}
int dequeue(Queue* q) {
if (!isQueueEmpty(q)) {
return q->items[q->front++];
return -1;
Queue q;
initQueue(&q);
if (graph[j][i]) {
inDegree[i]++;
if (inDegree[i] == 0) {
enqueue(&q, i);
}
int count = 0;
int topOrder[MAX];
while (!isQueueEmpty(&q)) {
topOrder[count++] = current;
if (graph[current][i]) {
inDegree[i]--;
if (inDegree[i] == 0) {
enqueue(&q, i);
if (count != numVertices) {
return;
}
printf("\n");
int main() {
int numVertices;
int graph[MAX][MAX];
scanf("%d", &numVertices);
scanf("%d", &graph[i][j]);
}
// Perform Topological Sort
topologicalSort(graph, numVertices);
return 0;
011000
000110
000001
000000
000001
000000
Topological Sort: 0 2 5 1 4 3
2.prims
#include <stdio.h>
#include <stdbool.h>
// Function to find the vertex with the minimum key value that is not yet included in MST
min = key[v];
minIndex = v;
}
return minIndex;
key[i] = INF;
mstSet[i] = false;
mstSet[u] = true;
parent[v] = u;
key[v] = graph[u][v];
printf("Edge \tWeight\n");
int main() {
int numVertices;
int graph[MAX][MAX];
scanf("%d", &numVertices);
scanf("%d", &graph[i][j]);
prims(graph, numVertices);
return 0;
02060
20385
03007
68009
05790
Edge Weight
0-1 2
1-2 3
0-3 6
1-4 5
3.Kruskals
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int u, v, weight;
} Edge;
Edge edges[MAX];
Edge mst[MAX];
int parent[MAX];
int find(int v) {
if (parent[v] != v) {
parent[v] = find(parent[v]);
return parent[v];
}
// Function to perform union of two subsets
parent[rootU] = rootV;
// Kruskal's Algorithm
void kruskal() {
parent[i] = i;
}
int u = edges[i].u;
int v = edges[i].v;
if (find(u) != find(v)) {
mstWeight += weight;
}
int main() {
scanf("%d", &numVertices);
scanf("%d", &numEdges);
kruskal();
return 0;
0 1 10
026
035
1 3 15
234
Edges in the Minimum Spanning Tree:
(2, 3) -> 4
(0, 3) -> 5
(0, 1) -> 10