Last active
October 7, 2020 14:33
-
-
Save naoh16/e19999b0d3ab86b6358c3557aa073219 to your computer and use it in GitHub Desktop.
情報工学実験B グラフ描画の例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Example of Graph A | |
A = [0 1 1 0 0; | |
1 0 0 0 0; | |
1 0 0 1 1; | |
0 0 1 0 0; | |
0 0 1 0 0 | |
]; | |
initial_node = 1; | |
target_node = 5; | |
% graphとしてプロットした際に戻り値を保存しておく | |
% 有向グラフなら digraph を使う | |
% 無向グラフなら graph を使う | |
G = graph(A); | |
p = plot(G, 'EdgeLabel',G.Edges.Weight); | |
% スタートとゴールを目立つようにする | |
% 星形 'p', 〇 'o' | |
highlight(p, initial_node, 'Marker', 'p', 'MarkerSize', 10); | |
highlight(p, target_node, 'Marker', 'o', 'MarkerSize', 10); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Example of Graph B | |
A = [0 1 0 1 0 0 0 0; | |
1 0 1 0 1 0 0 0; | |
0 1 0 1 0 1 0 0; | |
1 0 1 0 0 0 1 0; | |
0 1 0 0 0 1 0 1; | |
0 0 1 0 1 0 1 0; | |
0 0 0 1 0 1 0 1; | |
0 0 0 0 1 0 1 0 | |
]; | |
labels = {'S' 'A' 'B' 'C' 'D' 'E' 'F' 'G'}; % セル配列で与えるため,中かっこを使う. | |
initial_node = 1; | |
target_node = 8; | |
% graphとしてプロットした際に戻り値を保存しておく | |
% 有向グラフなら digraph を使う | |
% 無向グラフなら graph を使う | |
G = graph(A); | |
p = plot(G, 'EdgeLabel',G.Edges.Weight); | |
% ラベル情報を与える | |
p.NodeLabel = labels; | |
% XY座標も指定したいなら・・・ | |
p.XData = [1 2 2 2 3 3 3 4]; % x座標; | |
p.YData = [2 1 2 3 1 2 3 2]; % y座標 | |
% スタートとゴールを目立つようにする | |
% 星形 'p', 〇 'o' | |
highlight(p, initial_node, 'Marker', 'p', 'MarkerSize', 10); | |
highlight(p, target_node, 'Marker', 'o', 'MarkerSize', 10); | |
% 特定のパスを赤(r)で目立たせる | |
route = [1 2 3 6 7 8]; | |
highlight(p, route, 'EdgeColor','r'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Example of Graph C | |
A = [0 2 0 0 0 0 0 0; | |
2 0 2 0 2 0 0 0; | |
0 2 0 0 0 4 0 0; | |
0 0 0 0 1 0 2 0; | |
0 2 0 11 0 0 0 0; | |
0 0 4 0 0 0 3 0; | |
0 0 0 12 0 3 0 1; | |
0 0 0 0 0 0 1 0 | |
]; | |
labels = {'S' 'A' 'B' 'C' 'D' 'E' 'F' 'G'}; % セル配列で与えるため,中かっこを使う. | |
initial_node = 1; % または,= find([labels{:}] == 'S') | |
target_node = 8; % または,= find([labels{:}] == 'G') | |
% graphとしてプロットした際に戻り値を保存しておく | |
% 有向グラフなら digraph を使う | |
% 無向グラフなら graph を使う | |
G = digraph(A); | |
p = plot(G, 'EdgeLabel',G.Edges.Weight); | |
% ラベル情報を与える | |
p.NodeLabel = labels; | |
% XY座標も指定したいなら・・・ | |
p.XData = [1 1 1 2 2 3 3 3]; % x座標; | |
p.YData = [4 2 1 3 2 1 3 4]; % y座標 | |
% スタートとゴールを目立つようにする | |
% 星形 'p', 〇 'o' | |
highlight(p, initial_node, 'Marker', 'p', 'MarkerSize', 10); | |
highlight(p, target_node, 'Marker', 'o', 'MarkerSize', 10); | |
% 特定のパスを赤(r)で目立たせる | |
route = [1 2 3 6 7 8]; | |
highlight(p, route, 'EdgeColor','r'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment