-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathdepthFirstSearch.cs
49 lines (48 loc) · 1.16 KB
/
depthFirstSearch.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
using System;
using System.Collections.Generic;
class Graph
{
private int V;
private List<int>[] adj;
Graph(int v)
{
V = v;
adj = new List<int>[v];
for (int i = 0; i < v; ++i)
adj[i] = new List<int>();
}
void AddEdge(int v, int w)
{
adj[v].Add(w);
}
void DFSUtil(int v, bool[] visited)
{
visited[v] = true;
Console.Write(v + " ");
List<int> vList = adj[v];
foreach (var n in vList)
{
if (!visited[n])
DFSUtil(n, visited);
}
}
void DFS(int v)
{
bool[] visited = new bool[V];
DFSUtil(v, visited);
}
public static void Main(String[] args)
{
Graph g = new Graph(4);
g.AddEdge(0, 1);
g.AddEdge(0, 2);
g.AddEdge(1, 2);
g.AddEdge(2, 0);
g.AddEdge(2, 3);
g.AddEdge(3, 3);
Console.WriteLine("Following is Depth First Traversal " +
"(Starting from vertex 2)");
g.DFS(2);
Console.ReadKey();
}
}