拓扑排序_代码

拓扑排序_代码_哔哩哔哩_bilibili

 

// 拓扑排序 O(V+E) C++编译
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define MAXV 1000  //最大顶点数

//////////////////////链栈
typedef struct LiNode{
    int data;
    struct LiNode *next;
}LiNode,*LiStack;

//初始化栈
void InitStack(LiStack &L){
    L=(LiNode*)malloc(sizeof(LiNode));
    L->next=NULL;
}

//判断栈是否为空
bool StackEmpty(LiStack L){
    return L->next==NULL;
}

//入栈
void Push(LiStack &L,int e){
    LiNode *s=(LiNode*)malloc(sizeof(LiNode));
    s->data=e;
    s->next=L->next;
    L->next=s;
}

//出栈
void Pop(LiStack &L){
    if(StackEmpty(L)) return;
    LiNode *s=L->next;
    L->next=s->next;
    free(s);
}

//读栈顶
int Top(LiStack L){
    if(!StackEmpty(L))
        return L->next->data;
}

///////////////////////邻接表
typedef struct ArcNode{ //边结点
    int adjvex; //该弧指向的顶点下标
    struct ArcNode *nextarc; //指向下一条弧
}ArcNode;

typedef struct VexNode{ //顶点结点
    char data; //顶点信息
    ArcNode *firstarc; //指向该顶点的第一条弧
}VexNode;

typedef struct{
    VexNode AdjList[MAXV]; //邻接表
    int vexnum,arcnum;     //顶点数,弧数
}ALGraph;

/////////////////////拓扑排序
int topo[MAXV];
bool TopoSort(ALGraph G){
    int i,j,indegree[MAXV]; ArcNode *p;
    for(i=0; i<G.vexnum; i++) indegree[i]=0;
    for(i=0; i<G.vexnum; i++) //计算各顶点入度
        for(p=G.AdjList[i].firstarc;p;p=p->nextarc)
            indegree[p->adjvex]++;

    LiStack S; //声明链栈
    InitStack(S); //初始化栈
    for(int i=0;i<G.vexnum;i++)
        if(indegree[i]==0)
            Push(S,i); //将所有入度为0的顶点入栈
            
    int count=0; //记录顶点数
    while(!StackEmpty(S)){
        i=Top(S); //取出栈顶元素
        Pop(S);   //栈顶元素出栈
        topo[count++]=i; //记录拓扑序
        for(p=G.AdjList[i].firstarc;p;p=p->nextarc){
            j=p->adjvex; //获得i的邻接点j
            if((--indegree[j])==0) //若j入度为0,
                Push(S,j);         //则j入栈
        }
    }
    
    return count==G.vexnum; //成功true,失败false
}

int main(){
    ALGraph G;
    G.vexnum=4;
    G.arcnum=4;
    char vex[]={'A','B','C','D'};
    for(int i=0;i<G.vexnum;i++)
        G.AdjList[i].data=vex[i]; //顶点data

    ArcNode *p=(ArcNode*)malloc(sizeof(ArcNode));
    G.AdjList[0].firstarc=p;
    p->adjvex=1; //AB

    p=(ArcNode*)malloc(sizeof(ArcNode));
    G.AdjList[0].firstarc->nextarc=p;
    p->adjvex=2; //AC

    p=(ArcNode*)malloc(sizeof(ArcNode));
    G.AdjList[2].firstarc=p;
    p->adjvex=1; //CB
 
    p=(ArcNode*)malloc(sizeof(ArcNode));
    G.AdjList[1].firstarc=p;
    p->adjvex=3; //BD

    TopoSort(G); //拓扑排序
    
    for(int i=0;i<G.vexnum;i++)
        printf("%c ",G.AdjList[topo[i]].data);
    return 0;
}

 

 B3644 【模板】拓扑排序 / 家谱树 - 洛谷

// 拓扑排序 Kahn算法 O(V+E)
#include<bits/stdc++.h>
using namespace std;

const int MAXV=110;
int n; //顶点数
vector<int> G[MAXV]; //邻接表
int indegree[MAXV];  //入度
int topo[MAXV];      //拓扑序列

bool TopoSort(){
  stack<int> S;
  for(int i=1; i<=n; i++){
     if(indegree[i]==0) 
        S.push(i); //入度为0的点入栈
  }
  
  int count=0;
  while(!S.empty()){
    int u=S.top();   //取出栈顶元素
    S.pop();         //出栈
    topo[++count]=u; //记录拓扑序
    for(int v:G[u]){
        if(--indegree[v]==0)
            S.push(v); //v入栈
    }
  }
  
  return count==n;
}

int main(){
  cin>>n;
  for(int i=1,j; i<=n; i++){
    while(cin>>j,j){
      G[i].push_back(j);
      indegree[j]++; //入度
    }
  }
  
  TopoSort();
  for(int i=1; i<=n; i++) cout<<topo[i]<<" ";
  return 0;
}

 

// 拓扑排序 DFS算法 O(V+E)
#include<bits/stdc++.h>
using namespace std;

const int MAXV=110;
int n; //顶点数
vector<int> G[MAXV]; //邻接表
bool visited[MAXV]; //访问标记数组

void DFS(vector<int> G[],int u){
    visited[u]=true; //标记u已访问
    for(int v:G[u]){
        if(!visited[v])
            DFS(G,v); //递归访问v
    }
    printf("%d ",u);  //输出序列
}

void DFSTraverse(vector<int> G[]){
    for(int u=1; u<=n; ++u){
        if(!visited[u]) 
            DFS(G,u); //递归访问u
    }
}

int main(){
  cin>>n;
  for(int i=1,j; i<=n; i++){
    while(cin>>j,j){
      G[j].push_back(i); //建反图
    }
  }
  
  DFSTraverse(G);
  return 0;
}

 

.

posted @ 2026-10-25 08:05  董晓  阅读(18)  评论(0)    收藏  举报