How to find graph density in MATLAB for a given graph G?

 Réponse acceptée

You can simply calculate the number of non-zero elements relative to total elements in the adjacency matrix:
nnz(adjacency(G))./numel(adjacency(G))

3 commentaires

Thanks! Yes that works. Here is another version that I did which gives almost similar result:
num_edges = height(G.Edges); % number of edges
num_nodes = height(G.Nodes); % number of nodes
% http://reference.wolfram.com/language/ref/GraphDensity.html
graph_density = num_edges/num_nodes /(num_nodes -1)*2; % for directed graph remove factor 2
You can use the numnodes and numedges functions on a graph or digraph to get the numbers of nodes or edges respectively.
Great, thanks. So this one works too:
%http://reference.wolfram.com/language/ref/GraphDensity.html
graph_density = numedges(G)/numnodes(G) /(numnodes(G) -1)*2 %factor 2 for undirected, factor 1 for directed

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Centre d'aide et File Exchange

Produits

Version

R2021a

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by