Highlight 2 lowest weight edges out of 3 from node

조회 수: 1 (최근 30일)
Michal Zimnicki
Michal Zimnicki 2023년 1월 15일
편집: Rajeev 2023년 1월 23일
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