#!/usr/bin/python import sys data = open(sys.argv[1], 'r').readlines() data = [line[:-1] for line in data] links = [] depth = {} link = {} for line in data: if line.find(' 1: sons = [(depth[y], y) for x, y in links if x == found] sons.sort() print found, sons found = sons[-1][1]