Lekcja – Graf nieskierowany

V = ['Princess', 'Dwarf1', 'Dwarf2', 'King', 'Queen', 'Hunter']
E = [ 
    ['Princess', 'Dwarf1'], ['Princess', 'Dwarf2'], ['Dwarf1', 'Dwarf2'],
    ['Princess', 'King'], ['Princess', 'Queen'], ['Princess', 'Hunter'],
    ['King', 'Queen'], ['King', 'Hunter'], ['Queen','Hunter']
]

# who knows the king?
def who_knows_person(person, E):
    friends=[]
    for p1, p2 in E:
        if p1 == person:
            friends.append(p2)
        elif p2 == person:
            friends.append(p1)
    return friends

print(who_knows_person('Princess', E))

Lab

V = [1, 2, 3, 4]
E = [(1,2), (1,3),(2,4),(3,4)]

# Show all nodes reachable from node 1
for x,y in E:
    if x == 1:
        print(y)

print([y for (x,y) in E if x == 1])