1. Basic concepts of graphs
- Mathematical language description of the graph
G(V(G),E(G))
.- V(vertex) refers to the vertex set of the graph
- E(edge) refers to the edge set of the graph.
- Classification of graphs: According towhether the edges have direction, they are divided into directed graphs and undirected graphs.
- Undirected graphs are special directed graphs (one undirected edge can be seen as two directed edges)
- If the edge has a value: a weighted graph
2. Drawing
2.1 Online website (used when there are few nodes)
- Online drawing website
- Interface introduction:
2.2 Matlab drawing (used when there are many nodes)
-
△Note:
- Matlab can only start numbering from 1. If you force it to start from 0, an error will be reported.
- This function is only supported in versions after 2015b. If an error occurs during operation, please download a new version of Matlab.
-
See Section 8 code files for details.
code_graph.m
-
mapping
- Undirected graph:
% 函数graph(s,t):可在 s 和 t 中的对应节点之间创建边,并生成一个图 G1 = graph(s1, t1); plot(G1) % 函数graph(s,t,w):可在 s 和 t 中的对应节点之间以w的权重创建边,并生成一个图 G2 = graph(s2, t2); plot(G2, 'linewidth', 2) % 设置线的宽度 % 下面的命令是在画图后不显示坐标 set( gca, 'XTick', [], 'YTick', [] );
- directed graph: Just change
graph
todigraph
.
- Undirected graph:
3. Weighted adjacency matrix
1. Weighted adjacency matrix of undirected graph
- in conclusion:
- (1) The weighted adjacency matrix D corresponding to the undirected graph is a symmetric matrix;
- (2) The element on its main diagonal is 0.
- (3) Dij represents the weight from the i-th node to the j-th node
2. Weighted adjacency matrix of directed graph
- in conclusion:
- (1) The weighted adjacency matrix D corresponding to the directed graph is generally no longer a symmetric matrix.
- (2) The element on its main diagonal is 0.
- (3) Dij represents the weight from the i-th node to the j-th node
4. Dijkstra
- Purpose: Solve the shortest path (both directed and undirected graphs can be processed, but undirected graphs are more common in actual problems)
- Disadvantages: Cannot handle graphs with negative weights
- Algorithm that can handle negative weights: Bellman-Ford algorithm
- The difference between the Bellman-Ford algorithm and the DJ algorithm: Nodes are no longer distinguished into visited or unvisited states, because the Bellman-Ford model uses cycles to update weights, and every cycle, the Bellman-Ford algorithm will update all Node information.
- The Bellman-Ford algorithm actually deals with directed graphs with negative weights. (And the directed graph cannot contain negative weight cycles)
- Disadvantages: The Bellman-Ford algorithm does not support graphs containing negative weight cycles. (Nor can Floyd’s algorithm)
4.1 Negative weight loop
- There is a loop in the figure where the sum of the weights around a circle is negative.
- Undirected graphs containing negative weights are all negative weight loops, as shown in the figure below (can loop infinitely between 2 and 3)
- Don’t worry too much about problems that the algorithm cannot solve: because graphs with negative weights are very rare, and once negative weights appear, they are often in directed graphs.
4.2 Matlab calculates the shortest path
4.2.1 Return the shortest distance from the starting point to the end point
-
Code:
[P,d] = shortestpath(G,start,end [,'Method',algorithm] )
-
Note: This function is only available after matlab2015b
-
Function: Return the shortest path from the start node to the end node in graph G
-
Input parameters:
- ① G - Input graph (graph object | digraph object)
- ② start the starting node
- ③ end node of target
- ④ [,‘Method’,algorithm] is an optional parameter, indicating the algorithm for calculating the shortest path. Generally, there is no need to set it manually. The default setting is "auto". The specific parameters that can be set are as follows.
-
Output parameters:
- ① P – the node passed by the shortest path
- ② d – shortest distance
-
-
Matlab demo
- For the specific code part, see Section 8 Code Files
code.m
- △Note:
- The graph nodes in Matlab must be numbered starting from 1, so all 0s are changed to 9 in the above figure.
- It is best to number consecutively starting from 1. Do not define numbers casually.
- For the specific code part, see Section 8 Code Files
-
Matlab returns the shortest path result from the starting point to the end point:
4.2.2 Matlab returns the shortest path between any two points
- Code:
d = distances(G [,'Method',algorithm])
4.2.3 Find all points within a given range
- Code:
[nodeIDs,dist] = nearest(G,s,d [,'Method',algorithm])
- Returns all nodes in the graph G that are within a distance d of node s.
- nodeIDs are nodes that meet the criteria
- Dist is the distance between these nodes and s
5. After-class paper assignments
postscript
- The reference course can be found at Qingfeng Digital Analog at Station B. The above is only for personal notes after study.