Example #1
0
        public Digraph(Digraph g) : this(g.V)
        {
            this.E = g.E;

            for (int v = 0; v < g.V; v++)
            {
                Stack <int> reverse = new Stack <int>();

                foreach (int adjacentVertex in g.adjacencyList[v])
                {
                    reverse.Push(adjacentVertex);
                }

                foreach (int vertex in reverse)
                {
                    this.adjacencyList[v].AddLast(vertex);
                }
            }
        }
Example #2
0
        static void TraverseBreadthFirst(Digraph g, bool[] visited, Queue <int> queue)
        {
            int v = queue.Dequeue();

            if (!visited[v])
            {
                Console.WriteLine(v);
                visited[v] = true;

                foreach (var adjacent in g.GetAdjacentVertices(v))
                {
                    queue.Enqueue(adjacent);
                }
            }

            if (queue.Count > 0)
            {
                TraverseBreadthFirst(g, visited, queue);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            Digraph g = new Digraph(10);

            g.AddEdge(0, 1);
            g.AddEdge(0, 2);
            g.AddEdge(0, 7);
            g.AddEdge(1, 8);
            g.AddEdge(1, 7);
            g.AddEdge(1, 4);
            g.AddEdge(2, 7);
            g.AddEdge(7, 6);
            g.AddEdge(7, 4);
            g.AddEdge(6, 5);
            g.AddEdge(5, 9);
            g.AddEdge(9, 4);
            g.AddEdge(4, 8);

            Queue <int> q = new Queue <int>();

            q.Enqueue(0);
            TraverseBreadthFirstLoop(g, new bool[g.V], q);
        }