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

Method print_graph

graphs/depth_first_search_2.py:11–32  ·  view source on GitHub ↗

Print the graph vertices. Example: >>> g = Graph() >>> g.add_edge(0, 1) >>> g.add_edge(0, 2) >>> g.add_edge(1, 2) >>> g.add_edge(2, 0) >>> g.add_edge(2, 3) >>> g.add_edge(3, 3) >>> g.print_graph() {0: [1, 2], 1

(self)

Source from the content-addressed store, hash-verified

9
10 # for printing the Graph vertices
11 def print_graph(self) -> None:
12 """
13 Print the graph vertices.
14
15 Example:
16 >>> g = Graph()
17 >>> g.add_edge(0, 1)
18 >>> g.add_edge(0, 2)
19 >>> g.add_edge(1, 2)
20 >>> g.add_edge(2, 0)
21 >>> g.add_edge(2, 3)
22 >>> g.add_edge(3, 3)
23 >>> g.print_graph()
24 {0: [1, 2], 1: [2], 2: [0, 3], 3: [3]}
25 0 -> 1 -> 2
26 1 -> 2
27 2 -> 0 -> 3
28 3 -> 3
29 """
30 print(self.vertex)
31 for i in self.vertex:
32 print(i, " -> ", " -> ".join([str(j) for j in self.vertex[i]]))
33
34 # for adding the edge between two vertices
35 def add_edge(self, from_vertex: int, to_vertex: int) -> None:

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected