The graph's breadth and depth are traversed.
CLC;
Clear all;
Close all;
% Initialize the adjacent compressed table
Compresstable = [1 2; 1 3; 1 4; 2 4; 2 5; 3 6; 4 6; 4 7];
Max_vertex = max (compresstable (:)); % the maximum value in the compressed table is the width and height of the adjacent matrix.
Graph_matrix = compresstabletomatrix (compresstable); % Matrix Representation of the structure map from the list of joined tables
[X, y] = cylinder (1, max_vertex );
Plot (x (1, :), y (1, :), 'r * ', 'markersize', 12)
Hold on
For I = 1: max_vertex
TEM = ['V', int2str (I)];
Text (x (1, I) + 0.1, y (1, I), TEM );
End
For I = 1: length (compresstable)
Plot (x (1, compresstable (I, :)), y (1, compresstable (I, :)), 'K-', 'linewidth', 3 );
End
% BFS
% Head = 1; % construct the opposite
% Tail = 1; % construct team end
% Queue (head) = 1; % add the first node of the graph to the header
% Head = head + 1; % queue Extension
%
% Flag = 1; % mark accessed
% Result_matrix = []; % result matrix
% While tail ~ = Head
% I = Queue (tail );
% For j = 1: max_vertex
% If graph_matrix (I, j) = 1 & isempty (find (flag = J, 1 ))
% Queue (head) = J;
% Head = head + 1;
% Flag = [flag, J];
% Result_matrix = [result_matrix, I, j];
% End
% End
% Tail = tail + 1;
% End
% Graph_result_matrix = compresstabletomatrix (compresstable );
%
% [X, y] = cylinder (1, max_vertex );
% Plot (x (1, :), y (1, :), 'r * ', 'markersize', 12)
% Hold on
% For I = 1: max_vertex
% TEM = ['V', int2str (I)];
% Text (x (1, I) + 0.1, y (1, I), TEM );
% End
% For I = 1: length (compresstable)
% Plot (x (1, compresstable (I, :)), y (1, compresstable (I, :)),'m.-', 'linewidth', 1 );
% End
% DFS
Top = 1;
Stack (top) = 1;
Flag = 1; % mark accessed
Result_matrix = []; % result matrix
While top ~ = 0
Pre_len = length (stack );
I = stack (top );
For j = 1: max_vertex
If graph_matrix (I, j )~ = 0 & isempty (find (flag = J, 1 ))
Top = Top + 1;
Stack (top) = J;
Flag = [flag, J];
Result_matrix = [result_matrix, I, j];
End
End
If length (stack) = pre_len
Stack (top) = [];
Top = Top-1;
End
End
Graph_result_matrix = compresstabletomatrix (compresstable );
[X, y] = cylinder (1, max_vertex );
Plot (x (1, :), y (1, :), 'r * ', 'markersize', 12)
Hold on
For I = 1: max_vertex
TEM = ['V', int2str (I)];
Text (x (1, I) + 0.1, y (1, I), TEM );
End
For I = 1: length (compresstable)
Plot (x (1, compresstable (I, :)), y (1, compresstable (I, :)),'m.-', 'linewidth', 1 );
End
Function graph_matrix = compresstabletomatrix (compresstable)
Max_vertex = max (compresstable (:));
Graph_matrix = ones (max_vertex );
For I = 1: max_vertex
Graph_matrix (compresstable (I, 1), compresstable (I, 2) = 1;
Graph_matrix (compresstable (I, 2), compresstable (I, 1) = 1;
End
End
BST and DST simple MATLAB program (graph breadth and depth traversal)