理论:

深度优先搜索(Depth_Fisrst Search)遍历类似于树的先根遍历,是树的先根遍历的推广:

广度优先搜索(Breadth_First Search) 遍历类似于树的按层次遍历的过程:

java实现

Vertex.java

package 图;

public class Vertex{
String value;
boolean isVisited;
Vertex(String value)
{
this.value=value;
this.isVisited=false;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public boolean isVisited() {
return isVisited;
}
public void setVisited(boolean isVisited) {
this.isVisited = isVisited;
} }

Edge.java

package 图;

public class Edge{
Vertex start;
Vertex end;
int value;
public Vertex getStart() {
return start;
} public void setStart(Vertex start) {
this.start = start;
} public Vertex getEnd() {
return end;
} public void setEnd(Vertex end) {
this.end = end;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} Edge(Vertex start,Vertex end, int value){
this.start=start;
this.end=end;
this.value=value;
}
}

Graph.java

package 图;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Stack; public class Graph { public static List<Vertex> vertexList=new ArrayList<Vertex>();
public static List<Edge> EdgeQueue=new ArrayList<Edge>();public static List<Vertex> depthVertexQueue=new ArrayList<Vertex>();
public static List<Vertex> breathVertexQueue=new ArrayList<Vertex>(); public static void buildGraph(){
Vertex a=new Vertex("a");
vertexList.add(a);
Vertex b=new Vertex("b");
vertexList.add(b);
Vertex c=new Vertex("c");
vertexList.add(c);
Vertex d=new Vertex("d");
vertexList.add(d);
Vertex e=new Vertex("e");
vertexList.add(e);
Vertex f=new Vertex("f");
vertexList.add(f); addEdge(a,b,0);
addEdge(a,c,0);
addEdge(b,d,0);
addEdge(b,e,0);
addEdge(c,f,0); } public static void addEdge(Vertex start,Vertex end,int value){
Edge e=new Edge(start,end,value);
EdgeQueue.add(e);
} public static Vertex getFirstUnvisitedNeighbor(Vertex origin){ Vertex unvisitedNeighbor=null;
Iterator<Edge> iterator=EdgeQueue.iterator();
while(iterator.hasNext())
{
Edge edge=iterator.next();
if(edge.getStart()==origin)
{
if(!edge.getEnd().isVisited)
{
unvisitedNeighbor=edge.getEnd();
break;
} }
}
return unvisitedNeighbor;
} public static void depthFirstVisit(Vertex origin){
if(origin==null)
return;
depthVertexQueue.add(origin);
origin.setVisited(true); Vertex curVertex=origin;
Stack<Vertex> stack=new Stack<Vertex>();
stack.add(curVertex); while(!stack.isEmpty())
{
curVertex=stack.peek();
Vertex tempVertex=getFirstUnvisitedNeighbor(curVertex);
if(tempVertex!=null)
{
depthVertexQueue.add(tempVertex);
tempVertex.setVisited(true);
stack.push(tempVertex);
}
else
{
stack.pop();
}
} } public static void breathFirstVisit(Vertex origin){
if(origin==null)
return; breathVertexQueue.add(origin);
origin.setVisited(true); List<Vertex> list=new ArrayList<Vertex>();
Vertex curVertex=origin;
list.add(curVertex);
while(!list.isEmpty())
{
curVertex=list.remove(0);
while(getFirstUnvisitedNeighbor(curVertex)!=null)
{
Vertex tempVertex=getFirstUnvisitedNeighbor(curVertex);
breathVertexQueue.add(tempVertex);
tempVertex.setVisited(true);
list.add(tempVertex);
}
} } public static void main(String[] args) {
buildGraph();
depthFirstVisit(vertexList.get(0));
for(Vertex each:depthVertexQueue)
System.out.print(each.getValue()+" ");
}
}

图的遍历(DFS、BFS)的更多相关文章

  1. 图的遍历[DFS][BFS]

    #include<iostream> #include<iostream> #include<cstring> #include<queue> #inc ...

  2. 图的数据结构的实现与遍历(DFS,BFS)

    //图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public:    MGraph(T a[], ...

  3. 图的遍历DFS

    图的遍历DFS 与树的深度优先遍历之间的联系 树的深度优先遍历分为:先根,后根 //树的先根遍历 void PreOrder(TreeNode *R){ if(R!=NULL){ visit(R); ...

  4. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  5. PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]

    题目 One way that the police finds the head of a gang is to check people's phone calls. If there is a ...

  6. 图的遍历——DFS

    原创 图的遍历有DFS和BFS两种,现选用DFS遍历图. 存储图用邻接矩阵,图有v个顶点,e条边,邻接矩阵就是一个VxV的矩阵: 若顶点1和顶点5之间有连线,则矩阵元素[1,5]置1,若是无向图[5, ...

  7. 图的遍历——DFS(矩形空间)

    首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...

  8. 图的遍历 | 1076 bfs

    bfs踩了很多坑才写完.注意:出队时不做是否vis判断,但是要加上vis[出队顶点]=1 .入队时进行判断,并且也要 vis[入队顶点]=1 #include <stdio.h> #inc ...

  9. 图的遍历(bfs 和dfs)

    BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...

  10. PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]

    题目 Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and ...

随机推荐

  1. php中对MYSQL操作之批量运行,与获取批量结果

    <?php //批量运行,与获取结果 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysqlusername". ...

  2. 分享几个社交类网站常用并且也是最好用的jquery类库

    官网都有详细的文档说明,大家自行百度谷歌哈! artZoom:常用于微博,支持图片放大缩小旋转 AutoComplete:自动完成 BackTop:当内容多时出现“返回顶部” CFUpload:批量上 ...

  3. AES算法简介

    AES算法简介 一. AES的结构 1.总体结构 明文分组的长度为128位即16字节,密钥长度可以为16,24或者32字节(128,192,256位).根据密钥的长度,算法被称为AES-128,AES ...

  4. 页面全部加载完毕和页面dom树加载完毕

    dom树加载完毕 $(document).ready()//原生写法document.ready = function (callback) {            ///兼容FF,Google   ...

  5. line-hight-(行高)解析

    行高定义:line-height属性是指文本行基线之间的距离. 顶线.中线.基线.底线概念 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-alig ...

  6. js 操作剪切板

    1.IE浏览器 window.clipboardData: setData() //设置值 getData()//获取值 clearData()//删除值 /******* ** IE 浏览器下支持w ...

  7. 0301——SearchController

    创建显示的页面 SearchViewController * searchVC = [[SearchViewController alloc]init]; 告诉搜索控制器将结果显示在创建的页面上 se ...

  8. Objective-C 字典、可变字典

    字典相当于c++ stl中的map 字典NSDictionary #import <UIKit/UIKit.h> #import "AppDelegate.h" int ...

  9. 6、统计solr目录索引信息

    package com.main.java.solr.statistics; import org.apache.lucene.document.Document; import org.apache ...

  10. JavaScript高级程序设计(学习笔记)

    第13章 事件 一.事件 1.1事件冒泡:事件发生时从里面向外传播   如:div>body>html>document 1.2事件捕获:事件发生时从外层向里层传播   如  doc ...