-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathDepth_First_Search.cs
69 lines (59 loc) · 1.94 KB
/
Depth_First_Search.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
/// C# program for Depth First Search Traversal
using System;
using System.Collections.Generic; //used for including List
class DirectedGraph
{
List<int>[] adjacencylist;
int vertix;
DirectedGraph(int newv) //Constructor with same name as that of class
{
vertix = newv;
adjacencylist = new List<int>[newv];
for (int i = 0; i < newv; ++i)
adjacencylist[i] = new List<int>();
}
void DFSFunction(int newv, bool[] traversed)
{
traversed[newv] = true;
Console.Write(newv + " ");
List<int> vList = adjacencylist[newv];
foreach (var n in vList)
{
if (!traversed[n])
DFSFunction(n, traversed);
}
}
void Addition_of_Edge(int newv, int var)
{
adjacencylist[newv].Add(var);
}
void Depth_First_Search(int newv)
{
bool[] traversed = new bool[vertix];
DFSFunction(newv, traversed);
}
public static void Main(String[] args)
{
DirectedGraph newgraph = new DirectedGraph(4); // Number of vertices is 4
Console.WriteLine("Number of edges between the vertices to enter");
int n = Convert.ToInt32(Console.ReadLine());
for (int i=0;i<n;i++){
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
newgraph.Addition_of_Edge(x, y);
}
Console.WriteLine("Enter vertex which is the starting point");
int s = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Depth First Search Traversal starting from vertex " + s + " is : ");
newgraph.Depth_First_Search(s);
}
}
// Sample input:
// Number of edges between the vertices to enter
// 6
// 0 2 0 1 2 1 2 0 3 2 2 2
// Enter vertex which is the starting point
// 3
// Sample OP:
// Depth First Search Traversal starting from vertex 3 is :
// 3 2 1 0