pygraphs.edges_list_to_graph#

edges_list_to_graph(edges, n_vertices, *, weight_key='weight', skip_validation=False)[source]#

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

  • 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.

Parameters:
  • edges (Edges) – The edge list to convert into graph representation.

  • n_vertices (Integral) – The number of vertices in the graph.

  • weight_key (str, optional (default: 'weight')) – The key to use for storing edge weights in the graph representation when the edge list is weighted.

Returns:

graph – The graph represented as a dictionary.

Return type:

GraphRepresentation