Skip to content

Commit

Permalink
wip - ref shortest path solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannymassuia committed Nov 12, 2023
1 parent 685d6a0 commit cf2ff38
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,84 @@ public Map<NodeKey, Distance> calculateDistances(Map<NodeKey, Node> graph, NodeK
}

}

/*
- https://leetcode.com/problems/network-delay-time/solutions/4191136/bellman/
- https://github.com/wilsonneto-dev/Playing_with_Graphs
// Dijsktra
class Solution:
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
k, _max, edges = k -1, -1, defaultdict(list)
for s, t, w in times:
edges[s-1].append((t-1, w))
costs, priorities = {}, [(0, k)]
while priorities:
currCost, curr = heapq.heappop(priorities)
if curr in costs:
continue
costs[curr] = currCost
_max = max(_max, currCost)
for target, costToTarget in edges[curr]:
if target not in costs:
heapq.heappush(priorities, (currCost + costToTarget, target))
if len(costs) != n:
return -1
return _max
// Bellman-Ford
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
k = k -1
costs = [float('inf')] * n
costs[k] = 0
for _ in range(n-1):
change = False
for source, target, weight in times:
cost = costs[source-1] + weight
if costs[target-1] > cost:
change = True
costs[target-1] = cost
if not change:
break
latency = max(costs)
if latency == float('inf'):
return -1
return latency
// Floyd-Warshall
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:
mat = [[float('inf')] * n for _ in range(n)]
# fill diagnal with zeros
for i in range(n):
mat[i][i] = 0
# fill the edges weights
for source, target, weight in times:
mat[source-1][target-1] = weight
# get k as intermediate candidate
for intermediate in range(n):
for i in range(n):
for j in range(n):
if i != j:
cost = mat[i][intermediate] + mat[intermediate][j]
if mat[i][j] > cost:
mat[i][j] = cost
signalLatency = max(mat[k-1])
if signalLatency == float('inf'):
return -1
return signalLatency
* */

0 comments on commit cf2ff38

Please sign in to comment.