1

I am looking for a way to extract the branches of a digraph in MATLAB. Precisely, if I have a network (my_digraph) similar to the following: the network

When I enter my_digraph.plot I recieve this: the different branches of the network

I could now manually write down the branches by looking at the picture e.g. [1, 2], [1, 3], [1, 6, 7] etc. However, in bigger networks this is not possible anymore. With which function can I extract this information from the picture so it gives me something like an array containing these vectors?

Note: I know that the pictures above are not digraphs, since they do not have a direction. However, they still show the principle.

00koeffers
  • 327
  • 2
  • 11

1 Answers1

1

As I understand your question you want all shortest paths from node 1 to all "end-nodes" (nodes of degree 1).

Let's first define the graph:

edges = [
  [1,2],
  [1,3],
  [1,4],
  [1,5],
  [1,6],
  [6,7],
  [6,8],
  [6,9],
  [6,10],
  [6,11]]

G = graph(edges(:,1),edges(:,2))

Now, let's define our start node (1) and find all end nodes, which we obtain as the indices of all nodes of degree 1 (i.e. nodes connected only via one edge)

node_start = 1
nodes_degree = degree(G)
nodes_end = find(nodes_degree == 1)

Now we iterate through all end nodes and for each find the shortest path from the start node to the respective end node. We store the resulting arrays of nodes along the shortes path in the paths cell.

paths = {}
for path_idx = 1:numel(nodes_end)
  node_end = nodes_end(path_idx)
  path = shortestpath(G,node_start,node_end)
  paths{path_idx} = path
end

Our paths cell now holds all shortest paths. For example:

disp(paths{5})

>>>  1   6   7

Or to display all

cellfun(@(path) disp(path), paths) 

>>>  1     2

     1     3

     1     4

     1     5

     1     6     7

     1     6     8

     1     6     9

     1     6    10

     1     6    11
  • I had to use `outdegree(G)` instead of `degree(G)` since it is a digraph and therefore switch the `find(nodes_degree == 1)` to `find(nodes_degree == 0)` then the solution worked out great. – 00koeffers Oct 15 '19 at 15:14