-
- Notifications
You must be signed in to change notification settings - Fork 49.2k
Create graphs/dijkstra_alternate.py #7405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
17 commits Select commit Hold shift + click to select a range
e50b975 Update dijkstra.py
AtulRajput01 74c2f79 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0a17d6a Update dijkstra.py
AtulRajput01 52ae3d6 Update graphs/dijkstra.py
AtulRajput01 60886b5 Update graphs/dijkstra.py
AtulRajput01 1a5eff0 Update graphs/dijkstra.py
AtulRajput01 3f2b017 Update dijkstra.py
AtulRajput01 d3dc21c [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0b28bf9 Update dijkstra.py
AtulRajput01 1efce83 Apply suggestions from code review
cclauss f7c8139 Create dijkstra_alternate.py
cclauss ce2e683 Update dijkstra.py
cclauss 2e26c75 [pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 47d405c int(1e7)
cclauss 8890161 Update dijkstra_alternate.py
cclauss 78ab169 Update graphs/dijkstra_alternate.py
cclauss fa6d2ed sptset --> visited
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,118 +1,77 @@ | ||
| """ | ||
| pseudo-code | ||
| DIJKSTRA(graph G, start vertex s, destination vertex d): | ||
| //all nodes initially unexplored | ||
| 1 - let H = min heap data structure, initialized with 0 and s [here 0 indicates | ||
| the distance from start vertex s] | ||
| 2 - while H is non-empty: | ||
| 3 - remove the first node and cost of H, call it U and cost | ||
| 4 - if U has been previously explored: | ||
| 5 - go to the while loop, line 2 //Once a node is explored there is no need | ||
| to make it again | ||
| 6 - mark U as explored | ||
| 7 - if U is d: | ||
| 8 - return cost // total cost from start to destination vertex | ||
| 9 - for each edge(U, V): c=cost of edge(U,V) // for V in graph[U] | ||
| 10 - if V explored: | ||
| 11 - go to next V in line 9 | ||
| 12 - total_cost = cost + c | ||
| 13 - add (total_cost,V) to H | ||
| You can think at cost as a distance where Dijkstra finds the shortest distance | ||
| between vertices s and v in a graph G. The use of a min heap as H guarantees | ||
| that if a vertex has already been explored there will be no other path with | ||
| shortest distance, that happens because heapq.heappop will always return the | ||
| next vertex with the shortest distance, considering that the heap stores not | ||
| only the distance between previous vertex and current vertex but the entire | ||
| distance between each vertex that makes up the path from start vertex to target | ||
| vertex. | ||
| """ | ||
| import heapq | ||
| | ||
| | ||
| def dijkstra(graph, start, end): | ||
| """Return the cost of the shortest path between vertices start and end. | ||
| >>> dijkstra(G, "E", "C") | ||
| 6 | ||
| >>> dijkstra(G2, "E", "F") | ||
| 3 | ||
| >>> dijkstra(G3, "E", "F") | ||
| 3 | ||
| """ | ||
| | ||
| heap = [(0, start)] # cost from start node,end node | ||
| visited = set() | ||
| while heap: | ||
| (cost, u) = heapq.heappop(heap) | ||
| if u in visited: | ||
| continue | ||
| visited.add(u) | ||
| if u == end: | ||
| return cost | ||
| for v, c in graph[u]: | ||
| if v in visited: | ||
| continue | ||
| next_item = cost + c | ||
| heapq.heappush(heap, (next_item, v)) | ||
| return -1 | ||
| | ||
| | ||
| G = { | ||
| "A": [["B", 2], ["C", 5]], | ||
| "B": [["A", 2], ["D", 3], ["E", 1], ["F", 1]], | ||
| "C": [["A", 5], ["F", 3]], | ||
| "D": [["B", 3]], | ||
| "E": [["B", 4], ["F", 3]], | ||
| "F": [["C", 3], ["E", 3]], | ||
| } | ||
| | ||
| r""" | ||
| Layout of G2: | ||
| E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F | ||
| \ /\ | ||
| \ || | ||
| ----------------- 3 -------------------- | ||
| """ | ||
| G2 = { | ||
| "B": [["C", 1]], | ||
| "C": [["D", 1]], | ||
| "D": [["F", 1]], | ||
| "E": [["B", 1], ["F", 3]], | ||
| "F": [], | ||
| } | ||
| | ||
| r""" | ||
| Layout of G3: | ||
| E -- 1 --> B -- 1 --> C -- 1 --> D -- 1 --> F | ||
| \ /\ | ||
| \ || | ||
| -------- 2 ---------> G ------- 1 ------ | ||
| """ | ||
| G3 = { | ||
| "B": [["C", 1]], | ||
| "C": [["D", 1]], | ||
| "D": [["F", 1]], | ||
| "E": [["B", 1], ["G", 2]], | ||
| "F": [], | ||
| "G": [["F", 1]], | ||
| } | ||
| | ||
| short_distance = dijkstra(G, "E", "C") | ||
| print(short_distance) # E -- 3 --> F -- 3 --> C == 6 | ||
| | ||
| short_distance = dijkstra(G2, "E", "F") | ||
| print(short_distance) # E -- 3 --> F == 3 | ||
| | ||
| short_distance = dijkstra(G3, "E", "F") | ||
| print(short_distance) # E -- 2 --> G -- 1 --> F == 3 | ||
| | ||
| if __name__ == "__main__": | ||
| import doctest | ||
| | ||
| doctest.testmod() | ||
| class Graph: | ||
| def __init__(self, vertices): | ||
| self.V = vertices | ||
| self.graph = [[0 for column in range(vertices)] for row in range(vertices)] | ||
| | ||
| def printsolution(self, dist): | ||
AtulRajput01 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| print("Vertex \t Distance from Source") | ||
| for node in range(self.V): | ||
| print(node, "\t\t", dist[node]) | ||
| | ||
| # A utility function to find the vertex with | ||
| # minimum distance value, from the set of vertices | ||
| # not yet included in shortest path tree | ||
| def mindistance(self, dist, sptset): | ||
| | ||
| # Initialize minimum distance for next node | ||
| minimum = 1e7 | ||
| | ||
| # Search not nearest vertex not in the | ||
| # shortest path tree | ||
| for v in range(self.V): | ||
| if dist[v] < minimum and sptset[v] == False: | ||
cclauss marked this conversation as resolved. Outdated Show resolved Hide resolved AtulRajput01 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| minimum = dist[v] | ||
| min_index = v | ||
| | ||
| return min_index | ||
| | ||
| # Function that implements Dijkstra's single source | ||
| # shortest path algorithm for a graph represented | ||
| # using adjacency matrix representation | ||
| def dijkstra(self, src): | ||
| | ||
| dist = [1e7] * self.V | ||
| dist[src] = 0 | ||
| sptset = [False] * self.V | ||
| | ||
| for cout in range(self.V): | ||
cclauss marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| # Pick the minimum distance vertex from | ||
| # the set of vertices not yet processed. | ||
| # u is always equal to src in first iteration | ||
| u = self.mindistance(dist, sptset) | ||
| | ||
| # Put the minimum distance vertex in the | ||
| # shortest path tree | ||
| sptset[u] = True | ||
| | ||
| # Update dist value of the adjacent vertices | ||
| # of the picked vertex only if the current | ||
| # distance is greater than new distance and | ||
| # the vertex in not in the shortest path tree | ||
| for v in range(self.V): | ||
| if ( | ||
cclauss marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| self.graph[u][v] > 0 | ||
| and sptset[v] == False | ||
cclauss marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| and dist[v] > dist[u] + self.graph[u][v] | ||
| ): | ||
| dist[v] = dist[u] + self.graph[u][v] | ||
| | ||
| self.printsolution(dist) | ||
| | ||
| | ||
| # Driver program | ||
| g = Graph(9) | ||
| g.graph = [ | ||
| [0, 4, 0, 0, 0, 0, 0, 8, 0], | ||
| [4, 0, 8, 0, 0, 0, 0, 11, 0], | ||
| [0, 8, 0, 7, 0, 4, 0, 0, 2], | ||
| [0, 0, 7, 0, 9, 14, 0, 0, 0], | ||
| [0, 0, 0, 9, 0, 10, 0, 0, 0], | ||
| [0, 0, 4, 14, 10, 0, 2, 0, 0], | ||
| [0, 0, 0, 0, 0, 2, 0, 1, 6], | ||
| [8, 11, 0, 0, 0, 0, 1, 0, 7], | ||
| [0, 0, 2, 0, 0, 0, 6, 7, 0], | ||
| ] | ||
AtulRajput01 marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| g.dijkstra(0) | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.