wylee / Dijkstar

Graphs, Dijkstra, A*, shortest paths, HTTP graph server
MIT License
55 stars 16 forks source link

Path Information #9

Open srkankdr opened 5 years ago

srkankdr commented 5 years ago

Hello,

I have created a simple graph by using a distance matrix. Below you can find the code.

from dijkstar import Graph, find_path
matrix = ...... (distance matrix is created here)
graph = Graph()
for i in range(size):
    for j in range(size):
        graph.add_edge(i, j, {'cost': matrix[i][j]})        
cost_func = lambda u, v, e, prev_e: e['cost']
path = find_path(graph, 0, 6, cost_func=cost_func)
path

My question is, how am I able to learn the details of the founded path. There is a total cost, but I want to learn with which order of nodes I will get this cost. I want to learn the optimal order of nodes.

Thank you.

srkankdr commented 5 years ago

I suppose the problem may be related to the cost function that I used. I directly coppied from the documentation and I couldn't understand the usage of it.

wylee commented 5 years ago

find_path() returns a PathInfo object that has nodes, edges, costs, total_cost attributes, so you can access the ordered nodes with path.nodes.

Regarding the cost function, you don't need it if you're not dynamically computing costs. You could change your code to the following:

from dijkstar import Graph, find_path
matrix = ...... (distance matrix is created here)
graph = Graph()
for i in range(size):
    for j in range(size):
        graph.add_edge(i, j, matrix[i][j])
path = find_path(graph, 0, 6)
path