Code :

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

namespace Algorithm
{
    public class ShortestPath
    {

        public Dictionary<int, string> Build(double[,] map, out double[] outDistance)
        {
            //initialize
            int NodeCount = map.GetLength(0);

            Dictionary<int, string> paths = new Dictionary<int, string>();
            HashSet<int> S = new HashSet<int>();
            double[] distance = new double[NodeCount];
            HashSet<int> U = new HashSet<int>();

            //node 0
            for (int i = 0; i < NodeCount; i++)
            {
                U.Add(i);

                distance[i] = map[0, i];
                paths[i] = "0";
                if (distance[i] < Double.MaxValue)
                {
                    paths[i] = "0->" + i.ToString();
                }
                else
                {
                    paths[i] = "0->*";
                }
            }

            S.Add(0);
            U.Remove(0);

            //recursive
            int minIndex = 0;
            double minWight = Double.MaxValue;
            while (U.Count > 0)
            {
                //update weights in U
                foreach (int item in U)
                {
                    if (distance[minIndex] + map[minIndex, item] < distance[item])
                    {
                        paths[item] = paths[minIndex] + "->" + item;

                        distance[item] = distance[minIndex] + map[minIndex, item];
                    }
                }

                //find min weight in U
                minIndex = -1;
                minWight = Double.MaxValue;
                foreach (int item in U)
                {
                    if (distance[item] < minWight)
                    {
                        minWight = distance[item];
                        minIndex = item;
                    }
                }

                Debug.Assert(minIndex > -1);

                //
                S.Add(minIndex);
                U.Remove(minIndex);
            }


            outDistance = distance;
            return paths;
        }
    }
}

 

Test :

 

/// <summary>
      ///A test for Build
      ///</summary>
      [TestMethod()]
      public void BuildTest1()
      {
          //map1
          ShortestPath target = new ShortestPath();
          int nodeCount = 6;
          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, 2] = 9;
          map[0, 5] = 14;

          map[1, 0] = 7;
          map[1, 2] = 10;
          map[1, 3] = 15;

          map[2, 0] = 9;
          map[2, 1] = 10;
          map[2, 3] = 11;
          map[2, 5] = 2;

          map[3, 1] = 15;
          map[3, 2] = 11;
          map[3, 4] = 6;

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

          map[5, 0] = 14;
          map[5, 2] = 2;
          map[5, 4] = 9;

          //assert1
          Double[] distanct;
          Dictionary<int, string> actual = target.Build(map, out distanct);
          Assert.IsTrue(actual[0] == "0->0");
          Assert.IsTrue(actual[1] == "0->1");
          Assert.IsTrue(actual[2] == "0->2");
          Assert.IsTrue(actual[3] == "0->2->3");
          Assert.IsTrue(actual[4] == "0->2->5->4");
          Assert.IsTrue(actual[5] == "0->2->5");

          Assert.IsTrue(distanct[0] == 0);
          Assert.IsTrue(distanct[1] == 7);
          Assert.IsTrue(distanct[2] == 9);
          Assert.IsTrue(distanct[3] == 20);
          Assert.IsTrue(distanct[4] == 20);
          Assert.IsTrue(distanct[5] == 11);
      }

posted on 2011-07-13 14:19  netfuns  阅读(181)  评论(0编辑  收藏  举报