SHARE
TWEET

Untitled

a guest Feb 23rd, 2013 275 Never
  1. Just a quick response to the first answer (don't get this solution seriously).
  2.  
  3.     currentLabel = 0
  4.     f = {}
  5.     def DFS_Loop(graph):
  6.         checked = []
  7.         global currentLabel
  8.         currentLabel = len(graph)
  9.         for edge in graph.keys():
  10.             if edge not in checked :
  11.                 checked = DFS(graph,edge, checked)
  12.    
  13.     def DFS(graph, start, path=[]):
  14.         path = path + [start]
  15.         global currentLabel
  16.         global f
  17.         for edge in graph[start]:
  18.             if edge not in path:
  19.                 path = DFS(graph, edge, path)
  20.      
  21.         f[currentLabel] =  start
  22.         currentLabel = currentLabel - 1
  23.         return path
  24.    
  25.     graph = {
  26.         1: [2,11],
  27.         2: [3],
  28.         11: [12],
  29.         12: [13],
  30.         3: [],
  31.         13:[]
  32.         }
  33.     DFS_Loop(graph)
  34.     print f
RAW Paste Data
Top