//改变v1到v2弧的权值 public bool ChangeEdgeWei(Node <T> v1, Node <T> v2, int v) { //v1或v2不是图的顶点或者v1和v2之间不存在边 if (!IsNode(v1) || !IsNode(v2) || !IsEdge(v1, v2)) { //Console.WriteLine("Node is not belong to Graph!"); return(false); } //权值不对 if (v < 1) { //Console.WriteLine("Weight is not right!"); return(false); } //处理顶点v1的邻接表 adjListNode <T> p = adjList[GetIndex(v1)].FirstAdj; while (p.Adjvex != GetIndex(v2)) { p = p.Next; } p.Weight = v; return(true); }
//在顶点v1和v2之间添加权值为v的弧 public bool SetEdge(Node <T> v1, Node <T> v2, int v) { //v1或v2不是图的顶点或者v1和v2之间存在边 if (!IsNode(v1) || !IsNode(v2) || IsEdge(v1, v2)) { //Console.WriteLine("Node is not belong to Graph!"); return(false); } //权值不对 if (v < 1) { //Console.WriteLine("Weight is not right!"); return(false); } //处理顶点v1的邻接表 adjListNode <T> p = new adjListNode <T>(GetIndex(v2), v); if (adjList[GetIndex(v1)].FirstAdj == null)//顶点v1无邻接顶点 { adjList[GetIndex(v1)].FirstAdj = p; adjList[GetIndex(v1)].Outdegree++; adjList[GetIndex(v2)].Indegree++; } else//顶点v1有邻接顶点 { p.Next = adjList[GetIndex(v1)].FirstAdj; adjList[GetIndex(v1)].FirstAdj = p; //头插法 adjList[GetIndex(v1)].Outdegree++; adjList[GetIndex(v2)].Indegree++; } return(true); }
//构造器 public GraphAdjList(GraphAdjList <T> newGraph) { adjList = new VexNode <T> [newGraph.GetNumOfVertex()]; adjListNode <T> pre, p; for (int i = 0; i < newGraph.GetNumOfVertex(); ++i) { adjList[i] = new VexNode <T>(); adjList[i].Data = new Node <T>(newGraph.adjList[i].Data.Data); adjList[i].Indegree = newGraph.adjList[i].Indegree; adjList[i].Outdegree = newGraph.adjList[i].Outdegree; if (newGraph.adjList[i].FirstAdj == null) { adjList[i].FirstAdj = null; } else { adjListNode <T> GraphP = newGraph.adjList[i].FirstAdj; pre = new adjListNode <T>(GraphP.Adjvex, GraphP.Weight); adjList[i].FirstAdj = pre; while (GraphP.Next != null) { p = new adjListNode <T>(GraphP.Next.Adjvex, GraphP.Next.Weight); pre.Next = p; pre = p; GraphP = GraphP.Next; } pre.Next = GraphP.Next;//null } } }
//判断v1到v2是否存在弧 public bool IsEdge(Node <T> v1, Node <T> v2) { if (adjList == null) { return(false); } //v1或v2不是图的顶点 if (!IsNode(v1) || !IsNode(v2)) { //Console.WriteLine("Node is not belong to Graph!"); return(false); } adjListNode <T> p = adjList[GetIndex(v1)].FirstAdj; while (p != null) { if (p.Adjvex == GetIndex(v2)) { return(true); } p = p.Next; } return(false); }
//构造器 public VexNode(Node <T> nd, adjListNode <T> alNode) { data = nd; firstAdj = alNode; indegree = 0; outdegree = 0; }
//构造器 public VexNode(Node <T> nd) { data = nd; firstAdj = null; indegree = 0; outdegree = 0; }
//构造器 public VexNode() { data = null; firstAdj = null; indegree = 0; outdegree = 0; }
//删除顶点v1和v2之间的边 public bool DelEdge(Node <T> v1, Node <T> v2) { //v1或v2不是图的顶点 if (!IsNode(v1) || !IsNode(v2)) { //Console.WriteLine("Node is not belong to Graph!"); return(false); } //顶点v1到v2到有弧 if (IsEdge(v1, v2)) { //处理顶点v1的邻接表中的顶点v2的邻接表结点 adjListNode <T> p = adjList[GetIndex(v1)].FirstAdj; adjListNode <T> pre = null; //被删除的前一项 //firstAdj就是v2 if (p.Adjvex == GetIndex(v2)) { adjList[GetIndex(v1)].FirstAdj = p.Next; } else { while (p != null) { if (p.Adjvex != GetIndex(v2)) { pre = p; p = p.Next; } else { pre.Next = p.Next; p = p.Next; } } } adjList[GetIndex(v1)].Outdegree--; adjList[GetIndex(v2)].Indegree--; } return(true); }
//获取弧的数目 public int GetNumOfEdge() { if (adjList == null) { return(0); } int i = 0; foreach (VexNode <T> nd in adjList) //以nd为弧尾 { adjListNode <T> p = nd.FirstAdj; //以p为弧头 while (p != null) { ++i; p = p.Next; } } return(i);//有向图 //return i / 2;//无向图 }
//删除顶点 public bool DelNode(Node <T> v) { //v不在图中 if (!IsNode(v)) { //Console.WriteLine("Node isn't belong to Graph!"); return(false); } int index = this.GetIndex(v); //修改原表,删除有关v的边 for (int i = 0; i < adjList.Length; i++) { if (i == index) { continue; } //从adjlist[i]到v有弧 if (IsEdge(adjList[i].Data, v)) { DelEdge(adjList[i].Data, v); } //从v到adjlist[i]有弧 if (IsEdge(v, adjList[i].Data)) { DelEdge(v, adjList[i].Data); } } VexNode <T>[] oldAdjList = adjList; adjList = new VexNode <T> [adjList.Length - 1]; //原表复制 for (int i = 0; i < index; i++) { adjList[i] = new VexNode <T>(); adjList[i].Data = oldAdjList[i].Data; adjList[i].FirstAdj = oldAdjList[i].FirstAdj; adjList[i].Indegree = oldAdjList[i].Indegree; adjList[i].Outdegree = oldAdjList[i].Outdegree; //修改邻接表 for (adjListNode <T> p = adjList[i].FirstAdj; p != null; p = p.Next) { if (p.Adjvex > index) { p.Adjvex--; //当p的索引比删掉点的索引大,其值减一 } } } for (int i = index; i < adjList.Length; i++) { adjList[i] = new VexNode <T>(); adjList[i].Data = oldAdjList[i + 1].Data; adjList[i].FirstAdj = oldAdjList[i + 1].FirstAdj; adjList[i].Indegree = oldAdjList[i + 1].Indegree; adjList[i].Outdegree = oldAdjList[i + 1].Outdegree; //修改邻接表 for (adjListNode <T> p = adjList[i].FirstAdj; p != null; p = p.Next) { if (p.Adjvex > index) { p.Adjvex--; //当p的索引比删掉点的索引大,其值减一 } } } return(true); }
//构造器 public adjListNode(int vex, int wei) { adjvex = vex; next = null; weight = wei; }