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

Method add_edge

graphs/breadth_first_search.py:26–38  ·  view source on GitHub ↗

adding the edge between two vertices >>> g = Graph() >>> g.print_graph() >>> g.add_edge(0, 1) >>> g.print_graph() 0 : 1

(self, from_vertex: int, to_vertex: int)

Source from the content-addressed store, hash-verified

24 print(i, " : ", " -> ".join([str(j) for j in self.vertices[i]]))
25
26 def add_edge(self, from_vertex: int, to_vertex: int) -> None:
27 """
28 adding the edge between two vertices
29 >>> g = Graph()
30 >>> g.print_graph()
31 >>> g.add_edge(0, 1)
32 >>> g.print_graph()
33 0 : 1
34 """
35 if from_vertex in self.vertices:
36 self.vertices[from_vertex].append(to_vertex)
37 else:
38 self.vertices[from_vertex] = [to_vertex]
39
40 def bfs(self, start_vertex: int) -> set[int]:
41 """

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected