数据结构-图的算法DFS和BFS以及拓扑排序

以下为代码实现

#include <iostream>
#include <queue>
#include <stack>
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
#define MVNum 100 //max number of vertex
typedef int VerTexType;
typedef int OtherInfo;
typedef struct ArcNode
{
    int adjvex;
    struct ArcNode *next;
    OtherInfo info;
}ArcNode;
typedef struct VNode{
    VerTexType data;
    ArcNode *firstarc;
}VNode,AdjList[MVNum];
typedef struct
{
    AdjList vertices;
    int vexnum,arcnum;
}ALGraph;
int Locate(ALGraph G,VerTexType v){
    int i;
    for(i=0;i<G.vexnum;i++)
    if(G.vertices[i].data==v){
        return i;
    }
    return -1;
}
Status CreateUDG0(ALGraph &G)//无向图
{
    int i,j,k;
    VerTexType v1,v2;
    //Otherinfo tmp; //权值
    ArcNode *p1,*p2;
    cin>>G.vexnum>>G.arcnum;
    for(i=0;i<G.vexnum;++i)
    {
       cin>>G.vertices[i].data;
       G.vertices[i].firstarc=NULL;
    }
    for(k=0;k<G.arcnum;++k){
        cin>>v1>>v2;
        //cin>>tmp;
        i = Locate(G,v1);
        j = Locate(G,v2);
        p1 = new ArcNode;
        p1->adjvex=j;
        //p1->info=tmp;
        p1->next = G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;//head insert
        p2 = new ArcNode;
        p2->adjvex=i;
        //p2->info=tmp;
        p2->next = G.vertices[j].firstarc;
        G.vertices[i].firstarc=p2;
    }
    return OK;
}
Status CreateUDG1(ALGraph &G)//有向图
{
    int i,j,k;
    VerTexType v1,v2;
    //Otherinfo tmp; //权值
    ArcNode *p1;
    cin>>G.vexnum>>G.arcnum;
    for(i=0;i<G.vexnum;++i)
    {
       cin>>G.vertices[i].data;
       G.vertices[i].firstarc=NULL;
    }
    for(k=0;k<G.arcnum;++k){
        cin>>v1>>v2;
        //cin>>tmp;
        i = Locate(G,v1);
        j = Locate(G,v2);
        p1 = new ArcNode;
        p1->adjvex=j;
        //p1->info=tmp;
        p1->next = G.vertices[i].firstarc;
        G.vertices[i].firstarc=p1;//head insert
    }
    return OK;
}
bool visited[MVNum];
int FirstAdjVex(ALGraph G,int v){
    if(!G.vertices[v].firstarc) return -1;
    return G.vertices[v].firstarc->adjvex;
}
int NextAdjVex(ALGraph G,int v,int w){
    ArcNode *p;
    for(p=G.vertices[v].firstarc;p;p=p->next){
        if(p->adjvex==w&&p->next)
            return p->next->adjvex;
    }
    return -1;
}
void DFS(ALGraph G,int v){//from the v-th vertex
    cout<<G.vertices[v].data<<" ";
    visited[v]=true;
    for(int w=FirstAdjVex(G,v);w>=0;w=NextAdjVex(G,v,w)){
       if(!visited[w]) DFS(G,w);
    }
}
void DFSTraverse(ALGraph G){
    for(int v=0;v<G.vexnum;++v)
        visited[v]=false;
    for(int v=0;v<G.vexnum;++v){
        if(!visited[v]) DFS(G,v);
    }
    cout<<endl;
}

void BFS(ALGraph G,int v){
    bool isvisited[MVNum];
    for(int i=0;i<G.vexnum;i++)
        isvisited[i]=false;
    cout<<G.vertices[v].data<<" ";
    queue<int> Q;
    Q.push(v);
    while(!Q.empty()){
        int u = Q.front();
        Q.pop();
        for(int w=FirstAdjVex(G,u);w>=0;w=NextAdjVex(G,u,w)){
            if(!isvisited[w])
            {
                cout<<G.vertices[w].data<<" ";
                isvisited[w]=true;
                Q.push(w);
            }
        }
    }
    cout<<endl;
}
void FindInDegree(ALGraph G,int indegree[]){
    for(int v = 0;v<G.vexnum;v++)
        for(ArcNode* p = G.vertices[v].firstarc;p;p=p->next)
            indegree[p->adjvex]++;
}
Status TopologicalSort(ALGraph G,int topo[]){
    int indegree[G.vexnum]={0};
    ArcNode *p;
    FindInDegree(G,indegree);
    stack<int> S;
    int m=0,i=0,k;
    for(i=0;i<G.vexnum;++i)
        if(!indegree[i]) S.push(i);
    while(!S.empty()){
        i=S.top();
        S.pop();
        topo[m]=i;
        m++;
        p=G.vertices[i].firstarc;
        while(p!=NULL){
            k=p->adjvex;
            --indegree[k];
            if(indegree[k]==0) S.push(k);
            p=p->next;
        }
    }
    if(m<G.vexnum) return ERROR;
    return OK;
}
int main(){
    ALGraph G;
    CreateUDG1(G);
    DFSTraverse(G);
    BFS(G,0);
    int topo[G.vexnum]={0};
    TopologicalSort(G,topo);
    for(int i=0;i<G.vexnum;i++)
        cout<<G.vertices[topo[i]].data<<" ";
}
/*
6 8
1 2 3 4 5 6
1 4
1 3
1 2
3 5
3 2
4 5
6 5
6 4
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值