Question

I'm implementing the A* search algorithm on a graph. I'm using NetworkX in Python to avoid defining my own structure of the graph, but I can't find a way to assign the heuristic for every node.

I was able to give the weight for every edge, but how can I assign the heuristic to every node?

Thank you

Was it helpful?

Solution

You can add arbitrary node attributes, including functions, like this

In [1]: import networkx as nx

In [2]: G = nx.Graph()

In [3]: G.add_node(1,color='red')

In [4]: import math

In [5]: G.add_node('a',f = math.cos)

In [6]: G.node['a']['f'](math.pi)
Out[6]: -1.0
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top