mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Make some ruff fixes (#8154)
* Make some ruff fixes * Undo manual fix * Undo manual fix * Updates from ruff=0.0.251
This commit is contained in:
@ -139,10 +139,9 @@ def dijk(g, s):
|
||||
u = i
|
||||
known.add(u)
|
||||
for v in g[u]:
|
||||
if v[0] not in known:
|
||||
if dist[u] + v[1] < dist.get(v[0], 100000):
|
||||
dist[v[0]] = dist[u] + v[1]
|
||||
path[v[0]] = u
|
||||
if v[0] not in known and dist[u] + v[1] < dist.get(v[0], 100000):
|
||||
dist[v[0]] = dist[u] + v[1]
|
||||
path[v[0]] = u
|
||||
for i in dist:
|
||||
if i != s:
|
||||
print(dist[i])
|
||||
@ -243,10 +242,9 @@ def prim(g, s):
|
||||
u = i
|
||||
known.add(u)
|
||||
for v in g[u]:
|
||||
if v[0] not in known:
|
||||
if v[1] < dist.get(v[0], 100000):
|
||||
dist[v[0]] = v[1]
|
||||
path[v[0]] = u
|
||||
if v[0] not in known and v[1] < dist.get(v[0], 100000):
|
||||
dist[v[0]] = v[1]
|
||||
path[v[0]] = u
|
||||
return dist
|
||||
|
||||
|
||||
|
@ -15,11 +15,10 @@ def check_cycle(graph: dict) -> bool:
|
||||
visited: set[int] = set()
|
||||
# To detect a back edge, keep track of vertices currently in the recursion stack
|
||||
rec_stk: set[int] = set()
|
||||
for node in graph:
|
||||
if node not in visited:
|
||||
if depth_first_search(graph, node, visited, rec_stk):
|
||||
return True
|
||||
return False
|
||||
return any(
|
||||
node not in visited and depth_first_search(graph, node, visited, rec_stk)
|
||||
for node in graph
|
||||
)
|
||||
|
||||
|
||||
def depth_first_search(graph: dict, vertex: int, visited: set, rec_stk: set) -> bool:
|
||||
|
@ -27,7 +27,7 @@ def dfs(graph: dict, vert: int, visited: list) -> list:
|
||||
if not visited[neighbour]:
|
||||
connected_verts += dfs(graph, neighbour, visited)
|
||||
|
||||
return [vert] + connected_verts
|
||||
return [vert, *connected_verts]
|
||||
|
||||
|
||||
def connected_components(graph: dict) -> list:
|
||||
|
@ -112,7 +112,7 @@ class Graph:
|
||||
self.dist[src] = 0
|
||||
q = PriorityQueue()
|
||||
q.insert((0, src)) # (dist from src, node)
|
||||
for u in self.adjList.keys():
|
||||
for u in self.adjList:
|
||||
if u != src:
|
||||
self.dist[u] = sys.maxsize # Infinity
|
||||
self.par[u] = -1
|
||||
|
@ -163,9 +163,8 @@ class PushRelabelExecutor(MaximumFlowAlgorithmExecutor):
|
||||
self.graph[vertex_index][to_index]
|
||||
- self.preflow[vertex_index][to_index]
|
||||
> 0
|
||||
):
|
||||
if min_height is None or self.heights[to_index] < min_height:
|
||||
min_height = self.heights[to_index]
|
||||
) and (min_height is None or self.heights[to_index] < min_height):
|
||||
min_height = self.heights[to_index]
|
||||
|
||||
if min_height is not None:
|
||||
self.heights[vertex_index] = min_height + 1
|
||||
|
@ -130,11 +130,11 @@ def create_edge(nodes, graph, cluster, c1):
|
||||
"""
|
||||
create edge between the nodes
|
||||
"""
|
||||
for i in cluster[c1].keys():
|
||||
for i in cluster[c1]:
|
||||
count = 0
|
||||
c2 = c1 + 1
|
||||
while c2 < max(cluster.keys()):
|
||||
for j in cluster[c2].keys():
|
||||
for j in cluster[c2]:
|
||||
"""
|
||||
creates edge only if the condition satisfies
|
||||
"""
|
||||
@ -185,7 +185,7 @@ def find_freq_subgraph_given_support(s, cluster, graph):
|
||||
find edges of multiple frequent subgraphs
|
||||
"""
|
||||
k = int(s / 100 * (len(cluster) - 1))
|
||||
for i in cluster[k].keys():
|
||||
for i in cluster[k]:
|
||||
my_dfs(graph, tuple(cluster[k][i]), (["Header"],))
|
||||
|
||||
|
||||
|
@ -144,6 +144,7 @@ class Graph:
|
||||
self.rank[root1] += 1
|
||||
self.parent[root2] = root1
|
||||
return root1
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def boruvka_mst(graph):
|
||||
|
@ -44,10 +44,7 @@ class Heap:
|
||||
temp = position[index]
|
||||
|
||||
while index != 0:
|
||||
if index % 2 == 0:
|
||||
parent = int((index - 2) / 2)
|
||||
else:
|
||||
parent = int((index - 1) / 2)
|
||||
parent = int((index - 2) / 2) if index % 2 == 0 else int((index - 1) / 2)
|
||||
|
||||
if val < heap[parent]:
|
||||
heap[index] = heap[parent]
|
||||
|
@ -135,14 +135,14 @@ class MinPriorityQueue(Generic[T]):
|
||||
# only]
|
||||
curr_pos = self.position_map[elem]
|
||||
if curr_pos == 0:
|
||||
return
|
||||
return None
|
||||
parent_position = get_parent_position(curr_pos)
|
||||
_, weight = self.heap[curr_pos]
|
||||
_, parent_weight = self.heap[parent_position]
|
||||
if parent_weight > weight:
|
||||
self._swap_nodes(parent_position, curr_pos)
|
||||
return self._bubble_up(elem)
|
||||
return
|
||||
return None
|
||||
|
||||
def _bubble_down(self, elem: T) -> None:
|
||||
# Place a node at the proper position (downward movement) [to be used
|
||||
@ -154,24 +154,22 @@ class MinPriorityQueue(Generic[T]):
|
||||
if child_left_position < self.elements and child_right_position < self.elements:
|
||||
_, child_left_weight = self.heap[child_left_position]
|
||||
_, child_right_weight = self.heap[child_right_position]
|
||||
if child_right_weight < child_left_weight:
|
||||
if child_right_weight < weight:
|
||||
self._swap_nodes(child_right_position, curr_pos)
|
||||
return self._bubble_down(elem)
|
||||
if child_right_weight < child_left_weight and child_right_weight < weight:
|
||||
self._swap_nodes(child_right_position, curr_pos)
|
||||
return self._bubble_down(elem)
|
||||
if child_left_position < self.elements:
|
||||
_, child_left_weight = self.heap[child_left_position]
|
||||
if child_left_weight < weight:
|
||||
self._swap_nodes(child_left_position, curr_pos)
|
||||
return self._bubble_down(elem)
|
||||
else:
|
||||
return
|
||||
return None
|
||||
if child_right_position < self.elements:
|
||||
_, child_right_weight = self.heap[child_right_position]
|
||||
if child_right_weight < weight:
|
||||
self._swap_nodes(child_right_position, curr_pos)
|
||||
return self._bubble_down(elem)
|
||||
else:
|
||||
return
|
||||
return None
|
||||
|
||||
def _swap_nodes(self, node1_pos: int, node2_pos: int) -> None:
|
||||
# Swap the nodes at the given positions
|
||||
|
Reference in New Issue
Block a user