rustworkx.graph_adjacency_matrix#

graph_adjacency_matrix(graph, /, weight_fn=None, default_weight=1.0, null_value=0.0, parallel_edge='sum')#

Return the adjacency matrix for a PyGraph class

In the case where there are multiple edges between nodes the value in the output matrix will be assigned based on a given parameter. Currently, the minimum, maximum, average, and default sum are supported.

Parameters:
  • graph (PyGraph) – The graph used to generate the adjacency matrix from

  • weight_fn

    A callable object (function, lambda, etc) which will be passed the edge object and expected to return a float. This tells rustworkx/rust how to extract a numerical weight as a float for edge object. Some simple examples are:

    graph_adjacency_matrix(graph, weight_fn: lambda x: 1)
    

    to return a weight of 1 for all edges. Also:

    graph_adjacency_matrix(graph, weight_fn: lambda x: float(x))
    

    to cast the edge object as a float as the weight. If this is not specified a default value (either default_weight or 1) will be used for all edges.

  • default_weight (float) – If weight_fn is not used this can be optionally used to specify a default weight to use for all edges.

  • null_value (float) – An optional float that will treated as a null value. This is the default value in the output matrix and it is used to indicate the absence of an edge between 2 nodes. By default this is 0.0.

  • parallel_edge (String) – Optional argument that determines how the function handles parallel edges. "min" causes the value in the output matrix to be the minimum of the edges’ weights, and similar behavior can be expected for "max" and "avg". The function defaults to "sum" behavior, where the value in the output matrix is the sum of all parallel edge weights.

Returns:

The adjacency matrix for the input graph as a numpy array

Return type:

numpy.ndarray