forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Topological_Sort.dart
181 lines (117 loc) · 3.94 KB
/
Topological_Sort.dart
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Topological Sort implementation in Dart
* -------------------------------------------------------
*
* In a directed acyclic graph, if there is an edge from u to v. Then, u comes before v.
* This observation has many real life application like scheduling tasks, ordering things etc.,.
*
* Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that
* for every directed edge uv, vertex u comes before v in the ordering.
*
* Topological sorting is an application of Depth First Search.
*
*/
// Importing required libraries
import 'dart:io';
import 'dart:core';
// Class for a graph
class Graph{
// Number of vertices in a graph.
int vertices;
// Adjacency list to store the graph.
List< List< int > > adjlist;
// Constructor
Graph(int v){
// Assigning number of vertices
this.vertices = v;
// Initializing the graph
this.adjlist = new List< List< int > >(vertices);
for (int i = 0 ; i < vertices ; i ++){
this.adjlist[i] = new List< int >();
}
}
// Method to add an edge
void add_edge(int source , int destination){
this.adjlist[source].add(destination);
}
// Utility method for Topological Sort
void topologicalSortUtil(int source , List< int > stack , List< bool > visited){
// Marking source vertex visited
visited[source] = true;
int len = this.adjlist[source].length;
// Recursively visiting it's neighbours
for (int i = 0 ; i < len ; i ++){
if (visited[adjlist[source][i]] == false){
// Recursive call
topologicalSortUtil(adjlist[source][i] , stack , visited);
}
}
// Adding to stack
stack.add(source);
}
// Method to find the topological sort
void topologicalSort(){
// Stack required to print the topological sort
List< int > stack = new List< int >();
// Initializing the visited list.
List< bool > visited = new List< bool >.generate(this.vertices , ( i ) => false);
// Running the utility on all vertices to cover the entire graph.
for (int i = 0 ; i < this.vertices ; i ++){
if (visited[i] == false){
topologicalSortUtil(i , stack , visited);
}
}
// Converting the elements in the stack into a string and printing them.
String s;
List temp = new List< int >();
while ( ! stack.isEmpty ){
temp.add(stack.removeLast());
}
s = temp.join(" ");
// Printing the result
print(s);
}
}
// Driver function of the program
void main(){
// Taking input of number of vertices
print("Enter number of vertices:");
var input = stdin.readLineSync();
int n = int.parse(input);
Graph g = new Graph(n);
// Taking input of number of edges
print("Enter number of edges:");
input = stdin.readLineSync();
int m = int.parse(input);
// Taking input of edges
print("Enter edges for vertices in 0 - n range:");
for(int i = 0 ; i < m ; i ++){
input = stdin.readLineSync();
var lis = input.split(" ");
List list1 = lis.map(int.parse).toList();
g.add_edge(list1[0] , list1[1]);
}
// Printing the result
print("Following is the topological sort of the input graph: ");
g.topologicalSort();
}
/*
Sample Input
----------------------
Enter number of vertices:
8
Enter number of edges:
7
Enter edges for vertices in 0 - n range:
0 1
0 4
1 7
1 2
2 3
4 5
5 6
Sample Output
-----------------------
Following is the topological sort of the input graph:
0 4 5 6 1 2 3 7
*/