Given an adjacency list representation undirected graph. Write a function to count the number of edges in the undirected graph.
Expected time complexity : O(V)
Idea is based on Handshaking Lemma. Handshaking lemma is about undirected graph. In every finite undirected graph number of vertices with odd degree is always even. The handshaking lemma is a consequence of the degree sum formula (also sometimes called the handshaking lemma)
So we traverse all vertices, compute sum of sizes of their adjacency lists, and finally returns sum/2. Below implementation of above idea
Time Complexity: O(V)
This article is contributed by Nishant Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum number of edges among all connected components of an undirected graph
- Count the number of Prime Cliques in an undirected graph
- Program to count Number of connected components in an undirected graph
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Count of unique lengths of connected components for an undirected graph using STL
- Count ways to change direction of edges such that graph becomes acyclic
- Count total ways to reach destination from source in an undirected Graph
- Number of Triangles in an Undirected Graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Minimum number of edges between two vertices of a graph using DFS
- Maximum number of edges in Bipartite graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a Graph
- Undirected graph splitting and its application for number pairs
- Number of single cycle components in an undirected graph
- Program to find total number of edges in a Complete Graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Convert undirected connected graph to strongly connected directed graph
- Clone an Undirected Graph