forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Depth_First_Search.dart
100 lines (78 loc) · 2.83 KB
/
Depth_First_Search.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
// Required library to use inbuilt queue in LIFO manner in Dart
import 'dart:collection';
// Required library to use standard output facility of dart
import 'dart:io';
// Function to find the depth first search of a graph
void Depth_First_Search ( var graph, int source, int no_vertices ) {
// Queue is used in LIFO manner to obtain stack functionality.
var queue = Queue();
// A list to keep track of visited nodes
var visited = new List(no_vertices);
// Initializing the list
for(int i = 0; i < no_vertices; i ++ ) {
visited[i] = 0;
}
// Adding the source node to LIFO queue ( stack )
queue.add(source);
// Marking the node visited
visited[source] = 1;
int val;
while(queue.isNotEmpty){
val = queue.removeLast();
stdout.write("$val ");
//Visiting neighbours of the popped node
for(int i = 0; i < no_vertices; i ++ ){
if(graph[val][i] == 1 && visited[i] == 0){
queue.add(i);
visited[i] = 1;
}
}
}
}
// Driver method of the program
void main(){
// Reading from the standard input
print("Which graph do you want to enter ( directed/ undirected ):");
String mode = stdin.readLineSync().toString();
print("Enter number of vertices:");
var input = stdin.readLineSync();
int no_vertices = int.parse(input);
// Graph is implemented as adjacency matrix.
var graph = new List.generate(no_vertices, (_) => new List(no_vertices));
for(int i = 0; i < no_vertices; i++){
for(int j = 0; j < no_vertices; j++){
graph[i][j] = 0;
}
}
print("Enter number of edges:");
var input1 = stdin.readLineSync();
int no_edges = int.parse(input);
print("Enter source and destination vertices of edges:\n");
print("Note: Enter source and destination vertices as 0,1,2,...");
for(int i = 1; i <= no_edges; i++){
print("Edge #$i:");
print("Enter source vertex:");
int source = int.parse(stdin.readLineSync());
print("Enter destination vertex:");
int destination = int.parse(stdin.readLineSync());
if(mode == "undirected"){
graph[source][destination] = 1;
graph[destination][source] = 1;
}
else{
graph[source][destination] = 1;
}
}
print("Enter the source vertex from which DFS should begin:");
int source1 = int.parse(stdin.readLineSync());
// Sample input graph
// 0 - 1 - 2
// | /
// 3 - 4
print("Vertices in Depth First Search of the graph in sample input is: ");
// Depth First Search of the above graph
Depth_First_Search(graph,source1,no_vertices);
// Sample Output
// Vertices in Depth First Search of the graph in sample input is:
// 0 3 4 2 1
}