-
实验目的
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
-
调试过程
-
调试代码
#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;
}
-
运行结果
-
总结
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