java中图的两种存储方式

大家知道,图这种数据结构在java中有邻接表与邻接矩阵这两种存储方式,前者编程相对复杂但是效率较高,后者编程相对简单但是效率较低,我们利用java可以很容易的实现这两种数据结构。代码如下

邻接矩阵的存储方式:

public class MGraph {
	
	
	 private int[][] edges;//邻接矩阵
	 private int numOfEdges;//边的数目
	 private int numOfVetex;//顶点的数目
	 
	//初始化一个邻接矩阵 
	public MGraph(int n) {
		edges = new int[n][n];
		numOfEdges = 0;
		numOfVetex = n;
	}
	
	//初始化一个邻接矩阵并且以一个n*n的矩阵为其赋值
	public MGraph(int[][] edges,int n){
		numOfVetex=n;
		numOfEdges=0;
		this.edges = new int[n][n];
		for(int i=0;i");
		for (int i = 0; i < numOfVetex; i++) {
			if (edges[v][i] != -1 && edges[v][i] != 0 && visited[i] != 1) {
				DFSREC(i, visited);
			}
		}
	}
	
	//广度优先搜索
	public void BFS(int v) {
		int[] qu=new int[numOfVetex];
		int[] visited=new int[numOfVetex];
		int head=0;
		int tail=0;
		qu[tail++]=v;
		visited[v]=1;
		while(head!=tail){
			int t=qu[head++];
			System.out.print(t+"-->");
			for(int i=0;i");
				}
		}
		else{
			for(int t=0;t

邻接表的存储方式:

package Graph;

import java.util.ArrayList;

public class LGraph {
	//顶点表节点
	private class VNode{
		int data;              //起点的序号
		ArcNode firstArc;      //指向第一条边
		
		
		
		
		public VNode(int data, ArcNode firstArc) {
			this.data = data;
			this.firstArc = firstArc;
		}
		
		public int getData() {
			return data;
		}
		public void setData(int data) {
			this.data = data;
		}
		public ArcNode getFirstArc() {
			return firstArc;
		}
		public void setFirstArc(ArcNode firstArc) {
			this.firstArc = firstArc;
		}
		
		
	}
	
	//边表结点
	private class ArcNode{
		int adjvex;           //该边指向终点的序号
		ArcNode nextarc;      //下一条边的指针 
		int len;              //该边的长度
		
		public ArcNode(int adjvex, ArcNode nextarc, int len) {
			super();
			this.adjvex = adjvex;
			this.nextarc = nextarc;
			this.len = len;
		}

		public int getAdjvex() {
			return adjvex;
		}

		public void setAdjvex(int adjvex) {
			this.adjvex = adjvex;
		}

		public ArcNode getNextarc() {
			return nextarc;
		}

		public void setNextarc(ArcNode nextarc) {
			this.nextarc = nextarc;
		}

		public int getLen() {
			return len;
		}

		public void setLen(int len) {
			this.len = len;
		}
		
		
		
		
	}
	
	ArrayList vNodes;
	int numOfVetex;
	int numOfEdges;
	
	public LGraph() {
		vNodes=new ArrayList();
		numOfVetex=0;
		numOfEdges=0;
	}
	
	public LGraph(int[][] edges,int n){
		numOfVetex=n;
		numOfEdges=0;
		vNodes=new ArrayList();
		for(int i=0;i"+arc.getAdjvex());
				arc=arc.getNextarc();
			}
			System.out.println();
		}
	}
	
	//深度优先搜索的辅助函数
	private void DFSREC(int v,int visited[]){
		System.out.print(v+"-->");
		visited[v]=1;
		ArcNode arc=vNodes.get(v).getFirstArc();
		while(arc!=null){
			if(visited[arc.getAdjvex()]==0)
				DFSREC(arc.getAdjvex(),visited);
			arc=arc.getNextarc();
		}
	}
	
	//深度优先搜索
	public void DFS(int v){
		int[] visited=new int[numOfVetex];
		DFSREC(v,visited);
	}
	
	//广度优先搜索
	public void BFS(int v){
		int[] qu=new int[numOfVetex];
		int tail=0;
		int head=0;
		int[] visited=new int[numOfVetex];
		qu[tail++]=v;
		while(head!=tail){
			int t=qu[head++];
			visited[t]=1;
			System.out.print(t+"-->");
			ArcNode arc=vNodes.get(t).getFirstArc();
			while(arc!=null){
				if(visited[arc.getAdjvex()]!=1){
					qu[tail++]=arc.getAdjvex();
				}
				arc=arc.getNextarc();
			}
		}
	}
	
	//将邻接表存储的图转化为矩阵存储的图
	public MGraph toMGraph(){
		int[][] edges=new int[numOfVetex][numOfVetex];
		for(int i=0;i


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