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

Method add_edge

graphs/dijkstra_algorithm.py:235–256  ·  view source on GitHub ↗

Add edge going from node u to v and v to u with weight w: u (w)-> v, v (w) -> u Examples: >>> graph_test = Graph(1) >>> graph_test.add_edge(1, 2, 1) >>> graph_test.add_edge(2, 3, 2) >>> graph_test.adjList {1: [(2, 1)], 2: [(1, 1), (3, 2)], 3:

(self, u, v, w)

Source from the content-addressed store, hash-verified

233 self.par = [-1] * self.num_nodes # To store the path
234
235 def add_edge(self, u, v, w):
236 """
237 Add edge going from node u to v and v to u with weight w: u (w)-> v, v (w) -> u
238
239 Examples:
240 >>> graph_test = Graph(1)
241 >>> graph_test.add_edge(1, 2, 1)
242 >>> graph_test.add_edge(2, 3, 2)
243 >>> graph_test.adjList
244 {1: [(2, 1)], 2: [(1, 1), (3, 2)], 3: [(2, 2)]}
245 """
246 # Check if u already in graph
247 if u in self.adjList:
248 self.adjList[u].append((v, w))
249 else:
250 self.adjList[u] = [(v, w)]
251
252 # Assuming undirected graph
253 if v in self.adjList:
254 self.adjList[v].append((u, w))
255 else:
256 self.adjList[v] = [(u, w)]
257
258 def show_graph(self):
259 """

Callers 1

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected