title: Recursive depth first search python that returns weighted total
body: <p>I am trying to write a depth first search function in python. I am having trouble returning the total weight of the shortest path. This is what I have so far.. </p>
  
  <pre><code>def dfs(road,start,end,dist_so_far,path=[]):
      path = path + [start]
      if start == end:
          path.append(path)
          del path[-1]
          print(path)
      for node, weight in roads[start]:
          if node not in path:
              dfs(roads, node, end, dist_so_far, path)
      return #return total weight
  </code></pre>
  
  <p>I am lost at the moment, any ideas on what I need to do? The inputs look like:</p>
  
  <pre><code>road = {'f': [('d', 2.0), ('g', 7.0)], 
          'b': [('a', 5.0), ('d', 6.0)], 
          'c': [('a', 8.0), ('d', 2.0)], 
          'e': [('d', 12.0), ('g', 3.0)], 
          'g': [('e', 3.0), ('f', 7.0)], 
          'a': [('b', 5.0), ('c', 8.0)], 
          'd': [('b', 6.0), ('c', 2.0), ('e', 12.0), ('f', 2.0)]}
  
  print(dfs(road, 'a', 'b', 0.0))
  </code></pre>
  
  <p>I need the function to return total shortest distance. I'm still relatively new to python and what I have already could be completely wrong.</p>
  
  <p><strong>EDIT</strong> I think I figured it out, I was kinda going about it wrong..</p>
  
  <pre><code>def dfs(place, dist_so_far, roads, distances):
      if (place not in distances) or (distances[place] &gt; dist_so_far):
          distances[place] = dist_so_far
          for combo in roads[place]:
              to_city, dist = combo
              dfs(to_city, dist_so_far+dist, roads, distances)
  
  distances = { }
  start_place = 'a'
  dfs(start_place, 0.0, roads, distances )
  
  if destination in distances :
      print("Distance from {} to {} is {}".format(start_place,destination, distances[destination]))
  </code></pre>
  
  <p>Hopefully this helps someone else later on down the road.</p>