1function ssgplot (A, xyz, directed, nodename)
2%SSGPLOT draw a plot of the graph of a sparse matrix
3% Usage:
4%   ssgplot (A, xyz, directed, nodename)
5%
6% A: a square sparse matrix of size n-by-n.
7% xyz: an n-by-2 or n-by-3 array of the XY or XYZ coordinates of each node.
8% directed: 1 if A is directed, 0 otherwise.  0 if not present.
9% nodename: a char array with n rows, or empty, containing the name of each
10%   node.  Not used if not present.
11%
12% Example:
13%
14%   Problem = ssget ('Pajek/football') ;
15%   ssgplot (Problem.A, Problem.aux.coord, 1, Problem.aux.nodename) ;
16%
17% See also gplot, ssget.
18
19% Copyright 2006-2019, Timothy A. Davis
20
21%-------------------------------------------------------------------------------
22% check inputs
23%-------------------------------------------------------------------------------
24
25[m n] = size (A) ;
26if (m ~= n)
27    error ('A must be square') ;
28end
29
30if (nargin < 4)
31    nodename = [ ] ;
32end
33if (nargin < 3)
34    directed = 0 ;
35end
36
37%-------------------------------------------------------------------------------
38% determine if 2D or 3D
39%-------------------------------------------------------------------------------
40
41if (size (xyz, 2) == 2)
42
43    %---------------------------------------------------------------------------
44    % no Z coordinates, draw a 2D graph
45    %---------------------------------------------------------------------------
46
47    xy = xyz ;
48    if (directed)
49	ti = '2D directed graph' ;
50    else
51	ti = '2D undirected graph' ;
52    end
53
54else
55
56    %---------------------------------------------------------------------------
57    % a 3D graph; rotate for better viewing
58    %---------------------------------------------------------------------------
59
60    dx = -45 ;
61    dy = 45 ;
62    dz = 45 ;
63    a = dx * (2*pi/360) ;
64    xrotation = [
65	1      0      0
66	0  cos(a) sin(a)
67	0 -sin(a) cos(a) ] ;
68    b = dy * (2*pi/360) ;
69    yrotation = [
70	cos(b)  0 -sin(b)
71	    0   1      0
72	sin(b)  0  cos(b) ] ;
73    c = dz * (2*pi/360) ;
74    zrotation = [
75	 cos(c) sin(c) 0
76	-sin(c) cos(c) 0
77	     0      0  1 ] ;
78    r = xrotation * yrotation * zrotation ;
79    xy = xyz * r ;
80    xy = xy (:,1:2) ;
81    if (directed)
82	ti = '3D directed graph' ;
83    else
84	ti = '3D undirected graph' ;
85    end
86end
87
88%-------------------------------------------------------------------------------
89% draw the graph
90%-------------------------------------------------------------------------------
91
92[X,Y] = gplot (A, xy) ;
93if (n < 100)
94    msize = 12 ;
95else
96    msize = 6 ;
97end
98
99if (n < 200)
100    if (directed)
101	plot (X, Y, 'mo', 'MarkerEdgeColor', 'k', ...
102	    'MarkerFaceColor', [.49 1 .63], 'MarkerSize', msize) ;
103	hold on
104	axis equal
105	axis off
106	for k = 0:(length(X) / 3)-1
107	    [x1 y1] = dsxy2figxy (gca, X (3*k+1), Y (3*k+1)) ;
108	    [x2 y2] = dsxy2figxy (gca, X (3*k+2), Y (3*k+2)) ;
109	    annotation ('arrow', [x1 x2], [y1 y2], 'Color', [1 0 1], ...
110		'HeadWidth', 4, 'HeadLength', 8) ;
111	end
112    else
113	plot (X, Y, '-mo', 'MarkerEdgeColor', 'k', ...
114	    'MarkerFaceColor', [.49 1 .63], 'MarkerSize', msize) ;
115	hold on
116	axis equal
117	axis off
118    end
119    if (~isempty (nodename) && n < 100)
120	for k = 1:n
121	    text (xy (k,1), xy (k,2), ['  ' nodename(k,:)], ...
122		'Interpreter', 'none') ;
123	end
124    end
125else
126    plot (X, Y, '-m.', 'MarkerEdgeColor', 'k') ;
127    axis equal
128    axis off
129end
130
131title (ti) ;
132
133hold off
134