Figure (Graph)
He represents many relationship
review:
- Table linear relationship between 1 to 1
- Tree-to-many relationship
- Figure-many relationship
That is appreciated that the above model of a graph;
FIG terms and definitions:
- A graph (G) is defined as a coupled pair (V, E), referred to as G = (V, E); wherein V is a nonempty finite set of vertices (Vertex), denoted by V (G); E is the edge of set, referred to as E (G);
- Understood: As shown above, each node of a village that each village as a vertex, edge, vertex and shows the relationship between the vertices, i.e., unidirectional and bidirectional raises problems; undirected edges between the vertices and vertex interoperability;
- Edges are vertices as (v, w) belonging to E, where v, w belongs v. () Signify the edges, i.e., vertices and vertex v w interworking;
- Directed edges <v, w>, represents v-> w, v can come w, w can not come v, where <v, w> belongs to E, v, w belongs to V;
- Figure without considering the heavy side (only one side), and a self-loop (pointing to own their own);
- To sum up, there are two parts in FIG vertices and edges; finite set of vertices V is not empty, E is a finite set of edges; referred to as G (V, E)
the term
-
Undirected graph, all edges in the graph are undirected, undirected graph is called;
-
Directed graph, FIG edge portion is directed, that is important to the side direction, the official definition: FIG even vertex v to <v, w>, and w is between ordered, said graph G is FIG direction;
-
FIG added every edge weight (e.g., distance, cost, etc.), put this figure is called the network
More terms. . . Continually updated;
A diagram showing the program
Adjacency matrix
It represents a view of the two-digit group
g[n][n], n表示n个顶点的编号
若g[i][j] = 1 // 表示i和j 连接有边
若g[i][j] = 0 // 表示i和j 无边
g[0][1] = 1 // 表示0和1直接有单向边
g[1][0] = 1//表示1和0有单向边,
//若g[0][1]=g[1][0]=1,则1和0之间无向边;
g[0][2]=0 //表示0和2直接无边
As shown below;
It does not allow self-loop, so:
Diagonal axis, symmetrical, undirected graph
problem: for undirected graphs, the memory space is actually half of the waste;
Solution
- Only half of the memory, this one-dimensional array to store the map;
- Summation Formula 1,2,3 ... n n (n + 1) / 2; figure above so that g [i] [j] corresponding to the one-dimensional array subscript i * (i + 1) / 2 + j
Adjacency matrix
advantage:
- Intuitive, easy to understand
- Easy to find if any two vertices there is an edge
- Find all sides to facilitate a vertex;
- To facilitate the calculation of the vertex (the degree: The number of vertex points to the edge: the number of edges from the vertices out, the degrees)
Disadvantages:
- 浪费空间,稀疏的图,空间利用率差,对于稠密图,完全图(任意两个不同的顶点间都有一条边,又细分为完全有向图,完全无向图 )就很合算,空间利用率高;
- 浪费时间,稀疏图–统计有多少个边;
邻接表
真的很省内存空间吗?
- 对于无向图,实际都存了2个边
- 链表中还存有地址
- 对于网络中,结构中还需要加权重;
故:对于邻接表来说,一定要够稀疏才合算
优点:
- 方便找一个顶点的所有的邻接点
- 节约稀疏图的空间
- 需要n个头指针+2e个结点(详见上图)
- 方便计算仁一个结点的度?
- 对于无向图来说是。
- 对于有向图,不是;
- 出度易,入度有向图难
图的表示方法有很多种 非上述2种;
优化这个缺点,思想类似于线性表的思想,从数组转换为链表—邻接表表示法
图的遍历
DFS(Depth First Search)深度优先搜索
问题描述,从亮的那盏灯开始,如何点亮所有的灯
核心思想,递归;
//类似于树的先序遍历;
//伪代码
public void dfs(Vertex v){
v.visited = true; //或者引入map等存储已访问的数据,设置v被防伪过
for(x的邻接点 w:v){
if(!w.visited){
dfs(w);
}
}
}
若图里有n个顶点,e条边,则时间复杂度为?
- 用邻接表表示 O(n+e);
- 用邻接矩阵表示,O(n2)
BFS(Breadth First Search) 广度优先搜索
在树中,类似于层序遍历;见树的博客:https://blog.csdn.net/qq_32193775/article/details/104031481,
https://blog.csdn.net/qq_32193775/article/details/104107629;
回忆:
//伪代码如下
public void traversal(Tree tree){
if(tree!=null){
Queue que = Queue.getQueue(size);
que.add(tree);
while(!que.isEmpty()){
Tree treeTemp = que.pop();
System.out.println(treeTemp.data);
if(tree.left!=null)
que.add(tree.left);
if(tree.right!=null)
que.add(tree.right);
}
}
}
图中的BFS伪代码描述:
public void bfs(Vertex v){
Queue que = new Queue();
v.visited = true;
que.add(v);
while(!que.isEmpty){
Vertex v2 = que.pop();
for(v2的邻接点 w){
que.add(w);
w.visited = true;
}
}
}
若n个顶点,e条边,时间复杂度为?
- 邻接表 O(n+e)
- 邻接矩阵O(n2)
广度优先和深度优先的区别
-
深度优先是优先结点的子结点,处理完这些子结点后返回,直接深入该结点的子孙结点;
-
广度优先是利用了栈,在一圈一圈的处理,利用栈的先后顺序;在广度上逐步深入;