"""Given a graph G=(V,E). Implement BFS for a graph and output the sequence in which the graph will be searched.""" import time import sys t0=time.clock() a=[] b=[] c=[] d=[] e=[] f=[] queue=[] ans=[] fi=open("in18_8.txt",'r') w=open("out_18_18_8.txt",'w') count=0 for row in fi: list=row.split(" ") if count==0: for i in list: a.append(int(i)) if count==1: for i in list: b.append(int(i)) if count==2: for i in list: c.append(int(i)) if count==3: for i in list: d.append(int(i)) if count==4: for i in list: e.append(int(i)) if count==5: for i in list: f.append(int(i)) count+=1 print a,b,c,d,e,f queue.append('c') if c[0]==1: queue.append('a') if c[1]==1: queue.append('b') if c[3]==1: queue.append('d') if c[4]==1: queue.append('e') if c[5]==1: queue.append('f') ans.append('c') queue.remove('c') for k in range(len(queue)): le=len(queue) if le>0: if queue[0]=='a': ans.append('a') queue.remove('a') flag=0 for i in range(len(a)): if a[i]==1: if i==0: temp='a' if i==1: temp='b' if i==2: temp='c' if i==3: temp='d' if i==4: temp='e' if i==5: temp='f' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) if queue[0]=='b': ans.append('b') queue.remove('b') flag=0 for i in range(len(b)): if b[i]==1: if i==0: temp='a' if i==2: temp='c' if i==3: temp='d' if i==4: temp='e' if i==5: temp='f' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) if queue[0]=='c': ans.append('c') queue.remove('c') flag=0 for i in range(len(c)): if c[i]==1: if i==0: temp='a' if i==1: temp='b' if i==3: temp='d' if i==4: temp='e' if i==5: temp='f' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) if queue[0]=='d': ans.append('d') queue.remove('d') flag=0 for i in range(len(d)): if d[i]==1: if i==0: temp='a' if i==1: temp='b' if i==2: temp='c' if i==4: temp='e' if i==5: temp='f' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) if queue[0]=='e': ans.append('e') queue.remove('e') flag=0 for i in range(len(e)): if e[i]==1: if i==0: temp='a' if i==1: temp='b' if i==2: temp='c' if i==3: temp='d' if i==5: temp='f' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) if queue[0]=='f': ans.append('f') queue.remove('f') flag=0 for i in range(len(f)): if f[i]==1: if i==0: temp='a' if i==1: temp='b' if i==2: temp='c' if i==3: temp='d' if i==4: temp='e' for j in range(len(queue)): if queue[j]==temp: flag=flag+1 if flag==0: fl=0 for l in range(len(ans)): if ans[l]==temp: fl=fl+1 if fl==0: queue.append(temp) print ans for i in ans: w.write(i+'-') print queue t1=time.clock() t2=t1-t0 w.write('\n'+"time used ="+repr(t2)) mem=sys.getsizeof(t1)+sys.getsizeof(t2)+sys.getsizeof(i)+sys.getsizeof(j)+72 w.write('\n'+'memory used ='+ repr(mem)) fi.close() w.close()