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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum number of edges among all connected components of an undirected graph
- Ways to Remove Edges from a Complete Graph to make Odd Edges
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- 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
- Count the number of Prime Cliques in an undirected graph
- Program to count Number of connected components in an undirected graph
- Count ways to change direction of edges such that graph becomes acyclic
- Count total ways to reach destination from source in an undirected Graph
- Count of unique lengths of connected components for an undirected graph using STL
- Minimum number of edges between two vertices of a Graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Program to find total number of edges in a Complete Graph
- Number of Simple Graph with N Vertices and M Edges
- Maximum number of edges in Bipartite graph
- Minimum number of Edges to be added to a Graph to satisfy the given condition
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Minimum number of edges between two vertices of a graph using DFS
- Number of Triangles in an Undirected Graph
- Number of single cycle components in an undirected graph