Example #1
0
        public void DijkstraSPTest()
        {
            EdgeWeightedDiagraph ewg;

            using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyEWD.txt"))
            {
                ewg = new EdgeWeightedDiagraph(sr);
            }

            int        s  = 0;
            DijkstraSP SP = new DijkstraSP(ewg, s);



            for (int dest = 0; dest < ewg.V(); dest++)
            {
                if (SP.HasPathTo(dest))
                {
                    Console.Write(s + " to " + dest);
                    Console.Write(" (" + SP.DistanceTo(dest) + "):");
                    foreach (DirectedEdge de in SP.PathTo(dest))
                    {
                        Console.Write(de + "  ");
                    }
                    Console.WriteLine();
                }
            }
        }
Example #2
0
        public TopologicalOrder(EdgeWeightedDiagraph ewg)
        {
            DirectedCycle directedCycle = new DirectedCycle(ewg);

            if (!directedCycle.HasCycle()) // if it's a DAG, then we can continue to get the Topological Order
            {
                DepthFirstOrder dfsOrder = new DepthFirstOrder(ewg);
                topologicalOder = dfsOrder.PostReverseOrder();
            }
        }
Example #3
0
        public DepthFirstOrder(EdgeWeightedDiagraph g)
        {
            isMarked = new bool[g.V()];

            for (int i = 0; i < g.V(); i++)
            {
                if (!isMarked[i])
                {
                    DFS(g, i);
                }
            }
        }
Example #4
0
        //added overloaded constructor to handle EdgeWeightedDiagraph
        public DirectedCycle(EdgeWeightedDiagraph dg)
        {
            marked  = new bool[dg.V()];
            onStack = new bool[dg.V()];
            edgeTo  = new int[dg.V()];

            for (int i = 0; i < dg.V(); i++)
            {
                if (!marked[i])
                {
                    DFS(dg, i);
                }
            }
        }
Example #5
0
        void DFS(EdgeWeightedDiagraph g, int source)
        {
            isMarked[source] = false;

            preOrder.Enqueue(source);



            foreach (DirectedEdge de in g.AdjList(source))
            {
                int from = de.From(), to = de.To();
                int v;
                v = to;// source == from ? to : from;

                if (!isMarked[v])
                {
                    DFS(g, v);
                }
            }
            postOrder.Enqueue(source);
            postReverseOrder.Push(source);
        }
Example #6
0
        //added overloaded DFS method to handle EdgeWeightedDiagraph, ideally we should design EdgeWeightedDiagraph to inherit from Diagraph
        void DFS(EdgeWeightedDiagraph g, int s)
        {
            marked[s]  = true;
            onStack[s] = true;
            foreach (DirectedEdge de  in g.AdjList(s))
            {
                int from = de.From(), to = de.To();
                int v;
                v = to;// s == from ? to : from;

                if (hasCycle)
                {
                    return;
                }
                if (!marked[v])
                {
                    edgeTo[v] = s;
                    DFS(g, v);
                }
                else
                {
                    if (onStack[v]) // this means, we hit v agin in a cycle
                    {
                        hasCycle   = true;
                        stackCycle = new Stack <int>();
                        for (int x = s; x != v; x = edgeTo[x])//starting from the parent of the current node, until it hits the current node
                        {
                            stackCycle.Push(x);
                        }
                        stackCycle.Push(v); // push the current node
                        stackCycle.Push(s); // push the parent node of the current again to for the loop
                    }
                }
            }
            onStack[s] = false;
        }