您的位置:首页 > 其它

图的深度优先搜索和广度优先搜索

2016-07-26 16:42 281 查看
图见下:



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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: