Session Ready
Exercise

Finding nodes involved in triangles

NetworkX provides an API for counting the number of triangles that every node is involved in: nx.triangles(G). It returns a dictionary of nodes as the keys and number of triangles as the values. Your job in this exercise is to modify the function defined earlier to extract all of the nodes involved in a triangle relationship with a given node.

Instructions
100 XP
  • Write a function nodes_in_triangle() that has two parameters - G and n - and identifies all nodes in a triangle relationship with a given node.
    • In the for loop, iterate over all possible triangle relationship combinations.
    • Check whether the nodes n1 and n2 have an edge between them. If they do, add both nodes to the set triangle_nodes.
  • Use your function in an assert statement to check that the number of nodes involved in a triangle relationship with node 1 of graph T is equal to 35.