Skip to content

C++ to Python Implementation #2368

Closed Answered by ghost
jha-vineet69 asked this question in Graph and Algorithms
Jan 31, 2021 · 2 comments · 2 replies
Discussion options

You must be logged in to vote

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)

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@jha-vineet69
Comment options

Comment options

You must be logged in to vote
1 reply
@jha-vineet69
Comment options

Answer selected by lperron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Lang: Python Python wrapper issue Solver: Graph and Algorithm Solvers in the graph/ and algorithms/ directories
2 participants