图的遍历(DFS、BFS)
理论:

深度优先搜索(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)的更多相关文章
- 图的遍历[DFS][BFS]
#include<iostream> #include<iostream> #include<cstring> #include<queue> #inc ...
- 图的数据结构的实现与遍历(DFS,BFS)
//图的存储结构:const int MAXSIZE = 10;//邻接矩阵template<class T>class MGraph {public: MGraph(T a[], ...
- 图的遍历DFS
图的遍历DFS 与树的深度优先遍历之间的联系 树的深度优先遍历分为:先根,后根 //树的先根遍历 void PreOrder(TreeNode *R){ if(R!=NULL){ visit(R); ...
- 图的遍历——DFS和BFS模板(一般的图)
关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...
- 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 ...
- 图的遍历——DFS
原创 图的遍历有DFS和BFS两种,现选用DFS遍历图. 存储图用邻接矩阵,图有v个顶点,e条边,邻接矩阵就是一个VxV的矩阵: 若顶点1和顶点5之间有连线,则矩阵元素[1,5]置1,若是无向图[5, ...
- 图的遍历——DFS(矩形空间)
首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...
- 图的遍历 | 1076 bfs
bfs踩了很多坑才写完.注意:出队时不做是否vis判断,但是要加上vis[出队顶点]=1 .入队时进行判断,并且也要 vis[入队顶点]=1 #include <stdio.h> #inc ...
- 图的遍历(bfs 和dfs)
BFS的思想: 从一个图的某一个顶点V0出发,首先访问和V0相邻的且未被访问过的顶点V1.V2.……Vn,然后依次访问与V1.V2……Vn相邻且未被访问的顶点.如此继续,找到所要找的顶点或者遍历完整个 ...
- 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 ...
随机推荐
- Sonar入门(五):使用 Sonar 进行代码质量管理
Sonar 概述 Sonar 是一个用于代码质量管理的开放平台.通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具. 与持续集成工具(例如 Hudson/Jenkins ...
- QT正则表达式---针对IP地址
判断合法IP的QT正则表达式: bool IsIPaddress(QString ip) { QRegExp rx2("(//d+)(//.)(//d+)(//.)(//d+)(//.)(/ ...
- MS-SQL数据库备份方法
一.手动备份 打开企业管理器 --> 右键点击需要备份的数据库 --> 所有任务 --> 备份数据库 或者: 查询分析器: use master backup database 数 ...
- nginx和apache的特点优点和使用场景
Apache Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. Apache源于 ...
- OD调试4--去除NAG窗口
OD调试4--去除NAG窗口 nag本意是烦人的意思,nag窗口是软件设计者用来时不时提醒用户购买正版的警告窗口.软件设计者可能认为当用户忍受不了试用版中的这些烦人的窗口时,就会考虑购买正式版本. 一 ...
- linus用的是哪个桌面?
- 4 常量类--Map常量
public static final HashMap<String, String> ETL_SOURCE_INPUTTYPE_MAP = new HashMap<String, ...
- C语言中的sizeof和strlen
1.sizeof是算符,strlen是函数: 2.sizeof可以用类型做参数,strlen只能用char*做参数,且必须是以''\0''结尾的: 3.大部分编译程序,在编译的时候就把sizeof计算 ...
- Tornado模块分类和各模块之间的关系
1. Core web framework tornado.web — 包含web框架的大部分主要功能,包含RequestHandler和Application两个重要的类 tornado.https ...
- django post方法不能提交
def login(request): if request.method == 'GET': c = {} c.update(csrf(request)) return render_to_resp ...