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

Answer by zubinmehta for Shortest distance algorithm Python

$
0
0

Firstly create one graph out of this to connect all the nodes and then run the shortest_path code(There could be an efficient graph library to do this instead of the function mentioned below, nevertheless this one is elegant) and then find out all the number of movie names from the shortest path.

for i in movie_info:    actor_info[i] = movie_info[i]def find_shortest_path(graph, start, end, path=[]):    path = path + [start]    if start == end:        return path    if not start in graph:        return None    shortest = None    for node in graph[start]:        if node not in path:            newpath = find_shortest_path(graph, node, end, path)            if newpath:                if not shortest or len(newpath) < len(shortest):                    shortest = newpath    return shortestL = find_shortest_path(actor_info, 'act1', 'act2')print len([i for i in L if i in movie_info])

find_shortest_path Source: http://www.python.org/doc/essays/graphs/


Viewing all articles
Browse latest Browse all 3

Trending Articles



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