todo

image

 

Test:

 

/// <summary>
        ///A test for BuildSpanningTree
        ///</summary>
        [TestMethod()]
        public void BuildSpanningTree_wiki_Test()
        {
            CollectedGraph target = new CollectedGraph(); // TODO: Initialize to an appropriate value
            int nodeCount = 7;
            double[,] map = new double[nodeCount, nodeCount];
            for (int i = 0; i < nodeCount; i++)
            {
                for (int j = 0; j < nodeCount; j++)
                {
                    if (i == j)
                    {
                        map[i, j] = 0;
                    }
                    else
                    {
                        map[i, j] = double.MaxValue;
                    }
                }
            }

            map[0, 1] = 7;
            map[0, 3] = 5;

            map[1, 0] = 7;
            map[1, 2] = 8;
            map[1, 3] = 9;
            map[1, 4] = 7;

            map[2, 1] = 8;
            map[2, 4] = 5;


            map[3, 0] = 5;
            map[3, 1] = 9;
            map[3, 4] = 15;
            map[3, 5] = 6;

            map[4, 1] = 7;
            map[4, 2] = 5;
            map[4, 3] = 15;
            map[4, 5] = 8;
            map[4, 6] = 9;

            map[5, 3] = 6;
            map[5, 4] = 8;
            map[5, 6] = 11;

            map[6, 4] = 9;
            map[6, 5] = 11;

            List<string> edges;
            List<double> weights;
            List<int> actual = target.BuildSpanningTree(map, out edges, out weights);
            //node
            Assert.IsTrue(actual[0] == 0);
            Assert.IsTrue(actual[1] == 3);
            Assert.IsTrue(actual[2] == 5);
            Assert.IsTrue(actual[3] == 1);
            Assert.IsTrue(actual[4] == 4);
            Assert.IsTrue(actual[5] == 2);
            Assert.IsTrue(actual[6] == 6);

            ////edge
            Assert.IsTrue(edges[0] == "0->3");
            Assert.IsTrue(edges[1] == "3->5");
            Assert.IsTrue(edges[2] == "0->1");
            Assert.IsTrue(edges[3] == "1->4");
            Assert.IsTrue(edges[4] == "4->2");
            Assert.IsTrue(edges[5] == "4->6");

            ////weight
            Assert.IsTrue(weights[0] == 5);
            Assert.IsTrue(weights[1] == 6);
            Assert.IsTrue(weights[2] == 7);
            Assert.IsTrue(weights[3] == 7);
            Assert.IsTrue(weights[4] == 5);
            Assert.IsTrue(weights[5] == 9);

        }

 

Code:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Algorithm
{
    public class CollectedGraph
    {
        public Double[,] BuildRandom(int NodeCount)
        {
            Double[,] map = new Double[NodeCount, NodeCount];

            return map;

        }

        public List<int> BuildSpanningTree(Double[,] map, out List<string> edges,out List<double> weights)
        {
            //init

            List<string> minPaths = new List<string>();
            List<double> minWeights = new List<double>();
            List<int> S = new List<int>();
            List<int> U = new List<int>();
            int from =0;
            int to=0;
            S.Add(0);

            //edge from
            from =0;
            //edge to
            to=0;

            for (int i = 1; i < map.GetLength(0); i++)
            {
                U.Add(i);
            }

            //recursive
            while (U.Count > 0)
            {               
                double minWeight = Double.MaxValue;
                foreach (int a in S)
                {
                    foreach (int b in U)
                    {
                        if (map[a, b] < minWeight)
                        {
                            from=a;
                            to=b;
                            minWeight = map[a, b];                          
                        }
                    }
                }

                S.Add(to);
                U.Remove(to);
                minWeights.Add(minWeight);
             
                minPaths.Add(from.ToString()+"->"+to.ToString());
            }

            //paths count:n-1
            edges = minPaths;
            weights = minWeights;
            //output nodes
            return S;           
        }

    }
}

posted on 2011-07-15 11:32  netfuns  阅读(192)  评论(0编辑  收藏  举报