digraph

Graph with directed edges

Description

digraph objects represent directed graphs, which have directional edges connecting the nodes. After you create a digraph object, you can learn more about the graph by using the object functions to perform queries against the object. For example, you can add or remove nodes or edges, determine the shortest path between two nodes, or locate a specific node or edge.

G = digraph([1 1], [2 3])
e = G.Edges
G = addedge(G,2,3)
G = addnode(G,4)
plot(G)

Create Object

Use the digraph function to create a directed graph object.

For undirected graphs, use the graph function.

Properties

expand all

Edges of graph, returned as a table. By default this is an M-by-1 table, where M is the number of edges in the graph.

  • To add new edge properties to the graph, create a new variable in the Edges table.

  • To add or remove edges from the graph, use the addedge or rmedge object functions.

Example: G.Edges returns a table listing the edges in the graph

Example: G.Edges.Weight returns a numeric vector of the edge weights.

Example: G.Edges.Weight = [10 20 30 55]' specifies new edge weights for the graph.

Example: G.Edges.NormWeight = G.Edges.Weight/sum(G.Edges.Weight) adds a new edge property to the table containing the normalized weights of the edges.

Data Types: table

Nodes of graph, returned as a table. By default this is an empty N-by-0 table, where N is the number of nodes in the graph.

  • To add new node properties to the graph, create a new variable in the Nodes table.

  • To add or remove nodes from the graph, use the addnode or rmnode object functions.

Example: G.Nodes returns a table listing the node properties of the graph. This table is empty by default.

Example: G.Nodes.Names = {'Montana', 'New York', 'Washington', 'California'}' adds node names to the graph by adding the variable Names to the Nodes table.

Example: G.Nodes.WiFi = logical([1 0 0 1 1]') adds the variable WiFi to the Nodes table. This property specifies that certain airports have wireless internet coverage.

Data Types: table

Object Functions

addedge Add new edge to graph
rmedge Remove edge from graph
flipedge Reverse edge directions
addnode Add new node to graph
rmnode Remove node from graph
findedge Locate edge in graph
findnode Locate node in graph
numedges Number of edges in graph
numnodes Number of nodes in graph
reordernodes Reorder graph nodes
subgraph Extract subgraph
bfsearch Breadth-first graph search
dfsearch Depth-first graph search
centrality Measure node importance
toposort Topological order of directed acyclic graph
transclosure Transitive closure
transreduction Transitive reduction
isdag Determine if graph is acyclic
conncomp Connected graph components
condensation Graph condensation
maxflow Maximum flow in graph
isisomorphic Determine whether two graphs are isomorphic
isomorphism Compute equivalence relation between two graphs
shortestpath Shortest path between two single nodes
shortestpathtree Shortest path tree from node
distances Shortest path distances of all node pairs
adjacency Graph adjacency matrix
incidence Graph incidence matrix
indegree In-degree of nodes
outdegree Out-degree of nodes
predecessors Node predecessors
successors Node successors
nearest Nearest neighbors within radius
plot Graph plot

Examples

expand all

Create a digraph object with three nodes and three edges. One edge is from node 1 to node 2, another is from node 1 to node 3, and the third is from node 2 to node 1.

G = digraph([1 1 2],[2 3 1])
G = 

  digraph with properties:

    Edges: [3×1 table]
    Nodes: [3×0 table]

View the edge table of the graph. For directed graphs, the first column indicates the source nodes of each edge, and the second column indicates the target nodes.

G.Edges
ans = 

    EndNodes
    ________

    1    2  
    1    3  
    2    1  

Add node names to the graph, then view the new node and edge tables. The source and target nodes of each edge are now expressed using their node names.

G.Nodes.Name = {'A' 'B' 'C'}';
G.Nodes
ans = 

    Name
    ____

    'A' 
    'B' 
    'C' 

G.Edges
ans = 

     EndNodes 
    __________

    'A'    'B'
    'A'    'C'
    'B'    'A'

You can add or modify extra variables in the Nodes and Edges tables to describe attributes of the graph nodes or edges. However, you cannot directly change the number of nodes or edges in the graph by modifying these tables. Instead, use the addedge, rmedge, addnode, or rmnode functions to modify the number of nodes or edges in a graph.

For example, add an edge to the graph between nodes 2 and 3 and view the new edge list.

G = addedge(G,2,3)
G = 

  digraph with properties:

    Edges: [4×1 table]
    Nodes: [3×1 table]

G.Edges
ans = 

     EndNodes 
    __________

    'A'    'B'
    'A'    'C'
    'B'    'A'
    'B'    'C'

Introduced in R2015b

Was this topic helpful?