基于邻接矩阵实现的DFS深度优先搜索
2015-04-13 19:22
281 查看
这里假设有这样的有向图
我们要对他进行深度优先搜索(基于邻接矩阵)
邻接矩阵的意思就是用矩阵的形式来表示图中顶点直接的邻接关系
如图所示我们可以得到矩阵如下
假设矩阵名字为matrix
matrix[3][4]的值表示的就是从顶点3到顶点4有向边的存在关系
深度搜索的含义就是说
从图中某顶点v出发:
(1)访问顶点v;
(2)依次从v的未被访问的邻接点出发,对图进行深度优先遍历;直至图中和v有路径相通的顶点都被访问;
(3)若此时图中尚有顶点未被访问,则从一个未被访问的顶点出发,重新进行深度优先遍历,直到图中所有顶点均被访问过为止。
/**
* 1. initial the matrix's value (1 means that two points are connected, 0 means disconnected)
初始化邻接矩阵的值,1表示a[i][j] 中 i 顶点和 j 顶点相连 反之不相连
* 2. set every point is not visited at start (false)
设置所有顶点的初始状态为false未访问状态
* 3. begin to dfsSerach with a start point (add the start point into visitList at first and make it's visit status true)
开始深度优先搜索设置搜索的起始值,讲起始值加入到访问链表中 并将起始值所代表的顶点访问状态改为true表示已经被访问过了
* 4. while current node is connected with the n'th node and the n'th node is not visited before.add the node into the visit List
当当前的顶点与第n个顶点相连数组值为1 并且 第n个顶点为未访问的状态的时候 将第n个顶点加入到访问链表中并且改变其访问状态
* 5. if we get the n'th node, supposed it as a start point and call step 3
第n个顶点找到之后,以第n个顶点为起点,重复步骤3,直到所有的点全都被访问
* @author zero
*
*/
输入的结果为:
The sorted result is : 1 3 0 2 4
我们要对他进行深度优先搜索(基于邻接矩阵)
邻接矩阵的意思就是用矩阵的形式来表示图中顶点直接的邻接关系
如图所示我们可以得到矩阵如下
假设矩阵名字为matrix
matrix[3][4]的值表示的就是从顶点3到顶点4有向边的存在关系
深度搜索的含义就是说
从图中某顶点v出发:
(1)访问顶点v;
(2)依次从v的未被访问的邻接点出发,对图进行深度优先遍历;直至图中和v有路径相通的顶点都被访问;
(3)若此时图中尚有顶点未被访问,则从一个未被访问的顶点出发,重新进行深度优先遍历,直到图中所有顶点均被访问过为止。
/**
* 1. initial the matrix's value (1 means that two points are connected, 0 means disconnected)
初始化邻接矩阵的值,1表示a[i][j] 中 i 顶点和 j 顶点相连 反之不相连
* 2. set every point is not visited at start (false)
设置所有顶点的初始状态为false未访问状态
* 3. begin to dfsSerach with a start point (add the start point into visitList at first and make it's visit status true)
开始深度优先搜索设置搜索的起始值,讲起始值加入到访问链表中 并将起始值所代表的顶点访问状态改为true表示已经被访问过了
* 4. while current node is connected with the n'th node and the n'th node is not visited before.add the node into the visit List
当当前的顶点与第n个顶点相连数组值为1 并且 第n个顶点为未访问的状态的时候 将第n个顶点加入到访问链表中并且改变其访问状态
* 5. if we get the n'th node, supposed it as a start point and call step 3
第n个顶点找到之后,以第n个顶点为起点,重复步骤3,直到所有的点全都被访问
* @author zero
*
*/
import java.util.ArrayList; import java.util.List; /** * 1. initial the matrix's value (1 means that two points are connected, 0 means disconnected) * 2. set every point is not visited at start (false) * 3. begin to dfsSerach with a start point (add the start point into visitList at first and make it's visit status true) * 4. while current node is connected with the n'th node and the n'th node is not visited before.add the node into the visit List * 5. if we get the n'th node, supposed it as a start point and call step 3 * @author zero * */ public class adjMatrixDFS { private List<Integer> visitList = new ArrayList<Integer>(); //initial the matrix's value (1 means that two points are connected, 0 means disconnected) private int[][] matrix = {{1,0,1,0,1},{0,1,0,1,0},{1,1,1,0,1},{1,0,0,1,1},{1,0,0,0,0}}; //set every point is not visited at start private Boolean[] visitStatus = {false,false,false,false,false}; public void dfsSerach(int visitNode) { for(int i=0; i<5; i++) { if((matrix[visitNode][i] == 1 )&&(visitStatus[i]==false)) { visitList.add(i); visitStatus[i] = true; dfsSerach(i); }else { } } } public void setInitNode(int startNode) { visitList.add(startNode); visitStatus[startNode] = true; dfsSerach(startNode); } public void outSortResult() { System.out.print("The sorted result is : "); for(int i=0; i<visitList.size(); i++) { System.out.print(visitList.get(i) + " "); } } public static void main(String[] args) { adjMatrixDFS amdfs = new adjMatrixDFS(); amdfs.setInitNode(1); amdfs.outSortResult(); } }
输入的结果为:
The sorted result is : 1 3 0 2 4
相关文章推荐
- 基于邻接表实现的DFS深度优先搜索
- DFS--深度优先搜索--图的邻接矩阵
- 图的深度优先遍历DFS (邻接矩阵实现) c语言
- DFS--深度优先搜索--图的邻接矩阵
- 22.基于 邻接表 表示的 深度优先搜索dfs 和 广度优先搜索bfs
- 深度优先搜索遍历(DFS)——邻接矩阵存储
- 深度优先搜索DFS(邻接表实现)
- 深度优先搜索DFS-C语言实现、思路/解析-简笔
- 基于深度优先搜索的全排列实现方法
- 图的基本操作:图的创造(基于邻接矩阵)、深度搜索(DFS)、广度搜索(DFS)
- Java实现数据结构之深度优先搜索DFS和广度优先搜索BFS
- Java实现图:邻接矩阵表示、深度优先搜索、广度优先搜索、无向图的最小生成树
- 深度优先搜索_基于邻接矩阵
- 深度优先搜索 DFS(邻接矩阵表示)
- 深度优先搜索DFS——图邻接矩阵表示
- 数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历
- 无向图的邻接矩阵 -- DFS - 深度优先遍历
- DFS深度优先搜索结合剪枝的应用
- DFS深度优先搜索案例:马戏团叠罗汉
- 图的基本操作:图的创造(基于邻接表)、深度搜索(DFS)、广度搜索(DFS)