1function DiGraph = digraph (G, option)
2%DIGRAPH convert a GraphBLAS matrix into a MATLAB directed DiGraph.
3% DiGraph = digraph (G) converts a GraphBLAS matrix G into a directed
4% MATLAB DiGraph.  G must be square.  If G is logical, then no weights are
5% added to the DiGraph.  If G is single or double, these become the weights
6% of the MATLAB DiGraph.  If G is integer, the DiGraph is constructed with
7% weights of type double.
8%
9% DiGraph = digraph (G, 'omitselfloops') ignores the diagonal of G, and the
10% resulting MATLAB DiGraph has no self-edges.  The default is that
11% self-edges are created from any diagonal entries of G.
12%
13% Example:
14%
15%   G = GrB (sprand (8, 8, 0.2))
16%   DiGraph = digraph (G)
17%   h = plot (DiGraph) ;
18%   h.NodeFontSize = 20 ;
19%   h.ArrowSize = 20 ;
20%   h.LineWidth = 2 ;
21%   h.EdgeColor = [0 0 1] ;
22%   t = title ('random directed graph with 8 nodes') ;
23%   t.FontSize = 20 ;
24%
25% See also graph, digraph, GrB/graph.
26
27% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
28% SPDX-License-Identifier: GPL-3.0-or-later
29
30G = G.opaque ;
31
32[m, n, type] = gbsize (G) ;
33if (m ~= n)
34    error ('G must be square') ;
35end
36
37% get the string options
38omitself = false ;
39if (nargin > 1)
40    if (isequal (lower (option), 'omitselfloops'))
41        omitself = true ;
42    else
43        error ('unknown option') ;
44    end
45end
46
47% apply the options
48if (omitself)
49    % ignore diagonal entries of G
50    G = gbselect ('offdiag', G, 0) ;
51end
52
53% construct the digraph
54switch (type)
55
56    case { 'single' }
57
58        % The MATLAB digraph(...) function can accept x as single, but not
59        % from a MATLAB sparse matrix.  So extract the tuples of G first.
60        [i, j, x] = gbextracttuples (G) ;
61        DiGraph = digraph (i, j, x, n) ;
62
63    case { 'logical' }
64
65        % The MATLAB digraph(...) function allows for logical
66        % adjacency matrices (no edge weights are created).
67        DiGraph = digraph (gbmatlab (G, 'logical')) ;
68
69    otherwise
70
71        % typecast to double
72        DiGraph = digraph (gbmatlab (G, 'double')) ;
73end
74
75