SEY = 'Elizabeth Sey' VOLTA = 'Volta Hall' KWAPONG = 'Alexander Kwapong' SARBAH = 'Mensah Sarbah Hall' AKUAFO = 'Akuafo Hall' ARCHEOLOGYa = 'Archeology Annex' BALME = 'Balme Library' BANI = 'Business School' CC= 'Central Cafeteria' CIAD = 'Centre for International Affairs' CSG = 'Centre for Sensing and Geography' COMMONWEALTH = 'Commonwealth Hall' CSD = 'Crop Science Department' DABCS = 'Department of Animal Biology and Conservation Science' DB = 'Department of Botany' CS = 'Department of Computer Science' DStats = 'Department Of Status' DZOOLOGY ='Depatment Of Zoology' EFFUA ='Effua Sutherland Drama Studio' Evandy='Evandy Hostel' AGRIC='Faculty of Agric. Lecture Hall' LAW ='Faculty of Law' FRANK = 'Frank Tortor Chemistry Block' HILLALIMANN = 'Hilla Limann' IPO ='International Programmes Office' ISH = 'International Students Hostel' ISSER='Institute of Statistical, Social and Economic Research' JAMES ='James Topp Yanker' JEAN ='Jean Nelson' JUBILEE ='Jubilee Hall' BUSIA ='KA Busia Building' BOOKSHOP ='Legon Bookshop' LEGONHALL = 'Legon Hall' LEGONHALLb = 'Legon Hall Annex B' LEGONHALLc = 'Legon Hall Annex C' LEGONMOSQUE = 'legon Mosque' NBLOCK ='N-Block Lecture Hall' NewNBLOCK = 'New N-Block Lecture Hall' PENT = 'PENT -ALI Hall' ARTS ='School of Performing Arts' PHEALTH = 'School of Public Health' ENG='School of Engineering Sciences' SRCunion ='SRC Union Building' ACQUINAS ='St. Thomas Acquinas Catholic Church' TUTs ='Tutorial Office, Legon Hall' VALCO ='Valco Trust Hospital' graph = { SEY: [KWAPONG, SARBAH, VOLTA,VALCO], KWAPONG: [SEY, VOLTA, SARBAH], VOLTA: [KWAPONG, SEY, SARBAH], SARBAH: [VOLTA, KWAPONG], ARCHEOLOGYa:[AKUAFO,BALME], BALME:[VOLTA, LEGONHALL, CC], CC:[LEGONHALL, VOLTA, BALME], CIAD:[VOLTA, LEGONHALL, AKUAFO, KWAPONG, COMMONWEALTH], COMMONWEALTH:[VOLTA, LEGONHALL, AKUAFO, KWAPONG, CIAD,PENT], CSD:[SARBAH, AKUAFO, JUBILEE], DZOOLOGY:[BALME, AKUAFO, EFFUA], EFFUA:[BALME, ISSER, Evandy], Evandy:[AKUAFO, BALME, ENG, PENT, AGRIC], AGRIC:[AKUAFO, BALME, LAW], LAW:[FRANK], FRANK:[BALME,HILLALIMANN], HILLALIMANN:[BALME, IPO], IPO:[AKUAFO, BALME, ISH], ISH:[AKUAFO, BALME, ISSER], ISSER:[SEY,KWAPONG, AKUAFO,BALME, JEAN], JEAN:[SEY,KWAPONG, HILLALIMANN, JUBILEE], JUBILEE:[SARBAH, AKUAFO, BUSIA], BUSIA:[AKUAFO, BALME, LEGONHALL], LEGONMOSQUE:[LEGONHALL, VOLTA, NewNBLOCK], NewNBLOCK:[ISSER, ENG, PENT], PENT:[ISSER, ENG, ARTS,COMMONWEALTH], ARTS:[HILLALIMANN, JUBILEE, PHEALTH], PHEALTH:[ISSER, BALME, AKUAFO, JUBILEE, ENG], SRCunion:[SARBAH, LEGONHALL, ISH, VOLTA, BANI, ACQUINAS], ACQUINAS:[VOLTA, LEGONHALL], BANI:[VOLTA, LEGONHALL], VALCO:[SARBAH, ISH, LEGONHALL, VOLTA, BANI] } import random def find_path(graph, start, end, path=[]): path = path + [start] newp=[] if start == end: return path if start not in graph: return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath #print(newpath) newp.append(random.sample(path,2)) return newp #return None def find_all_paths(graph, start, end, path=[]): path = path + [start] if start == end: return [path] if start not in graph: return [] paths = [] for node in graph[start]: if node not in path: newpaths = find_all_paths(graph, node, end, path) for newpath in newpaths: paths.append(newpath) if len(paths)==0: newpaths = find_all_paths(graph,end,node,path) for newpath in newpaths: paths.append(newpath) return paths def find_shortest_path(graph, start, end, path=[]): path = path + [start] if start == end: return path # if start not in graph: # return None shortest = None for node in graph[start]: if node not in path: newpath = find_shortest_path(graph, node, end, path) if newpath: if not shortest or len(newpath) < len(shortest): shortest = newpath from pprint import pprint a=find_all_paths(graph,start=SEY,end=VOLTA) pprint(a)