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 a graph.

Problem Statement #

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

Input #

An undirected graph.

Output #

Returns the total number of 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.