The history of Graph Store(matrix, adjacency list, and orthogonal list)

来源:互联网 发布:小米摄像头app软件 编辑:程序博客网 时间:2024/04/30 17:33
        In mathematics and computer science, graph theory is a important part to solve some
    common problems. i.e..shorest path problem, minimum spanning tree, and so on. In addition
    ,we could effective extend this ability by the help of computer. Before that, a necessary
    problem is: How can we store a graph to a computer ? which data structure is more appropriate?

        There are some common data structures used to solve this problem. In this article, we
    discuss three of them: adjacency matrix, adjacency list, and orthogonal list. But before that, let
    us build a simple graph. It will be used by the following examples.

                   5 
         a-----------------d
         |\  1              |
         | \                 |
      4 |  c               | 3
         | / \ 2            |
         |/2  \             e
         b      f 


 1. Adjacency matrix

        In this structure, we use a two-dimention array to save all information. It is simple. Actually,
    even a example is enough to explain how to use this method. The following is the graph above
    represented as adjacency matrix.

                a   b   c   d   e   f
            a  0   4   1   5   U  U
            b  4   0   2   U   U  U
            c  1   2   0   U   U   2
            d  5  U   U   0   3   U
            e  U  U   U   3   0   U
            f   U  U   2   U   U   0
    In this array, A[i][j] means the edge weight from node i to node j. Because this is a undirected
    graph, the array is symmetric. As we can see, there is a obvious disadvantage in this method
    is that there are many position is empty. Any advices to save this problem ? See next section.


 2. Adjacency List

        Actually, at first sight, I thought maybe a mutiple linked list is a good choice. But It is not
    as simple as I thought. A key point is that the degree of a node is indeterminate. Some of nodes
    have a few of edges. But another have many. So maybe we need build a special list.
        Adjacency list is what we want. It provide a nice solution to this problem. If we use the
    graph above , we could get a adjacency list like this:  

            [node a]  --> ( edge to b) --> (edge to c) --> (edge to d)
            [node b]  --> ( edge to a) --> (edge to c)
            [node c]  --> ( edge to a) --> (edge to b) --> (edge to f)
            [node d]  --> ( edge to a) --> (edge to e)
            [node e]  --> ( edge to d)
            [node f]  --> (edge to c)

    In C, maybe the following structure is a example to build a adjacency list.
            struct NODE {
                    struct EDGE *next;
            };
            struct EDGE {
                    struct EDGE *next;
                    struct NODE *node;
            };

        Compare with the adjacency matrix, the advantage of adjacency list is that there is no
    empty item. For those graph which have a few of edges, it is usefull. Of curse, the data
    structure is become more complex. And still have some problem. Sometime, we need to find
    the in-degree or out-degree of a node. If the graph is a undirected, all is ok. But if it is a
    directed graph, that will be different. Based on our define for adjacency list, the list of a
    node is only contain of out-degree,there are no information about in-degree. That means
    we must travel all over the adjacency list if we want to get the in-degree. That is awful.
    So we build another adjacency list by reverse the define of normal one, it is called reverse
    adjacency list. Like this

            [node a]  --> (edge from b) --> (edge from c) --> (edge from d)
            [node b]  --> (edge from a) --> (edge from c)
            [node c]  --> (edge from a) --> (edge from b) --> (edge from f)
            [node d]  --> (edge from a) --> (edge from e)
            [node e]  --> (edge from d)
            [node f]  --> (edge from c)
    As we can see,we will convenient to get in-degree in reverse adjacency list.

 3. Orthogonal list

    For solve the problem, we introduce a new data structure--cross list.

                    [node a]       [node b]        [node c]      [node d]      [node e]     [node f]
                        |                  |                 |                |                 |               |
      [node a]-------------(edge to b)--(edge to c)--(edge to d)------------------
                        |                  |                 |                |                 |               |
      [node b]--(edge to a)-------------(edge to c) -------------------------------
                        |                  |                 |                |                 |               |
      [node c]--(edge to a)--(edge to b)---------------------------------(edge to f)
                        |                  |                 |                |                 |               |
      [node d]--(edge to a)-----------------------------------(edge to e)----------
                        |                  |                 |                |                 |               |
      [node e]-----------------------------------(edge to d)--------------------
                        |                  |                 |                |                 |               |
      [node f]-------------------------(edge to c)-------------------------------
                        |                  |                 |                |                 |               |

        It is similar with the adjacency matrix.the different between them is that we use a
    multilist in orthogonal list. The use of list help us skip "the empty item" in adjacence matrix.
    That is nice!! But the cost is we need more space to save the information about coordinates.
    It is necessary. For example: if we want to ensure the status between node c and node b,
    it is difficult to find the corresponding edge without the flag information. Based on our analysis,
    we could build a example in C.
            struct NODE {
                    struct EDGE *next;
            };
            struct EDGE {
                    int x;
                    int y;
                    struct EDGE *line;
                    struct EDGE *row;
            };

 4. Recall

        Recall our history of solve problems, we was introduce adjacency matrix for save
    information about graph. Then we found a problem: there are some empty item in the
    adjacency matrix. when the degree of the node is few, the situation become insufferable.
    So we was introduce a new data structure--adjacency list. It has been solve the problem
    about empty item. But introduce another problem, It is difficult to get both in-degree and
    out-degree in a single list. For solve this problem, we introduce another data structure
    --orthogonal list. To some extent, this is nice.
0 0
原创粉丝点击