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