- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试从图形文件创建邻接矩阵。
我需要读入一个包含顶点数的文本文件,然后列出图形格式
例如:
5
0 3 2
1 0 2
1 2 2
1 4 1
第一列数字是源顶点的id,第二列是目标顶点的id,第三列是边的权重
所以这应该返回矩阵
0 2 0 2 0
2 0 2 0 1
0 2 0 0 0
2 0 0 0 0
0 1 0 0 0
到目前为止,我已经阅读了文本文件并获得了顶点数,但我不确定从这里开始做什么。
我当前的代码:
#include <stdio.h>
#include <stdlib.h>
int main (){
printf("Enter the file contianing the graph\n");
char filename[20];
FILE *myFile;
scanf("%s",filename);
myFile = fopen (filename,"r");
int number_of_nodes, number_of_edges;
int source_id[100], target_id[100], weight[100];
int matrix[100][100];
int i;
if (myFile == NULL){
printf("File not found! Exiting...\n");
return -1;
}
else{
fscanf(myFile, "%d", &number_of_nodes);
printf("There are %d vertices.\n", number_of_nodes);
for(i = 0; i < (sizeof (source_id) / sizeof ( source_id[0] )); i++)
{
if( fscanf(myFile, "%d %d %d", &source_id[i], &target_id[i], &weight[i]) != 3)
break;
}
number_of_edges = i;
for (i = 0; i<99;i++){
for (int j = 0; j< 99; i++){
matrix[i][j]=0;
}
}
for (i = 0; i < number_of_edges; i++){
int x = source_id[i];
int y = target_id[i];
matrix[x][y] = weight[i];
matrix[y][x] = weight[i];
}
for (int y = 0; y < (number_of_nodes-1); y++){
for (int x = 0; x < (number_of_nodes -1); x++){
printf(matrix[x][y]);
printf(" \n");
}
}
}
fclose(myFile);
return 0;
}
最佳答案
因为你只贴了读取文件的代码,我就只评论和改进这部分。
首先,您可以更好地定义变量。在下面的代码中,我删除了您的 numberArray
,而是定义了一个 number_of_nodes
和三个独立的源、目标和权重数组。这使得以后更容易引用这些数据。
其次,由于您不知道文件中的项(边)数,因此您必须通过查看 fscanf()
的返回值来检查读取是否成功。它返回成功读取的元素数。在你的例子中,你一次读取 3 个数字,所以你可以将返回值与 3 进行比较。你还想在退出循环后存储 i
的值,所以你稍后会知道实际读取了多少边。
第三,使用 scanf("%s")
与使用 gets()
一样糟糕(Why is it bad?)。您可能应该考虑限制输入长度,例如使用 scanf("%19s")
。我没有在下面的代码中修复它,因为它不是一个直接的问题,但应该在您以后的开发中考虑到。
最后,为您的文件打开检查 +1。在继续之前确保先前的操作已成功完成是一种很好的做法。
这是我的固定代码:
#include <stdio.h>
#include <stdlib.h>
int main() {
printf("Enter the file contianing the graph\n");
char filename[20];
FILE *myFile;
scanf("%s", filename);
myFile = fopen(filename, "r");
int number_of_nodes, number_of_edges;
int source_id[100], target_id[100], weight[100];
int i;
if (myFile == NULL) {
printf("File not found! Exiting...\n");
return -1;
}
else {
fscanf(myFile, "%d", &number_of_nodes);
printf("There are %d vertices.\n", number_of_nodes);
for (i = 0; i < (sizeof(source_id) / sizeof(source_id[0])); i++) {
if (fscanf(myFile, " %d %d %d", &source_id[i], &target_id[i], &weight[i]) != 3)
break;
}
number_of_edges = i;
}
fclose(myFile);
return 0;
}
接下来要做什么,我会给你一些提示,而不是直接写完整的代码。如果您明白了要点,那就相对容易了。
您想创建一个“矩阵”,或者用 C 的话来说,一个数组的数组。为了简单起见,它应该看起来像这样
int matrix[100][100];
然后您可以初始化矩阵并将其重置为零:
// This is pseudo-code
for i = 0 to 99
for j = 0 to 99
matrix[i][j] = 0
然后根据您从文件中读取的内容,您可以将值分配给归零矩阵。记住要分配两个值(x 到 y 和 y 到 x)
for edge in edges
x = edge.source
y = edge.target
matrix[x][y] = edge.weight
matrix[y][x] = edge.weight
要打印出矩阵,只需按行遍历即可。不要忘记在每行之后打印一个换行符。
for y = 0 to (number_of_nodes - 1)
for x = 0 to (same as above)
print matrix[x][y] and a space
print a newline
如果你能理解以上所有的想法,那么你就可以开始了。
关于从图创建邻接矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53586964/
我在 ma 应用程序中使用 Jqgrid 树 View 模型,我可以看到它显示错误,因为不支持对象或属性我已经包含了 grid.Treeview.js 和其他 Jqgrid 脚本文件。我不知道可能是什
我正在尝试使用图中所示的符号在 matlab 上实现 Freeman Chain Code [4 adjacency]: 我的代码为我测试过的多个小矩阵提供了正确的链码。但是,当我在我的实际图像文件上
我有一张表,其中包含世界上所有地理位置及其关系的位置。 这是一个显示层次结构的示例。你会看到数据实际上存储为所有三个 枚举路径 邻接表 嵌套集 数据显然也不会改变。下面是英格兰布莱顿位置的直系祖先示例
我正在尝试从邻接树模型(id、parent_id)中的 MySQL 数据库中计算/创建或生成 PHP 目录。到目前为止,这是我在回显输出时所取得的成就。 1. Category 1 1 Subc
我知道 std::vector在内部连续存储它的数据(除非它是 std::vector )都在旧的 C++03 中标准和新的C++11 . 处理此问题并引用标准的好 stackoverflow 问题:
Development language and DB: PHP/MySQL 我有一张 geo_places 表,其中包含大约 800 万个地理位置。 这些地方都是分层次的,我用 parent_id
过去几个小时我一直在尝试在网上找到这个问题的解决方案。我找到了很多关于如何从嵌套集合转换为邻接的例子......但很少有相反的例子。我发现的示例要么不起作用,要么使用 MySQL 过程。不幸的是,我不
我是一名优秀的程序员,十分优秀!