深度优先遍历与广度优先遍历

代码一修改就乱了,CSDN这方面还有等提高啊~~~~~~~
//[email protected]
#include <iostream>
#include <cstdlib>

#define MAX_NUM 4

using std::cout;
using std::cin;
using std::endl;

typedef struct ArcNode{
    int adjvex;
    struct ArcNode *next;
}ArcNode;

typedef struct VexNode{
    int data;
    ArcNode * FirstArc;
}VexNode;

int visited[MAX_NUM];

typedef struct QNode{
    int data;
    struct QNode *next;
}QNode, *QueuePtr;

typedef struct{
    QueuePtr front;
    QueuePtr rear;
}LinkQueue;

void InitQueue(LinkQueue *&q){
    q = (LinkQueue *)malloc(sizeof(LinkQueue));
    q->front=q->rear=(QueuePtr)malloc(sizeof(struct QNode));
    if(!q->front)
        exit(0);
    q->front->next=NULL;
}

bool empty(LinkQueue *&q){
    return q->front==q->rear;
}

void enqueue(LinkQueue *&q, int e){
    QueuePtr node = (QueuePtr)malloc(sizeof(QNode));
    if( !node )
        exit(0);
    node->data = e;
    node->next=NULL;
    q->rear->next=node;
    q->rear = node;
}

void dequeue(LinkQueue *&q, int &e){
    QueuePtr node;
    if(empty(q))
        return;
    node = q->front->next;
    e = node->data;
    q->front->next=node->next;
    if( !node->next )   //这个地方要注意,队列空时,尾指针要指向“头结点” 
        q->rear = q->front;
    free(node);
}

void CreatGraph(VexNode G[], int n)
{
    int i, e;
    ArcNode *p, *q = NULL;
    cout<<"Input "<<MAX_NUM<<" vertexs' value:"<<endl;
    for(i=0; i<n; i++){
        cin>>G[i].data; //用scanf("%d",G[i])居然也可以
        G[i].FirstArc = NULL;
    }
    for(i=0; i<n; i++){
        cout<<"Creat the edges for the NO."<<i<<" vertex, input its adjvex..."<<endl;
        cin>>e;
        while(e != 1111){ //为什么以-1为结束判断标志不行... 
            p=(ArcNode *)malloc(sizeof(struct ArcNode));
            p->next = NULL;
            p->adjvex = e;
            if(G[i].FirstArc == NULL)
                G[i].FirstArc = p;
            else 
                q->next = p;
            q = p;
            cin>>e;
        }
    }
}

int FirstAdj(VexNode G[],int v){
    if(G[v].FirstArc != NULL)
        return (G[v].FirstArc)->adjvex;
    return -1;
}

int NextAdj(VexNode G[], int v){
    ArcNode *p;
    p = G[v].FirstArc;
    while( p!= NULL ){
        if( !visited[p->adjvex] )
            return p->adjvex;
        else
            p = p->next;
    }
    return -1;
}

void DFS(VexNode G[], int v){
    int w;
    
    visited[v]=1;
    cout<<G[v].data<<"->";

    for(w=FirstAdj(G, v); w != -1; w=NextAdj(G, v))
    {
        if( visited[w]==0 )
            DFS(G, w);
    }
}

void Travel_DFS(VexNode G[], int n)
{
    for(int i=0;i<n;i++){
        visited[i] = 0;
    }
    for(int i=0; i<n; i++)
        if(visited[i] == 0)
            DFS(G,i);
}

void BFS(VexNode G[], int v)
{
    LinkQueue *q ; //(LinkQueue *)malloc(sizeof(LinkQueue));
    int flag, w;
    
    InitQueue(q);
    cout<<G[v].data<<"->";
    visited[v]=1;
    enqueue(q, v);
    
    while(!empty(q)){
        dequeue(q, v);
        w = FirstAdj(G, v);
        while(w != -1){
            if(visited[w]==0){
                cout<<G[w].data<<"->";
                enqueue(q,w);
                visited[w]=1;
            }
            w = NextAdj(G, v);
        }
    }
}

void Travel_BFS(VexNode G[], int n)
{
    for(int i=0;i<n;i++){
        visited[i] = 0;
    }
    for(int i=0; i<n; i++)
        if(visited[i] == 0)
            BFS(G,i);
}

int main(){
    VexNode G[MAX_NUM];
    CreatGraph(G, MAX_NUM);
    cout<<"DFS:"<<endl;
    //DFS(G, 0);
    Travel_DFS(G,MAX_NUM);  //深度优先遍历 
    cout<<"NULL"<<endl;
    
    cout<<"BFS:"<<endl;
    //BFS(G, 0);  //此时调用,之前深度遍历时已将visited[]全变为1 
    Travel_BFS(G,MAX_NUM);  //广度优先遍历 
    
    cout<<"NULL"<<endl;
    
    system("PAUSE");
    return 0;
}
采用邻接链表表示法,n个顶点,e条边,二种遍历方式时间复杂度相同,首先是尝试从每个顶点遍历一遍,为O(n),然后查找邻接边相连的顶点,为O(e),总时间复杂度为O(n+e)。若采用邻接矩阵表示法,则时间复杂度为O(N^2)。

你可能感兴趣的:(深度优先遍历与广度优先遍历)