初学数据结构(图的遍历)

初学数据结构(图的遍历)

《数据结构教程》p308

exp8-2.cpp(图的两种遍历算法)

//
// Created by Snow on 2023/4/22.
//
#include"travsal.cpp"
#include<cstring>
int main()
{
    AdjGraph *G;
    int A[MAXV][MAXV]={{0,5,0,7,0,0},{0,0,4,0,0,0},{8,0,0,0,0,9},{0,0,5,0,0,6},{0,0,0,5,0,0},{3,0,0,0,1,0}};
    int n=6,e=10;
    CreateAdj(G,A,n,e);
    printf("输出邻接表G:\n");
    DispAdj(G);
    memset(visited,0,sizeof(visited));
    printf("递归DFS算法:\n");
    DFS(G,0);
    memset(visited,0,sizeof(visited));
    printf("\n非递归DFS算法:\n");
    DFS1(G,0);
    memset(visited,0,sizeof(visited));
    printf("BFS算法:\n");
    BFS(G,0);
    printf("销毁邻接表G\n");
    DestroyAdj(G);
}

travsal.cpp

//
// Created by Snow on 2023/4/22.
//
#include"graph.cpp"
int visited[MAXV];

//循环队列
#define MaxSize 100
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q)
{
    q=(SqQueue*)malloc(sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
    free(q);
}
bool QueueEmpty(SqQueue *q)
{
    return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
    if((q->rear+1)%MaxSize==q->front)
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
    if(q->front==q->rear)
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}

//递归DFS
void DFS(AdjGraph *G,int v)
{
    ArcNode *p;
    visited[v]=1;
    printf("%d ",v);
    p=G->adjlist[v].firstarc;
    while(p!=nullptr)
    {
        if(visited[p->adjvex]==0)
            DFS(G,p->adjvex);
        p=p->nextarc;
    }
}

//非递归DFS
void DFS1(AdjGraph *G,int v)
{
    int i;
    ArcNode *p;
    int st[MAXV];
    int top=-1;
    visited[v]=1;
    top++;
    st[top]=v;
    printf("%d ",v);
    while(top>-1)
    {
        i=st[top];
        p=G->adjlist[i].firstarc;
        while(p!=nullptr&&visited[p->adjvex]==1)
            p=p->nextarc;
        if(p==nullptr)
            top--;
        else
        {
            top++;
            st[top]=p->adjvex;
            visited[p->adjvex]=1;
            printf("%d ",p->adjvex);
        }
    }
    printf("\n");
}

//BFS
void BFS(AdjGraph *G,int v)
{
    int w;
    ArcNode *p;
    SqQueue *qu;
    InitQueue(qu);
    printf("%d ",v);
    visited[v]=1;
    enQueue(qu,v);
    while(!QueueEmpty(qu))
    {
        deQueue(qu,w);
        p=G->adjlist[w].firstarc;
        while(p!=nullptr)
        {
            if(visited[p->adjvex]==0)
            {
                printf("%d ",p->adjvex);
                visited[p->adjvex]=1;
                enQueue(qu,p->adjvex);
            }
            p=p->nextarc;
        }
    }
    DestroyQueue(qu);
    printf("\n");
}

exp8-3.cpp(连通图所有的深度优先遍历序列)

//
// Created by Snow on 2023/4/22.
//
#include"graph.cpp"
int visited[MAXV]={0};
void DFS_AllPath(AdjGraph *G,int v,int path[],int d)
{
    int w,i;
    ArcNode *p;
    visited[v]=1;
    d++;
    path[d]=v;
    p=G->adjlist[v].firstarc;
    while(p!=nullptr)
    {
        w=p->adjvex;
        if(d==G->n-1)
        {
            for(i=0;i<G->n;i++)
                printf("%d ",path[i]);
            printf("\n");
            break;
        }
        if(visited[w]==0)
            DFS_AllPath(G,w,path,d);
        p=p->nextarc;
    }
    visited[v]=0;
}
int main()
{
    int path[MAXV];
    AdjGraph *G;
    int A[MAXV][MAXV]={{0,1,0,1,1},{1,0,1,1,0},{0,1,0,1,1},{1,1,1,0,1},{1,0,1,1,0}};
    int n=5,e=16;
    CreateAdj(G,A,n,e);
    printf("输出邻接表G:\n");
    DispAdj(G);
    DFS_AllPath(G,1,path,-1);
    DestroyAdj(G);
}

exp8-4.cpp(深度优先生成树和广度优先生成树)

//
// Created by Snow on 2023/4/22.
//
#include"graph.cpp"
#include<cstring>
int visited[MAXV];

//循环队列
#define MaxSize 100
typedef int ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;
}SqQueue;
void InitQueue(SqQueue *&q)
{
    q=(SqQueue*)malloc(sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q)
{
    free(q);
}
bool QueueEmpty(SqQueue *q)
{
    return(q->front==q->rear);
}
bool enQueue(SqQueue *&q,ElemType e)
{
    if((q->rear+1)%MaxSize==q->front)
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)
{
    if(q->front==q->rear)
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}


void DFSTree(AdjGraph *G,int v)
{
    ArcNode *p;
    visited[v]=1;
    p=G->adjlist[v].firstarc;
    while(p!=nullptr)
    {
        int w=p->adjvex;
        if(visited[w]==0)
        {
            printf("(%d,%d) ",v,w);
            DFSTree(G,w);
        }
        p=p->nextarc;
    }
}
void BFSTree(AdjGraph *G,int v)
{
    SqQueue *qu;
    InitQueue(qu);
    ArcNode *p;
    int i,w;
    visited[v]=1;
    enQueue(qu,v);
    while(!QueueEmpty(qu))
    {
        deQueue(qu,i);
        p=G->adjlist[i].firstarc;
        while(p!=nullptr)
        {
            w=p->adjvex;
            if(visited[w]==0)
            {
                printf("(%d,%d) ",i,w);
                visited[w]=1;
                enQueue(qu,w);
            }
            p=p->nextarc;
        }
    }
    printf("\n");
    DestroyQueue(qu);
}
int main()
{
    AdjGraph *G;
    int A[MAXV][MAXV]={{0,1,1,1,0,0,0,0,0,0,0},
                       {1,0,0,0,1,1,0,0,0,0,0},
                       {1,0,0,1,0,1,1,0,0,0,0},
                       {1,0,1,0,0,0,0,1,0,0,0},
                       {0,1,0,0,0,0,0,0,0,0,0},
                       {0,1,1,0,0,0,0,0,0,0,0},
                       {0,0,1,0,0,0,0,1,1,1,0},
                       {0,0,0,1,0,0,1,0,0,0,1},
                       {0,0,0,0,0,0,1,0,0,0,0},
                       {0,0,0,0,0,0,1,0,0,0,0},
                       {0,0,0,0,0,0,0,7,0,0,0}};
    int n=11,e=26;
    CreateAdj(G,A,n,e);
    printf("输出邻接表G:\n");
    DispAdj(G);
    memset(visited,0,sizeof(visited));
    printf("从顶点3开始的深度优先生成树:\n");
    DFSTree(G,3);
    memset(visited,0,sizeof(visited));
    printf("\n从顶点3开始的广度优先生成树:\n");
    BFSTree(G,3);
    printf("销毁邻接表G\n");
    DestroyAdj(G);
}
posted @ 2023-04-23 10:15  SnowDreamXUE  阅读(8)  评论(0编辑  收藏  举报  来源