mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-27 00:46:43 +00:00
Resolved issues regarding ruff testing and merge conflicts
This commit is contained in:
commit
346a955946
@ -503,6 +503,7 @@
|
|||||||
* [Graphs Floyd Warshall](graphs/graphs_floyd_warshall.py)
|
* [Graphs Floyd Warshall](graphs/graphs_floyd_warshall.py)
|
||||||
* [Greedy Best First](graphs/greedy_best_first.py)
|
* [Greedy Best First](graphs/greedy_best_first.py)
|
||||||
* [Greedy Min Vertex Cover](graphs/greedy_min_vertex_cover.py)
|
* [Greedy Min Vertex Cover](graphs/greedy_min_vertex_cover.py)
|
||||||
|
* [Johnson Graph](graphs/johnson_graph.py)
|
||||||
* [Kahns Algorithm Long](graphs/kahns_algorithm_long.py)
|
* [Kahns Algorithm Long](graphs/kahns_algorithm_long.py)
|
||||||
* [Kahns Algorithm Topo](graphs/kahns_algorithm_topo.py)
|
* [Kahns Algorithm Topo](graphs/kahns_algorithm_topo.py)
|
||||||
* [Karger](graphs/karger.py)
|
* [Karger](graphs/karger.py)
|
||||||
|
@ -9,16 +9,16 @@ class JohnsonGraph:
|
|||||||
self.edges = []
|
self.edges = []
|
||||||
self.graph = {}
|
self.graph = {}
|
||||||
|
|
||||||
#add vertices for a graph
|
# add vertices for a graph
|
||||||
def add_vertices(self, u):
|
def add_vertices(self, u):
|
||||||
self.graph[u] = []
|
self.graph[u] = []
|
||||||
|
|
||||||
#assign weights for each edges formed of the directed graph
|
# assign weights for each edges formed of the directed graph
|
||||||
def add_edge(self, u, v, w):
|
def add_edge(self, u, v, w):
|
||||||
self.edges.append((u, v, w))
|
self.edges.append((u, v, w))
|
||||||
self.graph[u].append((v,w))
|
self.graph[u].append((v, w))
|
||||||
|
|
||||||
#perform a dijkstra algorithm on a directed graph
|
# perform a dijkstra algorithm on a directed graph
|
||||||
def dijkstra(self, s):
|
def dijkstra(self, s):
|
||||||
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
|
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
|
||||||
pq = [(0,s)]
|
pq = [(0,s)]
|
||||||
@ -31,8 +31,8 @@ class JohnsonGraph:
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
for node, w in self.graph[v]:
|
for node, w in self.graph[v]:
|
||||||
if distances[v]+w < distances[node]:
|
if distances[v] + w < distances[node]:
|
||||||
distances[node] = distances[v]+w
|
distances[node] = distances[v] + w
|
||||||
heapq.heappush(pq, (distances[node], node))
|
heapq.heappush(pq, (distances[node], node))
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
@ -43,8 +43,8 @@ class JohnsonGraph:
|
|||||||
|
|
||||||
for u in self.graph:
|
for u in self.graph:
|
||||||
for u, v, w in self.edges:
|
for u, v, w in self.edges:
|
||||||
if distances[u] != sys.maxsize-1 and distances[u]+w<distances[v]:
|
if distances[u] != sys.maxsize - 1 and distances[u] + w < distances[v]:
|
||||||
distances[v] = distances[u]+w
|
distances[v] = distances[u] + w
|
||||||
|
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
@ -52,7 +52,6 @@ class JohnsonGraph:
|
|||||||
# could not be handled by either the dijkstra
|
# could not be handled by either the dijkstra
|
||||||
#or the bellman ford algorithm efficiently
|
#or the bellman ford algorithm efficiently
|
||||||
def johnson_algo(self):
|
def johnson_algo(self):
|
||||||
|
|
||||||
self.add_vertices("#")
|
self.add_vertices("#")
|
||||||
for v in self.graph:
|
for v in self.graph:
|
||||||
if v != "#":
|
if v != "#":
|
||||||
@ -74,13 +73,14 @@ class JohnsonGraph:
|
|||||||
for u in self.graph:
|
for u in self.graph:
|
||||||
new_dist = self.dijkstra(u)
|
new_dist = self.dijkstra(u)
|
||||||
for v in self.graph:
|
for v in self.graph:
|
||||||
if new_dist[v] < sys.maxsize-1:
|
if new_dist[v] < sys.maxsize - 1:
|
||||||
new_dist[v] += n[v] - n[u]
|
new_dist[v] += n[v] - n[u]
|
||||||
distances.append(new_dist)
|
distances.append(new_dist)
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
|
|
||||||
g = JohnsonGraph()
|
g = JohnsonGraph()
|
||||||
#this a complete connected graph
|
# this a complete connected graph
|
||||||
g.add_vertices("A")
|
g.add_vertices("A")
|
||||||
g.add_vertices("B")
|
g.add_vertices("B")
|
||||||
g.add_vertices("C")
|
g.add_vertices("C")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user