Nodes belongto which Graph?
2 views (last 30 days)
Show older comments
Dear matlab,
Please let me know if we can findout from the graph table which nodes belong to which graph, as in G.Edges in the graph function only shows connected nodes and linkages and weights, i.e node1 node2 weight
Can we have information that shows node and graph it belongs to, ex node1 graph1 node2 graph1 node3 graph2 node4 graph2 node5 graph2 node6 graph3
2 Comments
Walter Roberson
on 22 Oct 2016
Did you perhaps want to ask about which vertices belong to which subgraph?
Eng. Fredius Magige
on 22 Oct 2016
Hi An adjacency matrix (table) is pure a node to node connection. Either, movement of: TOP to RIGHT to TOP to RIGHT ..... or others are generated graphical model. whatever you want, it scientifically possible to get
Answers (1)
Walter Roberson
on 23 Oct 2016
If you have a series of graphs, G1, G2, G3, ... each of which has been created with graph(), or digraph() then:
Graphs = {G1, G2, G3, ... }; %create a cell array of all of the graphs
GraphNames = {'graph1', 'graph2', 'G3', ...}; %names of graphs to use in report
numgraph = length(Graphs);
nodes_are_numeric = false(numgraph, 1);
for K = 1 : numgraph
these_nodes = unique(Graphs{K}.Edges.EndNodes(:));
if ~iscell(these_nodes); these_nodes = num2cell(these_nodes); end
used_nodes{K} = these_nodes;
nodes_are_numeric(K) = isnumeric(these_nodes{1});
end
node_fmt_is_char = ~all(nodes_are_numeric);
if all(nodes_are_numeric) ~= any(nodes_are_numeric)
%there is a mix of node types, need to convert the numeric ones to character
for K = find(nodes_are_numeric(:)).'
used_nodes{K} = cellfun(@num2str, used_nodes{K}, 'Uniform', 0);
end
end
unique_nodes = unique( vertcat(used_nodes{:}) );
num_unique_nodes = length(unique_nodes);
node_belongs_to = cell(num_unique_nodes, 1);
for K = 1 : num_unique_nodes
this_node = unique_nodes{K};
node_is_in_graph = cellfun(@(N) ismember(this_node, N), used_nodes);
node_member_of = GraphNames(node_is_in_graph);
node_belongs_to{K} = [{this_node}, node_member_of];
end
%now the printed report
if node_fmt_is_char
fmt = 'Node %s belongs to graphs: ';
else
fmt = 'Node %g belongs to graphs: ';
end
for K = 1 : num_unique_nodes
this_entry = node_belongs_to{K};
fprintf(fmt, this_entry{1});
fprintf('%s, ', this_entry{2:end} );
fprintf('\n');
end
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!