图的遍历(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 ...
随机推荐
- 关于sed的应用
公司让我做一个看一下在优化的程序和比原来的程序快多少,但是文件还在运行的服务器上,我需要把用到的文件复制到测试服务器上去.但是测试服务器上有的,目录不全,会导致scp出错.就发生了以下的故事. 首选我 ...
- Android之Activity启动的源码简介
从一个简单的startActivity开始 进入了Activity.java public void startActivity(Intent intent) { this.startActivity ...
- 创建和管理表【weber出品必属精品】
创建表 必须有 : 1. CREATE TABLE 的权限 SQL> conn /as sysdba 已连接. SQL> create user test default tablespa ...
- pods的问题处理
在使用pods添加的第三方删除的时候不能直接删除第三方,否则会出现
- poj 1190 DFS 不等式放缩进行剪枝
F - (例题)不等式放缩 Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submi ...
- C++中cin输入类型不匹配解决方法
#include <iostream> #include <set> using namespace std; int main() { int a; cin>>a ...
- Longest Palindromic Substring -LeetCode
题目 Given a string s,find the longest palindromic substring in S.You may assume that the maximum len ...
- VC中窗口ID,句柄,指针三者相互转换函数【转】
ID--HANDLE--HWND三者之间的互相转换id->句柄 hWnd = ::GetDlgItem(hParentWnd,id);id->指针 CWnd:: ...
- Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法
在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...
- Oracle11g R2学习系列 之八高级数据类型
所谓的高级数据类型,就是大数据类型,即BCNB(助记词:BC牛逼)+XML数据类型. B:blob,用来存储可变长度的二进制数据. C:clob,主要用来存储可变长度的字符型数据,也就是其他数据库中提 ...