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

Method add_vertex

graphs/graph_adjacency_matrix.py:118–133  ·  view source on GitHub ↗

Adds a vertex to the graph. If the given vertex already exists, a ValueError will be thrown.

(self, vertex: T)

Source from the content-addressed store, hash-verified

116 self.adj_matrix[v][u] = 0
117
118 def add_vertex(self, vertex: T) -> None:
119 """
120 Adds a vertex to the graph. If the given vertex already exists,
121 a ValueError will be thrown.
122 """
123 if self.contains_vertex(vertex):
124 msg = f"Incorrect input: {vertex} already exists in this graph."
125 raise ValueError(msg)
126
127 # build column for vertex
128 for row in self.adj_matrix:
129 row.append(0)
130
131 # build row for vertex and update other data structures
132 self.adj_matrix.append([0] * (len(self.adj_matrix) + 1))
133 self.vertex_to_index[vertex] = len(self.adj_matrix) - 1
134
135 def remove_vertex(self, vertex: T) -> None:
136 """

Callers 4

__init__Method · 0.95
test_add_verticesMethod · 0.95

Calls 2

contains_vertexMethod · 0.95
appendMethod · 0.45