MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __init__

Method __init__

graphs/graph_adjacency_list.py:32–58  ·  view source on GitHub ↗

Parameters: - vertices: (list[T]) The list of vertex names the client wants to pass in. Default is empty. - edges: (list[list[T]]) The list of edges the client wants to pass in. Each edge is a 2-element list. Default is empty. - directed: (bool) Indi

(
        self, vertices: list[T], edges: list[list[T]], directed: bool = True
    )

Source from the content-addressed store, hash-verified

30
31class GraphAdjacencyList[T]:
32 def __init__(
33 self, vertices: list[T], edges: list[list[T]], directed: bool = True
34 ) -> None:
35 """
36 Parameters:
37 - vertices: (list[T]) The list of vertex names the client wants to
38 pass in. Default is empty.
39 - edges: (list[list[T]]) The list of edges the client wants to
40 pass in. Each edge is a 2-element list. Default is empty.
41 - directed: (bool) Indicates if graph is directed or undirected.
42 Default is True.
43 """
44 self.adj_list: dict[T, list[T]] = {} # dictionary of lists of T
45 self.directed = directed
46
47 # Falsey checks
48 edges = edges or []
49 vertices = vertices or []
50
51 for vertex in vertices:
52 self.add_vertex(vertex)
53
54 for edge in edges:
55 if len(edge) != 2:
56 msg = f"Invalid input: {edge} is the wrong length."
57 raise ValueError(msg)
58 self.add_edge(edge[0], edge[1])
59
60 def add_vertex(self, vertex: T) -> None:
61 """

Callers

nothing calls this directly

Calls 2

add_vertexMethod · 0.95
add_edgeMethod · 0.95

Tested by

no test coverage detected