Nodes with prime degree in an undirected Graph
Given an undirected graph with N vertices and M edges, the task is to print all the nodes of the given graph whose degree is a Prime Number.
Examples:
Input: N = 4, arr[][] = { { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 3 }, { 2, 4 }, { 3, 4 } }
Output: 1 2 3 4
Explanation:
Below is the graph for the above information:
The degree of the node as per above graph is:
Node -> Degree
1 -> 3
2 -> 3
3 -> 3
4 -> 3
Hence, the nodes with prime degree are 1 2 3 4
Input: N = 5, arr[][] = { { 1, 2 }, { 1, 3 }, { 2, 4 }, { 2, 5 } }
Output: 1
Approach:
- Use Sieve of Eratosthenes to calculate the prime numbers upto 105.
- For each vertex, the degree can be calculated by the length of Adjacency List of the given graph at corresponding vertex.
- Print those vertex of the given graph whose degree is a Prime Number.
Below is the implementation of the above approach:
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; int n = 10005; // To store Prime Numbers vector<bool> Prime(n + 1, true); // Function to find the prime numbers // till 10^5 void SieveOfEratosthenes() { int i, j; Prime[0] = Prime[1] = false; for (i = 2; i * i <= 10005; i++) { // Traverse all multiple of i // and make it false if (Prime[i]) { for (j = 2 * i; j < 10005; j += i) { Prime[j] = false; } } } } // Function to print the nodes having // prime degree void primeDegreeNodes(int N, int M, int edges[][2]) { // To store Adjacency List of // a Graph vector<int> Adj[N + 1]; // Make Adjacency List for (int i = 0; i < M; i++) { int x = edges[i][0]; int y = edges[i][1]; Adj[x].push_back(y); Adj[y].push_back(x); } // To precompute prime numbers // till 10^5 SieveOfEratosthenes(); // Traverse each vertex for (int i = 1; i <= N; i++) { // Find size of Adjacency List int x = Adj[i].size(); // If length of Adj[i] is Prime // then print it if (Prime[x]) cout << i << ' '; } } // Driver code int main() { // Vertices and Edges int N = 4, M = 6; // Edges int edges[M][2] = { { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 3 }, { 2, 4 }, { 3, 4 } }; // Function Call primeDegreeNodes(N, M, edges); return 0; } |
1 2 3 4
Time Complexity: O(N + M), where N is the number of vertices and M is the number of edges.
Recommended Posts:
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Sum of degrees of all nodes of a undirected graph
- Count the number of Prime Cliques in an undirected graph
- Print the nodes with a prime degree in given Prufer sequence of a Tree
- Maximum sum of values of nodes among all connected components of an undirected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Largest component size in a graph formed by connecting non-co-prime nodes
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Degree of a Cycle Graph
- Find the Degree of a Particular vertex in a Graph
- Convert undirected connected graph to strongly connected directed graph
- Clone an Undirected Graph
- Eulerian Path in undirected graph
- Number of Triangles in an Undirected Graph
- Connected Components in an undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



