邻接表

来自集智百科 - 复杂系统|人工智能|复杂科学|复杂网络|自组织
923397935讨论 | 贡献2020年8月28日 (五) 19:41的版本
跳到导航 跳到搜索

此词条暂由彩云小译翻译,未经人工整理和审校,带来阅读不便,请见谅。
本词条由信白初步翻译

文件:Simple cycle graph.svg
This undirected cyclic graph can be described by the three unordered lists {b, c}, {a, c}, {a, b}.

This undirected cyclic graph can be described by the three unordered lists }, }, }.

图:这个无向循环图可以用三个无序列表来描述。


In graph theory and computer science, an adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. This is one of several commonly used representations of graphs for use in computer programs.

In graph theory and computer science, an adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a vertex in the graph. This is one of several commonly used representations of graphs for use in computer programs.

在图论和计算机科学中,邻接表 Adjacency List 是用来表示有限图的无序表的集合。每个列表描述图中一个顶点的邻居集合。这是计算机程序中常用的几种图形表示法之一。


Implementation details

{ | class = “ wikitable” align = “ right” style = “ width: 18em; ”
The graph pictured above has this adjacency list representation: The graph pictured above has this adjacency list representation: 上图中的图表有这样的邻接表示法:
a adjacent to b,c a adjacent to b,c

靠近 b 和 c

b adjacent to a,c b adjacent to a,c

靠近 a 和 c

c adjacent to a,b c adjacent to a,b

靠近 a 和 b

|}


An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring vertices or edges. There are many variations of this basic idea, differing in the details of how they implement the association between vertices and collections, in how they implement the collections, in whether they include both vertices and edges or only vertices as first class objects, and in what kinds of objects are used to represent the vertices and edges.

An adjacency list representation for a graph associates each vertex in the graph with the collection of its neighboring vertices or edges. There are many variations of this basic idea, differing in the details of how they implement the association between vertices and collections, in how they implement the collections, in whether they include both vertices and edges or only vertices as first class objects, and in what kinds of objects are used to represent the vertices and edges.

图的邻接表示法将图中的每个顶点与其邻接顶点或边的集合关联起来。这个基本思想有许多变体,在它们如何实现顶点和集合之间的关联,在它们如何实现集合,在它们是否包括顶点和边还是只包括顶点作为第一类对象,以及在什么类型的对象被用来表示顶点和边的细节上有所不同。

  • An implementation suggested by Guido van Rossum uses a hash table to associate each vertex in a graph with an array of adjacent vertices. In this representation, a vertex may be represented by any hashable object. There is no explicit representation of edges as objects.[1]
 | url = https://www.python.org/doc/essays/graphs/}}</ref>

| url = https://www.python.org/doc/essays/graphs/} </ref >

  • Cormen et al. suggest an implementation in which the vertices are represented by index numbers.[2] Their representation uses an array indexed by vertex number, in which the array cell for each vertex points to a singly linked list of the neighboring vertices of that vertex. In this representation, the nodes of the singly linked list may be interpreted as edge objects; however, they do not store the full information about each edge (they only store one of the two endpoints of the edge) and in undirected graphs there will be two different linked list nodes for each edge (one within the lists for each of the two endpoints of the edge).
 }}</ref> Their representation uses an array indexed by vertex number, in which the array cell for each vertex points to a singly linked list of the neighboring vertices of that vertex. In this representation, the nodes of the singly linked list may be interpreted as edge objects; however, they do not store the full information about each edge (they only store one of the two endpoints of the edge) and in undirected graphs there will be two different linked list nodes for each edge (one within the lists for each of the two endpoints of the edge).

} </ref > 他们的表示使用一个按顶点数索引的数组,其中每个顶点的数组单元格指向该顶点的相邻顶点的单链表。在这种表示中,单链表的节点可以解释为边对象; 然而,它们并不存储关于每条边的完整信息(它们只存储边的两个端点中的一个) ,在无向图中,每条边有两个不同的链表节点(边的两个端点中的每个端点的列表中都有一个)。

  • The object oriented incidence list structure suggested by Goodrich and Tamassia has special classes of vertex objects and edge objects. Each vertex object has an instance variable pointing to a collection object that lists the neighboring edge objects. In turn, each edge object points to the two vertex objects at its endpoints.[3] This version of the adjacency list uses more memory than the version in which adjacent vertices are listed directly, but the existence of explicit edge objects allows it extra flexibility in storing additional information about edges.
 | isbn = 0-471-38365-1}}</ref> This version of the adjacency list uses more memory than the version in which adjacent vertices are listed directly, but the existence of explicit edge objects allows it extra flexibility in storing additional information about edges.

| isbn = 0-471-38365-1} </ref > 这个版本的邻接列表比直接列出相邻顶点的版本使用更多的内存,但是显式边对象的存在允许它在存储额外的关于边的信息方面有额外的灵活性。


Operations

The main operation performed by the adjacency list data structure is to report a list of the neighbors of a given vertex. Using any of the implementations detailed above, this can be performed in constant time per neighbor. In other words, the total time to report all of the neighbors of a vertex v is proportional to the degree of v.

The main operation performed by the adjacency list data structure is to report a list of the neighbors of a given vertex. Using any of the implementations detailed above, this can be performed in constant time per neighbor. In other words, the total time to report all of the neighbors of a vertex v is proportional to the degree of v.

邻接表数据结构执行的主要操作是报告给定顶点的邻居列表。使用上面详细说明的任何实现,都可以在每个邻居的固定时间内执行。换句话说,报告一个顶点 v 所有邻居的总时间与 v 的度成正比。


It is also possible, but not as efficient, to use adjacency lists to test whether an edge exists or does not exist between two specified vertices. In an adjacency list in which the neighbors of each vertex are unsorted, testing for the existence of an edge may be performed in time proportional to the minimum degree of the two given vertices, by using a sequential search through the neighbors of this vertex. If the neighbors are represented as a sorted array, binary search may be used instead, taking time proportional to the logarithm of the degree.

It is also possible, but not as efficient, to use adjacency lists to test whether an edge exists or does not exist between two specified vertices. In an adjacency list in which the neighbors of each vertex are unsorted, testing for the existence of an edge may be performed in time proportional to the minimum degree of the two given vertices, by using a sequential search through the neighbors of this vertex. If the neighbors are represented as a sorted array, binary search may be used instead, taking time proportional to the logarithm of the degree.

也可以使用邻接表来测试两个指定顶点之间是否存在边,但效率不高。在一个邻接表中,每个顶点的邻居是无序的,测试边的存在可以按照给定顶点的最小度成比例的时间进行,通过使用一个线性搜索通过这个顶点的邻居。如果邻居被表示为一个排序数组,二进制搜索可以代替,采取时间成正比的对数度。


Trade-offs

The main alternative to the adjacency list is the adjacency matrix, a matrix whose rows and columns are indexed by vertices and whose cells contain a Boolean value that indicates whether an edge is present between the vertices corresponding to the row and column of the cell. For a sparse graph (one in which most pairs of vertices are not connected by edges) an adjacency list is significantly more space-efficient than an adjacency matrix (stored as a two-dimensional array): the space usage of the adjacency list is proportional to the number of edges and vertices in the graph, while for an adjacency matrix stored in this way the space is proportional to the square of the number of vertices. However, it is possible to store adjacency matrices more space-efficiently, matching the linear space usage of an adjacency list, by using a hash table indexed by pairs of vertices rather than an array.

The main alternative to the adjacency list is the adjacency matrix, a matrix whose rows and columns are indexed by vertices and whose cells contain a Boolean value that indicates whether an edge is present between the vertices corresponding to the row and column of the cell. For a sparse graph (one in which most pairs of vertices are not connected by edges) an adjacency list is significantly more space-efficient than an adjacency matrix (stored as a two-dimensional array): the space usage of the adjacency list is proportional to the number of edges and vertices in the graph, while for an adjacency matrix stored in this way the space is proportional to the square of the number of vertices. However, it is possible to store adjacency matrices more space-efficiently, matching the linear space usage of an adjacency list, by using a hash table indexed by pairs of vertices rather than an array.

邻接表的主要替代方法是邻接矩阵矩阵,这是一个矩阵,其行和列按顶点索引,其单元格包含一个布尔值,表明是否有一条边存在于对应于单元格行和列的顶点之间。对于稀疏图(其中大多数顶点对没有连接的边) ,邻接表的空间利用率明显高于邻接矩阵表(存储为二维数组) : 邻接表的空间利用率与图中边和顶点的数量成正比,而以这种方式存储的邻接矩阵表的空间利用率与顶点数的平方成正比。然而,通过使用哈希表索引的顶点对而不是数组,可以更有效地存储邻接矩阵,匹配邻接表的线性空间使用。


The other significant difference between adjacency lists and adjacency matrices is in the efficiency of the operations they perform. In an adjacency list, the neighbors of each vertex may be listed efficiently, in time proportional to the degree of the vertex. In an adjacency matrix, this operation takes time proportional to the number of vertices in the graph, which may be significantly higher than the degree. On the other hand, the adjacency matrix allows testing whether two vertices are adjacent to each other in constant time; the adjacency list is slower to support this operation.

The other significant difference between adjacency lists and adjacency matrices is in the efficiency of the operations they perform. In an adjacency list, the neighbors of each vertex may be listed efficiently, in time proportional to the degree of the vertex. In an adjacency matrix, this operation takes time proportional to the number of vertices in the graph, which may be significantly higher than the degree. On the other hand, the adjacency matrix allows testing whether two vertices are adjacent to each other in constant time; the adjacency list is slower to support this operation.

邻接表和邻接矩阵之间的另一个显著区别是它们执行操作的效率。在邻接表中,每个顶点的邻居可以有效地列出,在时间上与顶点的程度成正比。在邻接矩阵中,这个操作需要的时间与图中顶点的数量成正比,而顶点的数量可能明显高于度。另一方面,邻接邻接矩阵允许测试两个顶点是否在固定的时间内彼此相邻; 邻接表支持这一操作的速度较慢。


Data structures

For use as a data structure, the main alternative to the adjacency list is the adjacency matrix. Because each entry in the adjacency matrix requires only one bit, it can be represented in a very compact way, occupying only 模板:Abs2/8 bytes of contiguous space, where 模板:Abs is the number of vertices of the graph. Besides avoiding wasted space, this compactness encourages locality of reference.

For use as a data structure, the main alternative to the adjacency list is the adjacency matrix. Because each entry in the adjacency matrix requires only one bit, it can be represented in a very compact way, occupying only 2/8}} bytes of contiguous space, where }} is the number of vertices of the graph. Besides avoiding wasted space, this compactness encourages locality of reference.

作为一种数据结构,邻接表的主要替代方法是邻接矩阵。因为邻接矩阵中的每个条目只需要一个位,所以它可以以非常紧凑的方式表示,只占用连续空间的 < sup > 2 /8}字节,其中}}是图的顶点数。除了避免浪费空间,这种紧凑性鼓励访问局部性。


However, for a sparse graph, adjacency lists require less space, because they do not waste any space to represent edges that are not present. Using a naïve array implementation on a 32-bit computer, an adjacency list for an undirected graph requires about 2·(32/8)模板:Abs = 8模板:Abs bytes of space, where 模板:Abs is the number of edges of the graph.

However, for a sparse graph, adjacency lists require less space, because they do not waste any space to represent edges that are not present. Using a naïve array implementation on a 32-bit computer, an adjacency list for an undirected graph requires about = 8}} bytes of space, where }} is the number of edges of the graph.

然而,对于一个稀疏的图,邻接表需要较少的空间,因为他们不浪费任何空间来表示边不存在。在32位计算机上使用一个天真的数组实现,一个无向图的邻接表需要大约 = 8}字节的空间,其中}是图的边数。


Noting that an undirected simple graph can have at most (模板:Abs2-模板:Abs)/2 ≈ V 2 edges, allowing loops, we can let d = 模板:Abs/模板:Abs2 denote the density of the graph. Then, 8模板:Abs > 模板:Abs2/8 when 模板:Abs/模板:Abs2 > 1/64, that is the adjacency list representation occupies more space than the adjacency matrix representation when d > 1/64. Thus a graph must be sparse enough to justify an adjacency list representation.

Noting that an undirected simple graph can have at most 2-)/2 ≈ V 2}} edges, allowing loops, we can let /2}} denote the density of the graph. Then, > 2/8}} when /2 > 1/64}}, that is the adjacency list representation occupies more space than the adjacency matrix representation when . Thus a graph must be sparse enough to justify an adjacency list representation.

注意到一个无向简单图最多可以有 < sup > 2 -)/2≈ v < sup > 2 }边,允许循环,我们可以让/< sup > 2 }表示该图的密度。然后,> < sup > 2 /8}当/< sup > 2 > 1/64}时,即邻接表表示比邻接矩阵表示占用更多的空间。因此,图必须足够稀疏,以证明邻接表表示。


Besides the space trade-off, the different data structures also facilitate different operations. Finding all vertices adjacent to a given vertex in an adjacency list is as simple as reading the list. With an adjacency matrix, an entire row must instead be scanned, which takes O(模板:Abs) time. Whether there is an edge between two given vertices can be determined at once with an adjacency matrix, while requiring time proportional to the minimum degree of the two vertices with the adjacency list.

Besides the space trade-off, the different data structures also facilitate different operations. Finding all vertices adjacent to a given vertex in an adjacency list is as simple as reading the list. With an adjacency matrix, an entire row must instead be scanned, which takes )}} time. Whether there is an edge between two given vertices can be determined at once with an adjacency matrix, while requiring time proportional to the minimum degree of the two vertices with the adjacency list.

除了空间上的权衡,不同的数据结构也促进了不同的操作。在邻接表中找到与给定顶点相邻的所有顶点就像读取邻接表一样简单。对于邻接矩阵,必须扫描整行,这需要花费)}的时间。是否有一个给定的顶点之间的边可以确定与一个邻接矩阵一次,而需要时间成正比的最小程度的两个顶点与邻接表。


References

  1. Guido van Rossum 作者: 吉多·范罗苏姆 (1998 1998年). "Python Patterns ーー实现图形". {{cite web}}: Check date values in: |year= (help); line feed character in |author= at position 17 (help); line feed character in |year= at position 5 (help)
  2. [[Thomas H. Cormen 1 = 托马斯·科尔曼 |Thomas H. Cormen]]; [[Charles E. Leiserson 2 = 查尔斯·雷瑟尔森 |查尔斯·雷瑟尔森]]; [[Ronald L. Rivest 3 = 罗纳德·李维斯特 |罗纳德·李维斯特]]; [[Clifford Stein 4 = Clifford Stein |Clifford Stein 4 = Clifford Stein]] (2001 2001年). Introduction to Algorithms, Second Edition 算法导论,第二版. MIT Press and McGraw-Hill. pp. 527-529 of section 22.1: representation of graphs. ISBN 0-262-03293-7. 
  3. Michael T. Goodrich and Roberto Tamassia 作者: Michael t. Goodrich and Roberto Tamassia (2002 2002年). 算法设计: 基础、分析和互联网示例. 约翰威立. ISBN 0-471-38365-1. 


Further reading

  • David Eppstein

作者: David Eppstein (1996

1996年). "ICS 161 Lecture Notes: Graph Algorithms 161课堂笔记: 图形算法". {{cite web}}: Check date values in: |year= (help); line feed character in |author= at position 15 (help); line feed character in |title= at position 40 (help); line feed character in |year= at position 5 (help)

 | url = http://www.ics.uci.edu/~eppstein/161/960201.html}}

Http://www.ics.uci.edu/~eppstein/161/960201.html


External links

模板:Commons category


模板:Graph representations

Category:Graph data structures

类别: 图形数据结构


This page was moved from wikipedia:en:Adjacency list. Its edit history can be viewed at 邻接矩阵/edithistory