Exemple #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);
            }
        }
    }
        /// <inheritdoc/>
        public virtual List <IModuleDescriptor> ModuleSort <TModule>()
            where TModule : IAppModule
        {
            var moduleDescriptors = VisitModule(typeof(TModule));

            return(Topological.Sort(moduleDescriptors, o => o.Dependencies));
        }
    public AcyclicLP(EdgeWeightedDigraph ewd, int i)
    {
        this.distTo = new double[ewd.V()];
        this.edgeTo = new DirectedEdge[ewd.V()];
        for (int j = 0; j < ewd.V(); j++)
        {
            this.distTo[j] = double.NegativeInfinity;
        }
        this.distTo[i] = (double)0f;
        Topological topological = new Topological(ewd);

        if (!topological.hasOrder())
        {
            string arg_72_0 = "Digraph is not acyclic.";

            throw new ArgumentException(arg_72_0);
        }
        Iterator iterator = topological.order().iterator();

        while (iterator.hasNext())
        {
            int      i2        = ((Integer)iterator.next()).intValue();
            Iterator iterator2 = ewd.adj(i2).iterator();
            while (iterator2.hasNext())
            {
                DirectedEdge directedEdge = (DirectedEdge)iterator2.next();
                this.relax(directedEdge);
            }
        }
    }
Exemple #4
0
        public override string Solve(string input, bool part2)
        {
            //Set up the bags
            LoadBags(input);
            bagSorter = new Topological <Bag>(bags);
            bags      = bagSorter.Ordered;
            foreach (Bag bag in bags)
            {
                Console.WriteLine(bag.Name);
            }

            Console.WriteLine("".PadLeft(10, '='));
            Console.WriteLine("RESULT");
            Console.WriteLine("".PadLeft(10, '='));

            Bag myBag = bags.Single(x => x.Name == "shiny gold");

            if (part2)
            {
                return(GetRequiredBacks(myBag));
            }
            else
            {
                return(GetUsableBags(myBag));
            }
        }
        public AcyclicSP(IEdgeWeightedDIgraph G, int s) : base(G, s)
        {
            Topological top = new Topological(G);

            foreach (int v in top.Order)
            {
                Relax(G, v);
            }
        }
Exemple #6
0
    void Start()
    {
        SymbolDigraph sg          = new SymbolDigraph(txt, '/');
        Topological   topological = new Topological(sg.digraph());

        foreach (int v in topological.Order())
        {
            print(sg.nameOf(v));
        }
    }
Exemple #7
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);
        }
Exemple #9
0
        public AcylicSP(EdgeWeightedDigraph g, int s, PathType type)
        {
            _edgeTo   = new DirectedEdge[g.V];
            _distTo   = new double[g.V];
            _pathType = type;
            for (int v = 0; v < g.V; v++)
            {
                _distTo[v] = type == PathType.Lagrest ? double.NegativeInfinity : double.PositiveInfinity;
                _distTo[s] = 0.0;
            }

            Topological topol = new Topological(g);

            foreach (int v in topol.Order)
            {
                Relax(g, v);
            }
        }
Exemple #10
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);
            }
        }
Exemple #11
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();
        }
Exemple #12
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));
        }