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 up to 105.
- For each vertex, the degree can be calculated by the length of the Adjacency List of the given graph at the corresponding vertex.
- Print those vertices of the given graph whose degree is a Prime Number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int n = 10005;
vector< bool > Prime(n + 1, true );
void SieveOfEratosthenes()
{
int i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= 10005; i++) {
if (Prime[i]) {
for (j = 2 * i; j < 10005; j += i) {
Prime[j] = false ;
}
}
}
}
void primeDegreeNodes( int N, int M,
int edges[][2])
{
vector< int > Adj[N + 1];
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);
}
SieveOfEratosthenes();
for ( int i = 1; i <= N; i++) {
int x = Adj[i].size();
if (Prime[x])
cout << i << ' ' ;
}
}
int main()
{
int N = 4, M = 6;
int edges[M][2] = { { 1, 2 }, { 1, 3 },
{ 1, 4 }, { 2, 3 },
{ 2, 4 }, { 3, 4 } };
primeDegreeNodes(N, M, edges);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int n = 10005 ;
static boolean []Prime = new boolean [n + 1 ];
static void SieveOfEratosthenes()
{
int i, j;
Prime[ 0 ] = Prime[ 1 ] = false ;
for (i = 2 ; i * i <= 10005 ; i++)
{
if (Prime[i])
{
for (j = 2 * i; j < 10005 ; j += i)
{
Prime[j] = false ;
}
}
}
}
static void primeDegreeNodes( int N, int M,
int edges[][])
{
Vector<Integer> []Adj = new Vector[N + 1 ];
for ( int i = 0 ; i < Adj.length; i++)
Adj[i] = new Vector<Integer>();
for ( int i = 0 ; i < M; i++)
{
int x = edges[i][ 0 ];
int y = edges[i][ 1 ];
Adj[x].add(y);
Adj[y].add(x);
}
SieveOfEratosthenes();
for ( int i = 1 ; i <= N; i++)
{
int x = Adj[i].size();
if (Prime[x])
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int N = 4 , M = 6 ;
int edges[][] = { { 1 , 2 }, { 1 , 3 },
{ 1 , 4 }, { 2 , 3 },
{ 2 , 4 }, { 3 , 4 } };
Arrays.fill(Prime, true );
primeDegreeNodes(N, M, edges);
}
}
|
Python3
n = 10005 ;
Prime = [ True for i in range (n + 1 )]
def SieveOfEratosthenes():
i = 2
Prime[ 0 ] = Prime[ 1 ] = False ;
while i * i < = 10005 :
if (Prime[i]):
for j in range ( 2 * i, 10005 ):
Prime[j] = False
i + = 1
def primeDegreeNodes(N, M, edges):
Adj = [[] for i in range (N + 1 )];
for i in range (M):
x = edges[i][ 0 ];
y = edges[i][ 1 ];
Adj[x].append(y);
Adj[y].append(x);
SieveOfEratosthenes();
for i in range (N + 1 ):
x = len (Adj[i]);
if (Prime[x]):
print (i, end = ' ' )
if __name__ = = "__main__" :
N = 4
M = 6
edges = [[ 1 , 2 ], [ 1 , 3 ],
[ 1 , 4 ], [ 2 , 3 ],
[ 2 , 4 ], [ 3 , 4 ]];
primeDegreeNodes(N, M, edges);
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int n = 10005;
static bool []Prime = new bool [n + 1];
static void SieveOfEratosthenes()
{
int i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= 10005; i++)
{
if (Prime[i])
{
for (j = 2 * i; j < 10005; j += i)
{
Prime[j] = false ;
}
}
}
}
static void primeDegreeNodes( int N, int M,
int [,]edges)
{
List< int > []Adj = new List< int >[N + 1];
for ( int i = 0; i < Adj.Length; i++)
Adj[i] = new List< int >();
for ( int i = 0; i < M; i++)
{
int x = edges[i, 0];
int y = edges[i, 1];
Adj[x].Add(y);
Adj[y].Add(x);
}
SieveOfEratosthenes();
for ( int i = 1; i <= N; i++)
{
int x = Adj[i].Count;
if (Prime[x])
Console.Write(i + " " );
}
}
public static void Main(String[] args)
{
int N = 4, M = 6;
int [,]edges = { { 1, 2 }, { 1, 3 },
{ 1, 4 }, { 2, 3 },
{ 2, 4 }, { 3, 4 } };
for ( int i = 0; i < Prime.Length; i++)
Prime[i] = true ;
primeDegreeNodes(N, M, edges);
}
}
|
Javascript
<script>
let n = 10005;
let Prime = new Array(n + 1).fill( true );
function SieveOfEratosthenes()
{
let i, j;
Prime[0] = Prime[1] = false ;
for (i = 2; i * i <= 10005; i++) {
if (Prime[i]) {
for (j = 2 * i; j < 10005; j += i) {
Prime[j] = false ;
}
}
}
}
function primeDegreeNodes(N, M, edges)
{
let Adj = new Array();
for (let i = 0; i < N + 1; i++){
Adj.push( new Array())
}
for (let i = 0; i < M; i++) {
let x = edges[i][0];
let y = edges[i][1];
Adj[x].push(y);
Adj[y].push(x);
}
SieveOfEratosthenes();
for (let i = 1; i <= N; i++) {
let x = Adj[i].length;
if (Prime[x])
document.write(i + ' ' );
}
}
let N = 4, M = 6;
let edges = [ [ 1, 2 ], [ 1, 3 ],
[ 1, 4 ], [ 2, 3 ],
[ 2, 4 ], [ 3, 4 ] ];
primeDegreeNodes(N, M, edges);
</script>
|
Time Complexity: O(N + M), where N is the number of vertices and M is the number of edges.
Last Updated :
20 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...