实验六 图及图的操作.docx
- 文档编号:10565376
- 上传时间:2023-02-21
- 格式:DOCX
- 页数:28
- 大小:87.17KB
实验六 图及图的操作.docx
《实验六 图及图的操作.docx》由会员分享,可在线阅读,更多相关《实验六 图及图的操作.docx(28页珍藏版)》请在冰豆网上搜索。
实验六图及图的操作
实验报告六图及图的操作实验
一、实验目的:
1、掌握图的基本概念和术语
2、掌握图的存储结构及创建算法。
3、掌握图的遍历算法(递归或非递归算法)。
二、实验内容:
1、图邻接矩阵存储结构表示及基本操作算法实现
(1)邻接矩阵存储结构类定义:
自定义如下:
publicinterfaceLList
booleanisEmpty();
intlength();
Tget(inti);
voidset(inti,Tx);
voidinsert(inti,Tx);
voidappend(Tx);
Tremove(inti);
voidremoveAll();
}
publicclassSeqList
privateObject[]element;
privateintlen;
publicSeqList(intsize){
this.element=newObject[size];
this.len=0;
}
publicSeqList(SeqList
this(list.len);
this.len=list.len;
}
publicSeqList(){
this(64);
}
publicbooleanisEmpty(){
returnthis.len==0;
}
publicintlength(){
returnthis.len;
}
publicTget(inti){
if(i>=0&&i return(T)this.element[i]; returnnull; } publicvoidset(inti,Tx){ if(x==null) return; if(i>=0&&i this.element[i]=x; else thrownewIndexOutOfBoundsException(i+""); } publicStringtoString(){ Stringstr="("; if(this.len>0) str+=this.element[0].toString(); for(inti=1;i str+=","+this.element[i].toString(); returnstr+")"; } publicvoidinsert(inti,Tx){ if(x==null) return; if(this.len==element.length){ Object[]temp=this.element; this.element=newObject[temp.length*2]; for(intj=0;j this.element[j]=temp[j]; } if(i<0) i=0; if(i>this.len) i=this.len; for(intj=this.len-1;j>=i;j--) this.element[j+1]=this.element[j]; this.element[i]=x; this.len++; } publicvoidappend(Tx){ insert(this.len,x); } publicTremove(inti){ if(this.len==0||i<0||i>=len) returnnull; Told=(T)this.element[i]; for(intj=0;j this.element[j]=this.element[j+1]; this.element[this.len-1]=null; this.len--; returnold; } publicvoidremoveAll(){ this.len=0; } } (2)创建邻接矩阵算法 创建无向图邻接矩阵算法: publicclassMatrixGraph protectedSeqList protectedint[][]adjmatrix; privatefinalintMax=0; publicMatrixGraph(intsize){ size=size<10? 10: size; this.vertexlist=newSeqList this.adjmatrix=newint[size][size]; for(inti=0;i for(intj=0;j this.adjmatrix[i][j]=(i==j)? 0: Max; } publicMatrixGraph(T[]vertices,Edge[]edges){ this(vertices.length); if(vertices==null) return; for(inti=0;i insertVertex(vertices[i]); if(edges! =null) for(intj=0;j insertEdge(edges[j]); } publicintvertexCount(){ returnthis.vertexlist.length(); } publicTget(inti){ returnthis.vertexlist.get(i); } publicintgetWeight(inti,intj){ returnthis.adjmatrix[i][j]; } publicStringtoString(){ Stringstr="顶点集合: "+this.vertexlist.toString()+"\n邻接矩阵: \n"; intn=this.vertexCount(); for(inti=0;i for(intj=0;j str+=this.adjmatrix[i][j]==Max? "0": ""+this.adjmatrix[i][j]; str+="\n"; } returnstr; } publicintinsertVertex(Tx){ this.vertexlist.append(x); if(this.vertexCount()>this.adjmatrix.length){ inttemp[][]=adjmatrix,i,j; this.adjmatrix=newint[temp.length*2][temp.length^2]; for(i=0;i for(j=0;j this.adjmatrix[i][j]=temp[i][j]; for(j=temp.length;j this.adjmatrix[i][j]=Max; } for(i=temp.length;i for(j=0;j this.adjmatrix[i][j]=(i==j)? 0: Max; } returnthis.vertexlist.length()-1; } publicvoidinsertEdge(inti,intj,intweight){ intn=this.vertexCount(); if(i>=0&&i =j&&this.adjmatrix[i][j]==Max) this.adjmatrix[i][j]=weight; } publicvoidinsertEdge(Edgeedge){ this.insertEdge(edge.start,edge.dest,edge.weight); } } 创建无向网邻接矩阵算法: publicclassMatrixGraph protectedSeqList protectedint[][]adjmatrix; privatefinalintMax=99999; publicMatrixGraph(intsize){ size=size<10? 10: size; this.vertexlist=newSeqList this.adjmatrix=newint[size][size]; for(inti=0;i for(intj=0;j this.adjmatrix[i][j]=(i==j)? 0: Max; } publicMatrixGraph(T[]vertices,Edge[]edges){ this(vertices.length); if(vertices==null) return; for(inti=0;i insertVertex(vertices[i]); if(edges! =null) for(intj=0;j insertEdge(edges[j]); } publicintvertexCount(){ returnthis.vertexlist.length(); } publicTget(inti){ returnthis.vertexlist.get(i); } publicintgetWeight(inti,intj){ returnthis.adjmatrix[i][j]; } publicStringtoString(){ Stringstr="顶点集合: "+this.vertexlist.toString()+"\n邻接矩阵: \n"; intn=this.vertexCount(); for(inti=0;i for(intj=0;j str+=this.adjmatrix[i][j]==Max? "∞": ""+this.adjmatrix[i][j]; str+="\n"; } returnstr; } publicintinsertVertex(Tx){ this.vertexlist.append(x); if(this.vertexCount()>this.adjmatrix.length){ inttemp[][]=adjmatrix,i,j; this.adjmatrix=newint[temp.length*2][temp.length^2]; for(i=0;i for(j=0;j this.adjmatrix[i][j]=temp[i][j]; for(j=temp.length;j this.adjmatrix[i][j]=Max; } for(i=temp.length;i for(j=0;j this.adjmatrix[i][j]=(i==j)? 0: Max; } returnthis.vertexlist.length()-1; } publicvoidinsertEdge(inti,intj,intweight){ intn=this.vertexCount(); if(i>=0&&i =j&&this.adjmatrix[i][j]==Max) this.adjmatrix[i][j]=weight; } publicvoidinsertEdge(Edgeedge){ this.insertEdge(edge.start,edge.dest,edge.weight); } } 创建有向图邻接矩阵算法: (可使用前无向图邻接矩阵算法) 创建有向网邻接矩阵算法: (可使用前无向图邻接矩阵算法) (3)输出邻接矩阵结果算法 publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,1),newEdge(0,3,1),newEdge(1,0,1), newEdge(1,2,1),newEdge(1,3,1),newEdge(2,1,1),newEdge(2,3,1), newEdge(2,4,1),newEdge(3,0,1),newEdge(3,1,1),newEdge(3,2,1), newEdge(3,4,1),newEdge(4,2,1),newEdge(4,3,1),}; MatrixGraph System.out.println("无向图: "+graph.toString()); } publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,5),newEdge(0,3,2),newEdge(1,0,5), newEdge(1,2,7),newEdge(1,3,6),newEdge(2,1,7),newEdge(2,3,8), newEdge(2,4,3),newEdge(3,0,2),newEdge(3,1,6),newEdge(3,2,8), newEdge(3,4,9),newEdge(4,2,3),newEdge(4,3,9)}; MatrixGraph System.out.println("无向网: "+graph.toString()); } publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,1),newEdge(0,3,1), newEdge(1,3,1),newEdge(2,3,1), newEdge(2,4,1),newEdge(3,1,1),newEdge(3,2,1), newEdge(4,2,1),newEdge(4,3,1)}; MatrixGraph System.out.println("有向图: "+graph.toString()); } publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,5),newEdge(0,3,2), newEdge(1,3,6),newEdge(2,3,8), newEdge(2,4,3),newEdge(3,1,9),newEdge(3,2,2), newEdge(4,2,3),newEdge(4,3,9)}; MatrixGraph System.out.println("有向网: "+graph.toString()); } 测试结果粘贴如下: 2、图邻接表存储结构表示及基本操作算法实现 (1)邻接表存储结构类定义: 自定义如下: publicclassVertex publicTdata; publicSortedSinglyLinkedList publicVertex(Tdata){ this.data=data; this.adjlink=newSortedSinglyLinkedList } publicStringtoString(){ return"\n"+this.data.toString()+": "+this.adjlink.toString(); } } (2)创建邻接表算法 创建无向网邻接表算法: (可使用下有向网邻接表算法) 创建有向网邻接表算法: publicclassAdjListGraph protectedSeqList publicAdjListGraph(intsize){ size=size<10? 10: size; this.vertexlist=newSeqList } publicAdjListGraph(T[]vertices,Edge[]edges){ this(vertices.length*2); if(vertices==null) return; for(inti=0;i insertVertex(vertices[i]); if(edges! =null) for(intj=0;j insertEdge(edges[j]); } publicStringtoString(){ return"出边表: \n"+this.vertexlist.toString()+"\n"; } publicintinsertVertex(Tx){ this.vertexlist.append(newVertex returnthis.vertexlist.length()-1; } publicintvertexCount(){ returnthis.vertexlist.length(); } publicvoidinsertEdge(inti,intj,intweight){ if(i>=0&&i =j){ Edgeedge=newEdge(i,j,weight); SortedSinglyLinkedList Node while(p! =null&&pareTo(edge)<0){ front=p; p=p.next; } if(p! =null&&pareTo(edge)==0) return; front.next=newNode } } publicvoidinsertEdge(Edgeedge){ this.insertEdge(edge.start,edge.dest,edge.weight); } (3)输出邻接表结果算法 publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,5),newEdge(0,3,2),newEdge(1,0,5),newEdge(3,0,2), newEdge(2,4,3),newEdge(4,2,3)}; AdjListGraph System.out.println("无向网: "+graph.toString()); } publicstaticvoidmain(String[]args){ String[]vertices={"A","B","C","D","E"}; Edgeedges[]={newEdge(0,1,5),newEdge(0,3,2),newEdge(1,0,6),newEdge(1,2,7), newEdge(2,4,3),new
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验六 图及图的操作 实验 操作
![提示](https://static.bdocx.com/images/bang_tan.gif)