-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstTraversal.cs
More file actions
57 lines (49 loc) · 1.08 KB
/
DepthFirstTraversal.cs
File metadata and controls
57 lines (49 loc) · 1.08 KB
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
using System.Collections.Generic;
using DA.Graphs;
namespace DA.Algorithms.Searching
{
internal static class DepthFirstTraversal
{
public static void DFS_Stack (Graph graph)
{
int count = graph.Count;
int current;
int[] visited = new int[count];
Stack<int> stack = new Stack<int> ();
for (int i = 0; i < count; i++)
{
visited[i] = 0;
}
visited[0] = 1;
stack.Push (0);
while (stack.Count > 0)
{
current = stack.Pop ();
Graph.Node currentNode = graph.GetNode (current);
while (currentNode != null)
{
if (visited[currentNode.destination] == 0)
{
visited[currentNode.destination] = 1;
stack.Push (currentNode.destination);
}
currentNode = currentNode.next;
}
}
}
public static void DFS_Recursion (Graph graph, int index, int[] visited)
{
Graph.Node head = graph.GetNode (index);
while (head != null)
{
if (visited[head.destination] == 0)
{
visited[head.destination] = 1;
DFS_Recursion (graph, head.destination, visited);
}
head = head.next;
}
}
}
}