Challenge: Count the Number of Edges in an Undirected Graph

In this lesson, we will figure out if it's possible to count the total number of edges in an undirected graph.

Problem statement

You have to implement the num_edges() function which takes an undirected graph and computes the total number of bidirectional edges. An illustration is also provided for your understanding.

Input

An undirected graph.

Output

Returns the number of unique edges in the graph.

Sample input

graph = {
    0 - 2
    0 - 5
    2 - 3
    2 - 4
    5 - 3
    5 - 6
    3 - 6
    6 - 7
    6 - 8
    6 - 4
    7 - 8
}

Sample output

11

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.