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(____(____, ____)) == ____