实习报告——“邻接矩阵表示的带权有向图”演示程序(一)、程序的功能和特点主要实现的功能:1.使用邻接矩阵表示带权有向图;2.查找指定顶点序号;3.判断图是否为空;4.判断图是否满;5.取得顶点数、边数、一条边的权值;6.插入一个顶点、边;7.删除一个顶点、边;(二)、程序的算法设计“邻接矩阵的表示”算法:1.【逻辑结构与存储结构设计】逻辑结构:非线性结构——网状结构存储结构:内存中连续的存储结构,邻接矩阵2.【基本操作设计】按指定输入,生成图并打印该图删除一个顶点并打印删除一条边并打印3. 【算法设计】插入一个顶点的算法:首先判断该图是否已满,若已满:插入失败;否则进行插入:1.顶点表增加一个元素2.邻接矩阵增加一行一列删除一个顶点的算法:判断要删除顶点的存在性,若不存在:出错;否则:1.修改顶点表,即在顶点数组中删除该点;2.修改邻接矩阵,即需要统计与该顶点相关联的边,并将这些边也删除4.【高级语言代码】public class Graph {static int MaxEdges=50;static int MaxVertices=10;static int MaxValue=9999;//无穷大//存放顶点的数组private char VerticesList[]=new char[MaxVertices]; //邻接矩阵(存放两个顶点的权值)private int Edge[][]=new int[MaxVertices][MaxVertices];private int CurrentEdges;//现有边数private int CurrentVertices;//现有顶点数//构造函数:建立空的邻接矩阵public Graph(){for(int i=0;i<MaxVertices;i++)for(int j=0;j<MaxVertices;j++)if(i==j)Edge[i][j]=0;//对角线elseEdge[i][j]=MaxValue;CurrentEdges=0;//现有边数CurrentVertices=0;//现有顶点数}//取得一条边的权值public int GetWeigh(int v1,int v2){if(v1<0||v1>CurrentVertices-1)return -1;//用-1表示出错if(v2<0||v2>CurrentVertices-1)return -1;//用-1表示出错return Edge[v1][v2];}//取得第一个邻接点的序号public int GetFirstNeighbor(int v){if(v<0||v>CurrentVertices-1)return -1;//邻接矩阵的行号和列号是两个邻接点的序号for(int col=0;col<CurrentVertices;col++) if(Edge[v][col]>0&&Edge[v][col]<MaxValue) return col;return -1;}//插入一个顶点public int InsertVertex(char vertex){if(IsGraphFull())return -1;//插入失败//顶点表增加一个元素VerticesList[CurrentVertices]=vertex;//邻接矩阵增加一行一列for(int j=0;j<=CurrentVertices;j++){Edge[CurrentEdges][j]=MaxValue;Edge[j][CurrentEdges]=MaxValue;}Edge[CurrentEdges][CurrentEdges]=0;CurrentVertices++;return CurrentVertices;//插入位置}//插入一个边public boolean InsertEdge(int v1,int v2,int weight){ if(v1<0||v1>CurrentVertices-1)return false;//出错if(v2<0||v2>CurrentVertices-1)return false;//出错Edge[v1][v2]=weight;return true;}//删除一个顶点public boolean RemoveVertex(int v){ if(v<0||v>CurrentVertices-1)return false;//出错//修改顶点表for(int i=v+1;i<CurrentVertices;i++) VerticesList[i-1]=VerticesList[i];//修改邻接矩阵int k=0;//累计将要删去的边数for(int i=0;i<CurrentVertices;i++)if(Edge[v][i]>0&&Edge[v][i]<MaxValue) k++;//第v行for(int i=0;i<CurrentVertices;i++)if(Edge[i][v]>0&&Edge[i][v]<MaxValue) k++;//第v列//覆盖第v行int j;for(int i=v+1;i<CurrentVertices;i++) for(j=0;j<CurrentVertices;j++)Edge[i-1][j]=Edge[i][j];//覆盖第v列for( j=v+1;j<CurrentVertices;j++)for(int i=0;i<CurrentVertices;i++)Edge[i][j-1]=Edge[i][j];CurrentVertices--;//修改顶点数CurrentEdges-=k;//修改边数return true;}//删除一个边public boolean RemoveEdge(int v1,int v2){ if(v1<0||v1>CurrentVertices-1)return false;//出错if(v2<0||v2>CurrentVertices-1)return false;//出错if(v1==v2)return false;Edge[v1][v2]=MaxValue;return true;}(三)、程序中类的设计“Graph”类:1.【主要成员变量说明】static int MaxEdges=50;static int MaxVertices=10;static int MaxValue=9999;//无穷大//存放顶点的数组private char VerticesList[]=new char[MaxVertices];private int CurrentEdges;//现有边数private int CurrentVertices;//现有顶点数//邻接矩阵(存放两个顶点的权值)private int Edge[][]=new int[MaxVertices][MaxVertices];2.【主要成员方法说明】//查找指定的顶点序号public int FindVertex(char vertex){}//判断图是否为空public boolean IsGraphEmpty(){}//判断图是否满public boolean IsGraphFull(){}//取得顶点数public int NumberOfVertices(){}//取得边数public int NumberOfEdges(){}//按序号取得顶点值public char GetValue(int i){}//取得一条边的权值public int GetWeigh(int v1,int v2){ }//取得第一个邻接点的序号public int GetFirstNeighbor(int v){ }//插入一个顶点public int InsertVertex(char vertex){}//插入一个边public boolean InsertEdge(int v1,int v2,int weight){ }//删除一个顶点public boolean RemoveVertex(int v){}//删除一个边public boolean RemoveEdge(int v1,int v2){}4.【高级语言代码】package study_3;public class Graph {static int MaxEdges=50;static int MaxVertices=10;static int MaxValue=9999;//无穷大//存放顶点的数组private char VerticesList[]=new char[MaxVertices]; //邻接矩阵(存放两个顶点的权值)private int Edge[][]=new int[MaxVertices][MaxVertices];private int CurrentEdges;//现有边数private int CurrentVertices;//现有顶点数//构造函数:建立空的邻接矩阵public Graph(){for(int i=0;i<MaxVertices;i++)for(int j=0;j<MaxVertices;j++)if(i==j)Edge[i][j]=0;//对角线elseEdge[i][j]=MaxValue;CurrentEdges=0;//现有边数CurrentVertices=0;//现有顶点数}//查找指定的顶点序号public int FindVertex(char vertex){ //在顶点数组里面查找for(int i=0;i<CurrentVertices;i++) if(VerticesList[i]==vertex)return i;//返回下标return -1;}//判断图是否为空public boolean IsGraphEmpty(){return CurrentVertices==0;}//判断图是否满public boolean IsGraphFull(){returnCurrentVertices==MaxVertices||CurrentEdges==MaxEdges;}//取得顶点数public int NumberOfVertices(){return CurrentVertices;}//取得边数public int NumberOfEdges(){return CurrentEdges;}//按序号取得顶点值public char GetValue(int i){return i>0&&i<CurrentVertices-1?VerticesList[i]:' ';}//取得一条边的权值public int GetWeigh(int v1,int v2){if(v1<0||v1>CurrentVertices-1)return -1;//用-1表示出错if(v2<0||v2>CurrentVertices-1)return -1;//用-1表示出错return Edge[v1][v2];}//取得第一个邻接点的序号public int GetFirstNeighbor(int v){if(v<0||v>CurrentVertices-1)return -1;//邻接矩阵的行号和列号是两个邻接点的序号for(int col=0;col<CurrentVertices;col++) if(Edge[v][col]>0&&Edge[v][col]<MaxValue) return col;return -1;}//插入一个顶点public int InsertVertex(char vertex){if(IsGraphFull())return -1;//插入失败//顶点表增加一个元素VerticesList[CurrentVertices]=vertex;//邻接矩阵增加一行一列for(int j=0;j<=CurrentVertices;j++){Edge[CurrentEdges][j]=MaxValue;Edge[j][CurrentEdges]=MaxValue;}Edge[CurrentEdges][CurrentEdges]=0;CurrentVertices++;return CurrentVertices;//插入位置}//插入一个边public boolean InsertEdge(int v1,int v2,int weight){ if(v1<0||v1>CurrentVertices-1)return false;//出错if(v2<0||v2>CurrentVertices-1)return false;//出错Edge[v1][v2]=weight;return true;}//删除一个顶点public boolean RemoveVertex(int v){if(v<0||v>CurrentVertices-1)return false;//出错//修改顶点表for(int i=v+1;i<CurrentVertices;i++) VerticesList[i-1]=VerticesList[i];//修改邻接矩阵int k=0;//累计将要删去的边数for(int i=0;i<CurrentVertices;i++) if(Edge[v][i]>0&&Edge[v][i]<MaxValue) k++;//第v行for(int i=0;i<CurrentVertices;i++) if(Edge[i][v]>0&&Edge[i][v]<MaxValue) k++;//第v列//覆盖第v行int j;for(int i=v+1;i<CurrentVertices;i++) for(j=0;j<CurrentVertices;j++)Edge[i-1][j]=Edge[i][j];//覆盖第v列for( j=v+1;j<CurrentVertices;j++) for(int i=0;i<CurrentVertices;i++) Edge[i][j-1]=Edge[i][j]; CurrentVertices--;//修改顶点数CurrentEdges-=k;//修改边数return true;}//删除一个边public boolean RemoveEdge(int v1,int v2){ if(v1<0||v1>CurrentVertices-1)return false;//出错if(v2<0||v2>CurrentVertices-1)return false;//出错if(v1==v2)return false;Edge[v1][v2]=MaxValue;return true;}//打印邻接矩阵public void display(){int i,j;System.out.println("++++顶点表++++");for(i=0;i<CurrentVertices;i++)System.out.print(VerticesList[i]+" ");System.out.println();System.out.println("++++邻接矩阵+++++");for(i=0;i<CurrentVertices;i++){for(j=0;j<CurrentVertices;j++)if(Edge[i][j]==MaxValue)System.out.print('@'+" ");elseSystem.out.print(Edge[i][j]+" ");System.out.println();}}(四)、程序的输入输出和运行结果截屏删除顶点3:删除一个边:。