Graph

class compas.datastructures.Graph(name=None, default_node_attributes=None, default_edge_attributes=None)[source]

Bases: compas.datastructures.datastructure.Datastructure

Base graph data structure for describing the topological relationships between nodes connected by edges.

Parameters
  • name (str, optional) – The name of the graph. Defaults to “Graph”.

  • default_node_attributes (dict, optional) – Default values for node attributes.

  • default_edge_attributes (dict, optional) – Default values for edge attributes.

Attributes
  • node (dict) – The node dictionary. Each key in the node dictionary represents a node of the network and maps to a dictionary of node attributes.

  • edge (dict of dict) – The edge dictionary. Each key in the edge dictionary corresponds to a key in the node dictionary, and maps to a dictionary with connected nodes. In the latter, the keys are again references to items in the node dictionary, and the values are dictionaries of edge attributes. For example, an edge between node 1 and node 2 is represented as follows Graph.edge[1][2] -> {...}

  • adjacency (dict of dict) – The edges of the graph are directed. The undirected connectivity information is represented in the adjacency dict.

  • attributes (dict) – A dictionary of miscellaneous information about the graph.

  • default_node_attributes (dict) – A dictionary mapping node attribute names to their default values.

  • default_edge_attributes (dict) – A dictionary mapping edge attribute names to their default values.

  • data (dict) – A dictionary representing the essential data of a graph that can be used in serialization processes.

Examples

>>>

Attributes

DATASCHEMA

The schema of the data of this object.

JSONSCHEMANAME

data

Return a data dict of this data structure for serialization.

name

The name of the data structure.

Inherited Attributes

JSONSCHEMA

The schema of the JSON representation of the data of this object.

dtype

The type of the object in the form of a '2-level' import and a class name.

guid

The globally unique identifier of the object.

jsondefinitions

Reusable schema definitions.

jsonstring

The representation of the object data in JSON format.

jsonvalidator

JSON schema validator for draft 7.

Methods

add_edge

Add an edge and specify its attributes.

add_node

Add a node and specify its attributes (optional).

clear

Clear all the network data.

connected_edges

Return the edges connected to a node.

degree

Return the number of neighbors of a node.

degree_in

Return the numer of incoming neighbors of a node.

degree_out

Return the number of outgoing neighbors of a node.

delete_edge

Delete an edge from the network.

delete_node

Delete a node from the graph.

edge_attribute

Get or set an attribute of an edge.

edge_attributes

Get or set multiple attributes of an edge.

edge_sample

Get the identifiers of a set of random edges.

edges

Iterate over the edges of the network.

edges_attribute

Get or set an attribute of multiple edges.

edges_attributes

Get or set multiple attributes of multiple edges.

edges_where

Get edges for which a certain condition or set of conditions is true.

edges_where_predicate

Get edges for which a certain condition or set of conditions is true using a lambda function.

from_edges

from_networkx

Create a new graph instance from a NetworkX DiGraph instance.

get_any_edge

Get the identifier of a random edge.

get_any_edges

Get the identifiers of a set of random edges.

get_any_node

Get the identifier of a random node.

get_any_nodes

Get a list of identifiers of a random set of n nodes.

has_edge

Verify if the network contains a specific edge.

has_node

Verify if a specific node is present in the network.

index_key

Returns a dictionary that maps the indices of a node list to keys in a node dictionary.

index_uv

Returns a dictionary that maps edges in a list to the corresponding vertex key pairs.

is_leaf

Verify if a node is a leaf.

is_node_connected

Verify if a specific node is connected.

key_index

Returns a dictionary that maps node dictionary keys to the corresponding index in a node list or array.

leaves

Return all leaves of the network.

neighborhood

Return the nodes in the neighborhood of a node.

neighbors

Return the neighbors of a node.

neighbors_in

Return the incoming neighbors of a node.

neighbors_out

Return the outgoing neighbors of a node.

node_attribute

Get or set an attribute of a node.

node_attributes

Get or set multiple attributes of a node.

node_sample

Get a list of identifiers of a random set of n nodes.

nodes

Iterate over the nodes of the network.

nodes_attribute

Get or set an attribute of multiple nodes.

nodes_attributes

Get or set multiple attributes of multiple nodes.

nodes_where

Get nodes for which a certain condition or set of conditions is true.

nodes_where_predicate

Get nodes for which a certain condition or set of conditions is true using a lambda function.

number_of_edges

Compute the number of edges of the network.

number_of_nodes

Compute the number of nodes of the network.

summary

Print a summary of the graph.

to_networkx

Create a new NetworkX graph instance from a graph.

unset_edge_attribute

Unset the attribute of an edge.

unset_node_attribute

Unset the attribute of a node.

update_dea

Update the default edge attributes.

update_default_edge_attributes

Update the default edge attributes.

update_default_node_attributes

Update the default node attributes.

update_dna

Update the default node attributes.

uv_index

Returns a dictionary that maps edge keys (i.e.

Inherited Methods

copy

Make an independent copy of the data object.

from_data

Construct an object of this type from the provided data.

from_json

Construct an object from serialized data contained in a JSON file.

from_jsonstring

Construct an object from serialized data contained in a JSON string.

to_data

Convert an object to its native data representation.

to_json

Serialize the data representation of an object to a JSON file.

to_jsonstring

Serialize the data representation of an object to a JSON string.

validate_data

Validate the object's data against its data schema (self.DATASCHEMA).

validate_json

Validate the object's data against its json schema (self.JSONSCHEMA).