Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 23rd, 2013  |  syntax: None  |  size: 0.84 KB  |  views: 121  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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