实验六 图及图的操作.docx

上传人:b****7 文档编号:10565376 上传时间:2023-02-21 格式:DOCX 页数:28 大小:87.17KB
下载 相关 举报
实验六 图及图的操作.docx_第1页
第1页 / 共28页
实验六 图及图的操作.docx_第2页
第2页 / 共28页
实验六 图及图的操作.docx_第3页
第3页 / 共28页
实验六 图及图的操作.docx_第4页
第4页 / 共28页
实验六 图及图的操作.docx_第5页
第5页 / 共28页
点击查看更多>>
下载资源
资源描述

实验六 图及图的操作.docx

《实验六 图及图的操作.docx》由会员分享,可在线阅读,更多相关《实验六 图及图的操作.docx(28页珍藏版)》请在冰豆网上搜索。

实验六 图及图的操作.docx

实验六图及图的操作

实验报告六图及图的操作实验

一、实验目的:

1、掌握图的基本概念和术语

2、掌握图的存储结构及创建算法。

3、掌握图的遍历算法(递归或非递归算法)。

二、实验内容:

1、图邻接矩阵存储结构表示及基本操作算法实现

(1)邻接矩阵存储结构类定义:

自定义如下:

publicinterfaceLList{

booleanisEmpty();

intlength();

Tget(inti);

voidset(inti,Tx);

voidinsert(inti,Tx);

voidappend(Tx);

Tremove(inti);

voidremoveAll();

}

publicclassSeqListimplementsLList{

privateObject[]element;

privateintlen;

publicSeqList(intsize){

this.element=newObject[size];

this.len=0;

}

publicSeqList(SeqListlist){

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{

protectedSeqListvertexlist;

protectedint[][]adjmatrix;

privatefinalintMax=0;

publicMatrixGraph(intsize){

size=size<10?

10:

size;

this.vertexlist=newSeqList(size);

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=0&&i!

=j&&this.adjmatrix[i][j]==Max)

this.adjmatrix[i][j]=weight;

}

publicvoidinsertEdge(Edgeedge){

this.insertEdge(edge.start,edge.dest,edge.weight);

}

}

创建无向网邻接矩阵算法:

publicclassMatrixGraph{

protectedSeqListvertexlist;

protectedint[][]adjmatrix;

privatefinalintMax=99999;

publicMatrixGraph(intsize){

size=size<10?

10:

size;

this.vertexlist=newSeqList(size);

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=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),};

MatrixGraphgraph=newMatrixGraph(vertices,edges);

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)};

MatrixGraphgraph=newMatrixGraph(vertices,edges);

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)};

MatrixGraphgraph=newMatrixGraph(vertices,edges);

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)};

MatrixGraphgraph=newMatrixGraph(vertices,edges);

System.out.println("有向网:

"+graph.toString());

}

 

测试结果粘贴如下:

2、图邻接表存储结构表示及基本操作算法实现

(1)邻接表存储结构类定义:

自定义如下:

publicclassVertex{

publicTdata;

publicSortedSinglyLinkedListadjlink;

publicVertex(Tdata){

this.data=data;

this.adjlink=newSortedSinglyLinkedList();

}

publicStringtoString(){

return"\n"+this.data.toString()+":

"+this.adjlink.toString();

}

}

(2)创建邻接表算法

创建无向网邻接表算法:

(可使用下有向网邻接表算法)

创建有向网邻接表算法:

publicclassAdjListGraph{

protectedSeqList>vertexlist;

publicAdjListGraph(intsize){

size=size<10?

10:

size;

this.vertexlist=newSeqList>(size);

}

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(x));

returnthis.vertexlist.length()-1;

}

publicintvertexCount(){

returnthis.vertexlist.length();

}

publicvoidinsertEdge(inti,intj,intweight){

if(i>=0&&i=0&&j

=j){

Edgeedge=newEdge(i,j,weight);

SortedSinglyLinkedListadjlink=this.vertexlist.get(i).adjlink;

Nodefront=adjlink.head,p=front.next;

while(p!

=null&&pareTo(edge)<0){

front=p;

p=p.next;

}

if(p!

=null&&pareTo(edge)==0)

return;

front.next=newNode(edge,p);

}

}

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)};

AdjListGraphgraph=newAdjListGraph(vertices,edges);

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 哲学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1