标签:graph matrix adjacency list orthogonal list
In mathematics and computer science, graph theory is a important part to solve some 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
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.
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.
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;
};
The history of Graph Store(matrix, adjacency list, and orthogonal list),布布扣,bubuko.com
The history of Graph Store(matrix, adjacency list, and orthogonal list)
标签:graph matrix adjacency list orthogonal list
原文地址:http://blog.csdn.net/u012301943/article/details/38351553