邻接表表示图(有向、无向图)及广度、深度遍历)

邻接表表示图

#include <iostream>
#include <malloc.h>
#include <queue>
using namespace std;

#define VertexType char
#define MaxVertexNum 20         //最大顶点数

queue<int>Q;                    //辅助队列

enum GraphType{DG, UDG};        //DG表示有向图,UDG表示无向图

typedef struct ArcNode{         //边结点
    int adjvex;                 //邻接点的下标
    struct ArcNode *nextarc;    //后继链指针
}ArcNode;

typedef struct VNode{           //顶点结点
    VertexType data;            //顶点数据
    ArcNode *firstarc;          //边链头指针
}VNode, AdjList[MaxVertexNum];

typedef struct{
     AdjList vertices;           //邻接表
     int vexnum,arcnum;          //顶点数和边数
     GraphType kind;             //图种类标志
}ALGraph;

//建立有向图
void CreateDGGraph(ALGraph &G)
{
    int m, n;                   //边<Vm,Vn>的下标
    ArcNode *vm, *vn;           //构成边<Vm,Vn>的两个顶点
    cout << "请输入顶点数和边数:";
    cin >> G.vexnum >> G.arcnum;
    cout << "请输入顶点的信息:" << endl;

    //获取顶点信息
    for(int i=0; i<G.vexnum; i++)
    {
        cin >> G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }

    //建立邻接表,采用头插法
    for(int i=0; i<G.arcnum; i++)
    {
        cout << "请输入<Vi,Vj>的下标:";
        cin >> m >> n;
        vm=(ArcNode*)malloc(sizeof(ArcNode));
        vm->adjvex=n;

        vm->nextarc=NULL;
        if(G.vertices[m].firstarc==NULL)
        {
            vn=G.vertices[m].firstarc=vm;
        }
        else
        {
            vn=vn->nextarc=vm;
        }
    }
}

//建立无向图
void CreateUDGGraph(ALGraph &G)
{
    int m, n;                  //边<Vm,Vn>的下标
    ArcNode *pe;

    cout << "请输入顶点数和边数:";
    cin >> G.vexnum >> G.arcnum;
    cout << "请输入顶点的信息:" << endl;

    //获取顶点信息
    for(int i=0; i<G.vexnum; i++)
    {
        cin >> G.vertices[i].data;
        G.vertices[i].firstarc=NULL;
    }

    //建立邻接表,采用头插法
    for(int i=0; i<G.arcnum; i++)
    {
        cout << "请输入<Vi,Vj>的下标:";
        cin >> m >> n;
        pe=(ArcNode*)malloc(sizeof(ArcNode));
        pe->adjvex=n;

        pe->nextarc=G.vertices[m].firstarc;
        G.vertices[m].firstarc=pe;

        pe=(ArcNode*)malloc(sizeof(ArcNode));
        pe->adjvex=m;
        pe->nextarc=G.vertices[n].firstarc;
        G.vertices[n].firstarc=pe;
    }
}

void PrintALGraph(ALGraph &G)
{
    ArcNode *p;
	for (int i=0;i<G.vexnum;i++)
	{
		cout << "顶点" << i << "(" << G.vertices[i].data << ") "": ";
		for (p=G.vertices[i].firstarc; p!=NULL;p=p->nextarc)
			cout << p->adjvex << "(" << G.vertices[p->adjvex].data << ") ";
		if (p==NULL)
			cout << endl;
	}
}

//求图中顶点v的第一个邻接点,若有则返回顶点下标。若v没有邻接点或图中不存在v,则返回-1
int FirstNeighbor(ALGraph G, int v)
{
    ArcNode *next=G.vertices[v].firstarc;
    if(next)
    {
        return next->adjvex;
    }
    return -1;
}

//返回除顶点v外的下一个顶点w
int NextNeighbor(ALGraph G, int v, int w)
{
    ArcNode *next=G.vertices[v].firstarc;
    while(next)
    {
        if(next->adjvex==w)break;
        next=next->nextarc;
    }
    if(next)
    {
        ArcNode *nextNode=next->nextarc;
        if(nextNode)
        {
            return nextNode->adjvex;
        }
    }
    return -1;
}

void visit(ALGraph G, int v)
{
    cout << G.vertices[v].data << " ";
}

bool visited[MaxVertexNum];         //访问标记数组

//从顶点v出发,采用递归,广度遍历图G
void BFS(ALGraph G, int v)
{
    visit(G, v);
    visited[v]=true;                //设已访问标记
    Q.push(v);                      //顶点v入队
    while(!Q.empty())
    {
        v=Q.front();
        Q.pop();
        for(int w=FirstNeighbor(G, v); w>=0; w=NextNeighbor(G, v, w))
        {                           //检查v所以邻接点
            if(!visited[w])         //w为v的尚未访问的邻接顶点
            {
                visit(G, w);
                visited[w]=true;    //设已访问标记
                Q.push(w);          //顶点w入队
            }
        }
    }
}

//从顶点出发,进行广度优先遍历
void BFSTraverse(ALGraph G)
{
    for(int i=0; i<G.vexnum; i++)
    {
        visited[i]=false;           //访问标记数组初始化
    }

    for(int i=0; i<G.vexnum; i++)   //从0号顶点开始遍历
    {
        if(!visited[i])
        {
            BFS(G, i);              //vi未访问过,从vi开始BFS
        }
    }
}

//从顶点v出发,采用递归,深度遍历图G
void DFS(ALGraph G, int v)
{
    visit(G, v);                    //访问v
    visited[v]=true;                //设已访问标记
    for(int w=FirstNeighbor(G, v); w>=0; w=NextNeighbor(G, v, w))
    {                               //w为v的尚未访问的邻接顶点
        if(!visited[w])
        {
            DFS(G, w);
        }
    }
}

//从顶点出发,进行深度优先遍历
void DFSTraverse(ALGraph G)
{
    for(int i=0; i<G.vexnum; i++)
    {
        visited[i]=false;           //访问标记数组初始化
    }
    for(int i=0; i<G.vexnum; i++)   //从0号顶点开始遍历
    {
        if(!visited[i])
        {
            DFS(G, i);              //vi未访问过,从vi开始DFS
        }
    }
}

int main()
{
    ALGraph G;
    //CreateALGraph(G);
    int tag;                   //有向无向图标志
    cout << "请输入1(有向图),2(无向图):";
    cin >> tag;
    if(tag==1)
    {
        G.kind=DG;
        CreateDGGraph(G);      //建立有向图
    }
    else
    {
        G.kind=UDG;
        CreateUDGGraph(G);     //建立无向图
    }

    cout << "===========================" << endl;
    PrintALGraph(G);

    cout <<endl;
    cout << "广度遍历:";
    BFSTraverse(G);

    cout << endl;
    cout << "深度遍历:";
    DFSTraverse(G);

    return 0;
}

有向图测试

有向图的创建和广度、深度遍历

无向图测试

无向图的创建和广度、深度遍历

猜你喜欢

转载自blog.csdn.net/qq_36784975/article/details/84310656