Recursive deep search function

I am trying to write a recursive function, initialized from the command line, which takes a text file with a series of source points, end points and distances between these points, and then finds the shortest distance from a specific starting point to a specific end point.

For example, a text file would look something like this:

a,b,5
a,c,8
b,d,6
c,d,2
d,e,12
d,f,2
e,g,3
f,g,7

and will be called with something like:

python program_name.py a b text_file_name.txt

The idea of ​​this is based on a project from the future class, which I am going to take for the next term, on which I want to start work. The professor provided the extensive "source code" found here .

, , . ( ) :

if place not in distances:
    print('not in')
    distances[place] =roads[place]
    dist_so_far = distances[place]     
    dfs(place, 0.0, roads, distances)
elif place in distances and distances[place] <= dist_so_far:
    print('less than')
    #dfs(place,0.0, roads, distances)
elif place in distances and distances[place] > dist_so_far:
    print('greater than')
    distances[place] = dist_so_far
    dfs(place, 0.0, roads, distances)

, , , . , , .

+4
1

, , , !

, , , ( , ).

def read_distances(map_file):
    connections = dict()
    # ....
    return connections

, , - , . , "A, B, 5", read_distances , A ("B",5) B ("A",5). , . :

# Assume that map_file contained one line: "A,B,5"
distances = read_distances(map_file)
print(distances["A"])  # Will print "[('B',5)]"
print(distances["B"])  # Will print "[('A',5)]"

, , :

A,B,5
A,C,3
B,C,4

- :

distances = read_distances(map_file)
print(distances["A"])  # Will print "[('B',5),('C',3)]"
print(distances["B"])  # Will print "[('A',5),('C',4)]"
print(distances["C"])  # Will print "[('A',3),('B',4)]"

, distances[starting_point], , . 2- (.. 2 ​​), (other_point, distance_as_int).

, , , , , , . ( , : " ", , , .) , ( ), . , , , , .


1:

, , . distances[starting_point] , , - . .

connections = distances[start_point]
for connection in connections:
    end_point = connection[0]
    distance = connection[1]
    # Now do something with start_point, end_point, and distance
    # Precisely *what* you'll do with them is up to you

, Python : , , , "", :

connections = distances[start_point]
for end_point, distance in connections:
    # Now do something with start_point, end_point, and distance
    # Precisely *what* you'll do with them is up to you

"" . , , , , , ( 2). , , , connections, , . , :

for end_point, distance in distances[start_point]:
    # Now do something with start_point, end_point, and distance
    # Precisely *what* you'll do with them is up to you

, "" .

.. , Python , , . , pass . pass , "no-op", .. . , , , :

for end_point, distance in distances[start_point]:
    # Now do something with start_point, end_point, and distance
    # Precisely *what* you'll do with them is up to you
    pass

, pass IndentationError. pass , , , .


2:

, , , , . , .

def dfs(place, dist_so_far, roads, distances):
    """Depth-first search, which may continue from from_place if dist_so_far
        is the shortest distance at which it has yet been reached.
       Args:
          place: Currently searching from here
          dist_so_far:  Distance at which from_place has been reached
              this time (which may not be the shortest path to from_place)
          roads:  dict mapping places to lists of hops of the form (place, hop-distance)
          distances: dict mapping places to the shortest distance at which they
               have been reached so far (up to this time).
    """
    #FIXME
    #   Consider cases:
    #      - We've never been at place before (so it not in distances)
    #      - We've been at place before, on a path as short as this one (in distances)
    #      - We've been here before, but this way is shorter (dist_so_far)
    #    Consider which are base cases, and which require recursion.
    #    For the cases that require recursion, what is the progress step?

    # First scenario: we've never reached this place before
    if place not in distances:
        # Right now we only know one way to get to this place,
        # so that automatically the shortest known distance.
        distances[place] = dist_so_far

    # Second scenario: we've been here before, via a route
    # that was shorter than dist_so_far. If so, then any
    # roads from here lead to places we've also already
    # visited via a shorter route. Any distance we calculate
    # right now would just be longer than the distance we've
    # already found, so we can just stop right now!
    if dist_so_far > distances[place]:
        return

    # Third scenario: dist_so_far is actually the shortest
    # path we've found yet. (The first scenario is actually
    # a special case of this one!) We should record the
    # shortest distance to this place, since we'll want to
    # use that later. Then we'll look at all the roads from
    # this place to other places, and for each of those
    # other places, we'll make a recursive call to figure
    # out more paths.

    # Note no "if" statement needed: because of the return
    # statement earlier, if we get here, we know that the
    # current route is the best one yet known.
    distances[place] = dist_so_far

    # Now for some recursion:
    for other_place, hop_distance in roads[place]:
        dist_to_other_place = dist_so_far + hop_distance
        dfs(other_place, dist_to_other_place, roads, distances)

    # That it!

, . , , . , , .

, , , . : Python . , (, distances ) , , , , .

, BAD . , , , , , . ( . , , add_value_to_dict, , , , .)

, , . distances , , , . , dfs(), . , , , , . , .

, , , . , . -, , .

+2

Source: https://habr.com/ru/post/1614102/


All Articles