コード例 #1
0
        public static int ComputeMinSpanningTreeCost(string path)
        {
            IUndirectedWeightedGraph <int> graph = ParseGraph(path);
            var result = Foo(graph);

            return(42);
        }
コード例 #2
0
        private static int Foo(IUndirectedWeightedGraph <int> graph)
        {
            List <int> vertices = graph.GetVertices().ToList();
            var        x        = new List <int> {
                vertices.First()
            };

            // consider data structure for t
            var t      = new List <int>(); // Invariant: X = vertices spanned by tree-so-far T
            var result = 0;

            while (x.Count != vertices.Count)
            {
                // let e = (u, v) be the cheapest edge of G with u belongs X, v do not belongs X
                var cheapestScores = new List <(int vertex, int score)>();
                foreach (int u in x)
                {
                    (int v, int weight) = graph.GetAdjacentVertices(u)
                                          .Where(i => !x.Contains(i.w))         // expensive contains
                                          .OrderBy(edge => edge.weight)
                                          .First();
                    cheapestScores.Add((v, weight));                            // maybe better solution?
                }
                // var zzz = graph.GetAdjacentVertices(u)
                //     .Where(i => !x.Contains(i.w)).ToList()
                //     .OrderBy(edge => edge.weight)
                //     .First();

                var cheapestPath = cheapestScores.OrderBy(tuple => tuple.score)
                                   .First();                                    // min instead of OrderBy?
                // add e to T
                t.Add(cheapestPath.score);
                // add v to X
                x.Add(cheapestPath.vertex);

                result += cheapestPath.score;
            }

            return(result);
        }