mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-28 11:58:39 +00:00
Added tests
This commit is contained in:
parent
f9bf655086
commit
5fe9e9b639
@ -9,6 +9,11 @@ class JohnsonGraph:
|
|||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""
|
"""
|
||||||
Initializes an empty graph with no edges.
|
Initializes an empty graph with no edges.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.edges
|
||||||
|
[]
|
||||||
|
>>> g.graph
|
||||||
|
{}
|
||||||
"""
|
"""
|
||||||
self.edges: list[tuple[str, str, int]] = []
|
self.edges: list[tuple[str, str, int]] = []
|
||||||
self.graph: dict[str, list[tuple[str, int]]] = {}
|
self.graph: dict[str, list[tuple[str, int]]] = {}
|
||||||
@ -16,14 +21,27 @@ class JohnsonGraph:
|
|||||||
# add vertices for a graph
|
# add vertices for a graph
|
||||||
def add_vertices(self, vertex: str) -> None:
|
def add_vertices(self, vertex: str) -> None:
|
||||||
"""
|
"""
|
||||||
Adds a vertex `u` to the graph with an empty adjacency list.
|
Adds a vertex `vertex` to the graph with an empty adjacency list.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.add_vertices("A")
|
||||||
|
>>> g.graph
|
||||||
|
{'A': []}
|
||||||
"""
|
"""
|
||||||
self.graph[vertex] = []
|
self.graph[vertex] = []
|
||||||
|
|
||||||
# assign weights for each edges formed of the directed graph
|
# assign weights for each edges formed of the directed graph
|
||||||
def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None:
|
def add_edge(self, vertex_a: str, vertex_b: str, weight: int) -> None:
|
||||||
"""
|
"""
|
||||||
Adds a directed edge from vertex `u` to vertex `v` with weight `w`.
|
Adds a directed edge from vertex `vertex_a`
|
||||||
|
to vertex `vertex_b` with weight `weight`.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.add_vertices("A")
|
||||||
|
>>> g.add_vertices("B")
|
||||||
|
>>> g.add_edge("A", "B", 5)
|
||||||
|
>>> g.edges
|
||||||
|
[('A', 'B', 5)]
|
||||||
|
>>> g.graph
|
||||||
|
{'A': [('B', 5)], 'B': []}
|
||||||
"""
|
"""
|
||||||
self.edges.append((vertex_a, vertex_b, weight))
|
self.edges.append((vertex_a, vertex_b, weight))
|
||||||
self.graph[vertex_a].append((vertex_b, weight))
|
self.graph[vertex_a].append((vertex_b, weight))
|
||||||
@ -31,8 +49,18 @@ class JohnsonGraph:
|
|||||||
# perform a dijkstra algorithm on a directed graph
|
# perform a dijkstra algorithm on a directed graph
|
||||||
def dijkstra(self, start: str) -> dict:
|
def dijkstra(self, start: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Computes the shortest path from vertex `s`
|
Computes the shortest path from vertex `start`
|
||||||
to all other vertices using Dijkstra's algorithm.
|
to all other vertices using Dijkstra's algorithm.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.add_vertices("A")
|
||||||
|
>>> g.add_vertices("B")
|
||||||
|
>>> g.add_edge("A", "B", 1)
|
||||||
|
>>> g.dijkstra("A")
|
||||||
|
{'A': 0, 'B': 1}
|
||||||
|
>>> g.add_vertices("C")
|
||||||
|
>>> g.add_edge("B", "C", 2)
|
||||||
|
>>> g.dijkstra("A")
|
||||||
|
{'A': 0, 'B': 1, 'C': 3}
|
||||||
"""
|
"""
|
||||||
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
||||||
pq = [(0, start)]
|
pq = [(0, start)]
|
||||||
@ -52,8 +80,18 @@ class JohnsonGraph:
|
|||||||
# 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, start: str) -> dict:
|
def bellman_ford(self, start: str) -> dict:
|
||||||
"""
|
"""
|
||||||
Computes the shortest path from vertex `s`
|
Computes the shortest path from vertex `start` to
|
||||||
to all other vertices using the Bellman-Ford algorithm.
|
all other vertices using the Bellman-Ford algorithm.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.add_vertices("A")
|
||||||
|
>>> g.add_vertices("B")
|
||||||
|
>>> g.add_edge("A", "B", 1)
|
||||||
|
>>> g.bellman_ford("A")
|
||||||
|
{'A': 0, 'B': 1}
|
||||||
|
>>> g.add_vertices("C")
|
||||||
|
>>> g.add_edge("B", "C", 2)
|
||||||
|
>>> g.bellman_ford("A")
|
||||||
|
{'A': 0, 'B': 1, 'C': 3}
|
||||||
"""
|
"""
|
||||||
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
distances = {vertex: sys.maxsize - 1 for vertex in self.graph}
|
||||||
distances[start] = 0
|
distances[start] = 0
|
||||||
@ -74,7 +112,18 @@ class JohnsonGraph:
|
|||||||
def johnson_algo(self) -> list[dict]:
|
def johnson_algo(self) -> list[dict]:
|
||||||
"""
|
"""
|
||||||
Computes the shortest paths between
|
Computes the shortest paths between
|
||||||
all pairs of vertices using Johnson's algorithm.
|
all pairs of vertices using Johnson's algorithm
|
||||||
|
for a directed graph.
|
||||||
|
>>> g = JohnsonGraph()
|
||||||
|
>>> g.add_vertices("A")
|
||||||
|
>>> g.add_vertices("B")
|
||||||
|
>>> g.add_vertices("C")
|
||||||
|
>>> g.add_edge("A", "B", 1)
|
||||||
|
>>> g.add_edge("B", "C", 2)
|
||||||
|
>>> g.add_edge("A", "C", 4)
|
||||||
|
>>> optimal_paths = g.johnson_algo()
|
||||||
|
>>> optimal_paths
|
||||||
|
[{'A': 0, 'B': 1, 'C': 3}, {'A': None, 'B': 0, 'C': 2}, {'A': None, 'B': None, 'C': 0}]
|
||||||
"""
|
"""
|
||||||
self.add_vertices("#")
|
self.add_vertices("#")
|
||||||
for vertex in self.graph:
|
for vertex in self.graph:
|
||||||
@ -95,36 +144,26 @@ class JohnsonGraph:
|
|||||||
weight + hash_path[vertex_a] - hash_path[vertex_b])
|
weight + hash_path[vertex_a] - hash_path[vertex_b])
|
||||||
|
|
||||||
self.graph.pop("#")
|
self.graph.pop("#")
|
||||||
self.edges = [
|
|
||||||
(vertex1, vertex2, node_weight)
|
|
||||||
for vertex1, vertex2, node_weight in self.edges
|
|
||||||
if vertex1 != "#"
|
|
||||||
]
|
|
||||||
filtered_edges = []
|
filtered_edges = []
|
||||||
for vertex1, vertex2, node_weight in self.edges:
|
for vertex1, vertex2, node_weight in self.edges:
|
||||||
if vertex1 != "#":
|
|
||||||
filtered_edges.append((vertex1, vertex2, node_weight))
|
filtered_edges.append((vertex1, vertex2, node_weight))
|
||||||
self.edges = filtered_edges
|
self.edges = filtered_edges
|
||||||
|
|
||||||
for vertex in self.graph:
|
for vertex in self.graph:
|
||||||
self.graph[vertex] = [
|
self.graph[vertex] = []
|
||||||
(vertex2, node_weight)
|
|
||||||
for vertex1, vertex2, node_weight in self.edges
|
|
||||||
if vertex1 == vertex
|
|
||||||
]
|
|
||||||
|
|
||||||
filtered_neighbors = []
|
|
||||||
for vertex1, vertex2, node_weight in self.edges:
|
for vertex1, vertex2, node_weight in self.edges:
|
||||||
if vertex1 == vertex:
|
if vertex1 == vertex:
|
||||||
filtered_neighbors.append((vertex2, node_weight))
|
self.graph[vertex].append((vertex2, node_weight))
|
||||||
self.graph[vertex] = filtered_neighbors
|
|
||||||
|
|
||||||
distances = []
|
distances = []
|
||||||
for vertex1 in self.graph:
|
for vertex1 in self.graph:
|
||||||
new_dist = self.dijkstra(vertex1)
|
new_dist = self.dijkstra(vertex1)
|
||||||
for vertex2 in self.graph:
|
for vertex2 in self.graph:
|
||||||
if new_dist[vertex2] < sys.maxsize - 1:
|
if new_dist[vertex2] < sys.maxsize-1:
|
||||||
new_dist[vertex2] += hash_path[vertex1] - hash_path[vertex2]
|
new_dist[vertex2] += hash_path[vertex2] - hash_path[vertex1]
|
||||||
|
for key in new_dist:
|
||||||
|
if new_dist[key] == sys.maxsize-1:
|
||||||
|
new_dist[key] = None
|
||||||
distances.append(new_dist)
|
distances.append(new_dist)
|
||||||
return distances
|
return distances
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user