图的邻接表 表示 DFS 和BFS C++实现

来源:互联网 发布:广西农产品出口数据 编辑:程序博客网 时间:2024/05/19 16:47
/** File name  :   Lgraph.cpp* Function   :   图的学习, 邻接表   深度优先遍历和广度优先遍历        C++实现* Created on : 2016年5月30日* Author     : beijiwei@qq.com* Copyright  : 欢迎大家和我一起交流学习,转载请保持源文件的完整性。任何单位和个人不经本人允许不得用于商业用途input:5 6 0A 0 B 1 2A 0 C 2 5A 0 E 4 3B 1 D 3 4D 3 E 4 2E 4 C 2 5*/#include <cstdio>#include <iostream>#pragma warning(disable:4996)using namespace std;#define MAX 10typedef struct EdgeNode{     char start_name;     char end_name;     int  start;     int end;     int weight;     struct EdgeNode  * next;}EdgeNode;typedef struct {    int  vertex_num;    int  edge_num;    int  graph_type;    EdgeNode *   List[MAX];    int  mark[MAX];}Lgraph;/*************************************************************************************************/typedef struct  {    int  store[MAX * MAX];    int head;    int tail;}Squeue;void queue_init(Squeue & Q){    Q.head=0;    Q.tail=0;}void queue_in(Squeue & Q, int   V){    if( Q.tail == MAX * MAX )        cout<<"out of index"<<endl;    Q.store[ Q.tail ++]=V;}int queue_out(Squeue &Q){    if( Q.tail==Q.head)        cout<<"empty"<<endl;    return Q.store[ Q.head++];}bool queue_is_empty(Squeue &Q){    return ( Q.tail==Q.head) ? true : false;}/**************************************************************************************************/void  create_graph( Lgraph & G);void  graph_display( Lgraph & G);void gbfs( Lgraph & G);void gdfs(Lgraph & G);int main(int argc, char** argv){    freopen("input.txt","r",stdin);    Lgraph G;    create_graph( G);    graph_display(G);    gdfs(G);    gbfs(G);    return 0;}void create_graph( Lgraph & G){    cout<<"请输入图的顶点个数 和 边的个数: "<<endl;    cin>>G.vertex_num>>G.edge_num;    cout<<"请输入图的类型:  0无向图    1有向图"<<endl;    cin>>G.graph_type;    for(int i=0; i < G.vertex_num; i++)    {        G.List[i]=NULL;    }    for( int i=0; i < G.edge_num ; i++)    {        int start,end,weight;        char start_name,end_name;         EdgeNode * tmp= new EdgeNode;         cout<<"请输入第"<<i<<"条边的起点名字,起点序号,终点名字,终点序号和权值"<<endl;         cin>>start_name   >>start  >>end_name>>end>>weight;         tmp->start=start;         tmp->start_name=start_name;         tmp->end=end;         tmp->end_name=end_name;         tmp->weight=weight;         tmp->next=G.List[ start ];         G.List[ start ]=tmp;         if(  G.graph_type==0 )//是 无向图, 则 插入到终点的链表中         {                EdgeNode * T= new EdgeNode;                        T->start=end;                T->start_name=end_name;                T->end=start;                T->end_name=start_name;                T->weight=weight;                T->next=G.List[ end ];                G.List[ end ]=T;         }    }}void  graph_display( Lgraph & G){    EdgeNode * tmp;    for(int i=0; i<G.vertex_num;i++)    {        tmp=G.List[i];        if(tmp == NULL)            continue;        cout<<"顶点"<<i<<"名字是: "<<tmp->start_name<<"  ,  相邻顶点有:"<<endl;        while( tmp)        {            cout<<"      "<<tmp->end_name<<"  ,  该边权重是"<<tmp->weight<<endl;            tmp=tmp->next;        }    }}void dfs(Lgraph & G, int k){    int end;    G.mark[k]=1;    cout<<G.List[k]->start_name<<"\t";    end=G.List[k]->end;    if(   G.mark[end]==0)    {        dfs(G,end);    }}void gdfs(Lgraph & G){    cout<<"图的深度优先遍历 : "<<endl;    for( int i=0; i< G.vertex_num; i++)    {        G.mark[i]=0;    }    for(int k=0; k < G.vertex_num; k++)    {        if( G.mark[k]==0)        {            dfs(G,k);        }    }    cout<<endl;}void gbfs( Lgraph & G){      cout<<"图的广度优先遍历 : "<<endl;    for( int i=0; i< G.vertex_num; i++)    {        G.mark[i]=0;    }     for(int k=0; k < G.vertex_num; k++)    {        if( G.mark[k]==0)        {            G.mark[k]=1;             cout<<G.List[k]->start_name<<"\t";             Squeue Q;             queue_init(Q);             queue_in(Q, k);             while( queue_is_empty(Q)==0 )             {                int i=queue_out(Q) ;                int next=G.List[i]->end;                if(  G.mark[next]==0 )                {                        cout<<G.List[next]->start_name<<"\t";                        G.mark[next]=1;                        queue_in(Q,next);                }             }        }    }    cout<<endl;}

0 0