How to add an edge between two nodes of two different graphs?

I'm trying to connect the nodes of graph g (Blue) with the nodes of graph h (Red).
so=[1 1 1 2 2 2 2 3 3 3 5];
ta=[2 3 4 3 4 5 6 6 7 5 7];
g=graph(so,ta);
p=plot(g);
x=p.XData;
y=p.YData;
z2=[0 0 0 0 0 0 0];
subplot(2,2,1);plot(g,'XData',x,'YData',y,'ZData',z2)
s=[1 1 1 2 2 3 3 3 3 4 4 5 5 ];
t=[2 5 6 3 4 7 4 5 6 7 5 7 6 ];
h=graph(s,t);
z=[-3 -3 -3 -3 -3 -3 -3];
hold on; subplot(2,2,1);plot(h,'XData',x,'YData',y,'ZData',z)
Is it something doable?
Thank you all for your continuous support!

 Respuesta aceptada

You could also add the edges of both graphs to one larger graph, and then use addedge to connect them. You would probably not get a clear separation visually of what used to be the first graph and the second graph this way, though.
so=[1 1 1 2 2 2 2 3 3 3 5];
ta=[2 3 4 3 4 5 6 6 7 5 7];
g=graph(so,ta);
p=plot(g);
x=p.XData;
y=p.YData;
z2=[0 0 0 0 0 0 0];
s=[1 1 1 2 2 3 3 3 3 4 4 5 5 ];
t=[2 5 6 3 4 7 4 5 6 7 5 7 6 ];
h=graph(s,t);
z=[-3 -3 -3 -3 -3 -3 -3];
gh = graph([so, s+7], [ta, t+7])
gh = addedge(gh, 2, 4+7)
plot(gh,'XData',[x, x],'YData',[y, y],'ZData',[z2, z2-3])

6 comentarios

Wow! Such a smart approach to tackle my problem.
Thanks, Dr. Tobler for taking some time to help me on this.
I'm interested in using Matlab for Dynamic/temporal graphs, would you kindly recommend me some useful material and documentation?
Thanks, Christine.
Thanks, Wassem!
Asaf McRock, sorry I just noticed your comment now. MATLAB doesn't have dedicated functionality for dynamic or temporal graphs, the best way to go might be to (a) store a cell array of graph objects or (b) if only edge weights are changing for example, storing a separate numedges(G)-by-numberOfTimeSteps array with these.
I'd be interested to know more about what functionality are you looking for in dynamic or temporal graphs?
Thanks for your reply, Dr. Tobler! Much appreciated.
I read a very nice scientific paper recently about temporal/ dynamic graph which was written by folks from twitter. And I like the idea that a social network ( graph) evolve with time. Users are nodes, relationship between users are the links of the graph. A new user could join the network at one time step and another one might leave at different time step. They use edge weights to represent time steps, and this was new to me and I didn't fully understand it. So, I said maybe it's better for me to play with some MATLAB code about dynamic graph, you know, just to have a better understanding.
To be quite honest with you I could not understand points a and b you mentioned, I'd appreciate if you can please elaborate, perhaps by a simple line of code.
Thanks again!
Here are some small examples of what I meant
a)
graphs = {graph(zeros(10)};
for ii=1:10 % time steps
graphs{ii+1} = addedge(graphs{ii}, randi(10), randi(10));
end
This adds a random new edge to the graph at each timestep, and stores the graphs for each timestep in the cell array graphs.
b)
Let's say we have a graph representing a train network, where each edge is the connection between two places. If the dynamic is only about how many trains are running each edge per day, this can simply be stored in a matrix numTrains(edgeID, dayID). If a connection isn't used at all in a day, instead of removing the edge we can simply set that entry in numTrains to zero.
This only works for simpler cases, but in those cases it makes manipulating the data much easier than working with a cell array of graphs.
Asaf McRock
Asaf McRock el 3 de Feb. de 2021
Editada: Asaf McRock el 3 de Feb. de 2021
OMG! I wonder when can I reach this level of creativity.
Very interesting small examples indeed. I will start practicing and learning from here.
Thanks for your time and continuous support!

Iniciar sesión para comentar.

Más respuestas (1)

v1=[x(1) x(1)];
v2=[y(1) y(1)];
v3=[z2(1) z(1)];
j1=[x(2) x(4)];
j2=[y(2) y(4)];
j3=[z2(2) z(4)];
line(v1,v2,v3,'Color','red','LineStyle','--')
line(j1,j2,j3,'Color','red','LineStyle','--')

1 comentario

It's not possible to add an edge between nodes of different graphs. So, this is the only way, I guess, to visualize an interlink between nodes of different graph. It might be inefficient though especially for larger number of nodes.

Iniciar sesión para comentar.

Categorías

Más información sobre Networks en Centro de ayuda y File Exchange.

Preguntada:

el 15 de En. de 2021

Editada:

el 3 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by