C DFS:生成树的存在性

C DFS:生成树的存在性,c,computer-science,depth-first-search,C,Computer Science,Depth First Search,深度优先搜索是否可以用于确定某个图是否连接到一组未知大小的边和顶点,并且仅以起始顶点作为输入数据 struct node { int parent, rank; }; typedef struct node node; struct edge { int fromvertex, tovertex; float weight; }; typedef struct edge edge; node* nodes; edge* edges; int hasspantree(int sta

深度优先搜索是否可以用于确定某个图是否连接到一组未知大小的边和顶点,并且仅以起始顶点作为输入数据

struct node {
  int parent, rank;
};
typedef struct node node;

struct edge {
  int fromvertex, tovertex;
  float weight;
};
typedef struct edge edge;

node* nodes;
edge* edges;
int hasspantree(int startvertex)
{
  //find spanning tree?
}
节点和边在深度优先搜索之前运行的函数中指定,如下所示:

scanf("%d", nodecount);
scanf("%d", edgecount);
if ((nodes = malloc(*nodecount * sizeof(node))) == NULL) {
  printf("nodes malloc failed"); exit(1);
}
if((edges = malloc(*edgecount * sizeof(edge))) == NULL) {
  printf("edges malloc failed"); exit(1);
}
我可以这样做,因为函数声明:

int hasspantree(int startvertex, int edgecount, int nodecount)

但我希望能用之前的声明做到这一点

简单的回答是,如果您的数据是起始节点和两个未知大小的列表,则不可能执行DFS,因为您不知道图形,您不知道内存中与图形相关的数据或垃圾是什么,因为您不知道如何停止。所以你不能分析一个未知的图形。问题是,图的大小乘以图的大小,我的意思是,在你的例子中,定义图的2个数组的大小是显式的,而不是显式的,因为你的函数没有大小参数或隐式的,数据结构包含大小,或者有一种特殊的表示方式。因此,您可以使用终止符和空指针,类似于在字符串中使用NUL字符来指示数组的结束。另外,您可以有一个全局变量,但我们都知道这不是一个好的做法。

如果没有办法知道数组、节点和边的大小,就不可能创建代码来处理这些数组。@Fumu7我就是这么想的。。。我的教授坚持不修改他的函数头,并且hasspantree必须执行DFS。我想这些数组使用了一些规则。您知道字符串的变量表示为“char*variableName”。为什么我们觉得处理这个变量没有困难?因为我们知道空字符表示字符串的结尾。我想,您可能可以在描述中找到关于节点和边中数据的提示。