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

Method add_vertex

graphs/graph_adjacency_list.py:60–77  ·  view source on GitHub ↗

Adds a vertex to the graph. If the given vertex already exists, a ValueError will be thrown. >>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False) >>> g.add_vertex("A") >>> g.adj_list {'A': []} >>> g.add_vertex("A") Traceb

(self, vertex: T)

Source from the content-addressed store, hash-verified

58 self.add_edge(edge[0], edge[1])
59
60 def add_vertex(self, vertex: T) -> None:
61 """
62 Adds a vertex to the graph. If the given vertex already exists,
63 a ValueError will be thrown.
64
65 >>> g = GraphAdjacencyList(vertices=[], edges=[], directed=False)
66 >>> g.add_vertex("A")
67 >>> g.adj_list
68 {'A': []}
69 >>> g.add_vertex("A")
70 Traceback (most recent call last):
71 ...
72 ValueError: Incorrect input: A is already in the graph.
73 """
74 if self.contains_vertex(vertex):
75 msg = f"Incorrect input: {vertex} is already in the graph."
76 raise ValueError(msg)
77 self.adj_list[vertex] = []
78
79 def add_edge(self, source_vertex: T, destination_vertex: T) -> None:
80 """

Callers 4

__init__Method · 0.95
test_add_verticesMethod · 0.95

Calls 1

contains_vertexMethod · 0.95