-
Notifications
You must be signed in to change notification settings - Fork 0
/
q1.c
77 lines (65 loc) · 1.5 KB
/
q1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#define MAX 50
int op=0;
typedef struct node* np;
typedef struct node {
int data;
np next;
} node;
typedef struct gph {
int nver;
np* adlist;
int mat[MAX][MAX];
} gph;
void ini(gph* g, int nver) {
g->nver = nver;
g->adlist = (np*)malloc(nver * sizeof(np));
for (int i = 0; i < nver; i++) {
g->adlist[i] = NULL;
for (int j = 0; j < nver; j++) {
g->mat[i][j] = 0;
}
}
}
void add(gph* g, int u, int v) {
np new = (np)malloc(sizeof(node));
new->data = v;
new->next = g->adlist[u];
g->adlist[u] = new;
g->mat[u][v] = 1;
}
void closure(gph* g) {
for (int k = 0; k < g->nver; k++) {
for (int i = 0; i < g->nver; i++) {
for (int j = 0; j < g->nver; j++) {
op++;
g->mat[i][j] = g->mat[i][j] || (g->mat[i][k] && g->mat[k][j]);
}
}
}
printf("Transitive Closure :\n");
for (int i = 0; i < g->nver; i++) {
for (int j = 0; j < g->nver; j++) {
printf("%d ", g->mat[i][j]);
}
printf("\n");
}
}
int main() {
int nver, e, i, u, v;
printf("Enter the number of nver: ");
scanf("%d", &nver);
gph g;
ini(&g, nver);
printf("Enter the number of e: ");
scanf("%d", &e);
printf("Enter the e (u v):\n");
for (i = 0; i < e; ++i) {
scanf("%d %d", &u, &v);
add(&g, u, v);
}
closure(&g);
printf("Opcount = %d",op);
return 0;
}