Graph

class compas.datastructures.Graph[source]

Bases: compas.datastructures.datastructure.Datastructure

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

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

>>>

Methods

__init__()

Initialize self.

add_edge(u, v[, attr_dict])

Add an edge and specify its attributes.

add_node(key[, attr_dict])

Add a node and specify its attributes (optional).

clear()

Clear all the network data.

connected_edges(key)

Return the edges connected to a node.

copy([cls])

Make an independent copy of the datastructure object.

degree(key)

Return the number of neighbors of a node.

degree_in(key)

Return the numer of incoming neighbors of a node.

degree_out(key)

Return the number of outgoing neighbors of a node.

delete_edge(u, v)

Delete an edge from the network.

delete_node(key)

Delete a node from the graph.

edge_attribute(key, name[, value])

Get or set an attribute of an edge.

edge_attributes(key[, names, values])

Get or set multiple attributes of an edge.

edges([data])

Iterate over the edges of the network.

edges_attribute(name[, value, keys])

Get or set an attribute of multiple edges.

edges_attributes([names, values, keys])

Get or set multiple attributes of multiple edges.

edges_where(conditions[, data])

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

edges_where_predicate(predicate[, data])

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

from_data(data)

Construct a datastructure from structured data.

from_edges(edges)

from_json(filepath)

Construct a datastructure from structured data contained in a json file.

from_networkx(graph)

get_any_edge()

Get the identifier of a random edge.

get_any_edges(n)

Get the identifiers of a set of random edges.

get_any_node()

Get the identifier of a random node.

get_any_nodes(n[, exclude_leaves])

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

has_edge(u, v[, directed])

Verify if the network contains a specific edge.

has_node(key)

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(key)

Verify if a node is a leaf.

is_node_connected(key)

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(key[, ring])

Return the nodes in the neighborhood of a node.

neighbors(key)

Return the neighbors of a node.

neighbors_in(key)

Return the incoming neighbors of a node.

neighbors_out(key)

Return the outgoing neighbors of a node.

node_attribute(key, name[, value])

Get or set an attribute of a node.

node_attributes(key[, names, values])

Get or set multiple attributes of a node.

nodes([data])

Iterate over the nodes of the network.

nodes_attribute(name[, value, keys])

Get or set an attribute of multiple nodes.

nodes_attributes([names, values, keys])

Get or set multiple attributes of multiple nodes.

nodes_where(conditions[, data])

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

nodes_where_predicate(predicate[, data])

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_data()

Returns a dictionary of structured data representing the data structure.

to_json(filepath[, pretty])

Serialise the structured data representing the datastructure to json.

to_networkx()

unset_edge_attribute(key, name)

Unset the attribute of an edge.

unset_node_attribute(key, name)

Unset the attribute of a node.

update_dea([attr_dict])

Update the default edge attributes.

update_default_edge_attributes([attr_dict])

Update the default edge attributes.

update_default_node_attributes([attr_dict])

Update the default node attributes.

update_dna([attr_dict])

Update the default node attributes.

uv_index()

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

validate_data()

Validate the data of this object against its data schema (self.DATASCHEMA).

validate_json()

Validate the data loaded from a JSON representation of the data of this object against its data schema (self.DATASCHEMA).