Highlight 2 lowest weight edges out of 3 from node

1 次查看(过去 30 天)
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.

采纳的回答

Rajeev
Rajeev 2023-1-23
编辑:Rajeev 2023-1-23
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);

更多回答(0 个)

类别

Help CenterFile 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