数据结构-图的创建与遍历

DFS的伪代码描述:
        void DFS(Vertex v)
        {
            visited[v]=true;
        //每调用一次DFS(V),就把V所在的连通分量遍历了一遍
            for(访问v的每个顶点w)
                if(!visited[w]) DFS(w);
        }

BFS的伪代码描述:
void BFS(Vertex V)
        {
            visited[V]=true;
            Enqueue(V,Q);
            while(!IsEmpty(Q)){
                //自己先出队,再将孩子入队
                V=Dequeue(Q);
                for(V的每个邻接点W)
                    if(!visited[W]{
                        visited[W]=true;
                        Enqueue(W,Q);
                    }
            }
        }

下面是邻接表的创建,DFS和BFS算法.

#include<iostream>
#define MAX 100
using namespace std;
//====================邻接表的存储结构======================
struct Node{
    int adjver;
    Node* next;
};
struct verNode
{
    int vertex;
    Node* firstarc;
};
//===================全局变量=======================
verNode graph[MAX];
bool visited[MAX]={0};
//================创建和显示邻接表==========================
int createGraph(verNode* graph)
{
    int num_ver,num_edge,v1,v2;

    cout<<"请输入顶点数与边数:";
    cin>>num_ver>>num_edge;

    //graph=new verNode[num_ver+1];//

    Node* ptr;  //用于临时分配链表结点的指针
    //初始化,将firstart置NULL
    for(int i=1;i<=num_ver;i++)
        graph[i].firstarc=NULL;

    //创建邻接表的链表
    for(int i=1;i<=num_edge;i++){
        cout<<"v1,v2=";
        cin>>v1>>v2;
        ptr=new Node;
        ptr->adjver=v2;
        ptr->next=graph[v1].firstarc;
        graph[v1].firstarc=ptr;
        //无向图
        ptr=new Node;
        ptr->adjver=v1;
        ptr->next=graph[v2].firstarc;
        graph[v2].firstarc=ptr;
    }
    return num_ver; //返回顶点数
}
void show(const verNode* graph,int ver_num)
{
    Node* ptr;
    for(int i=1;i<=ver_num;i++){
        cout<<i;
        ptr=graph[i].firstarc;
        while(ptr!=NULL){
            cout<<"==>"<<ptr->adjver;
            ptr=ptr->next;
        }
        cout<<endl;
    }
}
//===============队列的一些函数=======================
struct Queue{
    int front;
    int rear;
    int queue[MAX];
};
bool IsEmpty(Queue q)
{
    return q.rear==q.front;
}
void initQueue(Queue& q)
{
    q.front=0;
    q.rear=0;
}
void Enqueue(int V,Queue& q)
{
    if(q.rear==MAX){
        cout<<"q is full!"<<endl;
        return;
    }
    q.rear++;
    q.queue[q.rear]=V;
}
int Dequeue(Queue& q)
{
    if(q.rear==q.front){
        cout<<"q is Is Empty!"<<endl;
        return 0;
    }
    q.front++;
    return q.queue[q.front];
}
//===============DFS&BFS=======================
void DFS(int V)
{
    cout<<V<<" ";
    visited[V]=true;
    Node* ptr=graph[V].firstarc;
    while(ptr!=NULL){
        if(!visited[ptr->adjver])
            DFS(ptr->adjver);
        ptr=ptr->next;
    }
}
void BFS(int V)
{
    Queue Q;
    initQueue(Q);

    visited[V]=true;
    cout<<V<<" ";
    Enqueue(V,Q);
    while(!IsEmpty(Q)){
        //自己先出队,再将孩子入队
        V=Dequeue(Q);
        Node* ptr=graph[V].firstarc;
        //V的每个邻接点ptr->adjver入队
        while(ptr!=NULL){
            if(!visited[ptr->adjver]){
                cout<<ptr->adjver<<" ";
                visited[ptr->adjver]=true;
                Enqueue(ptr->adjver,Q);
            }
            ptr=ptr->next;
        }
    }
}
//======================================
int main()
{
    //verNode* graph;//
    int ver_num=createGraph(graph);//
    show(graph,ver_num);
    DFS(1);cout<<endl;
    for(int i=0;i<MAX;i++)  visited[i]=0;
    BFS(1);cout<<endl;
    return 0;
}

下面是邻接矩阵的BFS和DFS算法.

#include<iostream>
#define MAX 100
using namespace std;
int graph[MAX][MAX];
int visited[MAX]={0};
//====================================================
int createGraph()
{
    int ver_num,edge_num,v1,v2;
    cout<<"请输入顶点数和边数:";
    cin>>ver_num>>edge_num;
    for(int i=1;i<=ver_num;i++)
        for(int j=1;j<=ver_num;j++)
            graph[i][j]=0;
    for(int i=1;i<=edge_num;i++){
        cout<<"v1,v2=";
        cin>>v1>>v2;
        graph[v1][v2]=1;
        graph[v2][v1]=1;
    }
    return ver_num;
}
void Show(int graph[][MAX],int ver_num)
{
    for(int i=1;i<=ver_num;i++){
        for(int j=1;j<=ver_num;j++)
            cout<<graph[i][j]<<'\t';
        cout<<endl;
    }
    cout<<endl;
}
//===============队列的一些函数=======================
struct Queue{
    int front;
    int rear;
    int queue[MAX];
};
bool IsEmpty(Queue q)
{
    return q.rear==q.front;
}
void initQueue(Queue& q)
{
    q.front=0;
    q.rear=0;
}
void Enqueue(int V,Queue& q)
{
    if(q.rear==MAX){
        cout<<"q is full!"<<endl;
        return;
    }
    q.rear++;
    q.queue[q.rear]=V;
}
int Dequeue(Queue& q)
{
    if(q.rear==q.front){
        cout<<"q is Is Empty!"<<endl;
        return 0;
    }
    q.front++;
    return q.queue[q.front];
}
//==============邻接矩阵的DFS和BFS=================================
void DFS(int graph[][MAX],int V,int ver_num)
{
    cout<<V<<" ";
    visited[V]=1;
    for(int i=1;i<=ver_num;i++){
        if(graph[V][i]==1&&visited[i]==0)
            DFS(graph,i,ver_num);
    }   
}
void BFS(int graph[][MAX],int V,int ver_num)
{
    Queue Q;
    initQueue(Q);

    cout<<V<<" ";
    visited[V]=1;
    Enqueue(V,Q);
    while(!IsEmpty(Q)){
        V=Dequeue(Q);
        for(int i=1;i<=ver_num;i++){
            if(graph[V][i]==1)
                if(!visited[i]){
                    cout<<i<<" ";
                    visited[i]=true;
                    Enqueue(i,Q);
                }
        }
    }

}
//=======================================================
int main()
{
    int ver_num=createGraph();
    Show(graph,ver_num);
    DFS(graph,1,ver_num);cout<<endl;
    for(int i=0;i<MAX;i++)  visited[i]=0;
    BFS(graph,1,ver_num);cout<<endl;
}
    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/vinater/article/details/47848161
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞