Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
                }
            }
        }
Esempio n. 4
0
        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);
                }
            }
        }