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 ...
随机推荐
- 工作学习笔记——GDI泄露检测利器
用.Net写的地图编辑器,最近在一个长时间使用的策划手里频繁挂掉.定位到原因应该是GDI泄露.但在几千行代码里手工寻找泄漏点实在是有些困难,直到在网上找到了这个检测GDI泄露的工具GDILeaks.它 ...
- C模块回调Lua函数的两种方法
作者:ani_di 版权所有,转载务必保留此链接 http://blog.csdn.net/ani_di C模块回调Lua函数的两种方法 lua和C通过虚拟栈这种交互方式简单而又可靠,缺点就是C做栈平 ...
- 学习pthreads,给线程传递多个參数
上篇博文中.boss线程给其它线程传递的仅仅有一个參数,那么假如是多个參数呢?怎么传递呢?也许你会有这种疑问,带着这个疑问,我们进入本文的世界,这里传递多个參数,採用结构体,为什么呢?由于结构体里能够 ...
- Swift中类的初始化器与继承
初始化是类,结构体和枚举类型实例化的准备阶段.这个阶段设置这个实例存储的属性的初始化数值和做一些使用实例之前的准备以及必须要做的其他一些设置工作. 通过定义构造器(initializers)实现这个实 ...
- setInterval()与clearInterval()的用法
setInterval() 方法可按照指定的周期来调用函数或计算表达式. --简单地说就是过一段时间调用一次该函数 setInterval() 方法会不停地调用函数,直到 clearInterval ...
- Javascript 递归函数
递归函数就是在函数内部调用它自己.在Javascript 中有很多写法,值得我们学习一下(Javascript太灵活了).还是用n的 阶乘 来写例子吧. 1. 首先,来看一个最普通 最正常的写法. f ...
- zoj 1366 Cash Machine
01背包加变形 动态规划的时候就犯浑了,每个状态都要记录的,我却只记录了当前状态的!! #include<stdio.h> #include<string.h> int max ...
- ajax联动
1.编写html代码,引入ajax文件 <script type="text/javascript" src="js/ajax.js"></s ...
- 编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。
package cn.itcast; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...