How can I get the root node of a given node from a directed graph ?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hello,
I am using graph for image processing, 1 node = 1 pixel.
I computed the graph but I need to find for each node its root node.
Is there any function or a quite performant solution as the number of nodes is quite high ?
Thank you,
Anatole Jimenez
채택된 답변
Guillaume
2019년 12월 18일
the first element of the vector returned by toposort would be your root node. Of course, your graph must be acyclic and if your graph has several roots, this will only be one of them.
Perhaps easier would be to look in the edge table and find which nodes don't appear at all as target nodes. Assuming the Nodes table is empty:
rootnodes = setdiff(1:height(yourgraph.Nodes), yourgraph.Edges.EndNodes(:, 2))
댓글 수: 8
Thank you for your answer.
Indeed, my graphs are acyclics but have many roots.
Unfortunately, listing all the nodes is not my request. What I want is to associate each nodes with its root node. Something like : rootnode = getRootNode( myGraph, node )
Attached a figure of one of my graphs.
The graph object would be more useful than the figure.
Some of your nodes have more than one root, what should be returned then?
What are you calling a root node?
When I look at one of the subgraph of your graph:
bins = G.conncomp('OutputForm', 'cell', 'Type', 'weak');
plot(G.subgraph(bins{1}))
I get this:

Node 1 has nodes 20, 21, 22, 23, 3, 4, 5, 10,15 as roots.
edit: perhaps what you call a root is node 1 in the above graph, in which case either your graph is directed the wrong way or you're using the wrong terminology. I'm not sure what the correct term actually is, but it is something like a sink or a terminal
Anyway, it doesn't matter much if you're looking for roots instead of terminals. Simply replace outdegree by indegree for roots in the code below
bins = G.conncomp('Type', 'weak'); %since the graph is DAG all components are weakly connected
terminalidx = find(G.outdegree == 0); %which nodes are terminal
assert(numel(unique(bins(terminalidx))) == numel(terminalidx), 'At least one subgraph has more than one terminal') %optional check
matchingterminal = terminalidx(bins); %replicate terminal index in each bin
matchingterminal(n) gives you the index of the terminal node for node n.
Sorry for the misunderstanding, by root node I meant the node 1. I agree that the direction of the edges can be misleading.
My graphs are supposed to link each nodes to its minimum neighbor, in order to group the nodes to a local minimum, here the node 1. I would like to have something like :
node1 = get_root_node( myGraph, node17 )
Thank you again for your time.
This is exactly what I am looking for. Thank you Guillaume for your help.
I just tried what you submit but It didn't work.
The bins from the G.conncomp('Type', 'weak') property are not sort with the terminalidx from find(G.outdegree == 0).
By example the node 6 has the node 1019 as terminal but the bins(6) is 2 and terminalidx(2) = 11.
I think bins number are not terminalidx index.
Indeed, I made an assumption that the terminals would be in the same order as the bins, which is generally incorrect. Fixed code:
bins = G.conncomp('Type', 'weak'); %since the graph is DAG all connections are weak
terminalidx = find(G.outdegree == 0); %which nodes are terminal
terminalbin = bins(terminalidx)
assert(isequal(unique(terminalbin), 1:numel(terminalbin)), 'At least one subgraph has more than one terminal') %optional check
[~, termorder] = sort(terminalbin); %find ordering of terminals according to the bin they're in
terminalidx = terminalidx(termorder); %and reorder them accordingly
matchingterminal = terminalidx(bins); %replicate terminal index in each bin
Note that I would store all this information, together with the nodes X and Y location into the node table:
G.Nodes.Isterminal = G.outdegree == 0;
G.Nodes.MatchingTerminal = matchingterminal;
G.Nodes.Subgraph = bins;
G.Nodes.X = ??? %Nx1 column vector
G.Nodes.Y = ??? %Nx1 column vector
which makes for easy plotting
plot(G, 'XData', G.Nodes.X, 'YData', G.Nodes.Y, 'NodeCdata', G.Nodes.IsTerminal+1); %to highlight the terminals
추가 답변 (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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
