public static void DFS1(Graph g, Vertex startVertex) { Stack <Vertex> stack = new Stack <Vertex>(); stack.Push(startVertex); while (stack.Count > 0) { Vertex v = stack.Pop(); if (!v.Visited) { v.Display(); v.Visited = true; } Vertex next = g.GetNextUnvisitedVertex(v); if (next != null) { stack.Push(v); stack.Push(next); } } g.ClearVisitInfo(); }
public static void BFS1(Graph g, Vertex startVertex) { if (g == null) { return; } Queue <Vertex> q = new Queue <Vertex>(); q.Enqueue(startVertex); startVertex.Visited = true; while (q.Count > 0) { Vertex v = q.Dequeue(); v.Display(); Vertex next = g.GetNextUnvisitedVertex(v); while (next != null) { q.Enqueue(next); next.Visited = true; next = g.GetNextUnvisitedVertex(v); } } g.ClearVisitInfo(); }