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 ...
随机推荐
- iOS开发宝典:String用法大全
一.NSString 创建字符串. NSString *astring = @"This is a String!"; 创建空字符串,给予赋值. NSString *astri ...
- javascript的函数相关属性和方法
作为一名前端初学者,应该坚持每天去学习,去总结 ,去复习,去接触更新鲜的事物.但是这段时间很浮躁,虽说也是在一直学习,自己能吸收的少之又少.今日在这突然冒出来,实感惭愧. 1.函数名.name 获得函 ...
- 判断联网wifi
UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@ ...
- Android应用开发提高篇(1)-----获取本地IP
链接地址:http://www.cnblogs.com/lknlfy/archive/2012/02/21/2361802.html 一.概述 习惯了Linux下的网络编程,在还没用智能机之前就一直想 ...
- CRM中直接创建SAP订单的实施方案记录
后台生成订单按钮JS do_alert('正在生成SAP订单,暂停所有操作,同时请不要关闭本窗口!'); var hefid = clientData.urlArgs.entityId; var ur ...
- css3 display:box
想做自适应的流体布局 box很有用 . 还没有得到firefox.Opera.chrome浏览器的完全支持,但可以使用它们的私有属性定义firefox(-moz-).opera(-o-).chrome ...
- codeforces 609F. Frogs and mosquitoes 二分+线段树
题目链接 F. Frogs and mosquitoes time limit per test 2 seconds memory limit per test 512 megabytes input ...
- HTML+CSS笔记 CSS中级 一些小技巧
水平居中 行内元素的水平居中 </a></li> <li><a href="#">2</a></li> &l ...
- 图标字体IcoMoon 使用
IcoMoon 使用官方地址 http://icomoon.io/实际上,它是一种字体,只不过这种字体的字象图标一样,比如windows中自带的MT Extra Webdings Wingdings字 ...
- jQuery validate入门
<html> <head> <meta charset="utf8"></meta> </head> <body& ...