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 formList[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 formList[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 inpygraphs.Graphclass and use theto_undirectedmethod to duplicate \((u, v)\) into \((u, v)\) and \((v, u)\). For a weighted graph, the formList[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 formList[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 thepygraphs.Graphclass.
|
Convert a graph represented as an adjacency list ( |
|
Convert a graph represented as an edge list ( |
|
Convert a graph represented as a dictionary ( |
|
Convert a graph represented as a dictionary ( |
|
Convert a graph represented as an adjacency list ( |
|
Convert a graph represented as an edge list ( |