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

Triangles में शामिल nodes ढूँढना

NetworkX हर नोड के साथ जुड़े त्रिकोणों की संख्या गिनने के लिए एक API देता है: nx.triangles(G). यह एक dictionary लौटाता है जिसमें keys नोड्स होते हैं और values त्रिकोणों की संख्या. इस अभ्यास में आपका काम पहले परिभाषित फंक्शन को संशोधित करना है ताकि दिए गए किसी नोड के साथ त्रिकोण संबंध में शामिल सभी नोड्स निकाले जा सकें.

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में नेटवर्क विश्लेषण का परिचय

पाठ्यक्रम देखें

अभ्यास निर्देश

  • एक फंक्शन nodes_in_triangle() लिखें जिसके दो पैरामीटर हों — G और n — और जो दिए गए नोड के साथ त्रिकोण संबंध में आने वाले सभी नोड्स पहचानता हो.
    • for लूप में, सभी संभावित त्रिकोण संबंध संयोजनों पर इटरेट करें.
    • जाँचें कि n1 और n2 के बीच edge है या नहीं. अगर है, तो दोनों नोड्स को सेट triangle_nodes में जोड़ दें.
  • अपने फंक्शन का उपयोग एक assert स्टेटमेंट में करें ताकि जाँच हो सके कि ग्राफ T में नोड 1 के साथ त्रिकोण संबंध में शामिल नोड्स की संख्या 35 के बराबर है.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

from itertools import combinations

# Write a function that identifies all nodes in a triangle relationship with a given node.
def nodes_in_triangle(G, n):
    """
    Returns the nodes in a graph `G` that are involved in a triangle relationship with the node `n`.
    """
    triangle_nodes = set([n])

    # Iterate over all possible triangle relationship combinations
    for n1, n2 in ____:

        # Check if n1 and n2 have an edge between them
        if ____:

            # Add n1 to triangle_nodes
            ____

            # Add n2 to triangle_nodes
            ____

    return triangle_nodes

# Write the assertion statement
assert len(____(____, ____)) == ____
कोड संपादित करें और चलाएँ