Esempio n. 1
0
        public void GetCyclesTest_GraphWithTwoCycle_TwoCycle()
        {
            List <List <int> > graph = new List <List <int> > {
                new List <int> {
                    1, 4
                }, new List <int> {
                    2, 3
                }, new List <int> {
                    0
                }, new List <int> {
                    0,
                }, new List <int> {
                    1
                }
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            List <List <int> > cycles = unweightedGraph.GetCycles();

            bool isTrueCount = cycles.Count == 2;

            Assert.IsTrue(isTrueCount && cycles[0].SequenceEqual(new List <int> {
                0, 1, 2, 0
            }) && cycles[1].SequenceEqual(new List <int> {
                0, 4, 1, 3, 0
            }));
        }
Esempio n. 2
0
        public void GetCycleTest_CycleOfFiveVertex_Cycle()
        {
            List <List <int> > graph = new List <List <int> > {
                new List <int> {
                    1
                }, new List <int> {
                    2
                }, new List <int> {
                    3
                }, new List <int> {
                    4
                }, new List <int> {
                    0
                }
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            unweightedGraph.IsAcyclic();
            List <int> cycle = unweightedGraph.GetCycle();

            Assert.IsTrue(cycle.SequenceEqual(new List <int>()
            {
                0, 1, 2, 3, 4, 0
            }));
        }
Esempio n. 3
0
        public void IsAcyclicTest_Empty_True()
        {
            List <List <int> > graph = new List <List <int> >()
            {
                new List <int>(), new List <int>(), new List <int>()
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            bool actual = unweightedGraph.IsAcyclic();

            Assert.IsTrue(actual);
        }
Esempio n. 4
0
        public void GetCycleTest_EmptyGraph_EmptyList()
        {
            List <List <int> > graph = new List <List <int> >()
            {
                new List <int>(), new List <int>(), new List <int>()
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            unweightedGraph.IsAcyclic();
            List <int> cycle = unweightedGraph.GetCycle();

            Assert.IsTrue(cycle.SequenceEqual(new List <int>()));
        }
Esempio n. 5
0
        public void GraphIsBuildCorrectly()
        {
            /*  graph visual:
             *      ┌──>va1──>va2──>va3<<─┐
             *  v1──┼──>vc1─────────────┤
             *      └──>vb1──>vb2──>vb3<<─┘
             */

            string       correctPathName = "correctPath";
            var          graph           = new UnweightedGraph();
            Vertex <int> v1  = new Vertex <int>(1);
            Vertex <int> va1 = new Vertex <int>(2);
            Vertex <int> va2 = new Vertex <int>(3);
            Vertex <int> va3 = new Vertex <int>(4);
            Vertex <int> vb1 = new Vertex <int>(5);
            Vertex <int> vb2 = new Vertex <int>(6);
            Vertex <int> vb3 = new Vertex <int>(7);
            Vertex <int> vc1 = new Vertex <int>(8);

            graph.AddEdge(v1, va1, "name");
            graph.AddEdge(va1, va2, "name");
            graph.AddEdge(va2, va3, "name");
            graph.AddEdge(v1, vb1, "name");
            graph.AddEdge(vb1, vb2, "name");
            graph.AddEdge(vb2, vb3, "name");
            graph.AddEdge(v1, vc1, correctPathName);
            graph.AddEdge(vc1, va3, correctPathName);
            graph.AddEdge(vc1, vb3, "name");


            Assert.Equal(8, graph.Vertices.Count);
            Assert.Equal(9, graph.Edges.Count());

            var bfs = graph.BreathFirstIterator(vb2).ToList();

            Assert.Equal(2, bfs.Count);

            Vertex <int> vstart = new Vertex <int>(1);
            Vertex <int> vend   = new Vertex <int>(4);
            var          sp     = graph.ShortestPath(vstart, vend).ToList();

            Assert.Equal(2, sp.Count());

            // Assert.All(sp, e => Assert.True(e. == correctPathName));
            Assert.Equal(v1, sp[0].From);
            Assert.Equal(vc1, sp[0].To);
            Assert.Equal(vc1, sp[1].From);
            Assert.Equal(va3, sp[1].To);
        }
Esempio n. 6
0
        public void IsAcyclicTest_CycleOfTwoVertex_False()
        {
            List <List <int> > graph = new List <List <int> >()
            {
                new List <int> {
                    1, 2
                }, new List <int>(), new List <int> {
                    0, 1
                }
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            bool actual = unweightedGraph.IsAcyclic();

            Assert.IsFalse(actual);
        }
Esempio n. 7
0
        public void GetCyclesTest_GraphWithoutCycles_ZeroCycle()
        {
            List <List <int> > graph = new List <List <int> > {
                new List <int> {
                    1
                }, new List <int> {
                    2
                }, new List <int>()
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            List <List <int> > cycles = unweightedGraph.GetCycles();

            bool isTrueCount = cycles.Count == 0;

            Assert.IsTrue(isTrueCount);
        }
Esempio n. 8
0
        public void GetCycleTest_CycleOfTwoVertex_Cycle()
        {
            List <List <int> > graph = new List <List <int> >()
            {
                new List <int> {
                    1, 2
                }, new List <int>(), new List <int> {
                    0, 1
                }
            };
            UnweightedGraph unweightedGraph = new UnweightedGraph(graph);

            unweightedGraph.IsAcyclic();
            List <int> cycle = unweightedGraph.GetCycle();

            Assert.IsTrue(cycle.SequenceEqual(new List <int>()
            {
                0, 2, 0
            }));
        }
Esempio n. 9
0
        // ReSharper disable once UnusedMember.Local
        private static void GraphUsageDemo()
        {
            var cities = new[]
            {
                "Seattle", "San Francisco", "Los Angeles", "Riverside", "Phoenix", "Chicago", "Boston", "New York",
                "Atlanta", "Miami", "Dallas", "Houston", "Detroit", "Philadelphia", "Washington"
            };

            var values = Enum.GetValues(typeof(City)).Cast <City>().ToList();
            var unweightedCityGraph = new UnweightedGraph <City>(values);
            var weightedCityGraph   = new WeightedGraph <City>(values);

            unweightedCityGraph.AddEdges(new List <(City first, City second)>
            {
                (City.Seattle, City.Chicago),
                (City.Seattle, City.SanFrancisco),
                (City.SanFrancisco, City.Riverside),
                (City.SanFrancisco, City.LosAngeles),
                (City.LosAngeles, City.Riverside),
                (City.LosAngeles, City.Phoenix),
                (City.Riverside, City.Philadelphia),
                (City.Riverside, City.Chicago),
                (City.Phoenix, City.Dallas),
                (City.Phoenix, City.Houston),
                (City.Dallas, City.Chicago),
                (City.Dallas, City.Atlanta),
                (City.Dallas, City.Houston),
                (City.Houston, City.Atlanta),
                (City.Houston, City.Miami),
                (City.Atlanta, City.Chicago),
                (City.Atlanta, City.Washington),
                (City.Atlanta, City.Miami),
                (City.Miami, City.Washington),
                (City.Chicago, City.Detroit),
                (City.Detroit, City.Boston),
                (City.Detroit, City.Washington),
                (City.Detroit, City.NewYork),
                (City.Boston, City.NewYork),
                (City.NewYork, City.Philadelphia),
                (City.Philadelphia, City.Washington)
            });

            weightedCityGraph.AddEdges(new List <(City first, City second, float weight)>
            {
                (City.Seattle, City.Chicago, 1737),
                (City.Seattle, City.SanFrancisco, 678),
                (City.SanFrancisco, City.Riverside, 386),
                (City.SanFrancisco, City.LosAngeles, 348),
                (City.LosAngeles, City.Riverside, 50),
                (City.LosAngeles, City.Phoenix, 357),
                (City.Riverside, City.Phoenix, 307),
                (City.Riverside, City.Chicago, 1704),
                (City.Phoenix, City.Dallas, 887),
                (City.Phoenix, City.Houston, 1015),
                (City.Dallas, City.Chicago, 805),
                (City.Dallas, City.Atlanta, 721),
                (City.Dallas, City.Houston, 225),
                (City.Houston, City.Atlanta, 702),
                (City.Houston, City.Miami, 968),
                (City.Atlanta, City.Chicago, 588),
                (City.Atlanta, City.Washington, 543),
                (City.Atlanta, City.Miami, 604),
                (City.Miami, City.Washington, 923),
                (City.Chicago, City.Detroit, 238),
                (City.Detroit, City.Boston, 613),
                (City.Detroit, City.Washington, 396),
                (City.Detroit, City.NewYork, 482),
                (City.Boston, City.NewYork, 190),
                (City.NewYork, City.Philadelphia, 81),
                (City.Philadelphia, City.Washington, 123)
            });

            Console.WriteLine(unweightedCityGraph.ToString());
            Console.WriteLine(weightedCityGraph.ToString());
            var result = weightedCityGraph.GetMinimumSpanningTree(0);

            weightedCityGraph.PrintWeightedPath(result);

            Console.WriteLine();

            var dijkstraResult = weightedCityGraph.GetDijkstraResult(City.LosAngeles);
            var nameDistance   = weightedCityGraph.DistanceArrayToDistanceMap(dijkstraResult.Distances);

            Console.WriteLine($"Distances from {nameof(City.LosAngeles)}:");
            foreach (var(name, distance) in nameDistance)
            {
                Console.WriteLine($"{name.ToString()} : {distance:F1}");
            }

            Console.WriteLine();

            Console.WriteLine($"Shortest path from {nameof(City.LosAngeles)} to {nameof(City.Boston)}");
            var path = WeightedGraph <City> .PathMapToPath(weightedCityGraph.IndexOf(City.LosAngeles),
                                                           weightedCityGraph.IndexOf(City.Boston), dijkstraResult.PathMap);

            weightedCityGraph.PrintWeightedPath(path);
        }
Esempio n. 10
0
    public HyperloopTests(ITestOutputHelper output)
    {
        _output = output;

        var cities = new[]
        {
            "Boston", "NewYork", "Philadelphia", "Washington", "Miami", "Atlanta", "Detroit", "Chicago",
            "Dallas", "Huston", "Phoenix", "Riverside", "LosAngeles", "SanFrancisco", "Seattle"
        };

        _cityGraph = new UnweightedGraph <string>(cities);

        _cityGraph.AddEdge("Boston", "NewYork");
        _cityGraph.AddEdge("Boston", "Detroit");
        _cityGraph.AddEdge("NewYork", "Detroit");
        _cityGraph.AddEdge("NewYork", "Philadelphia");
        _cityGraph.AddEdge("Washington", "Philadelphia");
        _cityGraph.AddEdge("Washington", "Detroit");
        _cityGraph.AddEdge("Washington", "Miami");
        _cityGraph.AddEdge("Washington", "Atlanta");
        _cityGraph.AddEdge("Miami", "Atlanta");
        _cityGraph.AddEdge("Miami", "Huston");
        _cityGraph.AddEdge("Atlanta", "Chicago");
        _cityGraph.AddEdge("Atlanta", "Huston");
        _cityGraph.AddEdge("Atlanta", "Dallas");
        _cityGraph.AddEdge("Huston", "Dallas");
        _cityGraph.AddEdge("Huston", "Phoenix");
        _cityGraph.AddEdge("Dallas", "Phoenix");
        _cityGraph.AddEdge("Dallas", "Chicago");
        _cityGraph.AddEdge("Detroit", "Chicago");
        _cityGraph.AddEdge("Riverside", "Chicago");
        _cityGraph.AddEdge("Riverside", "Phoenix");
        _cityGraph.AddEdge("Riverside", "LosAngeles");
        _cityGraph.AddEdge("Riverside", "SanFrancisco");
        _cityGraph.AddEdge("Phoenix", "LosAngeles");
        _cityGraph.AddEdge("SanFrancisco", "LosAngeles");
        _cityGraph.AddEdge("SanFrancisco", "Seattle");
        _cityGraph.AddEdge("Chicago", "Seattle");

        _weightedCityGraph = new WeightedGraph <string>(cities);
        _weightedCityGraph.AddEdge("Boston", "NewYork", 190);
        _weightedCityGraph.AddEdge("Boston", "Detroit", 613);
        _weightedCityGraph.AddEdge("NewYork", "Detroit", 482);
        _weightedCityGraph.AddEdge("NewYork", "Philadelphia", 81);
        _weightedCityGraph.AddEdge("Washington", "Philadelphia", 123);
        _weightedCityGraph.AddEdge("Washington", "Detroit", 396);
        _weightedCityGraph.AddEdge("Washington", "Miami", 923);
        _weightedCityGraph.AddEdge("Washington", "Atlanta", 543);
        _weightedCityGraph.AddEdge("Miami", "Atlanta", 604);
        _weightedCityGraph.AddEdge("Miami", "Huston", 968);
        _weightedCityGraph.AddEdge("Atlanta", "Chicago", 588);
        _weightedCityGraph.AddEdge("Atlanta", "Huston", 702);
        _weightedCityGraph.AddEdge("Atlanta", "Dallas", 721);
        _weightedCityGraph.AddEdge("Huston", "Dallas", 225);
        _weightedCityGraph.AddEdge("Huston", "Phoenix", 1015);
        _weightedCityGraph.AddEdge("Dallas", "Phoenix", 887);
        _weightedCityGraph.AddEdge("Dallas", "Chicago", 805);
        _weightedCityGraph.AddEdge("Detroit", "Chicago", 238);
        _weightedCityGraph.AddEdge("Riverside", "Chicago", 1704);
        _weightedCityGraph.AddEdge("Riverside", "Phoenix", 307);
        _weightedCityGraph.AddEdge("Riverside", "LosAngeles", 50);
        _weightedCityGraph.AddEdge("Riverside", "SanFrancisco", 386);
        _weightedCityGraph.AddEdge("Phoenix", "LosAngeles", 357);
        _weightedCityGraph.AddEdge("SanFrancisco", "LosAngeles", 348);
        _weightedCityGraph.AddEdge("SanFrancisco", "Seattle", 678);
        _weightedCityGraph.AddEdge("Chicago", "Seattle", 1737);
    }