Graph¶
- class ggsolver.graph.NodePropertyMap(graph, default=None)[source]¶
Implements a default dictionary that maps a node ID to its property value. To store data efficiently, only the non-default values are stored in the dictionary.
Raises an error if the node ID is invalid.
- class ggsolver.graph.EdgePropertyMap(graph, default=None)[source]¶
Implements a default dictionary that maps an edge (uid, vid, key) to its property value. To store data efficiently, only the non-default values are stored in the dictionary.
Raises an error if the edge (uid, vid, key) is invalid.
- class ggsolver.graph.Graph[source]¶
A MultiDiGraph class represented as a 5-tuple (nodes, edges, node_properties, edge_properties, graph_properties). In addition, the graph implements a serialization protocol, save-load and drawing functionality.
- add_edge(uid, vid)[source]¶
Adds a new edge between the give nodes.
- Warning
Duplication is NOT checked. Hence, calling the function twice adds two parallel edges between the same nodes.
- Returns
(int) Key of the added edge. Key = 0 means the first edge was added between the given nodes. If Key = k, then (k+1)-th edge was added.
- add_edges(edges)[source]¶
Adds multiple edge between the give nodes.
- Warning
Duplication is NOT checked. Hence, calling the function twice adds two parallel edges between the same nodes.
- Returns
(int) Key of the added edge. Key = 0 means the first edge was added between the given nodes. If Key = k, then (k+1)-th edge was added.
- add_node()[source]¶
Adds a new node to the graph.
- Warning
Duplication is NOT checked.
- Returns
(int) ID of the added node.
- add_nodes(num_nodes)[source]¶
Adds multiple nodes to the graph.
- Warning
Duplication is NOT checked.
- Parameters
num_nodes – (int) Number of nodes to be added.
- Returns
(list) IDs of added nodes.
- classmethod deserialize(obj_dict)[source]¶
Constructs a graph from a serialized graph object. The format is described in
Graph.serialize()
.- Returns
(Graph) A new
Graph
object..
- property edge_properties¶
Returns the edge properties as a dictionary of {“property name”: EdgePropertyMap object}.
- edges()[source]¶
List of all edges in the graph. Each edge is represented as a 3-tuple (uid, vid, key).
- property graph_properties¶
Returns the graph properties as a dictionary of {“property name”: property value}.
- has_edge(uid, vid, key=None)[source]¶
Checks whether the graph has the given edge or not.
- Parameters
uid – (int) Source node ID.
vid – (int) Target node ID.
key (int, optional) – If provided, checks whether the edge (u, v, k) is in the graph or not. Otherwise, checks if there exists an edge between nodes represented by uid and vid.
- Returns
(bool) True if given edge is in the graph, else False.
- has_node(uid)[source]¶
Checks whether the graph has the given node or not.
- Parameters
uid – (int) Node ID to be checked for containment.
- Returns
(bool) True if given node is in the graph, else False.
- is_isomorphic_to(other: Graph)[source]¶
Checks if the graph is isomorphic to the other graph.
- Parameters
other – (
Graph
object) Graph to be checked for isomorphism with current graph.- Returns
(bool) True, if graphs are isomorphic. Else, False.
- classmethod load(fpath, protocol='json')[source]¶
Loads the graph from file.
- Parameters
fpath – (str) Path to which the file should be saved. Must include an extension.
protocol – (str) The protocol to use to save the file. Options: {“json” [Default], “pickle”}.
Note
Pickle protocol is not tested.
- property node_properties¶
Returns the node properties as a dictionary of {“property name”: NodePropertyMap object}.
- save(fpath, overwrite=False, protocol='json')[source]¶
Saves the graph to file.
- Parameters
fpath – (str) Path to which the file should be saved. Must include an extension.
overwrite – (bool) Specifies whether to overwrite the file, if it exists. [Default: False]
protocol – (str) The protocol to use to save the file. Options: {“json” [Default], “pickle”}.
Note
Pickle protocol is not tested.
- serialize()[source]¶
Serializes the graph into a dictionary with the following format:
{ "graph": { "nodes": <number of nodes>, "edges": { uid: {vid: key}, ... } "node_properties": { "property_name": { "default": <value>, "dict": { "uid": <property value>, ... } }, ... }, "edge_properties": { "property_name": { "default": <value>, "dict": [{"edge": [uid, vid, key], "pvalue": <property value>} ...] }, ... }, "graph_properties": { "property_name": <value>, ... } } }
- Returns
(dict) Serialized graph
- to_png(fpath, nlabel=None, elabel=None)[source]¶
Generates a PNG image of the graph.
- Parameters
fpath – (str) Path to which the file should be saved. Must include an extension.
nlabel – (list of str) Specifies the node properties to use to annotate a node in image.
elabel – (list of str) Specifies the edge properties to use to annotate an edge in image.
- Warning
If the node labels are not unique, the generated figure may contain 0, 1, 2, … that avoid duplication.