Open triangles ढूँढना
अब open triangles खोजने पर आगे बढ़ते हैं! याद रखें, ये friend recommendation systems का आधार बनते हैं; अगर "A" "B" को जानता है और "A" "C" को जानता है, तो संभव है कि "B" भी "C" को जानता हो.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में नेटवर्क विश्लेषण का परिचय
अभ्यास निर्देश
- एक फंक्शन
node_in_open_triangle()लिखिए जिसमें दो पैरामीटर हों —Gऔरn— और जो यह पहचाने कि कोई node अपने पड़ोसियों के साथ किसी open triangle में है या नहीं.forलूप में, सभी संभावित त्रिकोण संबंध संयोजनों पर iterate कीजिए.- अगर nodes
n1औरn2के बीच edge नहीं है, तोin_open_triangleकोTrueसेट करें,ifसे बाहर आएँ औरin_open_trianglereturn करें.
- इस फंक्शन का उपयोग करके
Tमें मौजूद open triangles की संख्या गिनिए.forलूप में,Tके सभी nodes पर iterate करें.- अगर current node
nकिसी open triangle में है, तोnum_open_trianglesको increment करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
from itertools import combinations
# Define node_in_open_triangle()
def node_in_open_triangle(G, n):
"""
Checks whether pairs of neighbors of node `n` in graph `G` are in an 'open triangle' relationship with node `n`.
"""
in_open_triangle = False
# Iterate over all possible triangle relationship combinations
for n1, n2 in ____:
# Check if n1 and n2 do NOT have an edge between them
if not ____:
in_open_triangle = ____
break
return ____
# Compute the number of open triangles in T
num_open_triangles = 0
# Iterate over all the nodes in T
for n in ____:
# Check if the current node is in an open triangle
if ____:
# Increment num_open_triangles
____ += 1
print(num_open_triangles)