Last active
May 25, 2023 06:13
-
-
Save igorvanloo/b810f3974f6d606aab5e62e3fdb5083b to your computer and use it in GitHub Desktop.
p425.2 Widest Path Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import heapq | |
| def widest(graph, start = 0, INFINITY = 10**10): | |
| width = {} | |
| cloud = {} | |
| path = {} | |
| for x in graph: | |
| width[x] = INFINITY | |
| cloud[x] = False | |
| path[x] = (2,) #Optional, returns the actual path, useful for debugging | |
| width[start] = 0 | |
| queue = [(2, 2)] | |
| while len(queue) != 0: | |
| wu, u = heapq.heappop(queue) | |
| cloud[u] = True | |
| if wu == INFINITY: | |
| break | |
| for v, w in graph[u]: | |
| if cloud[v] == False: | |
| alt = min(width[v], max(wu, w)) | |
| if alt < width[v]: | |
| path[v] = path[u] + (v,) #Optional, returns the actual path, useful for debugging | |
| width[v] = alt | |
| heapq.heappush(queue, (width[v], v)) | |
| return width, path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment