Nodes with prime degree in an undirected Graph | Python3 implementation of the above approach ; To store Prime Numbers ; Function to find the prime numbers till 10 ^ 5 ; Traverse all multiple of i and make it false ; Function to print the nodes having prime degree ; To store Adjacency List of a Graph ; Make Adjacency List ; To precompute prime numbers till 10 ^ 5 ; Traverse each vertex ; Find size of Adjacency List ; If length of Adj [ i ] is Prime then print it ; Driver code ; Vertices and Edges ; Edges ; Function Call
n = 10005 ; NEW_LINE Prime = [ True for i in range ( n + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT i = 2 NEW_LINE Prime [ 0 ] = Prime [ 1 ] = False ; NEW_LINE while i * i <= 10005 : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , 10005 ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def primeDegreeNodes ( N , M , edges ) : NEW_LINE INDENT Adj = [ [ ] for i in range ( N + 1 ) ] ; NEW_LINE for i in range ( M ) : NEW_LINE INDENT x = edges [ i ] [ 0 ] ; NEW_LINE y = edges [ i ] [ 1 ] ; NEW_LINE Adj [ x ] . append ( y ) ; NEW_LINE Adj [ y ] . append ( x ) ; NEW_LINE DEDENT SieveOfEratosthenes ( ) ; NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT x = len ( Adj [ i ] ) ; NEW_LINE if ( Prime [ x ] ) : NEW_LINE INDENT print ( i , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE M = 6 NEW_LINE edges = [ [ 1 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] , [ 2 , 3 ] , [ 2 , 4 ] , [ 3 , 4 ] ] ; NEW_LINE primeDegreeNodes ( N , M , edges ) ; NEW_LINE DEDENT