I know there are modules for this type of structure, but I like and prefer to learn how things really works by myself.
So... I've being trying to expand a path from a graph, such as for example:
g = dict(
s=['a','d','s'],
a=['s','d','b'],
d=['s','a','e'],
b=['a','e','c'],
e=['d','b','f'],
f=['e','g'],
c=['b'],
g=['f'])
So far I can see the neighbors of a given node with:
def vecinosDe(n = ''):
return g[n]
I want to each time I call a function that has one argument given, a graph node, returns a list of the other nodes connected to that one given.
Then enter that same list given, created, to that same function to return the nodes connected to that list graph nodes given, and consecutively until it reaches to the 'g' node.
I also know that I need to check if the node connected to the node given has any recursion(loop).
Here's the code I have so far:
def expandir(n = '', lista = []):
lista.append([n]) #or lista = lista + [n]?
for v in g[n]:
for i in range(len(lista)): #?
lista[i].append(v)
return lista
This is what happens, which I know it's not good, lol.
>>> expandir('s')
[['s', 'd', 'a', 's']]
>>> expandir('d')
[['S', 'D', 'A', 'S', 'S', 'A', 'E'], ['D', 'S', 'A', 'E']]
In some part of the code within the second for loop I think I should check if there is a node equal to the node I want to expand, the node given. Here's where I get stuck.
Try this?
if n == v: #?
Also I'm thinking that this may need some kind of recursion, right? But I would like some tips to keep putting the puzzle together. :P
Should I return a list, a list of lists?... how? :S
Any tips? :)
thanks in advance