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

Method add_edge

graphs/graph_adjacency_matrix.py:61–88  ·  view source on GitHub ↗

Creates an edge from source vertex to destination vertex. If any given vertex doesn't exist or the edge already exists, a ValueError will be thrown.

(self, source_vertex: T, destination_vertex: T)

Source from the content-addressed store, hash-verified

59 self.add_edge(edge[0], edge[1])
60
61 def add_edge(self, source_vertex: T, destination_vertex: T) -> None:
62 """
63 Creates an edge from source vertex to destination vertex. If any
64 given vertex doesn't exist or the edge already exists, a ValueError
65 will be thrown.
66 """
67 if not (
68 self.contains_vertex(source_vertex)
69 and self.contains_vertex(destination_vertex)
70 ):
71 msg = (
72 f"Incorrect input: Either {source_vertex} or "
73 f"{destination_vertex} does not exist"
74 )
75 raise ValueError(msg)
76 if self.contains_edge(source_vertex, destination_vertex):
77 msg = (
78 "Incorrect input: The edge already exists between "
79 f"{source_vertex} and {destination_vertex}"
80 )
81 raise ValueError(msg)
82
83 # Get the indices of the corresponding vertices and set their edge value to 1.
84 u: int = self.vertex_to_index[source_vertex]
85 v: int = self.vertex_to_index[destination_vertex]
86 self.adj_matrix[u][v] = 1
87 if not self.directed:
88 self.adj_matrix[v][u] = 1
89
90 def remove_edge(self, source_vertex: T, destination_vertex: T) -> None:
91 """

Callers 4

__init__Method · 0.95
test_add_edgeMethod · 0.95

Calls 2

contains_vertexMethod · 0.95
contains_edgeMethod · 0.95

Tested by 3

test_add_edgeMethod · 0.76