Modify Nodes and Edges of Existing Graph
这个例子年代hows how to access and modify the nodes and/or edges in agraph
ordigraph
object using theaddedge
,rmedge
,addnode
,rmnode
,findedge
,findnode
, andsubgraph
functions.
Add Nodes
Create a graph with four nodes and four edges. The corresponding elements ins
andt
specify the end nodes of each graph edge.
s = [1 1 1 2]; t = [2 3 4 3]; G = graph(s,t)
G = graph with properties: Edges: [4x1 table] Nodes: [4x0 table]
View the edge list of the graph.
G.Edges
ans=4×1 tableEndNodes ________ 1 2 1 3 1 4 2 3
Useaddnode
to add five nodes to the graph. This command adds five disconnected nodes with node IDs5
,6
,7
,8
, and9
.
G = addnode(G,5)
G = graph with properties: Edges: [4x1 table] Nodes: [9x0 table]
删除节点
Usermnode
to remove nodes 3, 5, and 6 from the graph. All edges connected to one of the removed nodes also are removed. The remaining six nodes in the graph are renumbered to reflect the new number of nodes.
G = rmnode(G,[3 5 6])
G = graph with properties: Edges: [2x1 table] Nodes: [6x0 table]
Add Edges
Useaddedge
to add two edges toG
. The first edge is between node 1 and node 5, and the second edge is between node 2 and node 5. This command adds two new rows toG.Edges
.
G = addedge(G,[1 2],[5 5])
G = graph with properties: Edges: [4x1 table] Nodes: [6x0 table]
Remove Edges
Usermedge
to remove the edge between node 1 and node 3. This command removes a row fromG.Edges
.
G = rmedge(G,1,3)
G = graph with properties: Edges: [3x1 table] Nodes: [6x0 table]
Determine Edge Index
Determine the edge index for the edge between nodes 1 and 5. The edge index,ei
, is a row number inG.Edges
.
ei = findedge(G,1,5)
ei = 2
Determine Node Index
Add node names to the graph, and then determine the node index for node'd'
. The numeric node index,ni
, is a row number inG.Nodes
. You can use bothni
and the node name,'d'
, to refer to the node when using other graph functions, likeshortestpath
.
G.Nodes.Name = {'a''b''c''d''e''f'}'; ni = findnode(G,'d')
ni = 4
Extract Subgraph
Usesubgraph
to extract a piece of the graph containing only two nodes.
H = subgraph(G,[1 2])
H = graph with properties: Edges: [1x1 table] Nodes: [2x1 table]
View the edge list of the subgraph.
H.Edges
ans=tableEndNodes ______________ {'a'} {'b'}
Modify Node and Edge Tables with Variables Editor
The node and edge information for a graph object is contained in two properties:Nodes
andEdges
. Both of these properties are tables containing variables to describe the attributes of the nodes and edges in the graph. SinceNodes
andEdges
are both tables, you can use the Variables editor to interactively view or edit the tables. You cannot add or remove nodes or edges using the Variables editor, and you also cannot edit theEndNodes
财产的Edges
table. The Variables editor is useful for managing extra node and edge attributes in theNodes
andEdges
tables. For more information, seeCreate and Edit Variables.
See Also
graph
|digraph
|addedge
|rmedge
|addnode
|rmnode
|findedge
|findnode
|subgraph