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); } } } }
static void Main(string[] args) { GraphAdjL graph = new GraphAdjL(4); for (int i = 0; i < 4; i++) { graph.AddVertex(i); } graph.AddEdge(0, 1); graph.AddEdge(0, 2); graph.AddEdge(1, 2); graph.AddEdge(2, 3); DFS(graph, 1); Console.ReadLine(); }
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); } } } }