基于邻接矩阵实现的DFS深度优先搜索
来源:互联网 发布:网络交易平台有哪些 编辑:程序博客网 时间:2024/06/10 00:06
这里假设有这样的有向图
我们要对他进行深度优先搜索(基于邻接矩阵)
邻接矩阵的意思就是用矩阵的形式来表示图中顶点直接的邻接关系
如图所示我们可以得到矩阵如下
假设矩阵名字为matrix
matrix[3][4]的值表示的就是从顶点3到顶点4有向边的存在关系
深度搜索的含义就是说
/**
* 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,直到所有的点全都被访问
*
*/
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深度优先搜索
- 深度优先搜索 DFS(邻接矩阵表示)
- 图的广度优先搜索(BFS)、深度优先搜索(DFS)(邻接矩阵法和邻接表法java实现)
- 邻接矩阵的深度优先搜索
- 深度优先搜索_基于邻接矩阵
- 深度优先搜索(DFS)的实现
- 图的基本操作:图的创造(基于邻接矩阵)、深度搜索(DFS)、广度搜索(DFS)
- 邻接矩阵实现深度优先搜索,广度优先搜索
- 图的深度优先搜索(邻接矩阵)
- 邻接矩阵表示的深度优先搜索
- 图的深度优先搜索(邻接矩阵)
- [数据结构] 图的邻接矩阵深度优先搜索
- 深度优先搜索DFS——图邻接矩阵表示
- 深度优先搜索遍历(DFS)——邻接矩阵存储
- Android Fresco图片处理库用法API英文原文文档3(Facebook开源Android图片库)
- 暴力swap导致TLE问题解决办法
- easyui tree 单击文字展开/折叠
- Android自定义属性与自定义属性的获取
- SICP学习小结
- 基于邻接矩阵实现的DFS深度优先搜索
- 程序员高效率工作工具推荐(必备工具)
- 历届试题 波动数列
- 数据包发送流程
- Modelsim与Quartus ii联合仿真的一些问题
- memset的正确使用
- zoj 3865 Superbot(BFS)
- 历届试题 地宫取宝
- FTP 批量上传文件和文件夹 自动递归