Create a Graph from other structures#

The following functions are provided to convert between different graph representations. These functions can be used to create a graph from an adjacency list or an edge list, and to convert into graph dictionary format.

The three main graph representations are:

  • AdjacencyList: List[List[Integral]]]: A list of lists, where the outer list has a length equal to the number of vertices in the graph, and each inner list contains the neighbors (i.e., [v1, v2, ...]) of the corresponding vertex (from outer vertex to inner vertex). For a weighted graph, the form List[List[Tuple[Integral, Real]]] is used where the inner lists contain tuples of the form \((v, w)\), where \(v\) is a neighbor vertex and \(w\) is the weight of the edge connecting the vertex to its neighbor. To store more than weight, the form List[List[Tuple[Integral, Dict[str, Any]]]] is used.

  • Edges: List[Tuple[Integral, Integral]]: A list of tuples, where each tuple represents an edge \((u, v)\) in the graph (from vertex \(u\) to vertex \(v\)). For a undirected graph consider converting in pygraphs.Graph class and use the to_undirected method to duplicate \((u, v)\) into \((u, v)\) and \((v, u)\). For a weighted graph, the form List[Tuple[Integral, Integral, Real]] containing \((u, v, w)\) is used, where \(u\) and \(v\) are the vertices connected by the edge and \(w\) is the weight of the edge. To store more than weight, the form List[Tuple[Integral, Integral, Dict[str, Any]]] is used.

  • GraphRepresentation: Dict[int, Dict[int, Dict[str, Any]]]: A dictionary where the keys are vertex identifiers and the values are dictionaries mapping neighboring vertex identifiers to edge attributes (e.g., weight). This is the internal representation used by the pygraphs.Graph class.

adjacency_list_to_graph(adjacency, *[, ...])

Convert a graph represented as an adjacency list (AdjacencyList) to a graph represented as a dictionary (GraphRepresentation).

edges_list_to_graph(edges, n_vertices, *[, ...])

Convert a graph represented as an edge list (Edges) to a graph represented as a dictionary (GraphRepresentation).

graph_to_adjacency_list(graph, *[, ...])

Convert a graph represented as a dictionary (GraphRepresentation) to a graph represented as an adjacency list (AdjacencyList).

graph_to_edges_list(graph, *[, weighted, ...])

Convert a graph represented as a dictionary (GraphRepresentation) to a graph represented as an edge list (Edges).

adjacency_list_to_edges_list(adjacency, *[, ...])

Convert a graph represented as an adjacency list (AdjacencyList) to a graph represented as an edge list (Edges).

edges_list_to_adjacency_list(edges, ...[, ...])

Convert a graph represented as an edge list (Edges) to a graph represented as an adjacency list (AdjacencyList).