图的深度优先搜索和广度优先搜索
2016-07-26 16:42
281 查看
图见下:
![](http://img.blog.csdn.net/20160726163808569)
c++代码:对矩阵的操作比较简单,现在将矩阵转化成邻接链表:
![](http://img.blog.csdn.net/20160726164210891)
c++代码:对矩阵的操作比较简单,现在将矩阵转化成邻接链表:
#include <iostream> #include <queue> #include <stack> using namespace std; //0:表示不连通 //1:表示连通 //无自环路 const int graph_arr[11][11] = { {0,1,0,0,0,0,1,0,0,1,1},//V0 {1,0,1,0,0,1,1,0,0,0,1},//V1 {0,1,0,1,1,0,0,0,1,0,0},//V2 {0,0,1,0,1,0,0,0,0,0,0},//V3 {0,0,1,1,0,1,0,0,0,0,0},//V4 {0,1,0,0,1,0,0,0,0,0,0},//V5 {1,1,0,0,0,0,0,1,0,0,0},//V6 {0,0,0,0,0,0,1,0,1,1,0},//V7 {0,0,1,0,0,0,0,1,0,0,0},//V8 {1,0,0,0,0,0,0,1,0,0,1},//V9 {1,1,0,0,0,0,0,0,0,1,0} //V10 }; bool visited[11] = {false,false,false,false,false,false,false,false,false,false,false}; //bool visited[11] = {false}; struct Node{ int no; //顶点标号 struct Node* next; //与该顶点相连的顶点 Node():no(-1),next(NULL){ } }; struct EdgeNode{ struct Node* first_node;//每个便表集合的头节点,也就是 V0,V1,V2...V10 struct EdgeNode* next; //指向下一个头节点 int nodes_num; EdgeNode():nodes_num(-1),first_node(NULL),next(NULL){ } }; /** * 初始化图的邻接链表 */ EdgeNode* init_graph(){ EdgeNode* graph = (EdgeNode*)malloc(sizeof(struct EdgeNode)); EdgeNode* _graph = graph; for(int i=0;i<11;i++){ Node* node = (Node*)malloc(sizeof(struct Node)); node->no = i; graph->first_node = node; EdgeNode* tmp0 = (EdgeNode*)malloc(sizeof(struct EdgeNode)); graph->next = tmp0; graph->nodes_num = 0; for(int j=0;j<11;j++){ if(graph_arr[i][j] == 1){ Node* tmp = (Node*)malloc(sizeof(struct Node)); tmp->no = j; node->next = tmp; node = tmp; graph->nodes_num ++; } } graph = tmp0; } return _graph; } /** * 邻接链表 BFS * 非递归方式实现 */ void BFS(EdgeNode* graph){ queue<EdgeNode*> que; EdgeNode* tmp = graph; que.push(tmp); while(!que.empty()){ tmp = que.front(); que.pop(); Node* node = tmp->first_node; for(int j=0;j<= tmp->nodes_num;j++){ if(!visited[node->no]){ cout<<"编号:"<<node->no<<endl; visited[node->no] = true; EdgeNode* tmp0 = tmp->next; while(tmp0->next){ if(tmp0->first_node->no == node->no){ que.push(tmp0); break; } tmp0 = tmp0->next; } } node = node->next; } } } /** * 邻接链表 DFS * 递归方式实现 */ void DFS(EdgeNode* graph){ EdgeNode* tmp = graph; Node* node = tmp->first_node; for(int j=0;j<= tmp->nodes_num;j++){ if(!visited[node->no]){ cout<<"编号:"<<node->no<<endl; visited[node->no] = true; EdgeNode* tmp0 = tmp->next; while(tmp0->next){ if(tmp0->first_node->no == node->no){ DFS(tmp0); break; } tmp0 = tmp0->next; } } node = node->next; } } //寻找临接表中某个便表集合中没有被访问的结点 EdgeNode* find_no_visited(EdgeNode* first_node){ EdgeNode* edge_node = first_node; Node* node = first_node->first_node; while(node->no != -1){ cout<<node->no<<endl; if(!visited[node->no]){ while(edge_node){ if(edge_node->first_node->no == node->no){ return edge_node; } edge_node = edge_node->next; } } node = node->next; } cout<<endl; return NULL; } /** * 邻接链表 DFS * 非递归方式实现 */ void DFS0(EdgeNode* graph){ EdgeNode* tmp = graph; stack<EdgeNode*> stk; Node* node = tmp->first_node; stk.push(tmp); cout<<"编号:"<<node->no<<endl; visited[node->no] = true; while(!stk.empty()){ tmp = stk.top(); EdgeNode* no_visited = find_no_visited(tmp); if(no_visited->next!= NULL) { cout<<"编号:"; cout<<no_visited->first_node->no<<endl; visited[no_visited->first_node->no] = true; stk.push(no_visited); }else{ stk.pop(); } } } int main(int argc, char** argv) { // EdgeNode* graph = init_graph(); // visited[0] = true; // visited[1] = true; // visited[2] = true; // visited[3] = true; // visited[4] = true; // visited[5] = true; // visited[6] = true; // cout<<find_no_visited(graph)->first_node->no; DFS(init_graph()); system("pause"); return 0; }
相关文章推荐
- Data truncation: Data too long for column 'contents_o' at row 1 报错原因
- 通过广播启动另一个应用的Activity
- Linux小白-各种实用技巧
- Sql -- 练习3 递归查询
- hdu 2063(二分图最大匹配,匈牙利算法,水题)
- iOS UITextField 输入含小数的数字(位数限制)
- aapt问题
- iOS编程(双语版)-视图-Autolayout代码初步
- HNOI 2012 射箭 半平面交
- 一台服务器部署多个tomcat端口号修改详情记录
- hdu_2444The Accomodation of Students(二分图的判定和计算)
- DNS域名解析
- UITableViewCell - 右滑、左滑等操作
- 配置文件中的别名以及mapper中的namespace
- 转 展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告
- 4D地理空间处理和分析工具
- java 获得本地IP和远程服务器IP
- js运算符知识总结
- Swift weakSelf写法
- InnoDB VS MyISAM