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;
}
}
}