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

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.

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

时间: 2024-11-05 13:43:10

The history of Graph Store(matrix, adjacency list, and orthogonal list)的相关文章

Laplacian matrix(转)

转自:https://en.wikipedia.org/wiki/Laplacian_matrix#Deformed_Laplacian Laplacian matrix From Wikipedia, the free encyclopedia In the mathematical field of graph theory, the Laplacian matrix, sometimes called admittance matrix, Kirchhoff matrix or discr

拉普拉斯矩阵(Laplacian Matrix) 及半正定性证明

摘自 https://blog.csdn.net/beiyangdashu/article/details/49300479 和 https://en.wikipedia.org/wiki/Laplacian_matrix 定义 给定一个由n个顶点的简单图G,它的拉普拉斯矩阵定义为: L = D - A,其中,D是该图G度的矩阵,A为图G的邻接矩阵. 因为G是一个简单图,A只包含0,1,并且它的对角元素均为0. L中的元素给定为: 其中deg(vi) 表示顶点 i 的度. 对称归一化的拉普拉斯

[Algorithms] Graph Traversal (BFS and DFS)

Graph is an important data structure and has many important applications. Moreover, grach traversal is key to many graph algorithms. There are two systematic ways to traverse a graph, breadth-first search (BFS) and depth-frist search (DFS). Before fo

Articulation Points (or Cut Vertices) in a Graph

A vertex in an undirected connected graph is an articulation point (or cut vertex) iff removing it (and edges through it) disconnects the graph. Articulation points represent vulnerabilities in a connected network – single points whose failure would

history命令

history命令的功能是显示使用过的命令,并为其编号. history n 显示最近使用过的n条命令. history -c 将当前shell中历史清空. history -d 801 删除编号为801的命令. history -a 追加最新一条命令到历史文件中. history -n 显示还没有从历史文件中读取的历史记录. history -r 将历史文件中的记录作为当前shell的历史记录. history -w 将当前记录写入历史文件中,覆盖原内容. -p Perform history

react-router与react-redux跳转后保存store数据(基于"react-router": "^2.8.0")

1.router引入 import { Route, IndexRoute, Router, hashHistory, browserHistory } from 'react-router'; <Router routes={routes} history={browserHistory}/> 假设store结构如下,分别是主页home,详情页detail,评论页comment { comment: { comments:[], leng:0 } detail: { content: &qu

[Algorithm][Greedy]Dijsktra Algorithm

面试折在了dijsktra algorithm上,我一个暴风哭泣-- 本来是想复习一下Dijsktra算法,又看到了Prim's algorithm 等系列,今天就着这个机会都好好复习一下. ---------------------------正文分割线------------------------------------------ Dijsktra Algorithm 给出一个图以及一个起点,找到从起点到其他所有点的最短路径. Dijsktra 与 Prim 最小生成树算法类似, 也生成

Spark MLlib LDA 源代码解析

1.Spark MLlib LDA源代码解析 http://blog.csdn.net/sunbow0 Spark MLlib LDA 应该算是比較难理解的,当中涉及到大量的概率与统计的相关知识,并且还涉及到了Spark GraphX图计算方面的知识.要想明确当中的原理得要下一番功夫. LDA源代码解析前的基础知识: 1)LDA主题模型的理论知识 參照:LDA数学八卦 2)SparkGraphX 基础知识 http://blog.csdn.net/sunbow0/article/details/

[email&#160;protected] Strongly Connected Component

Strongly Connected Components A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in