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. 【高级】C++中虚函数机制的实现原理

    多态是C++中的一个重要特性,而虚函数却是实现多态的基石.所谓多态,就是基类的引用或者指针可以根据其实际指向的子类类型而表现出不同的功能.这篇文章讨论这种功能的实现原理,注意这里并不以某个具体的编译器 ...

  2. 双线服务器和CDN的区别

    双线机房是在国内这样特殊的网络环境下特有的产物,双线机房只能解决网通和电信访问瓶颈的问题,网络传输中的链路问题和其它各ISP互通问题还是没有得到解决. CDN的原理是边缘访问,访问者就近取数据,这样大 ...

  3. UIWindow 详解

    UIWindow对象是所有UIView的根视图,管理和协调的应用程序的显示.分发事件给View.UIWindow类是UIView的子类,可以看作是特殊的UIView.一般应用程序只有一个UIWindo ...

  4. elk 日志处理的一点思路

    zjtest7-frontend:/usr/local/logstash-2.3.4/bin# ./logstash -f ../config/logstash_agent.conf zjtest7- ...

  5. 关于js封装框架类库之DOM操作模块(一)

    在前端开发的过程中,javascript极为重要的一个功能就是对DOM对象的操作,而对其封装就是为了更好地进行DOM操作,提高浏览器的支持效率 现在给出一个案例:页面创建三个div,然后给其添加样式 ...

  6. Lowest Common Ancestor of a Binary Tree, with Parent Pointer

    Given a binary tree, find the lowest common ancestor of two given nodes in tree. Each node contains ...

  7. Problem C: Andy's First Dictionary

    Problem C: Andy’s First DictionaryTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 18 Solved: 5[Submit] ...

  8. C++对象模型3--无重写的单继承

    C++对象模型中加入单继承 不管是单继承.多继承,还是虚继承,如果基于“简单对象模型”,每一个基类都可以被派生类中的一个slot指出,该slot内包含基类对象的地址.这个机制的主要缺点是,因为间接性而 ...

  9. python实现进度条

    先说一下文本系统的控制符: \r: 将光标移动到当前行的首位而不换行: \n: 将光标移动到下一行,并不移动到首位: \r\n: 将光标移动到下一行首位. 环境: root@ubuntu16:/ale ...

  10. 帝国cms7.2自定义列表建立tag效果 代码 教程

    统计记录:(如:select count(*) as total from phome_ecms_news where classid=1 and checked=1) 注:这句SQL的意思是查找统计 ...