コード例 #1
0
ファイル: TransitiveClosure.cs プロジェクト: kismy/Algorithms
    void Start()
    {
        Digraph G = new Digraph(txt);

        TransitiveClosure tc = new TransitiveClosure(G);

        // print header
        string str = ("     ");

        for (int v = 0; v < G.V(); v++)
        {
            str += v;
        }
        print(str);

        // print transitive closure
        for (int v = 0; v < G.V(); v++)
        {
            str = (v + ": ");
            for (int w = 0; w < G.V(); w++)
            {
                if (tc.reachable(v, w))
                {
                    str += ("  T");
                }
                else
                {
                    str += ("   ");
                }
            }
            print(str);
        }
    }
コード例 #2
0
ファイル: Graph.cs プロジェクト: FuGangqiang/algorithms
        public void TransitiveClosureTest()
        {
            var g  = DigraphSample();
            var tc = new TransitiveClosure(g);

            Assert.True(tc.Reachable(0, 2));
            Assert.False(tc.Reachable(9, 6));
        }
コード例 #3
0
        public void Test_IsReachable()
        {
            Digraph graph = new Digraph(3);

            graph.AddEdge(0, 1);
            graph.AddEdge(0, 2);

            TransitiveClosure closure = TransitiveClosure.Create(graph);

            Assert.True(closure.IsReachable(0, 2));
        }
コード例 #4
0
        public void Test_NotReachable()
        {
            Digraph graph = new Digraph(4);

            graph.AddEdge(0, 1);
            graph.AddEdge(1, 2);
            graph.AddEdge(0, 3);

            TransitiveClosure closure = TransitiveClosure.Create(graph);

            Assert.False(closure.IsReachable(3, 1));
        }
コード例 #5
0
 /// <summary>
 /// does the id[] array contain the strongly connected components?
 /// </summary>
 /// <param name="g"></param>
 /// <returns></returns>
 public bool Check(Digraph g)
 {
     var tc = new TransitiveClosure(g);
     for (var v = 0; v < g.V; v++)
     {
         for (var w = 0; w < g.V; w++)
         {
             if (StronglyConnected(v, w) != (tc.Reachable(v, w) && tc.Reachable(w, v)))
                 return false;
         }
     }
     return true;
 }
コード例 #6
0
    private bool check(Digraph G)
    {
        TransitiveClosure tc = new TransitiveClosure(G);

        for (int v = 0; v < G.V(); v++)
        {
            for (int w = 0; w < G.V(); w++)
            {
                if (stronglyConnected(v, w) != (tc.reachable(v, w) && tc.reachable(w, v)))
                {
                    return(false);
                }
            }
        }
        return(true);
    }
コード例 #7
0
        /// <summary>
        /// does the id[] array contain the strongly connected components?
        /// </summary>
        /// <param name="g"></param>
        /// <returns></returns>
        public bool Check(Digraph g)
        {
            var tc = new TransitiveClosure(g);

            for (var v = 0; v < g.V; v++)
            {
                for (var w = 0; w < g.V; w++)
                {
                    if (StronglyConnected(v, w) != (tc.Reachable(v, w) && tc.Reachable(w, v)))
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
コード例 #8
0
    private bool check(Digraph digraph)
    {
        TransitiveClosure transitiveClosure = new TransitiveClosure(digraph);

        for (int i = 0; i < digraph.V(); i++)
        {
            for (int j = 0; j < digraph.V(); j++)
            {
                if (this.stronglyConnected(i, j) != ((!transitiveClosure.reachable(i, j) || !transitiveClosure.reachable(j, i)) ? false : true))
                {
                    return(false);
                }
            }
        }
        return(true);
    }
コード例 #9
0
ファイル: Program.cs プロジェクト: kaplunov93/Algorithms
 public static bool Reachable(this DiGraph dg, int first, int second)
 {
     TransitiveClosure ts=new TransitiveClosure(dg);
     return ts.reachable(first, second);
 }
コード例 #10
0
        public void Run()
        {
            Console.WriteLine("Choose file:");   // Prompt
            Console.WriteLine("1 - tinyDG.txt"); // Prompt
            Console.WriteLine("or quit");        // Prompt

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

            switch (fileNumber)
            {
            case "1":
                fileName = "tinyDG.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 tc = new TransitiveClosure(digraph);

            // print header
            Console.Write("     ");
            for (var i = 0; i < digraph.V; i++)
            {
                Console.Write($"{i,3}");
            }
            Console.WriteLine();
            Console.WriteLine("--------------------------------------------");

            // print transitive closure
            for (var i = 0; i < digraph.V; i++)
            {
                Console.Write($"{i,3}: ");
                for (var w = 0; w < digraph.V; w++)
                {
                    Console.Write(tc.Reachable(i, w) ? "  T" : "   ");
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }