Get startedGet started for free

Checking whether there are self-loops in the graph

As Eric discussed, NetworkX also allows edges that begin and end on the same node; while this would be non-intuitive for a social network graph, it is useful to model data such as trip networks, in which individuals begin at one location and end in another.

It is useful to check for this before proceeding with further analyses, and NetworkX provides a method for this purpose: nx.number_of_selfloops(G).

In this exercise as well as later ones, you'll find the assert statement useful. An assert-ions checks whether the statement placed after it evaluates to True, otherwise it will throw an AssertionError.

To begin, call on the nx.number_of_selfloops() function, passing in T, in the IPython Shell to get the number of edges that begin and end on the same node. A number of self-loops have been synthetically added to the graph. Your job in this exercise is to write a function that returns these edges.

This exercise is part of the course

Introduction to Network Analysis in Python

View Course

Exercise instructions

  • Define a function called find_selfloop_nodes() which takes one argument: G.
    • Using a for loop, iterate over all the edges in G (excluding the metadata).
    • If node u is equal to node v:
      • Append u to the list nodes_in_selfloops.
      • Return the list nodes_in_selfloops.
  • Check that the number of self loops in the graph equals the number of nodes in self loops. This has been done for you, so hit 'Submit Answer' to see the result!

Hands-on interactive exercise

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

# Define find_selfloop_nodes()
def ____:
    """
    Finds all nodes that have self-loops in the graph G.
    """
    nodes_in_selfloops = []

    # Iterate over all the edges of G
    for u, v in ____:

    # Check if node u and node v are the same
        if ____:

            # Append node u to nodes_in_selfloops
            ____

    return nodes_in_selfloops

# Check whether number of self loops equals the number of nodes in self loops
assert nx.number_of_selfloops(T) == len(find_selfloop_nodes(T))
Edit and Run Code