短视频直播源码,有向网的拓扑排序

2020-11-09 18:07:09 浏览数 (1)

短视频直播源码,有向网的拓扑排序实现的相关代码如下

代码语言:javascript复制
/*
 Author:Albert Tesla Wizard
 Time:2020/10/28 23:07
*/
#include<bits/stdc  .h>
#define OK 1
#define Error 0
#define MAXSIZE 20
using namespace std;
typedef enum{DG,UDG,DN,UDN}Grpahkind;
typedef struct ArcNode
{
    ArcNode* nextarc;
    int* info;
    int adjvertex;
};
typedef struct VNode
{
    char data;
    ArcNode* firstarc;
}VNode,AdjList[MAXSIZE];
typedef struct ALGraph
{
    AdjList vertex;
    int vertexnum,arcnum;
    int kind;
}ALGraph;
int Locate(char v,ALGraph&G)
{
    for(int i=1;i<=G.vertexnum;i  )
    {
        if(G.vertex[i].data==v)return i;
    }
    return -1;
}
int create(ALGraph&G)
{
        char c1,c2;
        int tail,head,w;
        cout<<"请输入有向网的顶点数目:"<<endl;
        cin>>G.vertexnum;
        cout<<"请输入有向网的弧的数目:"<<endl;
        cin>>G.arcnum;
        cout<<"请输入有向网的顶点集合:"<<endl;
        for(int i=1;i<=G.vertexnum;i  ){cin>>G.vertex[i].data;G.vertex[i].firstarc=NULL;}
        for(int i=1;i<=G.arcnum;i  )
        {
            cout<<"请输入组成有向网的弧的两个顶点c1,c2的值及该弧权值:"<<endl;
            cin>>c1>>c2>>w;
            tail=Locate(c1,G);
            head=Locate(c2,G);
            ArcNode* p=new ArcNode;
            p->info=new int;
            *(p->info)=w;
            p->adjvertex=head;
            p->nextarc=G.vertex[tail].firstarc;
            G.vertex[tail].firstarc=p;
        }
    return OK;
}
void print(ALGraph&G)
{
    cout<<"有向网的顶点数目为:"<<G.vertexnum<<endl;
    cout<<"有向网的顶点集合为:";
    for(int i=1;i<=G.vertexnum;i  )cout<<G.vertex[i].data<<" ";
    cout<<endl;
    cout<<"有向网的弧的数目为:"<<G.arcnum<<endl;
    cout<<"有向网弧的集合为:";
        for(int i=1;i<=G.vertexnum;i  )
        {
            ArcNode* p=G.vertex[i].firstarc;
            while(p)
            {
                cout<<"<"<<G.vertex[i].data<<","<<G.vertex[p->adjvertex].data<<","<<*(p->info)<<">";
                p=p->nextarc;
            }
        }
        cout<<endl;
}
void FindInDegree(int indegree[],ALGraph G)
{
    ArcNode* p;
    for(int i=1;i<=G.vertexnum;i  )indegree[i]=0;
    for(int i=1;i<=G.vertexnum;i  )
    {
        p=new ArcNode;
        p=G.vertex[i].firstarc;
        while(p)
        {
            indegree[p->adjvertex]  ;
            p=p->nextarc;
        }
    }
}
int TopologicalSort(ALGraph G)
{
    int cnt=0,indegree[MAXSIZE];//输出的顶点数
    stack<int>S;
    ArcNode* p;
    FindInDegree(indegree,G);
    for(int i=1;i<=G.vertexnum;i  )if(!indegree[i])S.push(i);
    while(!S.empty())
    {
        int j=S.top();
        S.pop();
        cout<<G.vertex[j].data<<" ";
        cnt  ;
        for(p=G.vertex[j].firstarc;p;p=p->nextarc)
        {
            int k=p->adjvertex;
            if(!(--indegree[k]))S.push(k);
        }
    }
    if(cnt<G.vertexnum){cout<<"该有向网有回路"<<endl;return Error;}
    else {cout<<"为一个拓扑序列"<<endl;return OK;}
}
int main()
{
    system("color 5E");
    ALGraph G;
    create(G);
    print(G);
    TopologicalSort(G);
    return 0;
}

以上就是短视频直播源码,有向网的拓扑排序实现的相关代码, 更多内容欢迎关注之后的文章

0 人点赞