mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-03-27 08:56:44 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
da0717b9ae
commit
18319a8733
@ -2,26 +2,27 @@ from collections import deque
|
||||
import heapq
|
||||
import sys
|
||||
|
||||
#First implementation of johnson algorithm
|
||||
|
||||
# First implementation of johnson algorithm
|
||||
class JohnsonGraph:
|
||||
def __init__(self):
|
||||
self.edges = []
|
||||
self.graph = {}
|
||||
|
||||
#add vertices for a graph
|
||||
# add vertices for a graph
|
||||
def add_vertices(self, 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):
|
||||
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):
|
||||
no_v = len(self.graph)
|
||||
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
|
||||
pq = [(0,s)]
|
||||
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
||||
pq = [(0, s)]
|
||||
|
||||
distances[s] = 0
|
||||
while pq:
|
||||
@ -31,28 +32,27 @@ class JohnsonGraph:
|
||||
continue
|
||||
|
||||
for node, w in self.graph[v]:
|
||||
if distances[v]+w < distances[node]:
|
||||
distances[node] = distances[v]+w
|
||||
if distances[v] + w < distances[node]:
|
||||
distances[node] = distances[v] + w
|
||||
heapq.heappush(pq, (distances[node], node))
|
||||
return distances
|
||||
|
||||
#carry out the bellman ford algorithm for a node and estimate its distance vector
|
||||
# carry out the bellman ford algorithm for a node and estimate its distance vector
|
||||
def bellman_ford(self, s):
|
||||
no_v = len(self.graph)
|
||||
distances = {vertex: sys.maxsize-1 for vertex in self.graph}
|
||||
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
||||
distances[s] = 0
|
||||
|
||||
for u in self.graph:
|
||||
for u, v, w in self.edges:
|
||||
if distances[u] != sys.maxsize-1 and distances[u]+w<distances[v]:
|
||||
distances[v] = distances[u]+w
|
||||
if distances[u] != sys.maxsize - 1 and distances[u] + w < distances[v]:
|
||||
distances[v] = distances[u] + w
|
||||
|
||||
return distances
|
||||
|
||||
#perform the johnson algorithm to handle the negative weights that could not be handled by either the dijkstra
|
||||
#or the bellman ford algorithm efficiently
|
||||
# perform the johnson algorithm to handle the negative weights that could not be handled by either the dijkstra
|
||||
# or the bellman ford algorithm efficiently
|
||||
def johnson_algo(self):
|
||||
|
||||
self.add_vertices("#")
|
||||
for v in self.graph:
|
||||
if v != "#":
|
||||
@ -74,13 +74,14 @@ class JohnsonGraph:
|
||||
for u in self.graph:
|
||||
new_dist = self.dijkstra(u)
|
||||
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]
|
||||
distances.append(new_dist)
|
||||
return distances
|
||||
|
||||
|
||||
g = JohnsonGraph()
|
||||
#this a complete connected graph
|
||||
# this a complete connected graph
|
||||
g.add_vertices("A")
|
||||
g.add_vertices("B")
|
||||
g.add_vertices("C")
|
||||
|
Loading…
x
Reference in New Issue
Block a user