图的广度优先搜索

package com.data.struct;

import java.util.Random;

public class Graphic {
	private Node[] list;
	private NodeArrayQueue queue;
	public Graphic(int v,int e){
		list=new Node[v];
		for(int i=0;i<v;i++){
			Node node=new Node();
			node.id=i;
			list[i]=node;
		}
		for(int i=0;i<e;i++){
			int v1=new Random().nextInt(v);
			int v2=new Random().nextInt(v);
			if(v1==v2){
				continue;
			}
			while(true){
				Node node=list[v1];
				boolean already=false;
				while(node.next!=null){
					if(node.next.id==v2){
						already=true;
						break;
					}
					node=node.next;
				}
				if(already==true){
					break;
				}
				Node ex=new Node();
				ex.id=v2;
				node.next=ex;
				break;
			}
		}
		queue=new NodeArrayQueue((v*(v-1)/2)+5);
	}
	public void broadFirstSearch()throws Exception{
		broadFirstSearch(list[0]);
	}
	public void broadFirstSearch(Node s)throws Exception{
		s.color=Node.GRAY;
		s.d=0;
		queue.enqueue(s);
		while(!queue.isEmpty()){
			Node u=queue.dequeue();
			Node v=u.next;
			Node prev=u;
			while(v!=null){
				if(v.color==Node.WHITE){
					v.color=Node.GRAY;
					v.d=prev.d+1;
					v.parent=prev;
					queue.enqueue(v);
				}
				prev=v;
				v=v.next;
			}
			
			u.color=Node.BLACK;
		}
	}
	public void printPath(Node s,Node v){
		if(v==s){
			System.out.print(s.id+"=>");
		}else if(v.parent==null){
			System.out.println();
			System.out.print(v.id+"=>");
		}else {
			printPath(s,v.parent);
			System.out.print(v.id+"=>");
		}
	}
	public void printAllPath(){
		System.out.println("path========");
		Node s=list[0];
		//for(int i=0;i<list.length;i++){
			Node node=s;//list[i];
			while(node.next!=null){
				printPath(s,node.next);
				System.out.println();
				node=node.next;
			}
		//}
	}
	public void print(){
		for(int i=0;i<list.length;i++){
			Node node=list[i];
			System.out.print(node.id+"=>");
			while(node.next!=null){
				System.out.print(node.next.id+"=>");
				node=node.next;
			}
			System.out.println();
		}
		
	}
	public static class Node{
		public static final int WHITE=1;
		public static final int BLACK=2;
		public static final int GRAY=3;
		private int id;
		private Node next;
		private int color=WHITE;
		private int d=Integer.MAX_VALUE;
		private Node parent;
	}
	public static void main(String[] args)throws Exception {
		Graphic g=new Graphic(5,30);
		g.print();
		g.broadFirstSearch();
		g.printAllPath();
		
		
	}

}

class NodeArrayQueue {
	private Graphic.Node []data;
	private int head;
	private int tail;
	private boolean full;
	public NodeArrayQueue(int size){
		data=new Graphic.Node[size];
		head=0;
		tail=0;
	}
	public void enqueue(Graphic.Node d)throws Exception{
		if(head-tail==0&&full||(head==0&&(tail==0)&&full)){
			throw new Exception("full");
		}
		data[tail]=d;
		tail=tail+1;
		if(tail==data.length){
			tail=0;
		}
		if(head==tail){
			full=true;
		}
		
	}
	
	public Graphic.Node dequeue()throws Exception{
		if(head==tail&&!full||(head==data.length&&tail==0)){
			throw new Exception("empty");
		}
		full=false;
		head=head+1;
		if(head==data.length){
			head=0;
			return data[data.length-1];
		}else{
			return data[head-1];
		}
		
	}
	
	public boolean isEmpty(){
		if(head==tail&&!full||(head==data.length&&tail==0)){
			return true;
		}else{
			return false;
		}
	}
	
}

bug修复

package com.data.struct;

import java.util.Random;

public class GraphicBroadFirst {
	private Node[] list;
	private NodeArrayQueue queue;

	public GraphicBroadFirst(int v, int e) {
		list = new Node[v];
		for (int i = 0; i < v; i++) {
			Node node = new Node();
			node.id = i;
			list[i] = node;
		}
		for (int i = 0; i < e; i++) {
			int v1 = new Random().nextInt(v);
			int v2 = new Random().nextInt(v);
			if (v1 == v2) {
				continue;
			}
			while (true) {
				Node node = list[v1];
				boolean already = false;
				while (node.next != null) {
					if (node.next.id == v2) {
						already = true;
						break;
					}
					node = node.next;
				}
				if (already == true) {
					break;
				}
				Node ex = new Node();
				ex.id = v2;
				node.next = ex;
				break;
			}
		}
		queue = new NodeArrayQueue((v * (v - 1) / 2) + 5);
	}

	public void broadFirstSearch() throws Exception {
		broadFirstSearch(list[0]);
	}

	public void broadFirstSearch(Node s) throws Exception {
		s.color = Node.GRAY;
		s.d = 0;
		queue.enqueue(s);
		while (!queue.isEmpty()) {
			Node u = queue.dequeue();
			Node v = u.next;
			Node prev = u;
			while (v != null) {
				if (list[v.id].color == Node.WHITE) {
					list[v.id].color = Node.GRAY;
					list[v.id].d = prev.d + 1;
					list[v.id].parent = prev;
					queue.enqueue(list[v.id]);
				}
				//prev = v;
				v = v.next;
			}

			u.color = Node.BLACK;
		}
	}

	public void printPath(Node s, Node v) {
		if (v == s) {
			System.out.print(s.id + "=>");
		} else if (v.parent == null) {
			System.out.println();
			System.out.print(v.id + "=>");
		} else {
			printPath(s, v.parent);
			System.out.print(v.id + "=>");
		}
	}

	public void printAllPath() {
		System.out.println("path========");
		Node s = list[0];
		// for(int i=0;i<list.length;i++){
		Node node = s;// list[i];
		for(int i=0;i<list.length;i++){
			printPath(list[0],list[i]);
			System.out.println();
		}
		/*while (node.next != null) {
			printPath(s, node.next);
			System.out.println();
			node = node.next;
		}*/
		// }
	}

	public void print() {
		for (int i = 0; i < list.length; i++) {
			Node node = list[i];
			System.out.print(node.id + "=>");
			while (node.next != null) {
				System.out.print(node.next.id + "=>");
				node = node.next;
			}
			System.out.println();
		}

	}

	public static class Node {
		public static final int WHITE = 1;
		public static final int BLACK = 2;
		public static final int GRAY = 3;
		private int id;
		private Node next;
		private int color = WHITE;
		private int d = Integer.MAX_VALUE;
		private Node parent;
	}

	public static void main(String[] args) throws Exception {
		GraphicBroadFirst g = new GraphicBroadFirst(5, 30);
		g.print();
		g.broadFirstSearch();
		g.printAllPath();

	}

}

class NodeArrayQueue {
	private GraphicBroadFirst.Node[] data;
	private int head;
	private int tail;
	private boolean full;

	public NodeArrayQueue(int size) {
		data = new GraphicBroadFirst.Node[size];
		head = 0;
		tail = 0;
	}

	public void enqueue(GraphicBroadFirst.Node d) throws Exception {
		if (head - tail == 0 && full || (head == 0 && (tail == 0) && full)) {
			throw new Exception("full");
		}
		data[tail] = d;
		tail = tail + 1;
		if (tail == data.length) {
			tail = 0;
		}
		if (head == tail) {
			full = true;
		}

	}

	public GraphicBroadFirst.Node dequeue() throws Exception {
		if (head == tail && !full || (head == data.length && tail == 0)) {
			throw new Exception("empty");
		}
		full = false;
		head = head + 1;
		if (head == data.length) {
			head = 0;
			return data[data.length - 1];
		} else {
			return data[head - 1];
		}

	}

	public boolean isEmpty() {
		if (head == tail && !full || (head == data.length && tail == 0)) {
			return true;
		} else {
			return false;
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hxpjava1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值