Recursive Depth first search graph(adj matrix)
1 深度优先遍历邻接矩阵
1 邻接矩阵初始化
2 访问数组初始化
3 深度优先遍历邻接矩阵图
算法如下:
bool MGraph[128][128];
bool visit[128];
int vexnum; //num of vertices void dfs(int u){
visit[u] = true; for(int v = 0; v < vexnum ; v++ ){
if( !visit[v] && MGraph[u][v])
dfs(v);
}
source code:
#include <iostream>
#include <stdlib.h>
using namespace std; #define MAX_VERTEX_NUM 128 bool MGraph[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
bool visit[MAX_VERTEX_NUM];
int vexnum; void dfs(int u){
visit[u] = true;
//for print
cout<<u + 1<<" "; for(int v = 0; v < vexnum ; v++ ){
if( !visit[v] && MGraph[u][v]){
dfs(v);
}
}
} int main(){
//initialize M Graph
int i, j, edge_num;
cout<<"enter vertex number and edge number:"<<endl;
cin>>vexnum>>edge_num; //initialize edge of graph
while(edge_num){
cout<<"enter edge:"<<endl;
cin>>i>>j;
MGraph[i-1][j-1] = true;
MGraph[j-1][i-1] = true;
edge_num--;
} for(i = 0; i < vexnum; i++)
visit[i] = false; for(i = 0; i < vexnum ; i++){
if(!visit[i])
dfs(i);
} system("pause");
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
Recursive Depth first search graph(adj matrix)的更多相关文章
- Non recursive Depth first search
深度优先非递归实现算法: 1 递归算法: //初始化相关数据结构 DFS(G) ------------------------------------------------------------ ...
- [Algorithm] Write a Depth First Search Algorithm for Graphs in JavaScript
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- 【leetcode】Search a 2D Matrix
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- Search a 2D Matrix | & II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix, ret ...
- LeetCode Search a 2D Matrix II
原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...
随机推荐
- 如何解决dns解析故障
在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成的.这个现象发生的机率比较 ...
- HDU 1002 A + B Problem II(大整数相加)
A + B Problem II Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- iOS设计模式——MVC(Model-View-Controller)
Modol View Controller(MVC)是一种最早的也是最成功的可重用的设计模式,70年代的时候首次在smaltalk编写的程序中成功使用.基于MVC设计 模式,Cocoa整体架构可以划分 ...
- .NET WIN7+IIS 7.5下URLRewriter组件伪静态设置
原文地址:WIN7+IIS 7.5伪静态的设置 --------------------------------------------------------偶是分割线君-------------- ...
- ELK(ElasticSearch, Logstash, Log4j)系统日志搭建
1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...
- BZOJ 1816: [Cqoi2010]扑克牌( 二分答案 )
二分答案.. 一开始二分的初始右边界太小了然后WA,最后一气之下把它改成了INF... -------------------------------------------------------- ...
- js正则语法
整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$只能输入数字:"^[0-9]*$".只能输入n位的数字:"^\d{n}$".只能输入至少n位的数 ...
- PhpStorm 10.0.1安装(win7-64位)
软件下载地址 http://www.xiazaiba.com/html/25261.html#j_down_list 或者:http://pan.baidu.com/s/1brSA9C 密码:tpc7 ...
- python初探-copy
python中,数据的拷贝有以下三种形式:赋值.浅copy和深copy.根据类型的不同,可以把数据分成以下两类:字符串和数字为一类,其他(包括列表.元祖.字典...)为一类. 在python中有池的概 ...
- 基于Visual C++2013拆解世界五百强面试题--题13-找最大公共子字符串
编程实现:找出两个字符串中最大公共子字符串,如"abccade"和"dgcadde"的最大子字符串为"cad". 如果不考虑效率的话直接比较 ...