public DirectedDfs(Digraph <T> digraph, T source) { if (digraph == null) { throw new ArgumentNullException(nameof(digraph)); } if (!digraph.Vertices.Contains(source)) { throw new ArgumentOutOfRangeException(nameof(source)); } marked = new HashSet <T>(digraph.VertexNumber); Dfs(digraph, source); }
private void DepthFirstTraverse(Digraph <T> digraph, T vertex, HashSet <T> visited) { preOrder.Enqueue(vertex); visited.Add(vertex); foreach (var neighbour in digraph.GetNeighbours(vertex)) { if (!visited.Contains(neighbour)) { DepthFirstTraverse(digraph, neighbour, visited); } } postOrder.Enqueue(vertex); reversePostOrder.Push(vertex); }
public DirectedCycle(Digraph <T> digraph) { if (digraph == null) { throw new ArgumentNullException(nameof(digraph)); } var currentPath = new HashSet <T>(); var edgeTo = new Dictionary <T, T>(); var visited = new HashSet <T>(digraph.VertexNumber); foreach (var vertex in digraph.Vertices) { if (!visited.Contains(vertex)) { DepthFirstTraverse(digraph, vertex, visited, currentPath, edgeTo); } } }
public DepthFirstOrder(Digraph <T> digraph) { if (digraph == null) { throw new ArgumentNullException(nameof(digraph)); } postOrder = new Queue <T>(digraph.VertexNumber); preOrder = new Queue <T>(digraph.VertexNumber); reversePostOrder = new Stack <T>(digraph.VertexNumber); var visited = new HashSet <T>(digraph.VertexNumber); foreach (var vertex in digraph.Vertices) { if (!visited.Contains(vertex)) { DepthFirstTraverse(digraph, vertex, visited); } } }