Quantcast
Channel: Shortest distance algorithm Python - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Shortest distance algorithm Python

$
0
0

I wanted to create a simple breadth first search algorithm, which returns the shortest path.

An actor information dictionary maps and actor to the list of movies the actor appears in:

actor_info = { "act1" : ["movieC", "movieA"], "act2" : ["movieA", "movieB"], "act3" :["movieA", "movieB"], "act4" : ["movieC", "movieD"], "act5" : ["movieD", "movieB"], "act6" : ["movieE"], "act7" : ["movieG", "movieE"], "act8" : ["movieD", "movieF"], "KevinBacon" : ["movieF"], "act10" : ["movieG"], "act11" : ["movieG"] }

The inverse of this maps movies to the list of actors appearing in them:

movie_info = {'movieB': ['act2', 'act3', 'act5'], 'movieC': ['act1', 'act4'], 'movieA': ['act1', 'act2', 'act3'], 'movieF': ['KevinBacon', 'act8'], 'movieG': ['act7', 'act10', 'act11'], 'movieD': ['act8', 'act4', 'act5'], 'movieE': ['act6', 'act7']}

so for a call

shortest_dictance("act1", "Kevin Bacon", actor_info, movie_info)

I should get 3 since act1 appears in movieC with Act4 who appears in movieD with Act8 who appears in movie F with KevinBacon. So the shortest distance is 3.

So far I have this:

def shotest_distance(actA, actB, actor_info, movie_info):'''Return the number of movies required to connect actA and actB.    If theres no connection return -1.'''    # So we keep 2 lists of actors:    #   1.The actors that we have already investigated.    #   2.The actors that need to be investigated because we have found a     #      connection beginning at actA. This list must be     #      ordered, since we want to investigate actors in the order we     #      discover them.    #      -- Each time we put an actor in this list, we also store    #         her distance from actA.    investigated = []    to_investigate = [actA]    distance = 0    while actB not in to_investigate and to_investigate!= []:        for actor in to_investigate:            to_investigated.remove(actA)            investigated.append(act)            for movie in actor_info[actor]:                for co_star in movie_info[movie]:                    if co_star not in (investigated and to_investigate):                        to_investigate.append(co_star) .... .... return d    

I can't figure the appropriate way to keep track of the distances discovered each of iteration of the code. Also the code seems to be very ineffecient time wise.


Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>