#Declaration
graph = {}
result = {}
list1 = []
list2 = []
list3 = []
templist = []
counter = 0
counter1 = 0
count = 1
m = 1

#open data file
data = open("assign1_graph1.txt","r")

#read all lines from data file in a list named "lines"
lines = [line[:-1] for line in data.readlines()[1:-1]]

#close data file
data.close()

#covert each item of each line into integer and created 3 list for storing first node(list1), second node(list2) and weight(list3)
for line in lines:
     a,b,weight = line.split(" ",2)
     weight = int(weight)
     a = int(a)
     b = int(b)
#test     print "weight:", weight
#test     print "a: ", a
#test     print "b: ", b
     list1.append(a)
     list2.append(b)
     list3.append(weight)

list1.append(-1)
list2.append(-1)
list3.append(-1)

#test print len(list1)
#test print len(list2)
#test print len(list3)

#create dictionary for all lists that start with root node - zero
def findrootnode (list1,list2,list3):
    rootlist = []  #initialize list to store list with root node
    for i in range (0,len(list1)):
        if list1[i] == 0:
            root = list1[i]
            child = list2[i]
            weight = list3[i]
            rootlist.append(root)
            rootlist.append(child)
            #print "rootlist content: ",i, rootlist
            graph[i] = rootlist
            rootlist = []  #initialize list to store list with root node
#test    print "graph: ",graph
#test    print len(graph)
    return graph
    return weight

#call function findroot node
findrootnode (list1,list2,list3)

#Create result list into a final dictionary
for j in range (0, len(graph)):
    templist = graph[j]
    child = templist[-1]
    print "templist before search", templist
    print "child: ", child
    if list1[-1] != -1:
        for k in range (1, len(list1)):
            if list1[k] == child:   #compare first node with previous node
                #newchild = list2[k] #if equal, second node become new child
                #print "new child, list2[k]: ", newchild, list2[k]
                print "graph",j,graph[j]
                templist.append(list1[k])
                templist.append(list2[k])
                print "templist after search", templist
            result[counter1] = templist
        print "result dictionary: ", result
        counter1 = counter1 + 1

print len(graph[1])

print "graph[0]: ", graph[0]
print "graph[1]: ", graph[1]
#print "rootlist: ", rootlist
print "weight: ", weight


