Example #1
0
        public void TestDigraph()
        {
            Random rand = new Random();
            int v = rand.Next(10, 30);

            Digraph g = new Digraph(v);
            int e0 = rand.Next(10, 30);
            string strs = string.Empty;
            for (int i = 0; i < e0; i++)
            {
                int v1 = rand.Next(0, v);
                int v2 = rand.Next(0, v);
                string str = v1 + " - " + v2 + ",";
                string str1 = v2 + " - " + v1 + ",";

                if (v1 == v2 || strs.Contains(str) || strs.Contains(str1))
                {
                    i--;
                    continue;
                }

                g.AddEdge(v1, v2);
                strs += str;
            }

            Debug.WriteLine("input e: " + strs);
            Debug.WriteLine(g.ToString());

            Digraph g1 = new Digraph(g);
            Debug.WriteLine("g1: ");
            Debug.WriteLine(g1.ToString());
        }
Example #2
0
 public Digraph Reverse()
 {
     Digraph r = new Digraph(V);
     for (int v = 0; v < V; v++)
         foreach (int w in this.Adj[v])
             r.AddEdge(w, v);
     return r;
 }
Example #3
0
        public void TestDirectedCycle()
        {
            Digraph g = new Digraph(6);
            g.AddEdge(0, 5);
            g.AddEdge(5, 4);
            g.AddEdge(4, 3);
            g.AddEdge(3, 5);
            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            Debug.WriteLine(g.ToString());

            DirectedCycle finder = new DirectedCycle(g);
            if (finder.HasCycle())
            {
                Debug.WriteLine("Directed cycle:");
                foreach (var v in finder.Cycle)
                    Debug.Write(" => " + v);
            }
            else
                Debug.WriteLine("No directed cycle!");
        }
Example #4
0
        public void TestKosarajuSCCHasNotCycle()
        {
            Digraph g = new Digraph(6);
            g.AddEdge(0, 1);
            g.AddEdge(1, 2);
            g.AddEdge(2, 3);
            g.AddEdge(0, 4);
            g.AddEdge(4, 5);
            Debug.WriteLine(g.ToString());

            DepthFirstOrder dfs = new DepthFirstOrder(g);
            StringBuilder sb = new StringBuilder();
            foreach (var item in dfs.ReversePost)
                sb.Append(item + " ");
            Debug.WriteLine(sb.ToString());

            KosarajuSCC scc = new KosarajuSCC(g);
            sb.Clear();
            for (int i = 0; i < scc.ID.Length; i++)
                sb.Append(i + " - " + scc.ID[i] + Environment.NewLine);
            Debug.WriteLine(sb.ToString());
        }