Beispiel #1
0
        public static void PrimsMinimumSpanningTree_1()
        {
            {
                var expected = MstGraphA.OrderBy(e => e.from).ToArray();
                var actual   = PrimAlgorithm.MinimumSpanningTreeWithHeap(new ArrayGraph(graphA))
                               .OrderBy(e => e.from)
                               .ToArray();

                Assert.AreEqualSequences(expected, actual, EdgeComparer.UndirectedEdgeComparer);
            }

            {
                var expected = MstGraphB.OrderBy(e => e.from).ToArray();
                var actual   = PrimAlgorithm.MinimumSpanningTreeWithHeap(new ArrayGraph(graphB))
                               .OrderBy(e => e.from)
                               .ToArray();

                Assert.AreEqualSequences(expected, actual, EdgeComparer.UndirectedEdgeComparer);
            }

            //foreach (var edge in actual)
            //{
            //    Console.WriteLine($"{edge.from} -> {edge.to} \t {edge.weight}");
            //}
        }
Beispiel #2
0
        private static void P2Aufgaben()
        {
            foreach (var hCurrentGraphFile in GraphFileRessources.P2GraphFiles)
            {
                var hNewGraph = AdjacentListGraphImporter.ImportAdjacentList(hCurrentGraphFile, EdgeKind.UndirectedWeighted);

                var hPrimAlgorithm = new PrimAlgorithm(hNewGraph);
                var hMstPrim       = hPrimAlgorithm.Execute();

                var hKruskalAlgorithm = new KruskalAlgorithm(hNewGraph);
                var hMstKruskal       = hKruskalAlgorithm.Execute();
                Console.WriteLine("");
            }
        }
Beispiel #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            Stopwatch t = new Stopwatch();

            t.Start();
            var p = PrimAlgorithm.Prim(GraphRepresentation.toWeightMatrix(graph), graph.Nodes.Count);

            t.Stop();

            double cost = 0;

            Graph dist = new Graph();

            foreach (var x in graph.Nodes)
            {
                dist.AddNode(x);
            }

            for (int i = 1; i < p.Length; i++)
            {
                foreach (var x in graph.Edges)
                {
                    if (x.From.Id == p[i] && x.To.Id == i || x.From.Id == i && x.To.Id == p[i])
                    {
                        x.IsMinE = true;
                        cost    += x.Weight;
                        dist.AddEdge(x);
                    }
                }
            }

            var distRez = DijkstraAlgorithm.Dijkstra(GraphRepresentation.toWeightMatrix(dist), 0, dist.Nodes.Count);

            textBoxResult.Text = "Расстояние в метрах от главного коммутатора до: \n\r";


            cost = 0;
            for (int i = 1; i < dist.Nodes.Count; i++)
            {
                textBoxResult.Text += "Коммутатора " + i + " = " + distRez[i] + " м." + Environment.NewLine;
                cost += distRez[i];
            }

            labelCost.Text = "Итоговая стоимость (р) " + cost * Convert.ToDouble(textBoxCost.Text);
            labelTime.Text = "Время в тиках " + t.ElapsedTicks;
            Repaint();
        }
Beispiel #4
0
        public static void PrimsMinimumSpanningTree_2()
        {
            {
                var expected = MstGraphA.OrderBy(e => e.from).ToArray();
                var actual   = PrimAlgorithm.MinimumSpanningTree(new ArrayGraph(graphA))
                               .OrderBy(e => e.from)
                               .ToArray();

                Assert.AreEqualSequences(expected, actual, EdgeComparer.UndirectedEdgeComparer);
            }

            {
                var expected = MstGraphB.OrderBy(e => e.from).ToArray();
                var actual   = PrimAlgorithm.MinimumSpanningTree(new ArrayGraph(graphB))
                               .OrderBy(e => e.from)
                               .ToArray();

                Assert.AreEqualSequences(expected, actual, EdgeComparer.UndirectedEdgeComparer);
            }
        }
        public IActionResult Get()
        {
            var sortAlgorithm = new QuickSortAlgorithm(new RandElementPartitionStrategy(new System.Random()));
            var graph         = new Graph(EdgeDirectionType.Undirected);

            graph.AddVertex(1);
            graph.AddVertex(2);
            graph.AddVertex(3);
            graph.AddVertex(4);

            graph.AddEdge(1, 2, 1, 1, EdgeDirectionType.Undirected);
            graph.AddEdge(2, 3, 2, 2, EdgeDirectionType.Undirected);
            graph.AddEdge(3, 4, 3, 5, EdgeDirectionType.Undirected);
            graph.AddEdge(4, 1, 4, 4, EdgeDirectionType.Undirected);
            graph.AddEdge(1, 3, 5, 3, EdgeDirectionType.Undirected);
            var algorithm = new PrimAlgorithm();
            var edges     = algorithm.GetMST(graph).ToList();

            return(Ok(edges));
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            List <House> houses = new List <House>
            {
                new House {
                    Address = "вул. Гагарiна, буд. 52"
                },
                new House {
                    Address = "вул. Коновальця, буд. 15"
                },
                new House {
                    Address = "вул. Абрамова-Голiкова, буд. 4б"
                },
                new House {
                    Address = "пров. Дозорцевої, буд. 7"
                },
                new House {
                    Address = "вул. Тополева, буд. 18"
                },
                new House {
                    Address = "вул. Енергетикiв, буд. 28"
                },
                new House {
                    Address = "вул. Енергетикiв, буд. 9а"
                }
            }; // N = 6; 1 <= N <= 400

            Distance[] distances = new Distance[]
            {
                new Distance {
                    House1 = houses[0], House2 = houses[1], Value = 7
                },                                                                  // A <-> B
                new Distance {
                    House1 = houses[0], House2 = houses[3], Value = 5
                },                                                                  // F -> D

                new Distance {
                    House1 = houses[1], House2 = houses[2], Value = 8
                },                                                                  // B <-> C
                new Distance {
                    House1 = houses[1], House2 = houses[3], Value = 9
                },                                                                  // B <-> D
                new Distance {
                    House1 = houses[1], House2 = houses[4], Value = 7
                },                                                                  // B <-> E

                new Distance {
                    House1 = houses[2], House2 = houses[4], Value = 5
                },                                                                  // C <-> E

                new Distance {
                    House1 = houses[3], House2 = houses[4], Value = 15
                },                                                                   // D <-> E
                new Distance {
                    House1 = houses[3], House2 = houses[5], Value = 6
                },                                                                  // D <-> F

                new Distance {
                    House1 = houses[4], House2 = houses[5], Value = 8
                },                                                                  // E <-> F
                new Distance {
                    House1 = houses[4], House2 = houses[6], Value = 9
                },                                                                  // E <-> G

                new Distance {
                    House1 = houses[5], House2 = houses[6], Value = 11
                },                                                                   // F <-> G
            };

            Console.Write("Усi будинки:\n{0}", houses[0].Address);
            for (int i = 1; i < houses.Count; i++)
            {
                Console.Write("\n{0}", houses[i].Address);
            }

            Console.WriteLine("\n\nМожливi сполучення:");
            for (int i = 0; i < distances.Length; i++)
            {
                Console.WriteLine("{0} - {1}: {2} метрiв.", distances[i].House1.Address, distances[i].House2.Address, distances[i].Value);
            }

            List <Distance> result = new List <Distance>();

            PrimAlgorithm.AlgorithmByPrim(houses.ToList(), distances.ToList(), result);

            int sumDistance = 0;

            Console.WriteLine("\nПотрiбнi сполучення з мiнiмальними затратами:");
            for (int i = 0; i < result.Count; i++)
            {
                sumDistance += result[i].Value;
                Console.WriteLine("{0} - {1}: {2} метрiв.", result[i].House1.Address, result[i].House2.Address, result[i].Value);
            }

            Console.WriteLine("\nЗагальна довжина сполучення: {0} метрiв.", sumDistance);

            Console.ReadKey();
        }