FIG storage and trees: the adjacency matrix and adjacency list

Basic algorithms and data structures Collection:
https://blog.csdn.net/GD_ONE/article/details/104061907

Summary

This paper describes an implementation of the adjacency matrix and adjacency list, no, and the difference between the directed graph, and the difference between FIGS dense and sparse graphs FIGS. And the use of two scenarios stored.FIG dense storage using adjacency matrix, using the sparse adjacency table storage FIG.

No difference between the directed graph, and FIG.

No special directed graph is a directed graph, Two points as long as there is no edge, can reach each other to FIG, built so as not to put it between two points when there FIG come two sides of a directed graph.

Figure sparse and dense graph

Sparse graph generally refers to a drawing points and edges are of the same order of magnitude, such as the number of points is 1000 variable is around 1000, and dense FIG generally refers to the number of sides of the square of points, such as points 100, and the number of edges 10000 .

Adjacency matrix

While listening to the adjacency matrix is ​​very powerful, very deep, but actually,It isOneTwo-dimensional array. So when a large sparse graph points, can not be used to keep the two-dimensional array.

Suppose a directed graph:
Here Insert Picture Description
So adjacency matrix D expressed as:
Here Insert Picture Description
where, INF + infinity, i.e., not connected edges between the point and the another point.
and soD [i] [j] is the length of the side between point number i to j number of points

Adjacency matrix initialization

Because of his own to the distance is 0, and if there is no edge to be connected to INF between two points, the first of these two cases to initialize.

Suppose there are a total of n points, provided adjacency matrix D
Code:

for(int i = 1; i <= n; i++){
	for(int j = 1; j <= n; i++){
		if(i == j){
			D[i][j] = 0;
		}
		else{
			D[i][j] = INF; 
		}
	}
}

Note: INF usually set: 0x3f3f3f3f

Reading adjacency matrix

Multiple edges and loopback:
weight side is the side of the input data is repeated between two given points.
Since the ring is connected to a point of their own.
There is no difference between the map and the map:
undirected graph is a special directed graph, we only have time to build a directed graph edge

Some of the questions and possible heavy side loopback, so the time to read the side of special judge it. For example, the smallest side will be stored when seeking the shortest path problem.

The general format is read :( assuming a total of m edges, each row gives three integers, a, b, W, represents a side length to b w)

for(int i = 1; i <= m; i++){
	int a = in.nextInt();
	int b = in.nextInt();
	int w = in.nextInt();
	D[a][b] = Math.min(D[a][b], w); //如果有重边则只存储最短的
}

Adjacency list

Adjacency list is used in the linked list of storage arrays +, and talked of the previous embodiment with a zipper hash table is implemented exactly the same method.

In the above there is an example view illustrating the adjacent table of FIG ::

Here Insert Picture Description
Therefore, an adjacency list is to point out all stored on a single side chain

Achieve adjacency list

Implementation adjacency table there are many, may be usedTwo-dimensional Vector(Dynamic array) to store each point of the edges. Alternatively, you can useAn array of analog single listThe method hereRecommended methods of using single chain analog array to achieve the adjacent table, the reason is high efficiency in this way. In addition, this solution to the conflict with the hash table is exactly the same method with a zipper. The only difference is, then storage side, we not only need to store the length of the edge needed to store the current point and which is connected to the point.
Another: (Static Neighbor table before the chain is also known as the Star)

int [] e = new int[N]; // 表示指向的点
int [] w = new int[N]; // 表示边长
int [] next = new int[N]; // next指针
int [] head = new int[N]; // 邻接表表头
int idx = 1; // 链表结点编号

// 插入
public static void add(int a, int b, int c){ // 将a和b之间建一条边
	e[idx] = b; // 存储a点指向哪个点
	w[idx] = c; // 存储边长
	next[idx] = head[a];
	head[a] = idx++; 
}

// 遍历一个点的所有出边
for(int i = head[a]; i != 0; i = next[i]){
	int b = e[i]; // a点指向的边
	int c = w[i]; // a到b的边长
}

// 无向图的读入, 假如有m条边
for(int i = 0; i < m; i++){
	int a = in.nextInt();
	int b = in.nextInt();
	int c = in.nextInt();
	add(a, b, c);
	add(b, a, c); //因为是无向图所以要反向建边。
}

With an array of analog adjacency list is to understand the three main functions, not difficult, finished school in this chapter, go and learn the most short-circuit it: the shortest path problem of five algorithms

Published 72 original articles · won praise 271 · views 40000 +

Guess you like

Origin blog.csdn.net/GD_ONE/article/details/104351714