MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / __init__

Method __init__

graphs/graph_adjacency_matrix.py:32–59  ·  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) Indic

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

Source from the content-addressed store, hash-verified

30
31class GraphAdjacencyMatrix[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.directed = directed
45 self.vertex_to_index: dict[T, int] = {}
46 self.adj_matrix: list[list[int]] = []
47
48 # Falsey checks
49 edges = edges or []
50 vertices = vertices or []
51
52 for vertex in vertices:
53 self.add_vertex(vertex)
54
55 for edge in edges:
56 if len(edge) != 2:
57 msg = f"Invalid input: {edge} must have length 2."
58 raise ValueError(msg)
59 self.add_edge(edge[0], edge[1])
60
61 def add_edge(self, source_vertex: T, destination_vertex: T) -> None:
62 """

Callers

nothing calls this directly

Calls 2

add_vertexMethod · 0.95
add_edgeMethod · 0.95

Tested by

no test coverage detected