コード例 #1
0
        public void SmokeTest()
        {
            var graph = new Digraph();

            graph.AddEdge(0, 5);
            graph.AddEdge(0, 1);
            graph.AddEdge(3, 5);
            graph.AddEdge(5, 2);
            graph.AddEdge(6, 0);
            graph.AddEdge(1, 4);
            graph.AddEdge(0, 2);
            graph.AddEdge(3, 6);
            graph.AddEdge(3, 4);
            graph.AddEdge(6, 4);
            graph.AddEdge(3, 2);

            var depthFirstOrder = new DepthFirstOrder(graph);

            Assert.AreEqual(3, depthFirstOrder.ReversePost[0]);
            Assert.AreEqual(6, depthFirstOrder.ReversePost[1]);
            Assert.AreEqual(0, depthFirstOrder.ReversePost[2]);
            Assert.AreEqual(1, depthFirstOrder.ReversePost[3]);
            Assert.AreEqual(4, depthFirstOrder.ReversePost[4]);
            Assert.AreEqual(5, depthFirstOrder.ReversePost[5]);
            Assert.AreEqual(2, depthFirstOrder.ReversePost[6]);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: ged29/Solution1
        static void Main(string[] args)
        {
            //var basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            //var mediumGPath = Path.Combine(basePath, "GraphFiles", "mediumG.txt");
            //var tinyGPath = Path.Combine(basePath, "GraphFiles", "tinyG.txt");
            //var unCycledGPath = Path.Combine(basePath, "GraphFiles", "unCycledG.txt");

            Stopwatch sw = new Stopwatch();

            Digraph digraph = new Digraph("tinyDAG.txt");

            sw.Reset();
            sw.Start();
            DepthFirstOrder dfo = new DepthFirstOrder(digraph);

            sw.Stop();
            Console.WriteLine(sw.ElapsedTicks);

            //Console.WriteLine("PreOrder: " + String.Join(",", dfo.PreOrder));
            //Console.WriteLine("PostOrder: " + String.Join(",", dfo.PostOrder));
            Console.WriteLine("ReversePostOrder: " + String.Join(",", dfo.ReversePostOrder));

            //DirectGraph.FormCodeProject.TestClient.Test();
            //DirectGraph.FromRosettacode.TestClient.Test();

            DirectGraph.FormCodeProject.TestClient.Test();
        }
コード例 #3
0
ファイル: RemoveUpDownBonds.cs プロジェクト: roddickchen/NCDK
        public override Graph Apply(Graph g)
        {
            Graph h = new Graph(g.Order);

            // copy atom/topology information this is unchanged
            for (int u = 0; u < g.Order; u++)
            {
                h.AddAtom(g.GetAtom(u));
                h.AddTopology(g.TopologyOf(u));
            }

            var Ordering = new DepthFirstOrder(g).visited;

            var replacements = new Dictionary <Edge, Edge>();
            var dbCentres    = new SortedSet <int>();

            // change edges (only changed added to replacement)
            for (int u = 0; u < g.Order; u++)
            {
                foreach (var e in g.GetEdges(u))
                {
                    if (e.Other(u) > u && e.Bond == Bond.Double)
                    {
                        RemoveRedundant(g, e, Ordering, replacements);
                        dbCentres.Add(u);
                        dbCentres.Add(e.Other(u));
                    }
                }
            }

            // ensure we haven't accidentally removed one between two
            foreach (var e in new HashSet <Edge>(replacements.Keys))
            {
                if (dbCentres.Contains(e.Either()) &&
                    dbCentres.Contains(e.Other(e.Either())))
                {
                    replacements.Remove(e);
                }
            }

            // append the edges, replacing any which need to be changed
            for (int u = 0; u < g.Order; u++)
            {
                foreach (var e in g.GetEdges(u))
                {
                    if (e.Other(u) > u)
                    {
                        Edge ee = e;
                        if (replacements.TryGetValue(e, out Edge replacement))
                        {
                            ee = replacement;
                        }
                        h.AddEdge(ee);
                    }
                }
            }

            return(h);
        }
コード例 #4
0
        public void Test_ReversePost()
        {
            DepthFirstOrder order = new DepthFirstOrder(this.CreateDigraph());

            AssertUtilities.Sequence(new Int32[6] {
                0, 1, 2, 3, 4, 5
            }, order.ReversePost());
        }
コード例 #5
0
        public void Test_Pre()
        {
            DepthFirstOrder order = new DepthFirstOrder(this.CreateDigraph());

            AssertUtilities.Sequence(new Int32[6] {
                0, 3, 4, 5, 1, 2
            }, order.Pre());
        }
コード例 #6
0
        public void Test_Post()
        {
            DepthFirstOrder order = new DepthFirstOrder(this.CreateDigraph());

            AssertUtilities.Sequence(new Int32[6] {
                5, 4, 3, 2, 1, 0
            }, order.Post());
        }
コード例 #7
0
ファイル: KosarajuSCC.cs プロジェクト: kaplunov93/Algorithms
        private bool[] marked; // reached vertices

        #endregion Fields

        #region Constructors

        public KosarajuSCC(DiGraph G)
        {
            marked = new bool[G.V];
            id = new int[G.V];
            DepthFirstOrder order = new DepthFirstOrder(G.reverse());
            foreach (int s in order.ReversePost())
                if (!marked[s])
                { dfs(G, s); count++; }
        }
コード例 #8
0
        public Topological(EdgeWeightedDigraph ewd)
        {
            EdgeWeightedDirectedCycle edgeWeightedDirectedCycle = new EdgeWeightedDirectedCycle(ewd);

            if (!edgeWeightedDirectedCycle.hasCycle())
            {
                DepthFirstOrder depthFirstOrder = new DepthFirstOrder(ewd);
                this.order = depthFirstOrder.reversePost();
            }
        }
コード例 #9
0
        public Topological(Digraph d)
        {
            DirectedCycle directedCycle = new DirectedCycle(d);

            if (!directedCycle.hasCycle())
            {
                DepthFirstOrder depthFirstOrder = new DepthFirstOrder(d);
                this.order = depthFirstOrder.reversePost();
            }
        }
コード例 #10
0
    public Topological(DirectedGraph g)
    {
        DirectedCycle cycleFinder = new DirectedCycle(g);

        if (!cycleFinder.HasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(g);
            m_order = dfs.ReversePost();
        }
    }
コード例 #11
0
ファイル: Topological.cs プロジェクト: kismy/Algorithms
    public Topological(EdgeWeightedDigraph G)
    {
        EdgeWeightedDirectedCycle finder = new EdgeWeightedDirectedCycle(G);

        if (!finder.hasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(G);
            order = dfs.reversePost();
        }
    }
コード例 #12
0
            public Topological(DirectedGraph graph)
            {
                var cycleFinder = new DirectedCycle(graph);

                if (!cycleFinder.HasCycle())
                {
                    var depthFirstOrder = new DepthFirstOrder(graph);
                    order = depthFirstOrder.ReversePost();
                }
            }
コード例 #13
0
    public Topological(Digraph g)
    {
        DirectedCycle cycle = new DirectedCycle(g);

        if (!cycle.HasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(g);

            order = dfs.Pre();
        }
    }
コード例 #14
0
ファイル: KosarajuSCC.cs プロジェクト: sve2-2018ss/Algorithms
        private int count;     // number of strong components
        public KosarajuSCC(DiGraph G)
        {
            marked = new bool[G.V];
            id     = new int[G.V];
            DepthFirstOrder order = new DepthFirstOrder(G.reverse());

            foreach (int s in order.ReversePost())
            {
                if (!marked[s])
                {
                    dfs(G, s); count++;
                }
            }
        }
コード例 #15
0
ファイル: TestClient.cs プロジェクト: ged29/Solution1
        public static void TestFileInputTopologicalSort()
        {
            string          absFileName   = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "GraphFiles", "topSort.txt");
            SymbolDigraph   symbolDigraph = new SymbolDigraph(absFileName, ' ');
            DepthFirstOrder dfsOrder      = new DepthFirstOrder(symbolDigraph.graph);

            string preOrderList         = String.Join(",", Array.ConvertAll(dfsOrder.PreOrder, x => symbolDigraph.GetKey(x)));
            string postOrderList        = String.Join(",", Array.ConvertAll(dfsOrder.PostOrder, x => symbolDigraph.GetKey(x)));
            string reversePostOrderList = String.Join(",", Array.ConvertAll(dfsOrder.ReversePostOrder, x => symbolDigraph.GetKey(x)));

            Console.WriteLine(string.Format("PreOrder:{0}", preOrderList));
            Console.WriteLine(string.Format("PostOrder:{0}", postOrderList));
            Console.WriteLine(string.Format("ReversePostOrder:{0}", reversePostOrderList));
        }
        public KosarajuDigraphStronglyConnectedComponents(IDigraph g)
        {
            _marked = new bool[g.V];
            _id     = new int[g.V];
            var dfo = new DepthFirstOrder(g.Reverse());

            foreach (int w in dfo.ReversePost)
            {
                if (!_marked[w])
                {
                    dfs(g, w); //помечаем все вершины, которые достижимы из данной
                    _count++;  // увеличиваем номер компонента
                }
            }
        }
コード例 #17
0
        public KosarajuStrongConnectedComponents(Digraph g)
        {
            marked = new bool[g.Vcount];
            id     = new int[g.Vcount];
            var order = new DepthFirstOrder(g.Reverse());

            foreach (var i in order.ReversePost())
            {
                if (!marked[i])
                {
                    Dfs(g, i);
                    count++;
                }
            }
        }
コード例 #18
0
    public KosarajuSCC(DirectedGraph g)
    {
        m_marked = new bool[g.V()];
        m_id     = new int[g.V()];
        DepthFirstOrder order = new DepthFirstOrder(g.Reverses());

        foreach (var s in order.ReversePost())
        {
            if (!m_marked[s])
            {
                Dfs(g, s);
                m_count++;
            }
        }
    }
コード例 #19
0
        public KosarajuCC(Digraph G)
        {
            mark = new bool[G.v()];
            id   = new int[G.v()];
            DepthFirstOrder order = new DepthFirstOrder(G.reverse());

            foreach (int s in order.getReversePost())
            {
                if (!mark[s])
                {
                    dfs(G, s);
                    count++;
                }
            }
        }
コード例 #20
0
        public KosarajuSharirCC(DirectedGraph graph)
        {
            marked = new bool[graph.V];
            _id    = new int[graph.V];
            DepthFirstOrder dfo = new DepthFirstOrder(graph.Reverse());

            foreach (int v in dfo.ReversePost())
            {
                if (!marked[v])
                {
                    dfs(graph, v);
                    Count++;
                }
            }
        }
コード例 #21
0
ファイル: KosarajuSCC.cs プロジェクト: allanma88/SampleCode
        public KosarajuSCC(Digraph G)
            : base(G)
        {
            _marked = new bool[G.V()];
            _id     = new int[G.V()];
            DepthFirstOrder order = new DepthFirstOrder(G.Reverse());

            foreach (var v in order.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(G, v);
                    _count++;
                }
            }
        }
コード例 #22
0
ファイル: KosarajuSCC.cs プロジェクト: wubh110/Algorithms
        public KosarajuSCC(Digraph digraph)
        {
            marked = new bool[digraph.Vertices];
            id     = new int[digraph.Vertices];

            var order = new DepthFirstOrder(digraph.Reverse());

            foreach (var s in order.ReversePost())
            {
                if (!marked[s])
                {
                    Dfs(digraph, s);
                    count++;
                }
            }
        }
コード例 #23
0
ファイル: Topological.cs プロジェクト: kismy/Algorithms
    private int[] rank;         // rank[v] = position of vertex v in topological order


    public Topological(Digraph G)
    {
        DirectedCycle finder = new DirectedCycle(G);

        if (!finder.hasCycle())
        {
            DepthFirstOrder dfs = new DepthFirstOrder(G);
            order = dfs.reversePost();
            rank  = new int[G.V()];
            int i = 0;
            foreach (int v in order)
            {
                rank[v] = i++;
            }
        }
    }
コード例 #24
0
    private int count;         // number of strongly-connected components

    public KosarajuSharirSCC(Digraph G)
    {
        // compute reverse postorder of reverse graph
        DepthFirstOrder dfs = new DepthFirstOrder(G.Reverse());

        // run DFS on G, using reverse postorder to guide calculation
        marked = new bool[G.V()];
        id     = new int[G.V()];
        foreach (int v in dfs.reversePost())
        {
            if (!marked[v])
            {
                DFS(G, v);
                count++;
            }
        }
    }
コード例 #25
0
    /**/ public static void main(string[] strarr)
    {
        In              i               = new In(strarr[0]);
        Digraph         digraph         = new Digraph(i);
        DepthFirstOrder depthFirstOrder = new DepthFirstOrder(digraph);

        StdOut.println("   v  pre post");
        StdOut.println("--------------");
        for (int j = 0; j < digraph.V(); j++)
        {
            StdOut.printf("%4d %4d %4d\n", new object[]
            {
                Integer.valueOf(j),
                Integer.valueOf(depthFirstOrder.pre(j)),
                Integer.valueOf(depthFirstOrder.post(j))
            });
        }
        StdOut.print("Preorder:  ");
        Iterator iterator = depthFirstOrder.pre().iterator();

        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Postorder: ");
        iterator = depthFirstOrder.post().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
        StdOut.print("Reverse postorder: ");
        iterator = depthFirstOrder.reversePost().iterator();
        while (iterator.hasNext())
        {
            int i2 = ((Integer)iterator.next()).intValue();
            StdOut.print(new StringBuilder().append(i2).append(" ").toString());
        }
        StdOut.println();
    }
コード例 #26
0
        private readonly bool[] _marked; // marked[v] = has vertex v been visited?

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Computes the strong components of the digraph <tt>G</tt>.
        /// </summary>
        /// <param name="g">g the digraph</param>
        public KosarajuSharirSCC(Digraph g)
        {
            // compute reverse postorder of reverse graph
            var dfs = new DepthFirstOrder(g.Reverse());

            // run DFS on G, using reverse postorder to guide calculation
            _marked = new bool[g.V];
            _id = new int[g.V];
            foreach (int v in dfs.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(g, v);
                    _count++;
                }
            }

            // check that id[] gives strong components
            //assert check(G);
        }
コード例 #27
0
        private readonly int _count;         // number of strongly-connected components

        /// <summary>
        /// Computes the strong components of the digraph <tt>G</tt>.
        /// </summary>
        /// <param name="g">g the digraph</param>
        public KosarajuSharirSCC(Digraph g)
        {
            // compute reverse postorder of reverse graph
            var dfs = new DepthFirstOrder(g.Reverse());

            // run DFS on G, using reverse postorder to guide calculation
            _marked = new bool[g.V];
            _id     = new int[g.V];
            foreach (int v in dfs.ReversePost())
            {
                if (!_marked[v])
                {
                    Dfs(g, v);
                    _count++;
                }
            }

            // check that id[] gives strong components
            //assert check(G);
        }
コード例 #28
0
    void Start()
    {
        Digraph G = new Digraph(txt);

        DepthFirstOrder dfs = new DepthFirstOrder(G);

        print("   v  pre post");
        print("--------------");
        string str = null;

        for (int v = 0; v < G.V(); v++)
        {
            str += (v + " " + dfs.Pre(v) + " " + dfs.Post(v) + "\n");
        }
        print(str);

        str = "Preorder:  ";

        foreach (int v in dfs.Pre())
        {
            str += (v + " ");
        }
        print(str);

        str = "Postorder: ";

        foreach (int v in dfs.Post())
        {
            str += (v + " ");
        }
        print(str);

        str = "Reverse postorder: ";

        foreach (int v in dfs.reversePost())
        {
            str += (v + " ");
        }
        print(str);
    }
コード例 #29
0
    public KosarajuSharirSCC(Digraph d)
    {
        DepthFirstOrder depthFirstOrder = new DepthFirstOrder(d.reverse());

        this.marked = new bool[d.V()];
        this.id     = new int[d.V()];
        Iterator iterator = depthFirstOrder.reversePost().iterator();

        while (iterator.hasNext())
        {
            int num = ((Integer)iterator.next()).intValue();
            if (!this.marked[num])
            {
                this.dfs(d, num);
                this.count++;
            }
        }
        if (!KosarajuSharirSCC.s_assertionsDisabled && !this.check(d))
        {
            throw new AssertionError();
        }
    }
コード例 #30
0
    public KosarajuSCC(Digraph g)
    {
        marked = new bool[g.V()];
        id     = new int[g.V()];

        Digraph reverse = g.Reverse();

        //逆后续
        DepthFirstOrder order = new DepthFirstOrder(reverse);
        Stack <int>     post  = order.ReverstPost();

        while (post.Count != 0)
        {
            //逆序深度搜索
            int v = post.Pop();
            Debug.Log(v);
            if (!marked[v])
            {
                DFS(g, v);
                count++;
            }
        }
    }
コード例 #31
0
        public void Testing()
        {
            var graph = new DirectedGraph(13);

            graph.InsertEdge(4, 2);
            graph.InsertEdge(2, 3);
            graph.InsertEdge(3, 2);
            graph.InsertEdge(6, 0);
            graph.InsertEdge(0, 1);
            graph.InsertEdge(2, 0);
            graph.InsertEdge(11, 12);
            graph.InsertEdge(12, 9);
            graph.InsertEdge(9, 10);
            graph.InsertEdge(9, 11);
            graph.InsertEdge(8, 9);
            graph.InsertEdge(10, 12);
            graph.InsertEdge(11, 4);
            graph.InsertEdge(4, 3);
            graph.InsertEdge(3, 5);
            graph.InsertEdge(7, 8);
            graph.InsertEdge(8, 7);
            graph.InsertEdge(5, 4);
            graph.InsertEdge(0, 5);
            graph.InsertEdge(6, 4);
            graph.InsertEdge(6, 9);
            graph.InsertEdge(7, 6);

            var vertexOutDegree = graph.VertexOutDegree(6);
            var vertexInDegre   = graph.VertexInDegree(6);
            var graphReveresed  = graph.Reverse();

            var depthFirstSearch      = new DepthFirstSearch(graph, 0);
            var depthFirstSearchCount = depthFirstSearch.Count();

            var depthFirstSearchTest0 = depthFirstSearch.Marked(0);
            var depthFirstSearchTest1 = depthFirstSearch.Marked(1);
            var depthFirstSearchTest2 = depthFirstSearch.Marked(2);
            var depthFirstSearchTest3 = depthFirstSearch.Marked(3);
            var depthFirstSearchTest4 = depthFirstSearch.Marked(4);
            var depthFirstSearchTest5 = depthFirstSearch.Marked(5);
            var depthFirstSearchTest6 = depthFirstSearch.Marked(6);

            var depthFirstSearchTestConnected = depthFirstSearchCount == graph.Vertices ? "Connected" : "NOT Connected";

            var depthFirstPath     = new DepthFirstPaths(graph, 0);
            var depthFirstPathTest = depthFirstPath.PathTo(2);

            var breadthFirstSearch      = new BreadthFirstSearch(graph, 0);
            var breadthFirstSearchCount = breadthFirstSearch.Count();

            var breadthFirstSearchTest0 = breadthFirstSearch.Marked(0);
            var breadthFirstSearchTest1 = breadthFirstSearch.Marked(1);
            var breadthFirstSearchTest2 = breadthFirstSearch.Marked(2);
            var breadthFirstSearchTest3 = breadthFirstSearch.Marked(3);
            var breadthFirstSearchTest4 = breadthFirstSearch.Marked(4);
            var breadthFirstSearchTest5 = breadthFirstSearch.Marked(5);
            var breadthFirstSearchTest6 = breadthFirstSearch.Marked(6);

            var breadthFirstSearchTestConnected = breadthFirstSearchCount == graph.Vertices ? "Connected" : "NOT Connected";

            var breadthFirstPath     = new BreadthFirstPaths(graph, 0);
            var breadthFirstPathTest = breadthFirstPath.PathTo(2);

            var depthFirstCycle      = new DepthFirstCycle(graph);
            var depthFirstCycleTest0 = depthFirstCycle.HasCycle();
            var depthFirstCycleTest1 = depthFirstCycle.Cycle();

            var graph2 = new DirectedGraph(13);

            graph2.InsertEdge(2, 3);
            graph2.InsertEdge(0, 6);
            graph2.InsertEdge(0, 1);
            graph2.InsertEdge(2, 0);
            graph2.InsertEdge(11, 12);
            graph2.InsertEdge(9, 12);
            graph2.InsertEdge(9, 10);
            graph2.InsertEdge(9, 11);
            graph2.InsertEdge(3, 5);
            graph2.InsertEdge(8, 7);
            graph2.InsertEdge(5, 4);
            graph2.InsertEdge(0, 5);
            graph2.InsertEdge(6, 4);
            graph2.InsertEdge(6, 9);
            graph2.InsertEdge(7, 6);

            var depthFirstCycle2      = new DepthFirstCycle(graph2);
            var depthFirstCycle2Test0 = depthFirstCycle2.HasCycle();
            var depthFirstCycle2Test1 = depthFirstCycle2.Cycle();

            var depthFirstOrder = new DepthFirstOrder(graph2);

            var depthFirstOrderTest0 = depthFirstOrder.PreOrder();
            var depthFirstOrderTest1 = depthFirstOrder.PostOrder();
            var depthFirstOrderTest2 = depthFirstOrder.ReversePostOrder();

            var symbolGraph = new SymbolGraph();

            symbolGraph.Insert(new[] { "JFK", "MCO" });
            symbolGraph.Insert(new[] { "ORD", "DEN" });
            symbolGraph.Insert(new[] { "ORD", "HOU" });
            symbolGraph.Insert(new[] { "DFW", "PHX" });
            symbolGraph.Insert(new[] { "JFK", "ATL" });
            symbolGraph.Insert(new[] { "ORD", "DFW" });
            symbolGraph.Insert(new[] { "ORD", "PHX" });
            symbolGraph.Insert(new[] { "ATL", "HOU" });
            symbolGraph.Insert(new[] { "DEN", "PHX" });
            symbolGraph.Insert(new[] { "PHX", "LAX" });
            symbolGraph.Insert(new[] { "JFK", "ORD" });
            symbolGraph.Insert(new[] { "DEN", "LAS" });
            symbolGraph.Insert(new[] { "DFW", "HOU" });
            symbolGraph.Insert(new[] { "ORD", "ATL" });
            symbolGraph.Insert(new[] { "LAS", "LAX" });
            symbolGraph.Insert(new[] { "ATL", "MCO" });
            symbolGraph.Insert(new[] { "HOU", "MCO" });
            symbolGraph.Insert(new[] { "LAS", "PHX" });

            symbolGraph.Build();

            var symbolGraphTest0 = symbolGraph.Contains("LAS");
            var symbolGraphTest1 = symbolGraph.Contains("MOO");
            var symbolGraphTest2 = symbolGraph.Index("LAX");
            var symbolGraphTest3 = symbolGraph.Name(symbolGraphTest2);

            var symbolGraph2 = new SymbolGraph();

            symbolGraph2.Insert(new[] { "Algorithms", "Theoretical CS", "Databases", "Scientific Computing" });
            symbolGraph2.Insert(new[] { "Introduction to CS", "Advanced Programming", "Algorithms" });
            symbolGraph2.Insert(new[] { "Advanced Programming", "Scientific Computing" });
            symbolGraph2.Insert(new[] { "Scientific Computing", "Computational Biology" });
            symbolGraph2.Insert(new[] { "Theoretical CS", "Computational Biology", "Artificial Intelligence" });
            symbolGraph2.Insert(new[] { "Linear Algebra", "Theoretical CS" });
            symbolGraph2.Insert(new[] { "Calculus", "Linear Algebra" });
            symbolGraph2.Insert(new[] { "Artificial Intelligence", "Neural Networks", "Robotics", "Machine Learning" });
            symbolGraph2.Insert(new[] { "Machine Learning", "Neural Networks" });

            symbolGraph2.Build();

            var topologicalSort   = new TopologicalSort(symbolGraph2.Graph());
            var topologicalSorted = new List <string>();

            foreach (var item in topologicalSort.Order)
            {
                topologicalSorted.Add(symbolGraph2.Name(item));
            }

            var depthFirstComponents = new DepthFirstComponents(graph);

            var depthFirstComponentsTest0 = depthFirstComponents.IsStronglyConnected(1, 7);
            var depthFirstComponentsTest1 = depthFirstComponents.IsStronglyConnected(0, 2);
            var depthFirstComponentsTest2 = depthFirstComponents.IsStronglyConnected(2, 7);
            var depthFirstComponentsTest3 = depthFirstComponents.IsStronglyConnected(9, 12);
            var depthFirstComponentsTest4 = depthFirstComponents.IsStronglyConnected(6, 8);
            var depthFirstComponentsTest5 = depthFirstComponents.IsStronglyConnected(7, 8);
        }
コード例 #32
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyDAG.txt"); // Prompt
            Console.WriteLine("or quit");         // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDAG.txt";
                break;

            case "quit":
                return;

            default:
                return;
            }


            var @in   = new In($"Files\\Graphs\\{fileName}");
            var lines = @in.ReadAllLines();

            var lineIterator = 0;
            var v            = 0;
            var e            = 0;
            var edges        = new List <EdgeD>();

            foreach (var line in lines)
            {
                if (lineIterator == 0)
                {
                    v = Convert.ToInt32(line);
                }
                if (lineIterator == 1)
                {
                    e = Convert.ToInt32(line);
                }
                if (lineIterator > 1)
                {
                    var lineSplitted = line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    var ve           = Convert.ToInt32(lineSplitted[0]);
                    var we           = Convert.ToInt32(lineSplitted[1]);
                    var edge         = new EdgeD(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var digraph = new Digraph(v, e, edges);

            Console.WriteLine(digraph);

            var dfs = new DepthFirstOrder(digraph);

            Console.WriteLine("   v  pre post");
            Console.WriteLine("--------------");
            for (var vi = 0; vi < digraph.V; vi++)
            {
                Console.Write($"{vi} {dfs.Pre(vi)} {dfs.Post(vi)}{Environment.NewLine}");
            }

            Console.Write("Preorder:  ");
            foreach (int vi in dfs.Pre())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Postorder: ");
            foreach (int vi in dfs.Post())
            {
                Console.Write($"{vi} ");
            }
            Console.WriteLine();

            Console.Write("Reverse postorder: ");
            foreach (int vi in dfs.ReversePost())
            {
                Console.Write($"{vi} ");
            }
            Console.ReadLine();
        }