Deep search - python

Asked

Viewed 972 times

0

import csv

with open('cidades.csv', 'r') as f:
   list2 = [tuple(line.values()) for line in csv.DictReader(f)]



def dfs_helper(list2, start_city, end_city):
    pilha = []
    visited = []
    adj_cities = get_connections(list2, start_city)
    dfs_visit(start_city,visited,pilha,adj_cities)

def dfs_visit(start_city,visited,pilha):
   visited.append(start_city)

   if(end_city in visited):
        return pilha

   for adj_city in start_city:
        pilha.append(adj_city)  
        if adj_city not in visited:
            dfs_visit(adj_city,visited,pilha)

 def get_connections(list2, city,adj_city):
   ligacoes = []

   for result in list2:
       if (result[0] == city):
           ligacoes.insert(0, result[1])
       elif (result[1] == city and result[0] != city):
           ligacoes.insert(0, result[0])

   ligacoes.reverse()
   return ligacoes
dfs_helper(list2, "Lisboa", "Moscovo")

I need to find the shortest way between the two cities. Tuples are of the form (Origin, Destination, Distance).

  • you have to use one of the algorithms that do this, you have a grapho, ie points with connections, weights are distances. https://pt.wikipedia.org/wiki/Problema_do_caminho_m%C3%ADnimo

  • "graph" or "Graph" ... "grapho" is bizarre. :-)

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.