-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreathFirstTraversal.cs
122 lines (108 loc) · 3.48 KB
/
BreathFirstTraversal.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AlgoPracticeCSharp.Graphs
{
//Directed graph- Adjacency list representation
class clsGraph
{
//total no of vertices
private int Vertices;
//adjency list array for all vertices.
private List<Int32>[] adj;
/* Example : vertices=4
* 0->[1,2]
* 1->[2]
* 2->[0,3]
* 3->[]
*/
//constructor
public clsGraph(int v) {
Vertices = v;
adj = new List<Int32>[v];
//Instantiate adjacecny list for all vertices
for (int i = 0; i < v; i++)
{
adj[i] = new List<Int32>();
}
}
//Add edge from v->w
public void AddEdge(int v,int w) {
adj[v].Add(w);
}
//Print BFS traversal
//s-> start node
//BFS uses queue as a base.
void BFS(int s) {
bool[] visited = new bool[Vertices];
//create queue for BFS
Queue<int> queue = new Queue<int>();
visited[s] = true;
queue.Enqueue(s);
//loop through all nodes in queue
while (queue.Count!=0) {
//Deque a vertex from queue and print it.
s = queue.Dequeue();
Console.WriteLine("next->"+s);
//Get all adjacent vertices of s
foreach (Int32 next in adj[s]) {
if (!visited[next]) {
visited[next] = true;
queue.Enqueue(next);
}
}
}
}
//DFS traversal
// DFS uses stack as a base.
public void DFS(int s) {
bool[] visited = new bool[Vertices];
//For DFS use stack
Stack<int> stack = new Stack<int>();
visited[s] = true;
stack.Push(s);
while (stack.Count !=0) {
s = stack.Pop();
Console.WriteLine("next->"+s);
foreach (int i in adj[s]) {
if (!visited[i]) {
visited[i] = true;
stack.Push(i);
}
}
}
}
public void PrintAdjacecnyMatrix() {
for (int i = 0; i < Vertices; i++)
{
Console.Write(i+":[");
string s = "";
foreach (var k in adj[i]) {
s= s+(k+",");
}
s = s.Substring(0,s.Length-1);
s = s + "]";
Console.Write(s);
Console.WriteLine();
}
}
//Calling methods
public static void Main() {
clsGraph graph = new clsGraph(4);
graph.AddEdge(0,1);
graph.AddEdge(0, 2);
graph.AddEdge(1,2);
graph.AddEdge(2,0);
graph.AddEdge(2,3);
graph.AddEdge(3,3);
//Print adjacency matrix
graph.PrintAdjacecnyMatrix();
Console.WriteLine("BFS traversal starting from vertex 2:");
graph.BFS(2);
Console.WriteLine("DFS traversal starting from vertex 2:");
graph.DFS(2);
}
}
}