Matleb Learning--Graph Theory Shortest Path Problem

1. Graph theory:

Graph Theory is a branch of mathematics. It takes pictures as the research object. A graph in graph theory is a graph composed of a number of given points and a line connecting two points. This graph is usually used to describe a certain relationship between certain things. Points represent things, and points connect two points. The line of represents that there is such a relationship between the corresponding two things.
There are two types of graphs in graph theory, directed graphs and undirected graphs . As the name implies, whether or not the line connecting two points has an arrowhead. In my opinion, an undirected graph is also a kind of directed graph, but the line connecting two points in a directed graph is a two-way arrow.

2. Two methods of drawing:

  1. Online drawing of the website ( recommended ) Click me to enter the website.
    After entering the website, the interface is as follows:
    insert image description hereFirst, let me talk about the necessary functions
Node Count: Set the number of points, that is, the number of circles on the right
Graph Data: A single column of numbers is to write the number points in the circle (the sum of the numbers in a single column is the Node Count), and a double column of numbers is to determine the relationship between the circles. It can also be increased to three columns, that is, to increase the weight. See the specific demonstration Below
0(1)-index The lowest number inside the circle starts from 0 (1)
Download as PNG export as image
Undirected(Directed) undirected graph (directed graph)

Then explain some functions
**Single-column number function and relationship with Node Count
insert image description here
**Double-column number function (be sure to pay attention to spaces)
insert image description here
**Add weighted graph theory
insert image description hereinsert image description here
**Graph theory with arrows
insert image description here

  1. Matleb drawing
    First upload the code, if you can understand the code, you can directly move to the third part. (The code is from Mr. Qingfeng, it can be deleted for infringement)
%% Matlab作无向图
% (1)无权重(每条边的权重默认为1)
% 函数graph(s,t):可在 s 和 t 中的对应节点之间创建边,并生成一个图
% s 和 t 都必须具有相同的元素数;这些节点必须都是从1开始的正整数,或都是字符串元胞数组。
s1 = [1,2,3,4];
t1 = [2,3,1,1];
G1 = graph(s1, t1);
plot(G1)
% 注意哦,编号最好是从1开始连续编号,不要自己随便定义编号

% 注意字符串元胞数组是用大括号包起来的哦
s2 = {
    
    '学校','电影院','网吧','酒店'};
t2 = {
    
    '电影院','酒店','酒店','KTV'};
G2 = graph(s2, t2);
plot(G2, 'linewidth', 2)  % 设置线的宽度
% 下面的命令是在画图后不显示坐标
set( gca, 'XTick', [], 'YTick', [] );  

% (2)有权重
% 函数graph(s,t,w):可在 s 和 t 中的对应节点之间以w的权重创建边,并生成一个图
s = [1,2,3,4];
t = [2,3,1,1];
w = [3,8,9,2];
G = graph(s, t, w);
plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) 
set( gca, 'XTick', [], 'YTick', [] );  

%% Matlab作有向图
% 无权图 digraph(s,t)
s = [1,2,3,4,1];
t = [2,3,1,1,4];
G = digraph(s, t);
plot(G)
set( gca, 'XTick', [], 'YTick', [] );  

% 有权图 digraph(s,t,w)
s = [1,2,3,4];
t = [2,3,1,1];
w = [3,8,9,2];
G = digraph(s, t, w);
plot(G, 'EdgeLabel', G.Edges.Weight, 'linewidth', 2) 
set( gca, 'XTick', [], 'YTick', [] );  

function description

graph(s,t) Edges can be created between corresponding nodes in s and t, and a graph
graph(s,t,w) can create edges with weight w between corresponding nodes in s and t, and generate a graph

Other code comments have been given in detail, just look at the code practice.

3. Weight adjacency matrix

The weight adjacency matrix is ​​to express the graph theory in a matrix. The relationship between itself and itself is 0, and the relationship between itself and others is infinite (Inf), and the relationship is the weight. According to graph theory, weight adjacency matrix can also be divided into weight adjacency matrix of directed graph and weight adjacency matrix of undirected graph. The elements on the main diagonal between the two are zero, and Dij represents the weight from the i-th node to the j-th node. The difference is that an undirected graph is a symmetric matrix, and a directed graph is generally not a symmetric matrix. The last two pictures are worth a thousand words.
insert image description hereinsert image description here

Guess you like

Origin blog.csdn.net/Fireworkyanhuo/article/details/107305872