En Kısa Yol III
Bu üçlünün son egzersizi! Artık, iki düğüm arasında yol yoksa False döndüren kodu yazarak problemi tamamlayacaksın.
Bu egzersiz, kursun bir parçasıdır
Python ile Ağ Analizine Giriş
Egzersiz talimatları
- Kuyruğun boşalıp boşalmadığını kontrol et. Bunu, kuyruğun son elemanını
[-1]ile inceleyerek yapabilirsin. - Bu iki düğüm arasında yol olup olmadığını belirtmek için uygun return ifadesini yerleştir.
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
def path_exists(G, node1, node2):
"""
This function checks whether a path exists between two nodes (node1, node2) in graph G.
"""
visited_nodes = set()
queue = [node1]
for node in queue:
neighbors = list(G.neighbors(node))
if node2 in neighbors:
print('Path exists between nodes {0} and {1}'.format(node1, node2))
return True
break
else:
visited_nodes.add(node)
queue.extend([n for n in neighbors if n not in visited_nodes])
# Check to see if the final element of the queue has been reached
if node == ____:
print('Path does not exist between nodes {0} and {1}'.format(node1, node2))
# Place the appropriate return statement
return ____