理论:

深度优先搜索(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. 分享一个option样式传递给select当前选中样式

    今天遇到一个很是纠结的问题,需求又改了!原生的select给option加样式,结果发现select选中仍是默认样式,如下图:

  2. Linux基本操作 1-----命令行BASH的基本操作

    1 Shell(壳)是用户与操作系统底层(通常是内核)之间交互的中介程序,负责将用户指令.操作传递给操作系统底层 shell 分为两种 CUI : Command Line Interface Lin ...

  3. Android自定义控件(四)——让每一个Activity UI都具有弹性

    前面我们已经介绍了如何让你的ScrollView,ListView具有弹性, 今天,我们在前面的基础上,做一下适当的修改,让那些既不是ScrollView,也不是ListView的Activity页面 ...

  4. 代码实现Layout android:layout_alignParentRight

    代码实现Layout android:layout_alignParentRight 例如: android:id="@+id/account_option" android:la ...

  5. Word文档分割总结

    Word文档分割总结 方法: 1. word创建子文件实现文件分割 2. VBA实现 3. 网上分割合并的插件软件 一. word创建子文件实现文件分割 打开需要分割的文件 >> 视图 & ...

  6. mongodb.open失效导致访问地址404

    今天做编辑文章功能的时候发现一个问题,编辑并保存完成后再次跳转到当前文章所在的地址,结果报404,打断点发现查询数据库的时候mongodb.open方法失效.百度后找到了原因: 编辑保存的时候打开了数 ...

  7. Javascript基础form表单

    <!DOCTYPE HTML> <html> <head> <script type="text/javascript" charset= ...

  8. Markdown 学习笔记: Basics

    Markdown 学习笔记: Basics 原文:Basics. 了解Markdown格式化句法的要点 本页对如何使用Markdown提供了一个简单的概述.在"句法"页中对Mark ...

  9. ORACLE函数详解【weber出品】

    一.什么是函数 一个函数: 1. 是命名的PL/SQL块,必须返回一个值 2. 可以存储到数据库中重复执行 3. 可以作为表达式的一部分或者提供一个参数值 二.创建函数的语法 必须至少有一个返回值,创 ...

  10. C#创建Windows服务的几个注意事项

    1.服务安装后的自动启动:服务的StartType即使配置成Automatic,在首次安装成功之后还是要在服务列表中找到并手工启动.此外,可以通过在ProjectInstaller中添加AfterIn ...