private static void dfs(IVertex vertex, IGraph graph) { visited.Add(vertex); foreach (var item in graph.Ady(vertex)) { if (!visited.Contains(item)) { dfs(item, graph); } } result.Insert(0, vertex); }
private static IEnumerable <IVertex> DfsRecursive(IVertex vertex, IGraph graph) { var marked = new List <IVertex>(new[] { vertex }); yield return(vertex); foreach (var item in graph.Ady(vertex).Where(x => !marked.Contains(x))) { var query = DfsRecursive(item, graph); foreach (var x in query) { yield return(x); } } }
public static IEnumerable <IVertex> Run(IGraph graph) { var marked = new List <IVertex>(); Queue <IVertex> m_Queue = new Queue <IVertex>(); m_Queue.EnQueue(graph.Vertexs.First()); while (m_Queue.Count > 0) { var vertex = m_Queue.DeQueue(); marked.Add(vertex); yield return(vertex); foreach (var item in graph.Ady(vertex).Where(x => !marked.Contains(x))) { marked.Add(item); m_Queue.EnQueue(item); } } }
private static IEnumerable <IVertex> DfsIterative(IVertex vertex, IGraph graph) { var marked = new List <IVertex>(new[] { vertex }); Stack <IVertex> m_Stack = new Stack <IVertex>(); m_Stack.Push(vertex); while (m_Stack.Count > 0) { vertex = m_Stack.Pop(); marked.Add(vertex); yield return(vertex); foreach (var item in graph.Ady(vertex).Where(x => !marked.Contains(x))) { marked.Add(item); m_Stack.Push(item); } } }