Hi all -
I plotted an graph and want to make some specific edges to be directed (digraph). Just to be a bit clearer: I have a full plotted and formatted minimum spanning tree from a graph which is not directed. I want to loop through the edges and, if a certain condition is met, replace the edge for a directed edge, pointing from node A to node B.
Is this possible? My graph would have both directed and undirected edges.
Best.

 채택된 답변

Christine Tobler
Christine Tobler 2017년 8월 4일

0 개 추천

I'm afraid it's not possible to have both directed and undirected edges in the same graph. To convert a graph to a digraph, you could use
gDirected = digraph(adjacency(gUndirected));
This will replace every undirected edge with two directed edges going in opposing directions. You could then use rmedge to remove the edges that go in the direction you don't want.
Within most graph algorithms, these two edges will behave the same way as the undirected edge did - but of course, the plot will look very different.

댓글 수: 5

Felipe  Schuback
Felipe Schuback 2017년 8월 7일
Hi Christine, thank you very much for the info. But just to make sure, you are saying that it is not possible in any way to get something like the photo I have attached? Some edges with no arrows and some with arrows (if some condition is met)?
Walter Roberson
Walter Roberson 2017년 8월 8일
In order to do that you would have to use two plot() of two different graphs, with the positions of each node carefully set so that everything lined up "just right".
Felipe  Schuback
Felipe Schuback 2017년 8월 8일
Thank you Walter, for your answer. I imagined I would have to do something like that. Do you have any suggestions on how I would be able to do something like this?
Reihaneh Jahedan
Reihaneh Jahedan 2023년 11월 8일
If our graph is weighted and has different weight in different edges, how can we preserve these weights while converting to directed graph?
In the following code, I replace self-loops by a single undirectional edge from the node to itself, with weight the same as the graph weight. There are some cases in which people might feel that instead a self-loop should be replaced with two unidirectional loops from the node to itself, each with the weight of the original self-loop... but that is not what I implemented.
G = graph([0.0 0.1 0.2 0.3
0.1 0.0 0.4 0.5
0.2 0.4 10.0 0.6
0.3 0.5 0.6 0.0
])
G =
graph with properties: Edges: [7×2 table] Nodes: [4×0 table]
HEdges1 = G.Edges;
HEdges2 = table(fliplr(HEdges1.EndNodes), HEdges1.Weight, 'VariableNames', {'EndNodes', 'Weight'});
HEdges = unique([HEdges1; HEdges2], 'rows');
H = digraph(HEdges);
H =
digraph with properties: Edges: [13×2 table] Nodes: [4×0 table]
plot(G,'EdgeLabel',G.Edges.Weight)
plot(H,'EdgeLabel',H.Edges.Weight)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by