c#实现Prim算法

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

namespace Prim算法
{
      ///
      /// 顶点
      ///
      public class Vertex
      {
            public String key;
            public Vertex(String key)
            {
                  this.key = key;
            }
      }
      ///
      /// 边
      ///
      public class Edge
      {
            public Vertex start;
            public Vertex end;
            public int key;
            public Edge(Vertex start, Vertex end, int key)
            {
                  this.start = start;
                  this.end = end;
                  this.key = key;
            }
      }
      public class Prim
      {
            public static List vertexList = new List();//结点集
            public static List EdgeQueue = new List();//边集
            public static List newVertex = new List();//已经 访问过的结点

            public static void buildGraph()
            {
                  Vertex v1 = new Vertex("a");
                  Prim.vertexList.Add(v1);
                  Vertex v2 = new Vertex("b");
                  Prim.vertexList.Add(v2);
                  Vertex v3 = new Vertex("c");
                  Prim.vertexList.Add(v3);
                  Vertex v4 = new Vertex("d");
                  Prim.vertexList.Add(v4);
                  Vertex v5 = new Vertex("e");
                  Prim.vertexList.Add(v5);
                  addEdge(v1, v2, 6);
                  addEdge(v1, v3, 7);
                  addEdge(v2, v3, 8);
                  addEdge(v2, v5, 4);
                  addEdge(v2, v4, 5);
                  addEdge(v3, v4, 3);
                  addEdge(v3, v5, 9);
                  addEdge(v5, v4, 7);
                  addEdge(v5, v1, 2);
                  //addEdge(v4, v2, 2);
            }
            public static void addEdge(Vertex a, Vertex b, int w)
            {
                  Edge e = new Edge(a, b, w);
                  Prim.EdgeQueue.Add(e);
            }
            public static void primTree()
            {
                  buildGraph();
                  Vertex start = vertexList.ElementAt(0);
                  newVertex.Add(start);
                  for (int n = 0; n < vertexList.Count; n++)
                  {
                        Vertex temp = new Vertex(start.key);
                        Edge tempedge = new Edge(start, start, 1000);
                        foreach (Vertex v in newVertex)
                        {
                              foreach (Edge e in EdgeQueue)
                              {
                                    if (e.start == v && !containVertex(e.end))
                                    {
                                          if (e.key < tempedge.key)
                                          {
                                                temp = e.end;
                                                tempedge = e;
                                          }
                                    }
                              }
                        }
                        if (!containVertex(temp))
                        {
                              newVertex.Add(temp);
                        }
                  }
                  foreach (Vertex item in newVertex)
                  {
                        Console.WriteLine(item.key);
                  }
            }
            public static bool containVertex(Vertex vte)
            {
                  foreach (Vertex v in newVertex)
                  {
                        if (v.key.Equals(vte.key))
                              return true;
                  }
                  return false;
            }
            public static void Main(String[] args)
            {
                  primTree();
            }
      }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值