Repository navigation
C++ to Python Implementation #2368
|
How can I use hamiltonian_path and DijkstraShortestPath etc in Python? I have data as a distance matrix. I don't understand the input structure of these functions. |
Answered by
ghost
Jan 31, 2021
Replies: 2 comments 2 replies
|
Dijkstra is a trivial implementation. You should look up for a better one.
The hamiltonian path is a bit more tricky, but is not wrapped in python.
So, create your own easy to use API in c++, wrap it in python and use that.
Laurent Perron | Operations Research | lperron@google.com | (33) 1 42 68 53
00
Le dim. 31 janv. 2021 à 19:21, Vineet Jha <notifications@github.com> a
écrit :
… How can I use hamiltonian_path and DijkstraShortestPath etc in Python? I
have data as a distance matrix. I don't understand the input structure of
these functions.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#2368>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACUPL3MODMX4OU42FLZQRFTS4WNRNANCNFSM4W3SX4HA>
.
|
1 reply
|
As Laurent said it isn't recommended, but here's an example for Dijkstra anyway: from ortools.graph import pywrapgraph
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],
]
start = 0
end = 8
nodes = len(graph)
dijkstra = pywrapgraph.DijkstraShortestPath(
nodes, start, end, lambda i, j: graph[i][j], disconnected_distance=0
)
print(dijkstra) |
1 reply
Answer selected by
lperron
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As Laurent said it isn't recommended, but here's an example for Dijkstra anyway: