private static void DFS(GraphAdjL graph, int vertex) { HashSet <int> hsVisited = new HashSet <int>(); Stack <int> stack = new Stack <int>(); stack.Push(vertex); while (stack.Count > 0) { var temp = stack.Pop(); if (!hsVisited.Contains(temp)) { Console.Write(temp + " "); hsVisited.Add(temp); } foreach (int neighbour in graph.GetAdjacent(temp)) { if (!hsVisited.Contains(neighbour)) { stack.Push(neighbour); } } } }
public static void BFS(GraphAdjL graph, int vertex) { HashSet <int> hsVisited = new HashSet <int>(); Queue <int> queue = new Queue <int>(); hsVisited.Add(vertex); queue.Enqueue(vertex); while (queue.Count > 0) { int temp = queue.Dequeue(); Console.Write(temp + " "); foreach (var neighbour in graph.GetAdjacent(temp)) { if (!hsVisited.Contains(neighbour)) { hsVisited.Add(neighbour); queue.Enqueue(neighbour); } } } }