ImageVerifierCode 换一换
格式:DOCX , 页数:28 ,大小:87.17KB ,
资源ID:10565376      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/10565376.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(实验六 图及图的操作.docx)为本站会员(b****7)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

实验六 图及图的操作.docx

1、实验六 图及图的操作实验报告六 图及图的操作实验一、实验目的:1、掌握图的基本概念和术语2、掌握图的存储结构及创建算法。3、掌握图的遍历算法(递归或非递归算法)。二、实验内容:1、图邻接矩阵存储结构表示及基本操作算法实现(1)邻接矩阵存储结构类定义:自定义如下:public interface LList boolean isEmpty(); int length(); T get(int i); void set(int i,T x); void insert(int i,T x); void append(T x); T remove(int i); void removeAll();pu

2、blic class SeqList implements LList private Object element; private int len; public SeqList(int size) this.element=new Objectsize; this.len = 0; public SeqList(SeqList list) this(list.len); this.len=list.len; public SeqList() this(64); public boolean isEmpty() return this.len=0; public int length()

3、return this.len; public T get(int i) if(i=0&i=0&i0) str += this.element0.toString(); for(int i=1;ithis.len;i+) str +=,+this.elementi.toString(); return str+); public void insert(int i, T x) if(x=null) return; if(this.len=element.length) Object temp = this.element; this.element=new Objecttemp.length*

4、2; for(int j=0;j temp.length;i+) this.elementj=tempj; if(ithis.len) i=this.len; for(int j=this.len-1;j=i;j-) this.elementj+1 = this.elementj; this.elementi=x; this.len+; public void append(T x) insert(this.len,x); public T remove(int i) if(this.len=0|i=len) return null; T old = (T)this.elementi; for

5、(int j=0;jthis.len-1;j+) this.elementj = this.elementj+1; this.elementthis.len-1=null; this.len-; return old; public void removeAll() this.len=0; (2)创建邻接矩阵算法创建无向图邻接矩阵算法:public class MatrixGraph protected SeqList vertexlist; protected int adjmatrix; private final int Max=0; public MatrixGraph(int siz

6、e) size=size10?10:size; this.vertexlist=new SeqList(size); this.adjmatrix=new intsizesize; for(int i=0;isize;i+) for(int j=0;jsize;j+) this.adjmatrixij=(i=j)?0:Max; public MatrixGraph(T vertices,Edge edges) this(vertices.length); if(vertices=null) return; for(int i=0;ivertices.length;i+) insertVerte

7、x(verticesi); if(edges!=null) for(int j=0;jedges.length;j+) insertEdge(edgesj); public int vertexCount() return this.vertexlist.length(); public T get(int i) return this.vertexlist.get(i); public int getWeight(int i,int j) return this.adjmatrixij; public String toString() String str=顶点集合:+this.verte

8、xlist.toString()+n 邻接矩阵:n; int n=this.vertexCount(); for(int i=0;in;i+) for(int j=0;jthis.adjmatrix.length) int temp=adjmatrix,i,j; this.adjmatrix=new inttemp.length*2temp.length2; for(i=0;itemp.length;i+) for(j=0;jtemp.length;j+) this.adjmatrixij=tempij; for(j=temp.length;jtemp.length*2;i+) this.ad

9、jmatrixij=Max; for(i=temp.length;itemp.length*2;i+) for(j=0;j=0&i=0&i!=j&this.adjmatrixij=Max) this.adjmatrixij=weight; public void insertEdge(Edge edge) this.insertEdge(edge.start,edge.dest,edge.weight); 创建无向网邻接矩阵算法:public class MatrixGraph protected SeqList vertexlist; protected int adjmatrix; pri

10、vate final int Max=99999; public MatrixGraph(int size) size=size10?10:size; this.vertexlist=new SeqList(size); this.adjmatrix=new intsizesize; for(int i=0;isize;i+) for(int j=0;jsize;j+) this.adjmatrixij=(i=j)?0:Max; public MatrixGraph(T vertices,Edge edges) this(vertices.length); if(vertices=null)

11、return; for(int i=0;ivertices.length;i+) insertVertex(verticesi); if(edges!=null) for(int j=0;jedges.length;j+) insertEdge(edgesj); public int vertexCount() return this.vertexlist.length(); public T get(int i) return this.vertexlist.get(i); public int getWeight(int i,int j) return this.adjmatrixij;

12、public String toString() String str=顶点集合:+this.vertexlist.toString()+n 邻接矩阵:n; int n=this.vertexCount(); for(int i=0;in;i+) for(int j=0;jthis.adjmatrix.length) int temp=adjmatrix,i,j; this.adjmatrix=new inttemp.length*2temp.length2; for(i=0;itemp.length;i+) for(j=0;jtemp.length;j+) this.adjmatrixij=

13、tempij; for(j=temp.length;jtemp.length*2;i+) this.adjmatrixij=Max; for(i=temp.length;itemp.length*2;i+) for(j=0;j=0&i=0&i!=j&this.adjmatrixij=Max) this.adjmatrixij=weight; public void insertEdge(Edge edge) this.insertEdge(edge.start,edge.dest,edge.weight); 创建有向图邻接矩阵算法:(可使用前无向图邻接矩阵算法)创建有向网邻接矩阵算法:(可使用

14、前无向图邻接矩阵算法) (3)输出邻接矩阵结果算法 public static void main(String args) String vertices=A,B,C,D,E; Edge edges=new Edge(0,1,1),new Edge(0,3,1),new Edge(1,0,1), new Edge(1,2,1),new Edge(1,3,1),new Edge(2,1,1),new Edge(2,3,1), new Edge(2,4,1),new Edge(3,0,1),new Edge(3,1,1),new Edge(3,2,1), new Edge(3,4,1),new

15、Edge(4,2,1),new Edge(4,3,1),; MatrixGraph graph=new MatrixGraph(vertices,edges); System.out.println(无向图:+graph.toString(); public static void main(String args) String vertices=A,B,C,D,E; Edge edges=new Edge(0,1,5),new Edge(0,3,2),new Edge(1,0,5), new Edge(1,2,7),new Edge(1,3,6),new Edge(2,1,7),new E

16、dge(2,3,8), new Edge(2,4,3),new Edge(3,0,2),new Edge(3,1,6),new Edge(3,2,8), new Edge(3,4,9),new Edge(4,2,3),new Edge(4,3,9); MatrixGraph graph=new MatrixGraph(vertices,edges); System.out.println(无向网:+graph.toString(); public static void main(String args) String vertices=A,B,C,D,E; Edge edges=new Ed

17、ge(0,1,1),new Edge(0,3,1), new Edge(1,3,1),new Edge(2,3,1), new Edge(2,4,1),new Edge(3,1,1),new Edge(3,2,1), new Edge(4,2,1),new Edge(4,3,1); MatrixGraph graph=new MatrixGraph(vertices,edges); System.out.println(有向图:+graph.toString(); public static void main(String args) String vertices=A,B,C,D,E; E

18、dge edges=new Edge(0,1,5),new Edge(0,3,2), new Edge(1,3,6),new Edge(2,3,8), new Edge(2,4,3),new Edge(3,1,9),new Edge(3,2,2), new Edge(4,2,3),new Edge(4,3,9); MatrixGraph graph=new MatrixGraph(vertices,edges); System.out.println(有向网:+graph.toString(); 测试结果粘贴如下:2、图邻接表存储结构表示及基本操作算法实现(1)邻接表存储结构类定义:自定义如下

19、:public class Vertex public T data; public SortedSinglyLinkedList adjlink; public Vertex(T data) this.data=data; this.adjlink=new SortedSinglyLinkedList(); public String toString() return n+this.data.toString()+: +this.adjlink.toString(); (2)创建邻接表算法创建无向网邻接表算法:(可使用下有向网邻接表算法)创建有向网邻接表算法:public class Ad

20、jListGraph protected SeqListVertex vertexlist; public AdjListGraph(int size) size=size10?10:size; this.vertexlist=new SeqListVertex(size); public AdjListGraph(T vertices,Edge edges) this(vertices.length*2); if(vertices=null) return; for(int i=0;ivertices.length;i+) insertVertex(verticesi); if(edges!

21、=null) for(int j=0;jedges.length;j+) insertEdge(edgesj); public String toString() return 出边表: n+this.vertexlist.toString()+n; public int insertVertex(T x) this.vertexlist.append(new Vertex(x); return this.vertexlist.length()-1; public int vertexCount() return this.vertexlist.length(); public void in

22、sertEdge(int i,int j,int weight) if(i=0&i=0&jvertexCount()&i!=j) Edge edge=new Edge(i,j,weight); SortedSinglyLinkedList adjlink=this.vertexlist.get(i).adjlink; Node front=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=new Node

23、(edge,p); public void insertEdge(Edge edge) this.insertEdge(edge.start,edge.dest,edge.weight); (3)输出邻接表结果算法 public static void main(String args) String vertices=A,B,C,D,E; Edge edges=new Edge(0,1,5),new Edge(0,3,2),new Edge(1,0,5),new Edge(3,0,2), new Edge(2,4,3),new Edge(4,2,3); AdjListGraph graph=new AdjListGraph(vertices,edges); System.out.println(无向网:+graph.toString(); public static void main(String args) String vertices=A,B,C,D,E; Edge edges=new Edge(0,1,5),new Edge(0,3,2),new Edge(1,0,6),new Edge(1,2,7), new Edge(2,4,3),new

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

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