Expanding the path from the graph

I know that there are modules for this type of structure, but I like and prefer to learn how everything really works. So ... I am trying to expand the path from the graph, for example, for example: graph:

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 see the neighbors of this node with:

def vecinosDe(n = ''):
    return g[n]

I want that every time I call a function with one argument, the node graph returns a list of other nodes associated with the specified one.
Then enter the same list that was created, created for the same function, to return the nodes associated with the specified nodes in the list of the list, and sequentially until it reaches the "g" node.
I also know that I need to check if there is any recursion (loop) for the node connected to the node.

Here is the code that I still have:

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

, , , lol.

>>> expandir('s')
[['s', 'd', 'a', 's']]
>>> expandir('d')
[['S', 'D', 'A', 'S', 'S', 'A', 'E'], ['D', 'S', 'A', 'E']]

, , node, node, , node. , .
?

if n == v:  #?

, - , ? , .: P

, ?... ?: S
- ?:)

+5
3

@tangentstorm answer, :

def pathiter(adjacent_vertexes, start, end, path=None):
    if path is None: path = (start,)

    for vertex in adjacent_vertexes[start]:
        if vertex == end:
            yield path + (vertex,)
        elif vertex not in path:
            for p in pathiter(adjacent_vertexes, vertex, end, path + (vertex,)):
                yield p

:

end = 'g'
for v in g:
    path = next(pathiter(g, v, end), None)
    if path is not None:
        print ' β†’ '.join(path)
    else:
        print "no path between %s and %s" % (v, end)

a β†’ s β†’ d β†’ e β†’ f β†’ g
c β†’ b β†’ a β†’ s β†’ d β†’ e β†’ f β†’ g
b β†’ a β†’ s β†’ d β†’ e β†’ f β†’ g
e β†’ f β†’ g
d β†’ s β†’ a β†’ b β†’ e β†’ f β†’ g
g β†’ f β†’ g
f β†’ g
s β†’ a β†’ d β†’ e β†’ f β†’ g

:

for v in graph:
    print "%s β‡’ %s:" % (v, end)
    path = None
    for path in pathiter(graph, v, end):
        print '\t'+' β†’ '.join(path)
    if path is None:
        print "no path between %s and %s" % (v, end)

a β‡’ g:
    a β†’ s β†’ d β†’ e β†’ f β†’ g
    a β†’ d β†’ e β†’ f β†’ g
    a β†’ b β†’ e β†’ f β†’ g
c β‡’ g:
    c β†’ b β†’ a β†’ s β†’ d β†’ e β†’ f β†’ g
    c β†’ b β†’ a β†’ d β†’ e β†’ f β†’ g
    c β†’ b β†’ e β†’ f β†’ g
b β‡’ g:
    b β†’ a β†’ s β†’ d β†’ e β†’ f β†’ g
    b β†’ a β†’ d β†’ e β†’ f β†’ g
    b β†’ e β†’ f β†’ g
e β‡’ g:
    e β†’ f β†’ g
d β‡’ g:
    d β†’ s β†’ a β†’ b β†’ e β†’ f β†’ g
    d β†’ a β†’ b β†’ e β†’ f β†’ g
    d β†’ e β†’ f β†’ g
g β‡’ g:
    g β†’ f β†’ g
f β‡’ g:
    f β†’ g
s β‡’ g:
    s β†’ a β†’ d β†’ e β†’ f β†’ g
    s β†’ a β†’ b β†’ e β†’ f β†’ g
    s β†’ d β†’ a β†’ b β†’ e β†’ f β†’ g
    s β†’ d β†’ e β†’ f β†’ g
+3

:

graph = g # from your code

def findPath(graph, node, path=tuple(), end='g'):
    for key in graph[node]:
        next = path + (key,)
        if key == end:
            raise Exception("Found it! Path is: %s" % '.'.join(path))
        elif key in path:
            pass # avoid loops
        else:
            # print next # if you want to see what it doing
            findPath(graph, key, next, end)
    else: # no break/raise at this level
        if len(path) == 0: print "no path found."

findPath(graph, 's')

, ( :)] , (lista) []. ,

, - , ? , .: P

, , .:)

+1

javascript http://eloquentjavascript.net/chapter7.html. , , javascript, . , , .

+1

All Articles