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)的更多相关文章

  1. Non recursive Depth first search

    深度优先非递归实现算法: 1 递归算法: //初始化相关数据结构 DFS(G) ------------------------------------------------------------ ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 【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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. LeetCode Search a 2D Matrix II

    原题链接在这里:https://leetcode.com/problems/search-a-2d-matrix-ii/ Write an efficient algorithm that searc ...

随机推荐

  1. mysql 函数执行权限

    mysql> show grants for query_all@'115.236.1x0.x'; +---------------------------------------------- ...

  2. 导出csv文件代码示例

    //当数据量达到一定级别后(大概60000以上),excel会溢出,不能全部显示,而新版的csv好像不会出现这个问题.为什么用好像,我也是听别人说,暂时没有去验证. <?php $sql = & ...

  3. 在XAML代码中为节点树安装事件监听器

    通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系. <Window x:Class="Demo002.MainW ...

  4. SDWebImage缓存

    缓存图片方法 [[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey]; 读取缓存 UIImage *myCache ...

  5. 一、Cocos2dx在visualStudio或者vc++中环境搭建(入门篇)

    本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=106 0.概述 Cocos2dx-win32的项目能够被向导生成 向导支持vs2008,vs2010 ...

  6. C#线程(一)

    本篇作为自己简要的笔记和整理,自己英语略渣  就不翻译了 参考http://www.albahari.com/threading 这里有翻译的http://www.cnblogs.com/miniwi ...

  7. linux下静态链接库的用法

    最近在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于unde ...

  8. [LeetCode]题解(python):127-Word Ladder

    题目来源: https://leetcode.com/problems/word-ladder/ 题意分析: 和上一题目类似,给定一个beginWord和一个endWord,以及一个字典list.这题 ...

  9. python的虚拟运行环境

    Python 虚拟环境:Virtualenv 博客分类: Python python 在进行python开发的时候避免不同版本python或python不同版本组件之间的冲突, 有必要配置python ...

  10. jQuery File Upload

    jQuery File Upload介绍.............................................. 2 实现基本原理......................... ...