How to find out number of source nodes and average indegree of a source node in directed graph?

조회 수: 1 (최근 30일)
I have a dataset from which I have created a directed graph.
How I want to find out indegree of source nodes and number of source node. And want to do the same thing for target nodes of that directed graph.
Here is my code.
m = readtable('rough.csv')
col1= m{:,1}
col2= m{:,2}
col3= m{:,3}
g= digraph(col1,col2,col3)
plot(g,'Layout','force','EdgeLabel',g.Edges.Weight)

채택된 답변

Srijith Kasaragod
Srijith Kasaragod 2021년 8월 5일
As per my understanding you have a directed graph and you want to compute indegree and number of source and target nodes. The following lines of code shows one of the possible ways:
%store edges and nodes into variables
edges= g.Edges;
nodes= g.Nodes;
%filter out source and target nodes from edges
s= unique([edges{:,1}(:,1)])
t= unique([edges{:,1}(:,2)])
%input graph and source or target nodes into ‘indegree’ function
in_deg_s= indegree(g,s);
in_deg_t= indegree(g,t);
%count the number of source and target nodes
no_source= numel(s);
no_target= numel(t);
You can refer this link to read more about 'indegree' function.
  댓글 수: 2
Mouli Sarkar
Mouli Sarkar 2021년 8월 6일
Thank you so much. Can you please tell me how to find selfloop? I mean if a node is regulating itself. How to find out those self regulating nodes?
Srijith Kasaragod
Srijith Kasaragod 2021년 8월 6일
@Mouli Sarkar I recommend you create another post for this question to get a faster response.

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

추가 답변 (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