学习广度优先搜索(Queue)
来源:互联网 发布:百战天虫java 编辑:程序博客网 时间:2024/05/23 01:12
package com.itheima.graph;/** *用队列实现广度优先搜索 * @author Linchong * */class GraphNode {int val;//标识当前节点GraphNode next;//方便建立队列GraphNode[] neighbors; //存储相邻节点的信息boolean visited; //标识是否访问public GraphNode(int val) {this.val = val;}public GraphNode(int val, GraphNode[] n) {this.val = val;this.neighbors = n;}public String toString() {return "value:"+this.val;}}class Queue {GraphNode first,last;public void enquque(GraphNode n){if(first == null) {first = n;last = first;}else{last.next = n;last = n;}}public GraphNode dequque() {if(first == null) {return null;}else{GraphNode temp = new GraphNode(first.val,first.neighbors);first = first.next;return temp;}}}public class GrapTest {public static void main(String[] args) {GraphNode n1 = new GraphNode(1);GraphNode n2 = new GraphNode(2);GraphNode n3 = new GraphNode(3);GraphNode n4 = new GraphNode(4);GraphNode n5 = new GraphNode(5);n1.neighbors = new GraphNode[]{n2,n3,n5};n2.neighbors = new GraphNode[]{n1,n4};n3.neighbors = new GraphNode[]{n1,n4,n5};n4.neighbors = new GraphNode[]{n2,n5};n5.neighbors = new GraphNode[]{n1,n3,n4};breathFirstSearch(n1,5);}private static void breathFirstSearch(GraphNode root, int x) {if(root.val == x) {System.out.println("find :" + x);return;}Queue queue = new Queue();queue.enquque(root);root.visited = true;while(queue.first != null) {GraphNode c = queue.dequque();for(GraphNode n : c.neighbors) {if(!n.visited) {System.out.print(n+ "\t");if(n.val == x) {System.out.println("find:" + n.val);}n.visited = true;queue.enquque(n);}}}}}
阅读全文
0 0
- 学习广度优先搜索(Queue)
- 广度优先搜索(BFS+STL queue)实现
- 广度优先搜索(bfs)的学习
- 广度优先搜索的学习
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- 路障(广度优先搜索)
- 广度优先搜索(BFS)
- 广度优先搜索(BFS)
- 广度优先搜索(入门)
- BFS(广度优先搜索)
- 【算法学习】二、深度优先搜索与广度优先搜索
- 搜索(广度优先搜索) BFS 1
- 搜索(广度优先搜索)BFS 2
- 算法学习笔记--广度优先搜索(1)
- 深度优先搜索(DFS)&广度优先搜索(BFS)
- 深度优先搜索、广度优先搜索(java)
- 深度优先搜索和广度优先搜索(1)
- jquery中的$(document).ready(function(){})和$(window).load()比较
- 【Sort】75. Sort Colors(计数排序、荷兰国旗问题)
- Chrome谷歌浏览器Flash开启⾃动运⾏指引
- emmet 一些用法
- JAVAEE第15天-static,final关键字,访问权限修饰符,内部类,静态代码块,包的声明和导入
- 学习广度优先搜索(Queue)
- 7.5提取联通区域的轮廓
- Bellman-ford最短路算法
- android中build.gradle文件简单说明
- 构造函数模式扩展
- 关于花生壳动态ip映射的一点唠叨
- 设计模式——小谈单例
- Socket到底是什么?
- 一个页面从输入URL到页面加载显示完成,这个过程都发生什java 基础知识-数组的7种算法(排序、求和、最值、遍历...)么?