DSAA之图论表示部分(二)

1. Adjacency matrix

  • One simple way to represent a graph is to use a two-dimensional array. This is known as an adjacency matrix representation.
  • For each edge ( u , v ) , we set a[u][v] = 1; otherwise the entry in the array is 0. If the edge has a weight associated with it, then we can set a[u][v] equal to the weight and use either a very large or a very small weight as a sentinel to indicate nonexistent edges.
    • For instance, if we were looking for the cheapest airplane route, we could represent nonexistent flights with a cost of . If we were looking, for some strange reason, for the most expensive airplane route, we could use (or perhaps 0) to represent nonexistent edges.
  • Although this has the merit of extreme simplicity, the space requirement is O ( | V | 2 ) , which can be prohibitive if the graph does not have very many edges.
    • An adjacency matrix is an appropriate representation if the graph is dense: | E | = O ( | V | 2 ) .
    • In most of the applications that we shall see, this is not true.

  该部分概念和上一篇的查并集有点相似,使用二维数组相当于暴力代表了所有的可能,如果有n个节点,那么空间复杂度为O(n^2)(包括了节点自身到自身)。如果边有权重的话,可以用一个数表示该权重,并用不可能用到的数表示不存在的路径。但是这种表示方法在一些场合下造成了空间的浪费,所以还有其他的表示方法。

2. Adjacency list

  • If the graph is not dense, in other words, if the graph is sparse, a better solution is an adjacency list representation. For each vertex, we keep a list of all adjacent vertices. The space requirement is then O | E | + | V | ) . 空间复杂度从n^2降低到线性n
    • The leftmost structure in Figure 9.2 is merely an array of header cells. The representation should be clear from Figure 9.2. If the edges have weights, then this additional information is also in the cells.
  • Adjacency lists are the standard way to represent graphs.
    • Undirected graphs can be similarly represented; each edge (u, v) appears in two lists, so the space usage essentially doubles.无向图,这种表示方法,会造成一些数据的二次存储。
  • A common requirement in graph algorithms is to find all vertices adjacent to some given vertex v, and this can be done, in time proportional to the number of such vertices found, by a simple scan down the appropriate adjacency list.

  这里写图片描述

  上图就是open hash的形式,所以Adjacency list可以使用hash map来实现:

  • In most real-life applications, the vertices have names, which are unknown at compile time, instead of numbers. Since we cannot index an array by an unknown name, we must provide a mapping of names to numbers.
  • The easiest way to do this is to use a hash table, in which we store a name and an internal number ranging from 1 to | V | for each vertex. The numbers are assigned as the graph is read.
    • The first number assigned is 1. As each edge is input, we check whether each of the two vertices has been assigned a number, by seeing if it is in the hash table. If so, we use the internal number. Otherwise, we assign to the vertex the next available number and insert the vertex name and number into the hash table.
  • With this transformation, all the graph algorithms will use only the internal numbers. Since eventually we will need to output the real vertex names and not the internal numbers, we must also record, for each internal number, the corresponding vertex name.
    • One way is to use an array of strings. If the vertex names are long, this can cost considerable space, because the vertex names are stored twice. An alternative is to keep an array of pointers into the hash table. The price of this alternative is a slight loss of the sanctity of the hash table ADT.

  关于上面的英文描述,当成一个题目来解决,现在的需求是这样:输入一组节点的名字和节点的关系,可能的输入形式为["red","black","blue"....]["red","black"],["blue",“read”]...。现在如何去表示这个图呢?
  使用邻接表的话,那么必须解决namenumber的转化的问题。记得以前学习hash的时候,可以用字符串的所有字符相加的和来代表该字符串,但是这种处理方式可能存在两个不同字符串具有相同的key值。所以上文给出了一种策略,假设从0到n给每个节点贴上标签,那么我只需要处理该标签,将其正确map。但是随之而来的问题是,如果我们求得了问题的解,是一组标签,如何将标签正确转换为刚开始的name呢?
  一种选择是使用二维数组存储每个输入的节点,这样数组的下标(第二维)就是每个节点对应的标签,但是这种方式还是浪费了大量的空间,我们存储了两倍的输入数据,另一种选择笔者思考的是使用指针的数组来指向输入数据,指针数组的下标代表数据的标签。

3. 总结

  邻接表的概念不太难,但是需要处理的问题比较多。如何在最优空间复杂度下实现邻接表是个小挑战,DSAA在这里并没有给出任何实现,所以需要看这本书的人,认真思考实现下。

猜你喜欢

转载自blog.csdn.net/LoveStackover/article/details/80527600