首先新建一个网图如下:
图的表示法有好多中,最常用的应该是邻接矩阵与邻接表。上面的图,边很少,用邻接表来表示就很不错。
对于以上图,可以对象出3个类。图、节点、边。3个实体类代码如下:
边Edge:
public class Edge { public string StartNodeID { get; set; } public string EndNodeID { get; set; } public int Weight { get; set; } }
节点Node:
public class Node { private string id; private IList<Edge> edgeList; public Node(string nid) { id = nid; edgeList = new List<Edge>(); } public string Id { get { return id; } } public IList<Edge> EdgeList { get { return edgeList; } } }
图Graph:
public class Graph { public List<Node> NodeList = new List<Node>(); }
由于要求的就是最短路径,路径对象模拟如下:
public class Path { public string CurrentNodeId; public bool IsProcessed = false; public int Weight = 99999999