java实现图的构造(邻接矩阵表示),图的深度优先、广度优先遍历,图的最小生成树

 

构造图结构(邻接矩阵表示&图的深度优先和广度优先遍历&图的最小生成树(prim算法)

public class AbstractGraph{
protected static final int MAX_WEIGHT=0x0000fff;
protected SeqListvertexlist;
public AbstractGraph(int length){
	this.vertexlist=new SeqList(length);
}
public int vertexCount(){
	return this.vertexlist.size();
}
public String toString(){
	return"顶点集合e:"+this.vertexlist.toString()+"\n";
}
public T getVertex(int i){
	 return this.vertexlist.get(i);
}
}
class SeqList{
	protected Object[]element;
	protected int n;
	public SeqList(int length){
		this.element=new Object[length];
		this.n=0;
	}
	public int size(){
		return this.n;
	}
	public int insert(int i,T x){
		if(x==null)
			throw new NullPointerException("x==null");
		if(i<0) i=0;
		if(i>this.n) i=this.n;
		Object[] source=this.element;
		if(this.n==element.length){
			this.element=new Object[source.length*2];
			for(int j=0;j=i;j--)
			this.element[j+1]=source[j];
			this.element[i]=x;
			this.n++;
			return i;
	}

	public int insert(T x){
		return this.insert(this.n,x);
	}
	public T get(int i){
		if(i>=0&&i0)
			str+=this.element[0].toString();
		for(int i=1;i

 

import java.util.LinkedList;
public class MatrixGraph extends AbstractGraph{	
protected Matrix matrix;
public MatrixGraph(int length){
	super(length);
	this.matrix=new Matrix(length);
}
public MatrixGraph(){
	this(10);
}
public MatrixGraph(T[]vertices){
	this(vertices.length);
	for(int i=0;iMAX_WEIGHT)
				weight=MAX_WEIGHT;
			this.matrix.set(i,j,weight);
		}
		else throw new IllegalArgumentException("不能插入自身环, i="+i+",j="+j);
	}
	public void insertEdge(Triple edge){
		this.insertEdge(edge.row,edge.column,edge.value);
	}
	public int insertVertex(T x){
		int i=this.vertexlist.insert(x);
		if(i>=this.matrix.getRows())
			this.matrix.setRowsColumns(i+1,i+1);
		for(int j=0;j=0&&i=-1&&j0&&this.matrix.get(i, k) graph){//深度优先遍历
		System.out.println("深度优先遍历:");
		for(int i=0;i que=new LinkedList();
		que.add(i);
		while(!que.isEmpty()){
			i=que.poll();
			for(int j=next(i,-1);j!=-1;j=next(i,j))
				if(!visited[j]){
					System.out.print(this.getVertex(j)+" ");
					visited[j]=true;
					que.add(j);
				}
		}
	}
	public void breadthfsResult(MatrixGraph graph){
		System.out.println("广度优先遍历:");
		for(int i=0;i graph=new MatrixGraph(vertices,edges);
		System.out.println("带权无向图G3(除顶点F)"+graph.toString());
		graph.depthfsResult(graph); graph.breadthfsResult(graph); 
		graph.minSpanTree();
	}
	}
class Triple{
	int row,column,value;
	public Triple(int row,int column,int value){
		if(row>=0&&column>=0){
		this.row=row;
		this.column=column;
		this.value=value;
		}
		else throw new IllegalArgumentException("行,列号不能为负数:row="+row+",column="+column);
	}
}
class Matrix{
	private int rows,columns;
	private int[][]element;
	public Matrix(int m,int n){
		this.element=new int[m][n];
		this.rows=m;
		this.columns=n;
	}
	public Matrix(int n){
		this(n,n);
	}
	public int getRows(){
		return this.rows;
	}
	public int get(int i,int j){
		if(i>=0&&i=0&&j

java实现图的构造(邻接矩阵表示),图的深度优先、广度优先遍历,图的最小生成树_第1张图片

你可能感兴趣的:(数据结构图(java))