我可以在图表的边缘插入数据吗?

标签 c

<分区>

我可以将数据放在相邻列表图的边缘吗?

我的要求有这些行:

(A) 所需信息: A-1. 100个城市,每个城市的游览时间 A2。 300 次带价格的站点间运输

(B) 显示直接访问的城市图表(100 个站点,其中有 300 条直接路径)

(C) 该图由具有一定距离的边组成


从(B)开始,我认为城市是顶点,路线(交通)是边。

(A. Needed information) 表示运输本身应该有价格 (C) 表示边缘有距离。

这就是为什么我对将数据放在边缘的方法感到好奇的原因。 我以为edge只是一个抽象的概念,实际上是通过将另一个节点的地址赋予该节点来实现的。

喜欢:

struct AdjListNode* newNode = newAdjListNode(dest); 
newNode->next = graph->array[src].head; 
graph->array[src].head = newNode;

如何用数据实现边缘?谢谢。

最佳答案

是的,您可以将数据与图形边相关联,这与您表示图形的方式无关。想想你会如何解决 shortest path problem ;您显然需要知道图中两个节点之间的距离,并且此信息是连接它们的边的属性,而不是单个节点。

在您的示例中,您可以:

// create reciprocal links between nodes 'src' and 'dst',
// containing the 'distance' information
void graph_link_two_cities(struct Graph* graph, int src, int dst, int distance)
{
    {
        struct Edge* head = graph->array[src].head; 
        struct Edge* node = { .id = dst, .distance = length, .next = head });
        graph->edges[src].head = node;
    }

    {
        struct Edge* head = graph->array[dst].head; 
        struct Edge* node = { .id = src, .distance = length, .next = head });
        graph->edges[dst].head = node;
    }
}

使用邻接矩阵,您只需存储 src 之间的距离和 dst进入matrix[src][dst] .

关于我可以在图表的边缘插入数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534544/

相关文章:

c - 错误: 'i' does not name a type while compiling with mingw

c - C 中的错误处理,void 返回函数

c - write 和 printf,哪个更快?

c++ - 检测 WM_MOUSEMOVE 是否由触摸/笔引起

比较 C 中的 double 值

c - C 中的通用数据类型的任何库?

c - 如何使用 C 在 Windows 中执行批处理 (.bat) 文件

c++ - 如何进行 size_t 乘法

连接两个堆栈(一个堆栈在另一个堆栈之上)

c - MPI_Recv 未收到所有 MPI_Send 请求