コード例 #1
0
        public CriticalPath(Graph p_graph)
        {
            DisToStart = new int[p_graph.VertexCount];
            for (int i = 0; i < DisToStart.Length; i++)
            {
                DisToStart[i] = int.MaxValue;
            }

            Dfo = new DepthFirstOrder(p_graph);
            Dfo.DFO();

            DisToStart[0] = 0;
            for (int i = 1; i < Dfo.ReversePostOrder.Count; i++)
            {
                int from = Dfo.SearchFrom[i];
                DisToStart[i] = DisToStart[from] + p_graph.GetWeight(from, i);
            }

            criticalPathDis = DisToStart.Max();
            int maxIndex = DisToStart.ToList().IndexOf(criticalPathDis);

            do
            {
                criticalPath.Push(maxIndex);
                maxIndex = Dfo.SearchFrom[maxIndex];
            } while (maxIndex >= 0);
        }
コード例 #2
0
        static void TestDiGraph()
        {
            Graph graph  = new DiGraph();
            var   random = new Random();

            Stopwatch watch = new Stopwatch();

            watch.Restart();

            graph.AddEdge(0, 1);
            int toAdd;
            int from;

            for (int i = 2; i < 10000; i++)
            {
                toAdd = i;
                from  = random.Next(0, i);
                graph.AddEdge(from, toAdd);
            }

            Console.WriteLine("DiGraph: " + watch.ElapsedMilliseconds);

            Console.WriteLine("DFP in DiGraph");
            watch.Restart();

            DepthFirstSearch dfp = new DepthFirstSearch(graph);

            dfp.DFS(0);
            Console.WriteLine("DFP in: " + watch.ElapsedTicks);
            //dfp.OutputOrder();
            dfp.OutputPath(5000);

            Console.WriteLine("BFP in DiGraph");
            watch.Restart();

            BreadthFirstSearch bfp = new BreadthFirstSearch(graph);

            bfp.Execute(0);
            Console.WriteLine("BFP in: " + watch.ElapsedTicks);
            //bfp.OutputOrder();
            bfp.OutputPath(5000);

            Console.WriteLine("Cycle in DiGraph");
            watch.Restart();

            GraphCycle cycle = new GraphCycle(graph);

            cycle.CS();
            Console.WriteLine("CS in: " + watch.ElapsedTicks);
            cycle.OutputCycle();

            Console.WriteLine("Topo sort in DiGraph");
            watch.Restart();

            DepthFirstOrder order = new DepthFirstOrder(graph);

            order.DFO();
            Console.WriteLine("Topo sort in: " + watch.ElapsedTicks);
            //order.OutputReversePost();
        }