function Graph = graph (G, varargin)
%GRAPH convert a GraphBLAS matrix into a undirected Graph.
% Graph = graph (G) converts a GraphBLAS matrix G into an undirected Graph.  G
% is assumed to be symmetric; only tril (G) is used by default.  G must be
% square.  If G is logical, then no weights are added to the Graph.  If G is
% single or double, these become the weights of the Graph.  If G is integer,
% the Graph is constructed with weights of type double.
%
% Graph = graph (G, ..., 'upper') uses triu (G) to construct the Graph.
% Graph = graph (G, ..., 'lower') uses tril (G) to construct the Graph.  The
% default is 'lower'.
%
% Graph = graph (G, ..., 'omitselfloops') ignores the diagonal of G, and the
% resulting Graph has no self-edges.  The default is that self-edges are
% created from any diagonal entries of G.
%
% Example:
%
%   G = GrB (bucky) ;
%   Graph = graph (G)
%   plot (Graph)
%
% See also graph, digraph, GrB/digraph.

% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2026, All Rights Reserved.
% SPDX-License-Identifier: Apache-2.0

if (gb_is_grb (G))
    G = struct (G) ;
end

[m, n, type] = gbmex_size (G) ;
if (m ~= n)
    error ('GrB:error', 'G must be square') ;
end

% get the string options
side = 'lower' ;
omitself = false ;
for k = 1:nargin-1
    arg = lower (varargin {k}) ;
    switch arg
        case { 'upper', 'lower' }
            side = arg ;
        case { 'omitselfloops' }
            omitself = true ;
        otherwise
            error ('GrB:error', 'unknown option') ;
    end
end

% apply the options
if (omitself)
    % ignore diagonal entries of G
    if (isequal (side, 'upper'))
        G2 = gzb_select (1, 'triu', G, 1) ;
    else
        G2 = gzb_select (1, 'tril', G, -1) ;
    end
else
    % include diagonal entries of G
    if (isequal (side, 'upper'))
        G2 = gzb_select (1, 'triu', G, 0) ;
    else
        G2 = gzb_select (1, 'tril', G, 0) ;
    end
end

switch (type)

    case { 'single' }

        % The graph(...) function can accept x as single, but not from a
        % built-in sparse matrix.  So extract the tuples of G2 first.
        gbmex_wait (G2) ;
        [i, j, x] = gbmex_extracttuples (1, G2) ;
        Graph = graph (i, j, x, n) ;

    case { 'logical' }

        % The digraph(...) function allows for logical
        % adjacency matrices (no edge weights are created).
        Graph = graph (gb_builtin (gzb_cast (G2, 'logical')), side) ;

    otherwise

        % typecast to double
        Graph = graph (gb_builtin (gzb_cast (G2, 'double')), side) ;

end

