图的广度和深度优先遍历

来源:互联网 发布:网络鲜花速递排名 编辑:程序博客网 时间:2024/05/15 10:21


#include <iostream>#include <deque>using namespace std;typedef char VextexType;typedef int  EdgeType;#define VexNum 5struct EdgeNode;struct EdgeNode {    VextexType HeadName;    VextexType TailName;    EdgeType   weight;    EdgeNode   *VexOut;    EdgeNode   *VexIn;};typedef struct {    VextexType name;    EdgeNode   *VexOutlink;    EdgeNode   *VexInlink;}VexNode;VexNode adjList[VexNum];bool visit[VexNum];void creatGraph(){    VextexType vextemp;    EdgeType   edgetemp;    char a[]={'A','B','C','D','E'};    int b[] = { 0,2, 0, 54, 0,        0, 0, 0, 0, 13,        9 ,0 ,0 ,0 ,0,        0 ,0, 5, 0, 8,        0 ,0 ,0 ,0 ,0};    //input n vextex    for ( int i=0; i<VexNum ; ++i ){        cin>>vextemp;        //vextemp = a[i];        adjList[i].name       = vextemp;        adjList[i].VexOutlink = NULL;        adjList[i].VexInlink  = NULL;    }    for ( int i=0; i<VexNum*VexNum; ++i ){        cin>>edgetemp;        //edgetemp = b[i];        if ( edgetemp==0 ){            continue;        }        EdgeNode *pEdge = new EdgeNode;        pEdge->HeadName = adjList[i/VexNum].name;        pEdge->TailName = adjList[i%VexNum].name;        pEdge->weight   = edgetemp;        pEdge->VexOut   = adjList[i/VexNum].VexOutlink;        if ( pEdge->VexOut ){            while ( pEdge->VexOut->VexOut ){                pEdge->VexOut =pEdge->VexOut->VexOut;            }            pEdge->VexOut->VexOut = pEdge;            pEdge->VexOut=NULL;        } else {            adjList[i/VexNum].VexOutlink = pEdge;            pEdge->VexOut = NULL;        }    }    for ( int i=0 ;i<VexNum ;++i ){        EdgeNode **pInLink = &adjList[i].VexInlink;        for ( int j=0; j<VexNum; ++j ){            if ( i==j ){                continue;            }            EdgeNode *p = adjList[j].VexOutlink;            while ( p ){                if ( p->TailName != adjList[i].name ){                    p = p->VexOut;                    continue;                }                *pInLink = p;                pInLink = &p->VexIn;                p = p->VexOut;            }        }        *pInLink = NULL;    }}void destroyGrape(){    for ( int i=0; i<VexNum ;++i ){        EdgeNode *p = adjList[i].VexOutlink;        EdgeNode *q;        while ( p ){            q = p;            p = p->VexOut;            delete q;        }    }}void printGrape(){    for ( int i=0; i<VexNum; ++i ){        cout<<adjList[i].name<<"-->";        EdgeNode *p = adjList[i].VexOutlink;        while ( p ){            cout<<"("<<p->HeadName<<","<<p->TailName<<","<<p->weight<<")";            p = p->VexOut;        }        cout<<endl;        p = adjList[i].VexInlink;        cout<<adjList[i].name<<"-->";        while ( p ){            cout<<"("<<p->HeadName<<","<<p->TailName<<","<<p->weight<<")";            p = p->VexIn;        }        cout<<endl;            }}void BFS(int i){    cout<<adjList[i].name;    visit[i]=true;    deque<VexNode*> s;    VexNode *tempVex;    EdgeNode *tempEdge;    int vexPos;    s.push_back(&adjList[i]);    while ( !s.empty() ){        tempVex = s.front();        s.pop_front();        if ( !tempVex->VexOutlink ){            continue;        }        tempEdge = tempVex->VexOutlink;                while ( tempEdge ){                        vexPos = tempEdge->TailName-'A';            tempEdge = tempEdge->VexOut;            if ( visit[vexPos] ){                continue;            }            cout<<"------>";            cout<<adjList[vexPos].name;            visit[vexPos]= true;            s.push_back(&adjList[vexPos]);        }    }}void BFSTraverse(){    //memset(visit,0,sizeof(bool)*VexNum);    for ( int i=0; i<VexNum; ++i ){        visit[i]=false;    }    for ( int i=0; i<VexNum; ++i ){        if ( visit[i] ){            continue;        }        BFS(i);        cout<<endl;    }}void DFS(int i,bool flage){    if ( visit[i] ){        return;     }    if ( flage ){        cout<<"-->";    }    cout<<adjList[i].name;    visit[i] = true;    if ( !adjList[i].VexOutlink  ){        return ;    }    int Pos =adjList[i].VexOutlink->TailName-'A';    DFS(Pos,true);}void DFSTraserve(){    for ( int i=0; i<VexNum; ++i ){        visit[i]=false;    }    for ( int i=0; i<VexNum; ++i ){        if ( visit[i] ){            continue;        }        DFS(i,false);        cout<<endl;    }}int main(){    creatGraph();    printGrape();    BFSTraverse();    DFSTraserve();    destroyGrape();}


0 0
原创粉丝点击