Alt grafikler I
Bazı zamanlarda ağdaki düğümlerin sadece bir alt kümesini analiz etmek isteyebilirsin. Bunu yapmak için, bunları G.subgraph(nodes) kullanarak başka bir grafik nesnesine kopyalayabilirsin; bu, (orijinal grafiğin türüyle aynı türde) yeni bir graph nesnesi döndürür ve içine geçirilen nodes yinelemesinden oluşur.
matplotlib.pyplot senin için plt olarak içe aktarıldı.
Bu egzersiz, kursun bir parçasıdır
Python ile Ağ Analizine Giriş
Egzersiz talimatları
Ggrafiğinden,nodes_of_interestve onların komşularından oluşan alt grafiği çıkaranget_nodes_and_nbrs(G, nodes_of_interest)adlı bir fonksiyon yaz.- İlk
fordöngüsünde,nodes_of_interestüzerinde yinele ve mevcut düğümn'inodes_to_drawlistesine ekle. - İkinci
fordöngüsünde,n'in komşuları üzerinde yinele ve tüm komşularınbrolaraknodes_to_drawlistesine ekle.
- İlk
- Fonksiyonu kullanarak
Tgrafiğinden, (önceden tanımlınodes_of_interestlistesinde yer alan) 29, 38 ve 42 düğümleri ve onların komşularından oluşan alt grafiği çıkar. SonucuT_drawolarak kaydet. T_drawalt grafiğini ekrana çiz.
Uygulamalı etkileşimli egzersiz
Bu egzersizi bu örnek kodu tamamlayarak deneyin.
nodes_of_interest = [29, 38, 42]
# Define get_nodes_and_nbrs()
def get_nodes_and_nbrs(G, nodes_of_interest):
"""
Returns a subgraph of the graph `G` with only the `nodes_of_interest` and their neighbors.
"""
nodes_to_draw = []
# Iterate over the nodes of interest
for n in ____:
# Append the nodes of interest to nodes_to_draw
____
# Iterate over all the neighbors of node n
for nbr in ____:
# Append the neighbors of n to nodes_to_draw
____
return G.subgraph(nodes_to_draw)
# Extract the subgraph with the nodes of interest: T_draw
T_draw = ____
# Draw the subgraph to the screen
____
plt.show()