程序员

AOV网与拓扑排序 - gonghr

作者:admin 2021-05-06 我要评论

声明:图片及内容基于: https://www.bilibili.com/video/BV1Wp4y1X79x?from=articleDetail 原理 AOV网 拓扑排序 数据结构 核心代码 void ALGraph::TopologicalS...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

声明:图片及内容基于:https://www.bilibili.com/video/BV1Wp4y1X79x?from=articleDetail

原理

AOV网

拓扑排序

数据结构

核心代码

void ALGraph::TopologicalSort(){
    for(int i=0;i<vertexNum;i++){   //in为0则压栈 
        if(adjList[i].in==0){
            s.push(adjList[i]);
        } 
    }
    while(!s.empty()){              //循环终止条件:栈为空 
        vertexNode v=s.top();       //弹栈输出 
        s.pop();
        cout<<v.vertex<<" ";
        count++;                    //计数加一 
        ArcNode *a=v.firstEdge;     
        while(a){                   //对弹出的结点遍历,所有遍历过的结点的in-1 
            adjList[a->adjvex].in--;
            int tmp=adjList[a->adjvex].in;
            if(tmp==0){             //如果某结点的in变为0,则将其压栈 
                s.push(adjList[a->adjvex])    ;    
            }
            a=a->next;
        }
    }
    if(count<vertexNum) cout<<"有环"<<endl; //如果计数小于顶点数则说明有环 
    else cout<<"无环,成功完成"<<endl; 
} 

完整代码

#include<iostream>
#include<stack>
#include<cstring>
#define MAX 100
using namespace std;
typedef struct ArcNode{         //边结点 
    int adjvex;                 //顶点下标 
    ArcNode *next;
}ArcNode;
typedef struct{
    int in;                     //in是入度 
    string vertex;              //顶点信息 
    ArcNode *firstEdge;             
}vertexNode,VertexNode[MAX];

class ALGraph{
    private:
        int vertexNum,arcNum;   //顶点数,边数 
        VertexNode adjList;     //顶点数组 
        stack<vertexNode> s;    //
        int count=0;            //计数 
    public:
        ALGraph(string v[],int n,int e);
        void display();         //打印邻接表 
        void TopologicalSort(); //拓扑排序 
};
void ALGraph::TopologicalSort(){
    for(int i=0;i<vertexNum;i++){   //in为0则压栈 
        if(adjList[i].in==0){
            s.push(adjList[i]);
        } 
    }
    while(!s.empty()){              //循环终止条件:栈为空 
        vertexNode v=s.top();       //弹栈输出 
        s.pop();
        cout<<v.vertex<<" ";
        count++;                    //计数加一 
        ArcNode *a=v.firstEdge;     
        while(a){                   //对弹出的结点遍历,所有遍历过的结点的in-1 
            adjList[a->adjvex].in--;
            int tmp=adjList[a->adjvex].in;
            if(tmp==0){             //如果某结点的in变为0,则将其压栈 
                s.push(adjList[a->adjvex])    ;    
            }
            a=a->next;
        }
    }
    if(count<vertexNum) cout<<"有环"<<endl; //如果计数小于顶点数则说明有环 
    else cout<<"无环,成功完成"<<endl; 
} 
ALGraph::ALGraph(string v[],int n,int e){   //构造函数 
    vertexNum=n;
    arcNum=e;
    for(int i=0;i<vertexNum;i++){           //顶点初始化 
        adjList[i].in=0;
        adjList[i].vertex=v[i];           
        adjList[i].firstEdge=NULL;
    }
    ArcNode *s;
    int vi,vj;
    for(int i=0;i<arcNum;i++){
        s=new ArcNode;
        cout<<"请输入边的两个端点:"<<endl; 
        cin>>vi>>vj;
        s->adjvex=vj;
        s->next=adjList[vi].firstEdge;      //头插法 
        adjList[vi].firstEdge=s;
        adjList[vj].in++;                   //入度加一 
    }
} 
void ALGraph::display(){
   cout<<"in"<<" 邻接表"<<endl;
for(int i=0;i<vertexNum;i++){ ArcNode *p=adjList[i].firstEdge; cout<<adjList[i].in<<" "; cout<<adjList[i].vertex; if(p) cout<<"->"; while
p=p->next; if(p) cout<<"->"; } cout<<endl; } } int main(){ int n,e; cout<<"请输入顶点数和边数"<<endl; cin>>n>>e; cout<<"请输入结点信息"<<endl; string v[MAX]; for(int i=0;i<n;i++){ cin>>v[i]; } ALGraph algraph(v,n,e); algraph.display(); algraph.TopologicalSort(); return 0; }

输入:

7 10
C1 C2 C3 C4 C5 C6 C7
0 2
0 3
1 3
1 5
2 4
3 4
3 6
3 5
4 6
5 6

输出:

in 邻接表

0 C1->C4 ->C3
0 C2->C6 ->C4
1 C3->C5
2 C4->C6 ->C7 ->C5
2 C5->C7
2 C6->C7
3 C7
C2 C1 C3 C4 C5 C6 C7 无环,成功完成

(注意拓扑排序答案可能不唯一)

-原文链接:https://www.cnblogs.com/gonghr/p/14617830.html

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 【20210405】读研规划(更新ing)

    【20210405】读研规划(更新ing)

  • 操作系统学习笔记----内存管理

    操作系统学习笔记----内存管理

  • Mallet算法及滤波器

    Mallet算法及滤波器

  • 剑指offer24:链表的反转

    剑指offer24:链表的反转

腾讯云代理商
海外云服务器