|
5 | 5 | from collections import deque
|
6 | 6 | from concurrent.futures import ThreadPoolExecutor
|
7 | 7 | from pydatastructs.utils.misc_util import (
|
8 |
| - _comp, raise_if_backend_is_not_python, Backend) |
| 8 | + _comp, raise_if_backend_is_not_python, Backend, AdjacencyListGraphNode) |
9 | 9 | from pydatastructs.miscellaneous_data_structures import (
|
10 | 10 | DisjointSetForest, PriorityQueue)
|
11 | 11 | from pydatastructs.graphs.graph import Graph
|
@@ -799,7 +799,7 @@ def _dijkstra_adjacency_list(graph: Graph, start: str, target: str):
|
799 | 799 | visited[u] = True
|
800 | 800 | for v in graph.vertices:
|
801 | 801 | edge_str = u + '_' + v
|
802 |
| - if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value > 0 and |
| 802 | + if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value >= 0 and |
803 | 803 | visited[v] is False and dist[v] > dist[u] + graph.edge_weights[edge_str].value):
|
804 | 804 | dist[v] = dist[u] + graph.edge_weights[edge_str].value
|
805 | 805 | pred[v] = u
|
@@ -900,6 +900,53 @@ def _floyd_warshall_adjacency_list(graph: Graph):
|
900 | 900 |
|
901 | 901 | _floyd_warshall_adjacency_matrix = _floyd_warshall_adjacency_list
|
902 | 902 |
|
| 903 | +def _johnson_adjacency_list(graph: Graph): |
| 904 | + new_vertex=AdjacencyListGraphNode('q') |
| 905 | + graph.add_vertex(new_vertex) |
| 906 | + |
| 907 | + for vertex in graph.vertices: |
| 908 | + if vertex != 'q': |
| 909 | + graph.add_edge('q',vertex,0) |
| 910 | + |
| 911 | + distances, predecessors=shortest_paths(graph,'bellman_ford','q') |
| 912 | + |
| 913 | + edges_to_remove=[] |
| 914 | + for edge in graph.edge_weights: |
| 915 | + edge_node=graph.edge_weights[edge] |
| 916 | + if edge_node.source.name=='q': |
| 917 | + edges_to_remove.append((edge_node.source.name,edge_node.target.name)) |
| 918 | + |
| 919 | + for u,v in edges_to_remove: |
| 920 | + graph.remove_edge(u,v) |
| 921 | + graph.remove_vertex('q') |
| 922 | + |
| 923 | + for edge in graph.edge_weights: |
| 924 | + edge_node=graph.edge_weights[edge] |
| 925 | + u,v=edge_node.source.name,edge_node.target.name |
| 926 | + graph.edge_weights[edge].value+=distances[u]-distances[v] |
| 927 | + |
| 928 | + print(graph.edge_weights) |
| 929 | + all_distances={} |
| 930 | + all_next_vertex={} |
| 931 | + |
| 932 | + for vertex in graph.vertices: |
| 933 | + u = vertex |
| 934 | + dijkstra_dist,dijkstra_pred=shortest_paths(graph, 'dijkstra', u) |
| 935 | + print(dijkstra_pred) |
| 936 | + all_distances[u]={} |
| 937 | + all_next_vertex[u] = {} |
| 938 | + for v in graph.vertices: |
| 939 | + if dijkstra_pred[v]==None or dijkstra_pred[v]==u : |
| 940 | + all_next_vertex[u][v]=u |
| 941 | + else: |
| 942 | + all_next_vertex[u][v]=None |
| 943 | + if v in dijkstra_dist: |
| 944 | + all_distances[u][v]=dijkstra_dist[v]-distances[u]+distances[v] |
| 945 | + else: |
| 946 | + all_distances[u][v]=float('inf') |
| 947 | + |
| 948 | + return (all_distances,all_next_vertex) |
| 949 | + |
903 | 950 | def topological_sort(graph: Graph, algorithm: str,
|
904 | 951 | **kwargs) -> list:
|
905 | 952 | """
|
|
0 commit comments