Recursive Depth first search graph(adj matrix)
2014-05-18 10:57
288 查看
1 深度优先遍历邻接矩阵
1 邻接矩阵初始化
2 访问数组初始化
3 深度优先遍历邻接矩阵图
算法如下:
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; }
相关文章推荐
- 图——深度优先搜索(Graph - Depth First Search)
- Non recursive Depth first search
- 第十二章 ALDS1_11_B:Depth First Search 深度优先搜索
- Depth-first Search -- Leetcode problem113. Path Sum II
- 部分和问题(Depth-First-Search):
- 深度优先搜索Depth-first search (DFS) for undirected graphs
- 图邻接矩阵的c语言实现 graph_adj_matrix.cpp
- DFS --- Depth First Search 深度优先搜索算法
- [数据结构]深度优先搜索算法(Depth-First-Search,DFS)
- DFS(Depth-First-Search)
- (Basic algorithm学习笔记)《基础算法四》- 深度优先搜索(Depth First Search,DFS )
- Depth-first Search -- Leetcode problem104. Maximum Depth of Binary Tree
- 图——广度优先搜索(Graph - Breadth First Search)
- POJ2225Asteroids![Depth first search]
- 利用DFS拆分数系列;Depth First Search
- 深度优先搜索(Depth First Search)
- Depth-first Search -- Leetcode problem100. Same Tree
- Depth-First-Search(DFS伪代码)
- 深度优先搜索(Depth-First-Search)精髓
- Adjacency List表示下Depth-First-Search(DFS)及Breadth-First-Search(BFS)的c++实现