digraph

Creates a directed graph from edges or an adjacency matrix.

Category: Mathematics > Graph and Network Algorithms

Syntax

D = digraph(s, t)

Edge k goes from node s(k) to node t(k).

D = digraph(s, t, w)

Also gives the edges weights w.

D = digraph(s, t, w, names)

Names the nodes.

D = digraph(A)

Builds the digraph from a square adjacency matrix; nonzero entries are edges and their values are weights.

Description

Unlike graph, each edge has a direction, and s -> t and t -> s are different edges that can coexist. D.Edges is a table whose first column EndNodes holds the pairs and which gains a Weight column when weights are given. D.Nodes holds node properties such as Name.

Directed graphs model one-way relations: dependencies, flows, links. Functions such as successors, predecessors, indegree, isdag and conncomp(..., 'Type', 'strong') only make sense with directions.

Input arguments

s, t
Source and target nodes: positive integers, or node names given as cell arrays of text.
w
Numeric weights, one per edge.
A
Square matrix. A(i,j) ~= 0 makes an edge from i to j.

Output arguments

D
A digraph object with the properties Edges and Nodes, both tables.

Example

From edge lists and from a matrix

D = digraph([1 2], [2 3], [5 6]);
disp(D.Edges)
E = digraph([0 1 0; 0 0 1; 1 0 0]);
disp(E.Edges)
Output
    EndNodes    Weight
    ________    ______

     1    2       5   
     2    3       6   
    EndNodes    Weight
    ________    ______

     1    2       1   
     2    3       1   
     3    1       1

See also

graph successors predecessors isdag shortestpath