title: Developing A* algorithm in Python
body: <p>I´m trying to develop a algorithm A* in Python in a recursive way. But it´s not correct because it should have to consider the cost of path and the cost of state. My code is the follow, it gives the right answer but I think it is lucky. Can anybody help fix my code? The total cost is wrong.</p>
  <pre><code>graph= {'A':{'B':6, 'F':3},
      'B':{'C':3, 'D':2},
      'C':{'E':5, 'D':1},
      'D':{'E':8},
      'E':{'J':5},
      'F':{'G':1, 'H':7},
      'G':{'I':3},
      'H':{'I':2},
      'I':{'E':5, 'J':3},
      'J':{}}
  
  state = {'A':10,
       'B':8,
       'C':5,
       'D':7,
       'E':3,
       'F':6,
       'G':5,
       'H':3,
       'I':1,
       'J':0}
  </code></pre>
  <p><a href="https://i.stack.imgur.com/t5cxG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/t5cxG.png" alt="enter image description here" /></a></p>
  <pre><code>def A_star(graph, start_node, end_node, state):
      m ={}
      value = 0
      if start_node == end_node:
          return
      for node, v in graph[start_node].items():
          value = state[node] + v
          m[node] = value 
      if (m != {}):  
          path[min(m, key=m.get)] = value
          A_star(graph, min(m, key=m.get), end_node, state)
      return path
  
  
  path = {}
  path['A'] = 0
  bp = (A_star(graph,'A','J',state))
  
  print (list(bp))
  print(&quot;Total cost: {}&quot;.format(sum(bp.values())))
  print(bp)
  </code></pre>
  <blockquote>
  <p>['A', 'F', 'G', 'I', 'J'] Total cost: 26 {'A': 0, 'F': 9, 'G': 10,
  'I': 4, 'J': 3}</p>
  </blockquote>