शुरू करेंमुफ़्त में शुरू करें

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_triangle return करें.
  • इस फंक्शन का उपयोग करके 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)
कोड संपादित करें और चलाएँ