Exemplo n.º 1
0
    private DirectedEdge[] edgeTo;   // edgeTo[v] = last edge on shortest s->v path


    public AcyclicSP(EdgeWeightedDigraph G, int s)
    {
        distTo = new double[G.V()];
        edgeTo = new DirectedEdge[G.V()];

        validateVertex(s);

        for (int v = 0; v < G.V(); v++)
        {
            distTo[v] = double.PositiveInfinity;
        }
        distTo[s] = 0.0;

        // visit vertices in toplogical order
        Topological topological = new Topological(G);

        if (!topological.hasOrder())
        {
            throw new System.Exception("Digraph is not acyclic.");
        }
        foreach (int v in topological.Order())
        {
            foreach (DirectedEdge e in G.Adj(v))
            {
                relax(e);
            }
        }
    }
Exemplo n.º 2
0
    void Start()
    {
        SymbolDigraph sg          = new SymbolDigraph(txt, '/');
        Topological   topological = new Topological(sg.digraph());

        foreach (int v in topological.Order())
        {
            print(sg.nameOf(v));
        }
    }
Exemplo n.º 3
0
        public void TopologicalTest()
        {
            var g    = DagSample();
            var topo = new Topological(g);

            Assert.True(topo.IsDAG());
            var result = new int[] { 8, 7, 2, 3, 0, 5, 1, 6, 9, 11, 10, 12, 4 };
            int i      = 0;

            foreach (var v in topo.Order())
            {
                Assert.Equal(v, result[i]);
                i++;
            }
        }
        // I wrote the solution based on Topological sort in the Algorithms Book,
        // and its code is here. https://github.com/kevin-wayne/algs4.
        // In topological sort, you are using a directed graph.
        // If a -> b, then it means, b depends on a,
        // in other words, 'a' needs to be built before 'b.'
        // After a topological sort, you expect all the nodes to be arranged such that it is flowing in one direction.
        // Since a cycle is not allowed, the code checks if a cycle exists and returns.
        // In real life, jobs cannot have circular dependencies.
        public override void Run()
        {
            var diGraph = new DirectedGraph('f');

            diGraph.AddEdge('a', 'd'); // d depends on a
            diGraph.AddEdge('f', 'b'); // b depends on f
            diGraph.AddEdge('b', 'd');
            diGraph.AddEdge('f', 'a');
            diGraph.AddEdge('d', 'c');

            var topological = new Topological(diGraph);
            var order       = topological.Order();

            AssortedMethods.PrintList(order);
        }
Exemplo n.º 5
0
        public AcyclicSP(EdgeWeightedDigraph graph, int startVertex)
        {
            edgeTo = new DirectedEdge[graph.Vertices];
            distTo = new double[graph.Vertices];

            for (int v = 0; v < graph.Vertices; v++)
            {
                distTo[v] = double.MaxValue;
            }

            distTo[startVertex] = 0.0f;

            var topological = new Topological(graph);

            foreach (var v in topological.Order())
            {
                Relax(graph, v);
            }
        }
Exemplo n.º 6
0
        public void Run()
        {
            Console.WriteLine("Choose file:"); // Prompt
            Console.WriteLine("1 - jobs.txt"); // Prompt
            Console.WriteLine("or quit");      // Prompt

            var    fileNumber = Console.ReadLine();
            string fileName;
            char   delimiter;

            switch (fileNumber)
            {
            case "1":
                fileName  = "jobs.txt";
                delimiter = '/';
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = !fileName.EndsWith("zip") ? @in.ReadAllLines() : @in.ReadAllLinesFromZip();

            var sg = new SymbolDigraph(lines, delimiter);

            Console.WriteLine(sg.G);

            var topological = new Topological(sg.G);

            foreach (int v in topological.Order())
            {
                Console.WriteLine(sg.Name(v));
            }

            Console.ReadLine();
        }
Exemplo n.º 7
0
        public static void Test()
        {
            int    target = int.Parse(Console.ReadLine()); // 输入目标顶点
            IGraph graph  = CreateDirectedGraph();         // 生成图数据

            Topological search = new Topological(CreateDirectedGraph());

            if (search.IsDAG())
            {
                foreach (var item in search.Order())
                {
                    Console.Write(item + " ");
                }
            }

            //GraphSearchTest(new UnionFindSearch(graph, target));
            //GraphSearchTest(new DepthFirstSearch(graph, target));

            //GraphPathSearchTest(new DFSPathSearch(graph, target), target);
            //GraphPathSearchTest(new BFSPathSearch(graph, target), target);

            //GraphConnectedComponentTest(new DFSConnectedComponent(graph));
        }