如何实现Knuth';是用C排序的吗?
我试图用C实现Knuth的拓扑排序算法。当我搜索在线资源时,我看到的都是Kahn算法的实现,这让我很困惑。他们都一样吗?还是它们不同?这是我的实现基于我所研究的如何实现Knuth';是用C排序的吗?,c,sorting,graph,directed-acyclic-graphs,knuth,C,Sorting,Graph,Directed Acyclic Graphs,Knuth,我试图用C实现Knuth的拓扑排序算法。当我搜索在线资源时,我看到的都是Kahn算法的实现,这让我很困惑。他们都一样吗?还是它们不同?这是我的实现基于我所研究的 #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX 1000 void create_graph(); void add(int vertex); int del(); int isEmpty(); int fin
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 1000
void create_graph();
void add(int vertex);
int del();
int isEmpty();
int find_indegree_of_vertex(int vertex);
int total_vertices;
int adjacent_matrix[MAX][MAX];
int queue[MAX];
int front = -1;
int rear = -1;
int main()
{
int i, vertex, count, topological_sort[MAX], indegree[MAX];
create_graph();
for(i = 1; i <= total_vertices; i++)
{
indegree[i] = find_indegree_of_vertex(i);
if(indegree[i] == 0)
{
add(i);
}
}
count = 0;
while(!isEmpty() && count < total_vertices)
{
vertex = del();
topological_sort[++count] = vertex;
for(i = 1; i <= total_vertices; i++)
{
if(adjacent_matrix[vertex][i] == 1)
{
adjacent_matrix[vertex][i] = 0;
indegree[i] = indegree[i] - 1;
if(indegree[i] == 0)
{
add(i);
}
}
}
}
for(i = 1; i <= count; i++)
{
printf("%d ", topological_sort[i]);
}
printf("\n");
return 0;
}
void add(int vertex)
{
if(!(rear == MAX - 1))
{
if(front == -1)
{
front = 0;
}
rear = rear + 1;
queue[rear] = vertex ;
}
}
int isEmpty()
{
if(front == -1 || front > rear)
{
return 1;
}
else
{
return 0;
}
}
int del()
{
int element;
if(front == -1 || front > rear)
{
exit(1);
}
else
{
element = queue[front];
front = front + 1;
return element;
}
}
int find_indegree_of_vertex(int vertex)
{
int count, total_indegree = 0;
for(count = 0; count < total_vertices; count++)
{
if(adjacent_matrix[count][vertex] == 1)
{
total_indegree++;
}
}
return total_indegree;
}
void create_graph()
{
int count, maximum_edges, origin_vertex, destination_vertex;
char v1[1000], v2[1000];
char temp[10];
scanf("%d\n", &total_vertices);
maximum_edges = total_vertices * (total_vertices - 1);
for(count = 1; count <= maximum_edges; count++)
{
fgets(temp, sizeof(temp), stdin);;
char * splitter;
splitter = strtok(temp, " ");
strncpy(v1, splitter, strlen(splitter)+1);
splitter = strtok(NULL, " ");
strncpy(v2, splitter, strlen(splitter)+1);
origin_vertex = atoi(v1);
destination_vertex = atoi(v2);
if((origin_vertex == 0) && (destination_vertex == 0))
{
break;
}
else
adjacent_matrix[origin_vertex][destination_vertex] = 1;
}
}
输出:
4 7 8 5 1 6 12 2 10 13 3 11 9 14 15
预期输出(来自我们的课堂活动):
我的代码接受成对的输入,并在应用toposort后返回顺序。为简单起见,我假设该条目是拓扑排序的有效图形。如果您阅读第三版Edn中Knuth的TAOCP(计算机编程艺术)第1卷第2.2.3节,您会发现Knuth的“算法T(拓扑排序)”以及注释:
A.B.Kahn,CACM 5(1962),558-562首次发布了一种类似于算法T的拓扑排序技术(但没有队列链接的重要特征)
这表明Knuth的算法T不同于Kahn的算法。如果您阅读第三版Edn中Knuth的TAOCP(计算机编程艺术)第1卷第2.2.3节,您会发现Knuth的“算法T(拓扑排序)”以及注释:
A.B.Kahn,CACM 5(1962),558-562首次发布了一种类似于算法T的拓扑排序技术(但没有队列链接的重要特征)
这表明Knuth的算法T不同于Kahn的算法。可以这样实现:
#包括
#定义最大值200
int n,adj[MAX][MAX];
int front=-1,rear=-1,队列[MAX];
void main(){
int i,j=0,k;
int-topsort[MAX],indeg[MAX];
创建_图();
打印(“邻接矩阵为:\n”);
显示();
对于(i=1;i而言,可以这样实现:
#包括
#定义最大值200
int n,adj[MAX][MAX];
int front=-1,rear=-1,队列[MAX];
void main(){
int i,j=0,k;
int-topsort[MAX],indeg[MAX];
创建_图();
打印(“邻接矩阵为:\n”);
显示();
对于(i=1;i我认为问题在于Knuth的拓扑排序算法有多个版本。1968年出版的第一个版本与Kahn算法(1962年出版)相同。
Donald Knuth于1964年发布了一种生成拓扑排序的所有可能解的算法(它使用了一个deque D
用作排序的计数器数组)
我认为问题在于Knuth的拓扑排序算法有多个版本。1968年出版的第一个版本与Kahn算法(1962年出版)相同。
Donald Knuth于1964年发布了一种生成拓扑排序的所有可能解的算法(它使用了一个deque D
用作排序的计数器数组)
嗯……我不清楚你在问什么。代码有问题吗?对不起。我的问题是,卡恩的算法和克努特的不同吗?如果有,克努特的算法是如何实现的?我有自己的实现,但这是基于卡恩的。你从哪里得到克努特算法的规范的?TAOCP?其他地方?你在哪里找过Kahn的算法?对于Kahn的算法,我从geeksforgeks.org/topology-sorting-indegree-based-solution/和codezclub.com/c-topology-sorting-algorithm-example/得到它。我根据我的输入做了一些修改。对于Knuth,我还没有看到任何,但有Knuth的topology-sort的文章如果你真的读过TAOCP(计算机编程的艺术,第3版第1卷,第2.2.3节),你会发现Knuth的“算法T(拓扑排序)”和评论:一种类似于算法T(但没有队列链接的重要特征)的拓扑排序技术是由A.B.Kahn,CACM 5(1962)首次出版的,558-562..这表明Knuth的算法T不同于Kahn的算法。如果你还没有自己的TAOCP副本,现在可能是进行投资的好时机。如果没有,是时候搜查一个库了。嗯…我不清楚你在问什么。代码有问题吗?对不起。我的问题是,Kahn是吗算法与Knuth的不同?如果是的话,Knuth的算法到底是如何实现的?我有自己的实现,但这是基于Kahn的。你从哪里得到Knuth算法的规范?TAOCP?其他地方?你在哪里找到Kahn的算法?对于Kahn的算法,我从Geeksforgeks.org/Topology-so得到rting indegree-based solution/和codezclub.com/c-topology-sorting-algorithm-example/我根据我的输入做了一些修改。对于Knuth,我还没有看到任何修改,但有Knuth的Toposort的文章。好吧,如果你真的读过TAOCP(计算机编程艺术,第三版第1卷,第2.2.3节),你会发现Knuth的“算法T(拓扑排序)”和注释:A.B.Kahn,CACM 5(1962)首次发布了一种与算法T类似的拓扑排序技术(但没有队列链接的重要特征),558-562..这表明Knuth的算法T与Kahn的算法不同。如果您还没有自己的TAOCP副本,现在可能是进行投资的好时机。如果没有,是时候对库进行raid了。此代码不是在稍微现代的编译器下编译的-任何使用C99、C11或C18的代码都不会接受它。The代码不完整:函数insert\u queue()
和delete\u queue()使用了
,但既没有声明也没有定义。代码包含一条奇怪的线i-;
-这应该是i-;
?你应该使用比你年轻的标准编译,而不是C90。为了有用,你应该包含省略的函数。还有很多其他需要修复的;display()
应该是void display(void)
,create\u graph()
应该是void create\u graph()
,
4 7 8 5 1 6 12 2 10 13 3 11 9 14 15
4 7 8 5 6 12 1 13 10 2 9 14 11 3 15 (Notice the difference!)