示例#1
0
        private (Graph g, double res, Edge[] cycle) Test04()
        {
            Graph       g     = new AdjacencyMatrixGraph(true, 5);
            List <Edge> cycle = new List <Edge>();

            g.AddEdge(0, 1);
            g.AddEdge(1, 2, 0.1);
            g.AddEdge(2, 0);
            g.AddEdge(2, 3);
            g.AddEdge(3, 4);
            g.AddEdge(4, 5);
            g.AddEdge(5, 2);


            cycle.Add(new Edge(0, 1));
            cycle.Add(new Edge(1, 3));
            cycle.Add(new Edge(3, 4));
            cycle.Add(new Edge(4, 5));
            cycle.Add(new Edge(5, 0));


            double res = 5;

            return(g, res, cycle.ToArray());
        }
        /// <summary>
        /// Na podstawie danych krawędzi, tworzy graf i zwraca go wraz ze słownikiem
        /// </summary>
        /// <param name="edges">Krawędzie mają być oznaczone zgodnie z oznaczeniem grafu oryginalnego</param>
        static PartGraph createPartGraphFromEdges(HashSet <Edge> edges)
        {
            HashSet <int> vertices = new HashSet <int>();

            foreach (Edge e in edges)
            {
                vertices.Add(e.From);
                vertices.Add(e.To);
            }

            var verticesList = vertices.ToList();

            verticesList.Sort();

            var dict = new Dictionary <int, int>();

            for (int i = 0; i < verticesList.Count; i++)
            {
                dict[i] = verticesList[i];
            }

            var revDict = dict.ToDictionary(x => x.Value, x => x.Key);

            var graph = new AdjacencyMatrixGraph(false, vertices.Count);

            foreach (Edge e in edges)
            {
                graph.AddEdge(revDict[e.From], revDict[e.To], e.Weight);
            }

            return(new PartGraph {
                Graph = graph, NumbersInOriginal = dict
            });
        }
示例#3
0
        //cykl + krawędź
        private (Graph g, double res, Edge[] cycle) TestAbdula()
        {
            Graph       g     = new AdjacencyMatrixGraph(true, 4);
            List <Edge> cycle = new List <Edge>();

            g.AddEdge(0, 1, 4);
            g.AddEdge(0, 2, 12);
            g.AddEdge(0, 3, 7);


            g.AddEdge(1, 3, 18);
            g.AddEdge(1, 0, 5);

            g.AddEdge(2, 3, 6);
            g.AddEdge(2, 0, 11);

            g.AddEdge(3, 0, 10);
            g.AddEdge(3, 1, 2);
            g.AddEdge(3, 2, 3);

            cycle.Add(new Edge(0, 2, 12));
            cycle.Add(new Edge(2, 3, 6));
            cycle.Add(new Edge(3, 1, 2));
            cycle.Add(new Edge(1, 0, 5));
            double res = 25;

            return(g, res, cycle.ToArray());
        }
示例#4
0
        //cykl + krawędź
        private (Graph g, Graph t) Test01()
        {
            Graph t = new AdjacencyMatrixGraph(false, 4);

            t.AddEdge(0, 1);
            t.AddEdge(1, 2);
            t.AddEdge(0, 3);
            Graph g = t.Clone();

            g.AddEdge(1, 3);
            return(g, t);
        }
示例#5
0
        /// <summary>
        /// Funkcja sprawdza, czy drugi argument jest poprawnym cyklem Hamiltona dla grafu w pierwszym argumencie.
        /// Oczywiście chodzi o cykl Hamiltona utworzony z możliwością przeskoków.
        /// </summary>
        public static bool isCorrectHamiltonianCycle(Graph g, List <int> cycle)
        {
            // Cykl Hamiltona musi być tak długi, jak ilość wierzchołków w grafie
            if (cycle.Count() != g.VerticesCount)
            {
                return(false);
            }

            // W cyklu Hamiltona wierzchołki nie mogą się powtarzać
            bool isUnique = cycle.Distinct().Count() == cycle.Count();

            if (!isUnique)
            {
                return(false);
            }

            var onesGraph = new AdjacencyMatrixGraph(false, g.VerticesCount);

            for (int i = 0; i < g.VerticesCount; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    if (!double.IsNaN(g.GetEdgeWeight(i, j)))
                    {
                        onesGraph.AddEdge(i, j);
                    }
                }
            }

            List <PathsInfo[]> pathsFromVertices = new List <PathsInfo[]>();

            for (int i = 0; i < onesGraph.VerticesCount; i++)
            {
                PathsInfo[] path;
                onesGraph.DijkstraShortestPaths(i, out path);
                pathsFromVertices.Add(path);
            }

            bool good = true;

            for (int i = 0; i < cycle.Count - 1; i++)
            {
                if (pathsFromVertices[cycle[i]][cycle[i + 1]].Dist > 3)
                {
                    good = false;
                    Console.WriteLine($"{cycle[i]}, {cycle[i + 1]}, cost: {pathsFromVertices[cycle[i]][cycle[i + 1]].Dist}");
                }
            }

            return(good);
        }
        static Graph generateTree(int numberOfVertices)
        {
            var graph = new AdjacencyMatrixGraph(false, numberOfVertices);

            for (int i = 0; i < numberOfVertices; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    graph.AddEdge(i, j, rand.Next(3, 8));
                }
            }

            return(graph);
        }
示例#7
0
        public void AStarTest()
        {
            Graph g = new AdjacencyMatrixGraph(false, 4);

            g.Add(new Edge(0, 1, 13));
            g.Add(new Edge(0, 2, 1));
            g.Add(new Edge(2, 1, 2));
            g.Add(new Edge(0, 3, 11));
            g.Add(new Edge(1, 3, 7));

            var exists = g.AStar(0, 3, out var edges);

            exists.Should().BeTrue();
            edges.Should().ContainInOrder(new Edge(0, 2, 1), new Edge(2, 1, 2), new Edge(1, 3, 7));
        }
示例#8
0
        public static Graph Kruskal(Graph graph)
        {
            var edges = getGraphEdgesMin(graph);

            UnionFind uf       = new UnionFind(graph.VerticesCount);
            var       retGraph = new AdjacencyMatrixGraph(false, graph.VerticesCount);

            foreach (Edge e in edges.ToArray())
            {
                if (uf.Find(e.From) != uf.Find(e.To))
                {
                    retGraph.AddEdge(e);
                    uf.Union(e.From, e.To);
                }
            }

            return(retGraph);
        }
示例#9
0
        public Graph graphFromFile()
        {
            using (StreamReader sr = File.OpenText(path))
            {
                int numberOfVertices = int.Parse(sr.ReadLine());

                var ret = new AdjacencyMatrixGraph(false, numberOfVertices);

                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    var numbers = line.Split(' ');
                    ret.AddEdge(int.Parse(numbers[0]), int.Parse(numbers[1]), int.Parse(numbers[2]));
                }

                return(ret);
            }
        }
示例#10
0
        static void testMergeGraphs()
        {
            Graph one = new AdjacencyMatrixGraph(false, 4);

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

            Dictionary <int, int> dictOne = new Dictionary <int, int>
            {
                { 0, 8 },
                { 1, 7 },
                { 2, 5 },
                { 3, 6 }
            };

            Graph two = new AdjacencyMatrixGraph(false, 3);

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

            Dictionary <int, int> dictTwo = new Dictionary <int, int>
            {
                { 0, 12 },
                { 1, 10 },
                { 2, 8 }
            };

            var res = MergeGraphs(
                new PartGraph {
                Graph = one, NumbersInOriginal = dictOne
            },
                new PartGraph {
                Graph = two, NumbersInOriginal = dictTwo
            });

            printGraph(res, "");
        }