图的广度优先搜索
来源:互联网 发布:linux less 查找字符 编辑:程序博客网 时间:2024/05/28 15:19
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;}}}
0 0
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的 广度优先 搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的广度优先搜索
- 图的深度优先搜索和广度优先搜索模板
- 图的深度优先搜索与广度优先搜索
- 图的广度优先搜索和深度优先搜索
- 图的深度优先搜索与广度优先搜索
- 静态成员应用
- Belady奇异现象和Thrashing抖动现象的比较
- poj 2516 Minimum Cost(最小费用最大流)
- Node学习笔记——eclipse配置nodejs开发环境
- 读取用户输入的内容(read命令的使用)
- 图的广度优先搜索
- 白话机器学习算法(十六)HMM 隐马尔科夫链
- hdu 1027 STL next_permutation
- Tomcat性能调优
- NYOJ517 最小公倍数
- GoogleMap 使用心得
- stl-set
- 23种设计模式(3):抽象工厂模式
- Android-广播笔记