简体   繁体   English

如何根据列表表示图形的边缘?

[英]How can I represent edges for a graph in terms of a list?

我们如何将图表表示为边缘列表?

There's three common ways of doing this: 这有三种常见的方法:

  1. Adjacency matrix: AV * V table of edge weights, where the ith column on the jth row is the weight of the edge between vertices i and j. 邻接矩阵:边缘权重的AV * V表,其中第j行的第i列是顶点i和j之间的边的权重。 If there is no edge, infinity is often used (or you can use some sentinel value, like -1). 如果没有边缘,则经常使用无穷大(或者您可以使用一些标记值,如-1)。

  2. Adjacency lists: An array of V linked lists. 邻接列表:V链表的数组。 Each ith list in the array is a list of edges leaving the ith vertice. 数组中的每个第i个列表是离开第i个顶点的边列表。

  3. Edge list: Simply a list of tuples (u, v) of edges. 边缘列表:只是边缘元组(u,v)的列表。

Different ones are suitable for different purposes. 不同的适用于不同的目的。 Personally I find the adjacency lists to be the most useful, but if you have a very dense graph then an adjacency matrix can improvement performance and memory usage. 我个人认为邻接列表是最有用的,但如果你有一个非常密集的图形,那么邻接矩阵可以改善性能和内存使用。

If you wanna store exactly edges, use matrix of weights: int** M; 如果你想准确存储边缘,使用权重矩阵: int** M; , where M[i][t] is the length of the edge between i and t vertices. 其中M[i][t]是i和t顶点之间的边长。

If your graph edges have weight of 1, you could store graph in adjacency matrix, where M[i][t] equals to: 如果图形边缘的权重为1,则可以将图形存储在邻接矩阵中,其中M[i][t]等于:

  • 0 if there is no edges between i and t vertices 如果i和t顶点之间没有边,则为0
  • 1 if there is edge from i to t 如果从i到t有边缘,则为1
  • alpha if i == t and there is a loop in i (or t) vertex alpha如果i == t并且i(或t)顶点有一个循环

If you requre structure, critical for memory usage, store your graph in the linked list, where each vertex has structure: 如果您需要对内存使用至关重要的结构,请将图表存储在链表中,每个顶点都有结构:

struct Vertex
{
  int N;
  Vertex* next;
};

So you would have array of Vertex structures, each containing pointer to the next it is connected to. 因此,您将拥有Vertex结构数组,每个结构都包含指向它所连接的下一个结构的指针。 For example, here is some linked-list-graph: 例如,这是一些链表图:

  • 1 -> 3 -> 4 1 - > 3 - > 4
  • 2 -> 5 2 - > 5
  • 3 -> 4 3 - > 4
  • 4 -> 3 4 - > 3
  • 5 -> 2 5 - > 2

Unless you really want to implement the representation yourself, as a learning exercise or to have more control, consider using a library such as igraph which is a C library for representing and manipulating graphs. 除非你真的想自己实现表示,作为学习练习或者有更多控制,否则考虑使用诸如igraph之类的库来表示和操作图形。 Or go one abstraction level down - build adjacency lists or edge lists yourself, but use the list-building capabilities of glib rather than rolling your own. 或者降低一个抽象级别 - 自己构建邻接列表或边缘列表,但使用glib的列表构建功能而不是自己编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM