Topological Sort拓扑排序

在这里插入图片描述
在这里插入图片描述
用数组

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Queue
{
    int array[100];
    int front;
    int rear;
}QUEUE;
QUEUE Q;

void Push(int n)
{
    Q.array[Q.rear++]=n;
}
void output(int i);
int translate(char ch)
{
    if(ch=='S')
    {
        return 0;
    }
    else if(ch=='A')
    {
        return 1;
    }
    else if(ch=='B')
    {
        return 2;
    }
    else if(ch=='C')
    {
        return 3;
    }
    else if(ch=='D')
    {
        return 4;
    }
    else if(ch=='E')
    {
        return 5;
    }
    else if(ch=='F')
    {
        return 6;
    }
    else if(ch=='G')
    {
        return 7;
    }
    else if(ch=='H')
    {
        return 8;
    }
    else if(ch=='I')
    {
        return 9;
    }
    else 
    {
        return 10;
    }
}
int main()
{
    
    int graph[11][11]={0};
    int indegree[11]={0};
    int indegree1[11]={0};
    char a,b,c;
    int a1,c1;
    int i=0,j=0;
    scanf("%c%c%c",&a,&b,&c);
    for(i=0;i<11;i++)
    {
        for(j=0;j<11;j++)
        {
            graph[i][j]=0;
        }
    }
    a1=translate(a);
    c1=translate(c);
    
    //已经删掉一组的//
    graph[0][1]=1;
    graph[1][2]=1;
    graph[4][1]=1;
    graph[1][5]=1;
    graph[2][3]=1;
    graph[3][10]=1;
    graph[5][3]=1;
    graph[6][3]=1;
    graph[0][4]=1;
    graph[0][7]=1;
    graph[4][5]=1;
    graph[7][4]=1;
    graph[7][5]=1;
    graph[7][8]=1;
    graph[5][6]=1;
    graph[8][5]=1;
    graph[8][9]=1;
    graph[5][9]=1;
    graph[6][10]=1;
    graph[9][6]=1;
    graph[9][10]=1;
    graph[a1][c1]=0;
    for(j=0;j<11;j++)
    {
        for(i=0;i<11;i++)
        {
            if(graph[i][j]==1)
            {
                indegree[j]++;
            }
        }
    }
    for(i=0;i<11;i++)
    {
        indegree1[i]=indegree[i];
    }
    /*for(i=0;i<11;i++)
    {
        printf("%d,",indegree[i]);
    }*/
    for(int k=0;k<11;k++)
    {
        for(i=0;i<11;i++)
        {
            if(indegree[i]==0)
            {
                Push(i);
                for(j=0;j<11;j++)
                {
                    if(graph[i][j]==1)
                    {
                        indegree1[j]--;
                    }
                }
                indegree1[i]=-1;
            }
        }
        for(int p=0;p<11;p++)
        {
            indegree[p]=indegree1[p];
        }
    }
    //printf("\n");
    for(i=Q.front;i<Q.rear;i++)
    {
        output(i);
    }
    //printf("\nfront:%d",Q.rear);
    //printf("\nrear:%d",Q.front);
    return 0;
}
void output(int i)
{
    if(Q.array[i]==0)
    {
        printf("S,");
    }
    else if(Q.array[i]==1)
    {
        printf("A,");
    }
    else if(Q.array[i]==2)
    {
        printf("B,");
    }
    else if(Q.array[i]==3)
    {
        printf("C,");
    }
    else if(Q.array[i]==4)
    {
        printf("D,");
    }
    else if(Q.array[i]==5)
    {
        printf("E,");
    }
    else if(Q.array[i]==6)
    {
        printf("F,");
    }
    else if(Q.array[i]==7)
    {
        printf("G,");
    }
    else if(Q.array[i]==8)
    {
        printf("H,");
    }
    else if(Q.array[i]==9)
    {
        printf("I,");
    }
    else
    {
        printf("T,");
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值