基于邻接矩阵的无向图的深度广度遍历实现

图的创建及深度广度遍历的代码实现,基于的是邻接矩阵,图这里是无向图,并且两个顶点之间有连接则邻接矩阵非零,如果没有连接则为零

public class Graph {
	//图的邻接矩阵形式
	private int[][] edges;
	private int num;//图的结点数量
	private boolean[] visited ;//结点是否被访问过
	private Vertex[] vertex ;
	private int pre;//用来记录前一个访问的结点
	public Graph(){
		
	}
	public void createGraph(){
		Scanner in = new Scanner(System.in);
		System.out.print("please input the info:");
		String str = in.next();
		String[] temp = str.split(",");
		System.out.print(temp.length);
		this.num = temp.length;
		System.out.print(num);
		visited = new boolean[num];
		vertex = new Vertex[num];
		for(int i=0;i

public class Vertex {
	//图的顶点信息
  public	int no;//顶点的标号
  public String info;//顶点的信息
  public Vertex(int i,String info){
	  this.no = i;
	  this.info = info;
  }
	

}

public class Queue {
	private static int maxSize=100;
	private Vertex[] data;
	private int front;
	private int rear;
	
	public static int getMaxSize() {
		return maxSize;
	}
	public static void setMaxSize(int maxSize) {
		Queue.maxSize = maxSize;
	}
	public Vertex[] getData() {
		return data;
	}
	public void setData(Vertex[] data) {
		this.data = data;
	}
	public int getFront() {
		return front;
	}
	public void setFront(int front) {
		this.front = front;
	}
	public int getRear() {
		return rear;
	}
	public void setRear(int rear) {
		this.rear = rear;
	}
	public 	Queue(int maxSize){
		data = new Vertex[maxSize];
		front = 0;
		rear = 0;
		
	}
	public static boolean isEmpty(Queue q){
		if (q.front==q.rear){
			return true;
		}
		else return false;
		
	}
	public static void EnQueue(Queue q,Vertex node){
		System.out.print(maxSize);
		if((q.rear+1)%maxSize==q.front){
			System.out.print("队列已经满了");
			return;
			
		}else{
			q.data[q.rear]=node;
			q.rear =( q.rear+1)%maxSize;
		}
		
	}
	public static Vertex DeQueue(Queue q){
		if(isEmpty(q)){
			System.out.print("该队列为空");
			return null;
			
		}
		else{
			Vertex node = q.data[q.front];
			q.front = (q.front+1)%maxSize;
			return node;
		}
		
	}
	

}

public class Client_Graph {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Graph graph = new Graph();
		graph.createGraph();
		graph.dFSTrave();
		graph.bFSTrave();
		

	}

}

输入格式为:a,b,c,d代表四个个顶点

输入矩阵:0 0 1 0

                   0 0 1 1

                    1 1 0 0

                   0 1 0 0

逐个输入

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