是的,您可以将数据与图形边缘相关联,并且它与您表示图形的方式无关。想想你将如何解决这个问题
最短路径问题
;你显然需要知道图中两个节点之间的距离,而这个信息是连接它们的边缘的属性,而不是单个节点。
在您的示例中,您可以:
// 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;
}
}
</code>
使用邻接矩阵,您只需存储它们之间的距离
src
和
dst
成
matrix[src][dst]
。