Пример #1
0
        //获取总权重值最小,又没有被访问过路径
        static VisitVertex GetLowVertex(Dictionary <Vertex, VisitVertex> d)
        {
            int         val   = Int32.MaxValue;
            VisitVertex visit = null;

            foreach (var i in d)
            {
                if (i.Value.IsVisited == false)
                {
                    if (i.Value.Weight < val)  //新路径比旧路径权重值小
                    {
                        val   = i.Value.Weight;
                        visit = i.Value;
                    }
                }
            }

            if (visit != null)
            {
                //标志该路径以被访问过
                visit.IsVisited = true;
            }
            return(visit);
        }
Пример #2
0
        static void SearchLow(Vertex x)
        {
            Dictionary <Vertex, Vertex>      parent = new Dictionary <Vertex, Vertex>();      //保存最短节点与节点的关系
            Dictionary <Vertex, VisitVertex> weight = new Dictionary <Vertex, VisitVertex>(); //记录当前最小权重

            //初始化三个字典,起点开始
            //获取到起点开始指向的边
            Edge[] edges;
            dict.TryGetValue("O", out edges);
            foreach (Edge e in edges)
            {
                parent.Add(e.To, e.From);
                weight.Add(e.To, new VisitVertex(e.Weight, false, e.To));
            }


            VisitVertex go = null;

            while ((go = GetLowVertex(weight)) != null)
            {
                Edge[] edges2;
                dict.TryGetValue(go.Vertex.Name, out edges2);
                if (edges2 == null)
                {
                    break;
                }

                foreach (Edge ee in edges2)
                {
                    int         myWeight = go.Weight + ee.Weight;
                    VisitVertex orightWeight;
                    weight.TryGetValue(ee.To, out orightWeight);
                    if (orightWeight == null)
                    {
                        //如果没有过该路径,就添加
                        weight.Add(ee.To, new VisitVertex(myWeight, false, ee.To));
                        parent.Add(ee.To, ee.From);
                    }
                    else
                    {
                        if (orightWeight.Weight > myWeight)
                        {
                            //发现更短的路径,更新权重值,更改为没有访问过,修改父节点(新路径了)
                            orightWeight.Weight    = myWeight;
                            orightWeight.IsVisited = false;
                            parent[ee.To]          = ee.From;
                        }
                    }
                }
            }


            //输出路径和权重总值
            VisitVertex finVist;

            weight.TryGetValue(x, out finVist);
            Console.WriteLine("总权重值:" + finVist.Weight);

            Console.WriteLine(x.Name);
            Vertex cur = x;

            while (true)
            {
                Vertex aaa;
                parent.TryGetValue(cur, out aaa);
                cur = aaa;
                if (aaa == null)
                {
                    break;
                }

                Console.WriteLine(aaa.Name);
            }
        }