Ejemplo n.º 1
0
        public void BreadthFirstPathsTest1()
        {
            Graph             g = TestHelpers.buildGraph();
            BreadthFirstPaths bfps;

            bfps = new BreadthFirstPaths(g, 0);

            Assert.AreEqual(0, bfps.DistTo(0));
            Assert.AreEqual(1, bfps.DistTo(1));
            Assert.AreEqual(3, bfps.DistTo(4));

            Assert.AreEqual(int.MaxValue, bfps.DistTo(10));
            Assert.AreEqual(int.MaxValue, bfps.DistTo(12));
            Assert.AreEqual(int.MaxValue, bfps.DistTo(5));

            bfps = new BreadthFirstPaths(g, 10);
            Assert.IsTrue(bfps.HasPathTo(11));
            Assert.AreEqual(1, bfps.DistTo(11));

            bfps = new BreadthFirstPaths(g, 12);
            Assert.AreEqual(2, bfps.DistTo(15));

            Assert.IsFalse(bfps.HasPathTo(11));
            Assert.IsFalse(bfps.HasPathTo(3));
            Assert.IsFalse(bfps.HasPathTo(9));
        }
Ejemplo n.º 2
0
        public void BFS_Test()
        {
            BreadthFirstPaths BFS = new BreadthFirstPaths(basicGraph, 0);

            Assert.IsTrue(BFS.HasPathTo(3));
            Assert.IsFalse(BFS.HasPathTo(8));
            var path = BFS.PathTo(7);
            var SB   = new StringBuilder();

            foreach (int v in path)
            {
                SB.Append(v.ToString());
            }
            Assert.AreEqual("027", SB.ToString());
        }
Ejemplo n.º 3
0
        public void bfsTest()
        {
            using (StreamReader sr = new StreamReader(@"E:\Study\ALG2017\ALGRKC\dataSelf\tinyCG.txt"))
            {
                Graph             g      = new Graph(sr);
                int               source = 0;
                BreadthFirstPaths bfp    = new BreadthFirstPaths(g, source);
                bfp.bfs(g, source);

                for (int i = 0; i < g.V(); i++)
                {
                    Console.Write(source + " to " + i + " : ");
                    if (bfp.HasPathTo(i))
                    {
                        foreach (int v in bfp.PathTo(i))
                        {
                            if (v == source)
                            {
                                Console.Write(source);
                            }
                            else
                            {
                                Console.Write("-" + v);
                            }
                        }
                        Console.WriteLine();
                    }
                    else
                    {
                        Console.WriteLine("no path");
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void BFS_MultiSource_Test()
        {
            var sources = new HashSet <int>()
            {
                0, 3
            };
            BreadthFirstPaths BFS = new BreadthFirstPaths(basicGraph, sources);

            Assert.IsTrue(BFS.HasPathTo(3));
            Assert.IsFalse(BFS.HasPathTo(8));
            var path = BFS.PathTo(7);
            var SB   = new StringBuilder();

            foreach (int v in path)
            {
                SB.Append(v.ToString());
            }
            Assert.AreEqual("027", SB.ToString());
            Stack <int> path4 = (Stack <int>)BFS.PathTo(4); //We know that returned type is an stack

            Assert.AreEqual(3, path4.Pop());
        }
Ejemplo n.º 5
0
        public void BreadthFirstPathsTest()
        {
            var g   = GraphSample();
            var bfs = new BreadthFirstPaths(g, 0);

            Assert.True(bfs.HasPathTo(5));
            var path = bfs.PathTo(5);

            int[] result = { 5, 0 };
            int   i      = 0;

            foreach (int v in path)
            {
                Assert.Equal(result[i], v);
                i++;
            }
        }
    private static void SeverLink(Graph graph, int skyNetLocation)
    {
        var bfp          = new BreadthFirstPaths(graph, skyNetLocation);
        var shortestExit = -1;
        var exitLength   = Int32.MaxValue;

        foreach (var exit in graph.Exits)
        {
            if (bfp.HasPathTo(exit))
            {
                var currLen = bfp.PathTo(exit).Count();
                if (currLen < exitLength)
                {
                    exitLength   = currLen;
                    shortestExit = exit;
                }
            }
        }

        var targetNode = bfp.PathTo(shortestExit).Skip(1).First();

        graph.RemoveEdge(skyNetLocation, targetNode);
        Console.WriteLine("{0} {1}", skyNetLocation, targetNode);
    }
Ejemplo n.º 7
0
        public void Run()
        {
            Console.WriteLine("Choose file:");    // Prompt
            Console.WriteLine("1 - tinyCG.txt");  // Prompt
            Console.WriteLine("2 - mediumG.txt"); // Prompt
            Console.WriteLine("3 - largeG.zip");  // Prompt
            Console.WriteLine("or quit");         // Prompt

            var fileNumber = Console.ReadLine();
            var fieName    = string.Empty;

            switch (fileNumber)
            {
            case "1":
                fieName = "tinyCG.txt";
                break;

            case "2":
                fieName = "mediumG.txt";
                break;

            case "3":
                fieName = "largeG.zip";
                break;

            case "quit":
                return;

            default:
                return;
            }


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

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

            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 EdgeU(ve, we);
                    edges.Add(edge);
                }

                lineIterator++;
            }

            var graph = new Graph(v, e, edges);

            if (fileNumber != "3")
            {
                Console.WriteLine(graph);
            }


            const int s    = 0;
            var       bfs1 = new BreadthFirstPaths(graph, s);

            for (var vi = 0; vi < graph.V; vi++)
            {
                if (bfs1.HasPathTo(vi))
                {
                    Console.Write($"{s} to {vi}:  ");
                    foreach (int x in bfs1.PathTo(vi))
                    {
                        if (x == s)
                        {
                            Console.Write(x);
                        }
                        else
                        {
                            Console.Write($"-{x}");
                        }
                    }
                    Console.WriteLine();
                }

                else
                {
                    Console.WriteLine($"{s} to {v}:  not connected{Environment.NewLine}");
                }
                if (vi >= 1 && fileNumber == "3")
                {
                    break;
                }
            }

            //Console.WriteLine("------------------------------------------------");

            Console.ReadLine();
        }