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

Method add_edge

graphs/depth_first_search_2.py:35–55  ·  view source on GitHub ↗

Add an edge between two vertices. :param from_vertex: The source vertex. :param to_vertex: The destination vertex. Example: >>> g = Graph() >>> g.add_edge(0, 1) >>> g.add_edge(0, 2) >>> g.print_graph() {0: [1, 2]} 0

(self, from_vertex: int, to_vertex: int)

Source from the content-addressed store, hash-verified

33
34 # for adding the edge between two vertices
35 def add_edge(self, from_vertex: int, to_vertex: int) -> None:
36 """
37 Add an edge between two vertices.
38
39 :param from_vertex: The source vertex.
40 :param to_vertex: The destination vertex.
41
42 Example:
43 >>> g = Graph()
44 >>> g.add_edge(0, 1)
45 >>> g.add_edge(0, 2)
46 >>> g.print_graph()
47 {0: [1, 2]}
48 0 -> 1 -> 2
49 """
50 # check if vertex is already present,
51 if from_vertex in self.vertex:
52 self.vertex[from_vertex].append(to_vertex)
53 else:
54 # else make a new vertex
55 self.vertex[from_vertex] = [to_vertex]
56
57 def dfs(self) -> None:
58 """

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected