【C语言】拓扑排序算法查找有限偏序集(A,£)、理解最大元、最小元、最大元、最小元的定义和性质。

  • 实验目的

Acquiring proficiency in topological sorting algorithm and using it to find a topological sorting of a finite poset(A, £). Comprehending the definition and properties of greatest element, least element, maximal element, minimal element.

  • 实验内容

Using C-language to find a topological sorting of a finite poset(A, £), and judge whether there is a greatest element, a least element, find the maximal element, minimal element.

  • 使用环境

Win 11 & visual studio code 2022

  • 算法介绍

Algorithm: topological sorting algorithm

Input: a finite poset(A, £)

Output: a array “SORT” with all elements of the poset

Begin

Step 1: Choose a minimal element a of A.

Step 2: Make a the next entry of SORT and replace A with A {a}.

Step 3: Repeat step 1 and 2 until A = { }.

End

Find the maximal element, the minimal element.

Begin

Step 1: find the column with every entries are 0

Step 2: Find the corresponding element       , that is the minimal element

Step 1: find the row with every entries are 0

Step 2: Find the corresponding element       , that is the maximal element

End

  • 调试过程

  1. 调试代码

#include <stdio.h>
int R[100][2], M[10][10] = {0}; // R是关系,M是关系矩阵
void topu(int a)
{
    int i, j, sum = 0, col = 0, row = 0, max[a], min[a], sort[a];
    for (j = 0; j < a; j++) //找最小
    {
        for (i = 0; i < a; i++)
        {
            sum += M[i][j];
        }
        if (sum == 0)
        {
            min[col] = j;
            col++;
        }
        else
            sum = 0;
    }
    for (i = 0; i < a; i++) //找最大
    {
        for (j = 0; j < a; j++)
        {
            sum += M[i][j];
        }
        if (sum == 0)
        {
            max[row] = i;
            row++;
        }
        else
            sum = 0;
    }
    int b = 0, t1 = col - 1, t2 = 0, t3 = col; // t为临时变量
    while (b < a)                              //拓扑排序
    {
        for (; t1 >= t2; t1--)
        {
            sort[b] = min[t1] + 1;
            b++;
            for (j = 0; j < a; j++)
                M[min[t1]][j] = 0;
        }
        t2 = t3;
        t1 = t3;
        for (j = 0; j < a; j++)
        {
            if (j == min[0] || j == min[1] || j == min[2])
                continue;
            else
            {
                for (i = 0; i < a; i++)
                {
                    sum += M[i][j];
                }
                if (sum == 0)
                {
                    min[t1] = j;
                    t1++;
                }
                else
                    sum = 0;
            }
        }
        t3 = t1;
        t1--;
    }
    for (b = 0; b < a; b++) //输出拓扑排序
    {
        printf("%d", sort[b]);
    }
    printf(" is the topu order\n");
    if (col > 1) //输出最小元,极小元
    {
        printf("There is no least element\n");
        printf("The minimal elements are ");
        for (col = col - 1; col >= 0; col--)
            printf("%d ", min[col] + 1);
    }
    else
        printf("The least element is %d\n", min[col] + 1);
    if (row > 1) //输出最大元,极大元
    {
        printf("\nThere is no greatest element\n");
        printf("The maximal elements are ");
        for (row = row - 1; row >= 0; row--)
            printf("%d ", max[row] + 1);
    }
    else
        printf("The greatest element is %d\n", max[row] + 1);
}
int main()
{
    int vert, edge, i, j;
    int(*p1)[2], (*p2)[10];
    p1 = R;
    p2 = M;
    printf("input the vertices and edges:");
    scanf("%d %d", &vert, &edge);
    printf("input the relation(such '1 3'):");
    for (i = 0; i < edge; i++) //输入关系
        scanf("%d %d", (*(p1 + i)), (*(p1 + i) + 1));
    for (i = 0; i < edge; i++) //关系转化为矩阵
    {
        M[*(*(p1 + i)) - 1][*(*(p1 + i) + 1) - 1] = 1;
    }
    for (i = 0; i < vert; i++) //输出M
    {
        for (j = 0; j < vert; j++)
        {
            printf("%d ", *(*(p2 + i) + j));
        }
        printf("\n");
    }
    topu(vert);
    return 0;
}
  1. 运行结果

  • 总结

Two array structures are defined to store the entry degree of each vertex and the vertex serial number of the topological sorting record. Starting from the first vertex without entry degree, output all vertices without entry degree in turn and remove them from the existing graph. At the same time, remove the edges attached to this node and other nodes. The final output vertex sequence is the topological sort sequence. It should be noted that the results of topological sort sequence of some directed acyclic graphs are not unique.

  • 参考文献

[1]谭浩强,C 程序设计[M] (第四版).北京:清华大学出版社,2010年6月(中国高等院校计算机基础教育课程体系规划教材)

[2]谭浩强, C 程序设计( 第四版 )学习辅导 ,北京:清华大学出版社,2010年7月(中国高等院校计算机基础教育课程体系规划教材)

[3]C Primer Plus (第6版)中文版,Stephen Prata 著;姜佑译 ——北京 :人名邮电出版社,2019.11

  • 20
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值