I have two graphs, G1 and G2, and I would need to calculate the connected graph components conncomp() of the "merged graph" G1+G2.
Here an example:
% Create G1
s = [1 1 2 3 4 4 6 7 8];t = [2 5 3 5 6 7 9 9 9];x = [1 1 1 2 2 2 3 3.5 3];y = [1 2 3 4 1 3 4 3.5 3];G1 = graph(s,t);% Create G2
s2 = [1 2 3 4 1];t2 = [2 3 4 1 5];x2 = [1.5 1.5 3.5 3.5 1];y2 = [1.5 3.5 3.5 1.5 1];G2 = graph(s2,t2);% Plot G1 and G2
hold onplot(G1,'XData',x,'YData',y,'linewidth',2,'MarkerSize',2);plot(G2,'XData',x2,'YData',y2,'linewidth',2,'MarkerSize',2);hold off% Calculate conncomp of G1 and of G2
bin = conncomp(G1)bin = conncomp(G2)
Here below what I would like to get:
% Calculate conncomp of the merged graph G1+G2
bin = conncomp(G1+G2)
Initially, I was thinking to add all the nodes of the smallest graph into the largest one using addnode(), and then calculate conncomp(), but I am not sure about it anymore since I would need to modify iteratively both graphs for several times and calculate the resulting conncomp() after eavery graphs modification….
Any idea about a possible way to do it?
Best Answer