Modifying Graphs
Adding nodes and edges is a fairly straightforward process. To add a new node you simply invoke the addNode(name) command which will create a new node with the default characteristics and the name “name.” Adding an edge is also simple but there are two commands depending on if you want a directed or undirected/bidirected edge.
-
addDirectedEdge(source,destination)
-
addUndirectedEdge(node1,node2)
-
addEdge(node1,node2) is equivalent to addUndirectedEdge(…)
An example of creating two nodes and adding a directed edge between them would be:
addNode(“vx”) addNode(“vy”) addDirectedEdge(vx,vy)
Note that GUESS will create variables called vx and vy in the top level namespace.
GUESS also support the copyNode(source_node , new_node_name,extra arguments) which uses the source node as a template for the new node. Note that the copy command will happily copy everything from the original node including the label, so you’ll usually want to add the extra argument to overload the label. For example if we want to create a new node using v44 as a template with a different color, we could do:
copyNode(v44,”newnode”,label=”newnode”,color=red)
Removing is done by calling:
-
removeNode(node) – A list including the removed node(s) and any removed edges is returned with this call
-
removeEdge(edge) – Returns a list including the removed edge(s)
-
remove(set of nodes and edges) – A list including the removed node(s) and edge(s) is returned from this call
GUESS will not eliminate a reference to a node that has been removed from the namespace. As of the .6 version of GUESS, remove methods return back a set of objects that have been removed from the graph. This is useful if you would like to reinsert nodes or edges.
Continuing in our previous example you could remove the node from the graph and put it back in later. This may be useful for working through different graph variants:
removeNode(vx) … addNode(vx)
Prior to the .6 version of GUESS, removing and re-adding nodes and edges made GUESS forget about the values of user fields. This is no longer the case. Deleted nodes/edges are now held in a special state table called “_deleted.” When you make an ordinary query (e.g. “weight > 5”) GUESS will only return matched elements that are still in the graph. If you would like to find deleted edges that match the criteria you can use the state notation (see states and dynamic graph sections) and type: “weight[‘_deleted’] > 5”
Adding a new field is equally simple. You simply decide which type of field (node or edge) you want to add and then invoke the method:
addNodeField(name,type,default) addEdgeField(name,type,default)
The name is the new column name, the type is int value corresponding to the java.sql.Types class (e.g. Types.INTEGER, Types.BOOLEAN, Types.TINYINT, Types.VARCHAR, etc.) and the default is an object corresponding to the default value.
Here’s an example of the command in action. We’re going to add a new node field called shortest and set it to the distance of that node to the v4 (in the sample database).
addNodeField(“shortest”,Types.INTEGER,Integer(0))
for n in g.nodes:
n.shortest = len(v4.unweightedShortestPath(n))
colorize(shortest,pink,black)
This command will result in the following figure:
Dealing with Node/Edge properties: To programmatically get a list of node or edge properties you can call the fieldNames() or allFields() methods of the Node and Edge classes (e.g. Node.fieldNames()). The former will give you the textual names of the fields, and the latter will give you a reference to the actual field object. Using textual string names you can invoke the __setattr__(fieldname,value) and __getattr__(fieldname) commands on specific nodes/edges or sets (e.g. v44.__setattr__(“color”,red) or v44.__getattr__(“color”)).



