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

Method add_edge

graphs/graph_list.py:84–147  ·  view source on GitHub ↗

Connects vertices together. Creates and Edge from source vertex to destination vertex. Vertices will be created if not found in graph

(
        self, source_vertex: T, destination_vertex: T
    )

Source from the content-addressed store, hash-verified

82 self.directed = directed
83
84 def add_edge(
85 self, source_vertex: T, destination_vertex: T
86 ) -> GraphAdjacencyList[T]:
87 """
88 Connects vertices together. Creates and Edge from source vertex to destination
89 vertex.
90 Vertices will be created if not found in graph
91 """
92
93 if not self.directed: # For undirected graphs
94 # if both source vertex and destination vertex are both present in the
95 # adjacency list, add destination vertex to source vertex list of adjacent
96 # vertices and add source vertex to destination vertex list of adjacent
97 # vertices.
98 if source_vertex in self.adj_list and destination_vertex in self.adj_list:
99 self.adj_list[source_vertex].append(destination_vertex)
100 self.adj_list[destination_vertex].append(source_vertex)
101 # if only source vertex is present in adjacency list, add destination vertex
102 # to source vertex list of adjacent vertices, then create a new vertex with
103 # destination vertex as key and assign a list containing the source vertex
104 # as it's first adjacent vertex.
105 elif source_vertex in self.adj_list:
106 self.adj_list[source_vertex].append(destination_vertex)
107 self.adj_list[destination_vertex] = [source_vertex]
108 # if only destination vertex is present in adjacency list, add source vertex
109 # to destination vertex list of adjacent vertices, then create a new vertex
110 # with source vertex as key and assign a list containing the source vertex
111 # as it's first adjacent vertex.
112 elif destination_vertex in self.adj_list:
113 self.adj_list[destination_vertex].append(source_vertex)
114 self.adj_list[source_vertex] = [destination_vertex]
115 # if both source vertex and destination vertex are not present in adjacency
116 # list, create a new vertex with source vertex as key and assign a list
117 # containing the destination vertex as it's first adjacent vertex also
118 # create a new vertex with destination vertex as key and assign a list
119 # containing the source vertex as it's first adjacent vertex.
120 else:
121 self.adj_list[source_vertex] = [destination_vertex]
122 self.adj_list[destination_vertex] = [source_vertex]
123 # For directed graphs
124 # if both source vertex and destination vertex are present in adjacency
125 # list, add destination vertex to source vertex list of adjacent vertices.
126 elif source_vertex in self.adj_list and destination_vertex in self.adj_list:
127 self.adj_list[source_vertex].append(destination_vertex)
128 # if only source vertex is present in adjacency list, add destination
129 # vertex to source vertex list of adjacent vertices and create a new vertex
130 # with destination vertex as key, which has no adjacent vertex
131 elif source_vertex in self.adj_list:
132 self.adj_list[source_vertex].append(destination_vertex)
133 self.adj_list[destination_vertex] = []
134 # if only destination vertex is present in adjacency list, create a new
135 # vertex with source vertex as key and assign a list containing destination
136 # vertex as first adjacent vertex
137 elif destination_vertex in self.adj_list:
138 self.adj_list[source_vertex] = [destination_vertex]
139 # if both source vertex and destination vertex are not present in adjacency
140 # list, create a new vertex with source vertex as key and a list containing
141 # destination vertex as it's first adjacent vertex. Then create a new vertex

Callers

nothing calls this directly

Calls 1

appendMethod · 0.45

Tested by

no test coverage detected