-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB4- topol.C
96 lines (90 loc) · 1.22 KB
/
LAB4- topol.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void Top_str();
int g[10][10],sv,n,visited[10],count=0;
void main()
{
int i,j;
clrscr();
printf("Enter Number of nodes for the Gaph:\n");
scanf("%d",&n);
printf("Enter the Adjacency matrix for the Diagraph:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&g[i][j]);
}
}
for(i=1;i<=n;i++)
{
visited[i]=0;
}
printf("The Solution for the graph is:\n");
for(i=1;i<=n;i++)
{
Top_str();
}
if(count!=n)
{
printf("\n graph indicates acyclic Digraph");
}
getch();
}
/*Topological Sorting*/
void Top_str()
{
int i,j,indeg[10];
for(i=1;i<=n;i++)
{
indeg[i]=0;
}
/* Finding the Indegree of teh vertices which are not visited*/
for(i=1;i<=n;i++)
{
if(visited[i]==0)
{
for(j=1;j<=n;j++)
{
if(g[j][i]==1)
{
indeg[i]++;
}
}
}
}
/*Identifing the vertex whose indegree is zero*/
for(i=1;i<=n;i++)
if(visited[i]==0 && indeg[i]==0)
{
sv=i;
break;
}
/*Removing the Vertex Along with their edges and printing*/
if(visited[sv]==0)
{
for(i=1;i<=n;i++)
{
if(g[sv][i]==1)
{
g[sv][i]=0;
}
}
visited[sv]=1;
count=count+1;
printf("\t%d->",sv);
}
}
Input 1:
4
0 0 1 1
0 0 1 0
0 0 0 0
0 1 0 0
Input 2:
4
0 1 0 0
0 0 0 1
1 0 0 0
0 0 1 0