Get startedGet started for free

Identifying triangle relationships

Now that you've learned about cliques, it's time to try leveraging what you know to find structures in a network. Triangles are what you'll go for first. We may be interested in triangles because they're the simplest complex clique. Let's write a few functions; these exercises will bring you through the fundamental logic behind network algorithms.

In the Twitter network, each node has an 'occupation' label associated with it, in which the Twitter user's work occupation is divided into celebrity, politician and scientist. One potential application of triangle-finding algorithms is to find out whether users that have similar occupations are more likely to be in a clique with one another.

This exercise is part of the course

Introduction to Network Analysis in Python

View Course

Exercise instructions

  • Import combinations from itertools.
  • Write a function is_in_triangle() that has two parameters - G and n - and checks whether a given node is in a triangle relationship or not.
    • combinations(iterable, n) returns combinations of size n from iterable. This will be useful here, as you want combinations of size 2 from list(G.neighbors(n)).
    • To check whether an edge exists between two nodes, use the .has_edge(node1, node2) method. If an edge exists, then the given node is in a triangle relationship, and you should return True.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

____

# Define is_in_triangle()
def is_in_triangle(G, n):
    """
    Checks whether a node `n` in graph `G` is in a triangle relationship or not.

    Returns a boolean.
    """
    in_triangle = False

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

        # Check if an edge exists between n1 and n2
        if ____:
            in_triangle = ____
            break
    return in_triangle
Edit and Run Code