Highlight 2 lowest weight edges out of 3 from node

1 Ansicht (letzte 30 Tage)
Michal Zimnicki
Michal Zimnicki am 15 Jan. 2023
Bearbeitet: Rajeev am 23 Jan. 2023
So I created digraph which have edges with weights.
s = [1 2 1 3 1 4 2 3 3 4 2 4];
t = [2 1 3 1 4 1 3 2 4 3 4 2];
weights = [10 5 15 6 20 8 9 13 9 12 10 8];
names = {'1' '2' '3' '4'};
G = digraph(s,t,weights,names)
p = plot(G,'Layout','force','EdgeLabel',G.Edges.Weight)
I wanted highlight edges which have lowest weights, so I started combine with mink.
[eid,nid] = mink(outedges(G,3),2)
It worked well with nodes 1,2 and 4, but on node 3 it highlighted wrong edges (should highlight egdes with weights 6 and 9).
I noticed that highlight is based on edges id. The problem is I have no idea how for example implement G.Edges.Weight into mink or what is another solution.

Akzeptierte Antwort

Rajeev
Rajeev am 23 Jan. 2023
Bearbeitet: Rajeev am 23 Jan. 2023
Hi,
You can pair the edges with their weights and then apply "mink" to get the edges with least weights.
Creating a function would be helpful in this case to avoid redundancy.
Also, I am not sure about the "nid" variable. The name suggests it to be the node id, but if we have the edge id, then we can get the source and the target node from it.
The sample function given below can be used to get the minimum k out edges from a node:
function [eid,wts] = get_k_min_outedg(G,k,n)
wght_outedge = [G.Edges.Weight(outedges(G,n)),outedges(G,n)];
min_wght_outedge = mink(wght_outedge,k);
wts = min_wght_outedge(:,1);
eid = min_wght_outedge(:,2);
end
Instead of getting the node ID, the function returns the weights along with the edge ids:
[eid,wts] = get_k_min_outedg(G,2,3);

Weitere Antworten (0)

Kategorien

Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by