1) 算法思想:
利用构造反圈逐一寻找连通子图。初始化原图中的每个顶点为false,将已经找到的完整连通子图的顶点存入一个矩阵C中的一行,并将原始图中已加入反圈中的顶点标记为true;继续在原图中标记为false的顶点中构造反圈,寻找下一个连通子图,并将对应顶点加入矩阵C的下一行;如此循环,直到原图中所有顶点都标记为true为止。下面将此思想转化为具体的算法,不妨令算法名称为:反圈法寻找无向图的所有连通子图。
2) 算法描述:
3) 程序
- 构造边结构程序
package sy1; /* * 构造图的边结构 */ public class Arc { public Vnode firstNode;//定义边的始点 public Vnode LastNode;//定义边的中点 public Arc NextArc;//定义边的下一条边 public boolean flag;//标识边是否访问过 public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public Arc getNextArc() { return NextArc; } public void setNextArc(Arc nextArc) { NextArc = nextArc; } public Vnode getLastNode() { return LastNode; } public void setLastNode(Vnode lastNode) { LastNode = lastNode; } public int weight;//定义边对应的权值 //建立一条具有权值weight的边 public Arc(int weight){ this(null,null,null,weight); } public Arc(Vnode firstNode,Vnode LastNode,int weight){ this(firstNode,LastNode,null,weight); } public Arc(Vnode firstNode,Vnode LastNode,Arc NextArc,int weight){ this.firstNode=firstNode; this.LastNode=LastNode; this.NextArc=NextArc; this.weight=weight; } public Vnode getFirstNode() { return firstNode; } public void setFirstNode(Vnode firstNode) { this.firstNode = firstNode; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } }
- 构造顶点结构程序
package sy1; /* * 建立一个图中的顶点结构 */ public class Vnode { public int vw;//顶点的序数(即表示第几个顶点) public String V;//顶点的名称 public boolean flag;//标识顶点是否访问过,如果flag=false表示没有被访问过 public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } public String getV() { return V; } public void setV(String v) { V = v; } public int getVw() { return vw; } public void setVw(int vw) { this.vw = vw; } public Arc aw[];//表明从顶点出发的边的权重集 public Vnode next[];//从顶点出发的边的终点 public Vnode[] getNext() { return next; } public Arc[] getAw() { return aw; } public void setNext(Vnode next[]) { this.next = next; } public Vnode(String V,int vw,boolean flag){ this.vw=vw; this.V=V; this.flag=flag; } //返回顶点位置 public Vnode(String V,int vw){ this(V,vw,false); } }
- 定义网络流图的边结构
package sy1; /* * 定义网络流图的边结构 */ public class FlowArc { public Vnode firstNode;//定义边的始点 public Vnode LastNode;//定义边的中点 public Arc NextArc;//定义边的下一条边 public int flow;//定义边的流量值 public int Capacity;//定义边的容量值 public int cost;//定义边的费用值 //构造函数 public FlowArc(Vnode u,Vnode v,Arc s,int c,int f,int b){ this.firstNode=u; this.LastNode=v; this.NextArc=s; this.flow=f; this.Capacity=c; this.cost=b; } //始点 public Vnode getFirstNode() { return firstNode; } public void setFirstNode(Vnode firstNode) { this.firstNode = firstNode; } //终点 public Vnode getLastNode() { return LastNode; } public void setLastNode(Vnode lastNode) { LastNode = lastNode; } //邻接边 public Arc getNextArc() { return NextArc; } public void setNextArc(Arc nextArc) { NextArc = nextArc; } //流量 public int getFlow() { return flow; } public void setFlow(int flow) { this.flow = flow; } //容量 public int getCapacity() { return Capacity; } public void setCapacity(int capacity) { Capacity = capacity; } //费用 public int getCost() { return cost; } public void setCost(int cost) { this.cost = cost; } }
- 图的类型
package sy1; public enum GraphKind { Gra,//无向图 Digra,//有向图 FlowGra,//费用流图 CostFlowGra;//网络流图 }
- 图结构接口程序
package sy1; /* * 新建一个图结构接口 */ public interface IGraph { public void createGraph() throws Exception;//新建图 public int getVexNum();//返回图中顶点数 public int getArcNum();//返回图中边数 public String getVex(int v) throws Exception;//给定顶点v所处的位置,返回顶点对应顶点名称 public int locateVex(String v) throws Exception;//给定顶点名称,返回对应顶点的位置 public int firstAdjVex(int v) throws Exception;//给定顶点所处位置,返回该顶点的第一个邻接点 public int nextAdjVex(int v,int w) throws Exception;//给定顶点v以及它的邻接点w对应的位置,返回顶点相对于w的下一个邻接点的位置 }
- 构造一条路径的长度
package sy1; /* * 构造路径长度路径结构 */ public class lengthOfPath { int lamda;//路径长度 String recent;//本路径中当下顶点的前一个顶点 String past; //初始化路径 public lengthOfPath(String v){ this.lamda=0; this.recent=v; this.past=null; } public int getLamda() { return lamda; } public void setLamda(int lamda) { this.lamda = lamda; } public String getRecent() { return recent; } public void setRecent(String recent) { this.recent = recent; } public String getPast() { return past; } public void setPast(String past) { this.past = past; } }
- 构造图结构程序
package sy