连通图 Collected Graph

1. 生成连通图 (至少有一颗生成树)

2.生成连通图 (至少有一颗生成树,或者更多)

 

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

namespace Algorithm
{
    public class CollectedGraph
    {
        /// <summary>
        /// Build a random collected Graph.
        /// Every node has at leave one node to collect
        /// </summary>
        /// <param name="NodeCount"></param>
        /// <returns></returns>
        public Double[,] BuildRandom(int NodeCount)
        {
            Double[,] map = new Double[NodeCount, NodeCount];
            HashSet<string> edges = new HashSet<string>();
            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;
                    }
                }

            Random rand = new Random();
            List<int> S = new List<int>();
            List<int> U = new List<int>();
            for (int k = 1; k < NodeCount; k++)
            {
                U.Add(k);
            }

            S.Add(0);

            while (U.Count > 0)
            {
                int rndFrom = rand.Next(0, S.Count);
                int rndTo = rand.Next(0, U.Count);

                int from = S[rndFrom];
                int to = U[rndTo];
                S.Add(to);
                U.Remove(to);
                map[from, to] = rand.Next(1000);
                edges.Add(from.ToString() + "-" + to.ToString());

            }

            return map;

        }

        /// <summary>
        /// Build a random collected Graph.
        /// More node are collected .
        /// like a real graph.
        /// </summary>
        /// <param name="NodeCount"></param>
        /// <returns></returns>
        public Double[,] BuildRandomMore(int NodeCount)
        {
            Double[,] map = BuildRandom(NodeCount);
            Random rand = new Random();
            for (int i = 0; i < NodeCount; i++)
            {
                int from = rand.Next(0, NodeCount);
                int to = rand.Next(from, NodeCount);
                map[from, to] = rand.Next(0, 1000);
            }

            return map;

        }

 

     }
}

 

 

Unit Test:

 

/// <summary>
        ///A test for BuildRandom
        ///</summary>
        [TestMethod()]
        public void BuildRandomTest()
        {
            CollectedGraph target = new CollectedGraph(); // TODO: Initialize to an appropriate value
            int NodeCount = 1000;

            double[,] map;

            //map = target.BuildRandom(NodeCount);
            map = target.BuildRandomMore(NodeCount);

            List<string> edges;
            List<double> weights;
            List<int> tree = target.BuildSpanningTree(map, out edges, out weights);

            ShortestPath pathTest = new ShortestPath();
            Double[] distanct;
            Dictionary<int, string> actual = pathTest.Build(map, out distanct);

        }

转载于:https://www.cnblogs.com/netfuns/archive/2011/07/19/2110814.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值