Lekcja – Lista sąsiadów (adjacent list)
def add_node(V, node): if node in V.keys(): return else: V[node] = [] def add_edge(V, source, dest): if not source in V.keys() or not dest in V.keys(): return else: if not dest in V[source]: V[source].append(dest) V = {} add_node(V, 'DL') add_node(V, 'D') add_node(V, 'C') add_node(V, 'E') add_node(V, 'R') add_node(V, 'B') add_edge(V, 'DL', 'D') add_edge(V, 'DL', 'C') add_edge(V, 'D', 'R') add_edge(V, 'C', 'E') add_edge(V, 'E', 'R') add_edge(V, 'E', 'C') add_edge(V, 'R', 'B') print(V)
Lab
metro = { 'Staromestska' : ['Mustek'], 'Mustek' : ['Staromestska', 'Namesti Republiky', 'Muzeum', 'Narodni trida'], 'Muzeum' : [ 'Mustek', 'Hlavni nadrazi'], 'Narodni trida' : ['Mustek'], 'Namesti Republiky' : ['Mustek', 'Florenc'], 'Florenc': ['Namesti Republiky', 'Hlavni Nadrazi'], 'Hlavni nadrazi' : ['Florenc', 'Muzeum'] } print("Muzeum is neighbour of Staromestska?", 'Muzeum' in metro['Staromestska']) print('Hlavni Nadrazi is neighbour of Florenc', 'Hlavni Nadrazi' in metro['Florenc'])