codeparrot/xlcost-text-to-code huggingface dataset:
Prime triplets consisting of values up to N having difference between two elements equal to the third | Python3 program for the above approach ; Stores 1 and 0 at indices which are prime and non - prime respectively ; Function to find all prime numbers from the range [ 0 , N ] ; Iterate over the range [ 2 , sqrt ( N ) ] ; If p is a prime ; Update all tultiples of p as false ; Function to find all prime triplets satisfying the given conditions ; Generate all primes up to N ;  ; Iterate over the range [ 3 , N ] ; Check for the condition ; Store the triplets ; Print all the stored triplets ; Driver Code
from math import sqrt NEW_LINE prime = [ True for i in range ( 100000 ) ] NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def findTriplets ( N ) : NEW_LINE INDENT SieveOfEratosthenes ( N ) NEW_LINE DEDENT / * List < List < int > > V = new List < List < int >> ( ) ; * / NEW_LINE INDENT for i in range ( 3 , N + 1 , 1 ) : NEW_LINE INDENT if ( 2 + i <= N and prime [ i ] and prime [ 2 + i ] ) : NEW_LINE INDENT V . append ( [ 2 , i , i + 2 ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( V ) ) : NEW_LINE INDENT print ( V [ i ] [ 0 ] , V [ i ] [ 1 ] , V [ i ] [ 2 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE findTriplets ( N ) NEW_LINE DEDENT
Smallest number which is not coprime with any element of an array | Python 3 program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Store the prime numbers ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Print the answer ; Driver Code ; Given array ; Stores the size of the array
MAX = 50 NEW_LINE import sys NEW_LINE from math import sqrt , gcd NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findPrime ( primes ) : NEW_LINE INDENT global MAX NEW_LINE for i in range ( 2 , MAX + 1 , 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT primes . append ( i ) NEW_LINE DEDENT DEDENT DEDENT def findMinimumNumber ( arr , N ) : NEW_LINE INDENT primes = [ ] NEW_LINE findPrime ( primes ) NEW_LINE ans = sys . maxsize NEW_LINE n = len ( primes ) NEW_LINE for i in range ( 1 , ( 1 << n ) , 1 ) : NEW_LINE INDENT temp = 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT temp *= primes [ j ] NEW_LINE DEDENT DEDENT check = True NEW_LINE for k in range ( N ) : NEW_LINE INDENT if ( gcd ( temp , arr [ k ] ) == 1 ) : NEW_LINE INDENT check = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( check ) : NEW_LINE INDENT ans = min ( ans , temp ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE findMinimumNumber ( arr , N ) NEW_LINE DEDENT
Count numbers from a given range that can be expressed as sum of digits raised to the power of count of digits | Python 3 program for the above approach ; Function to check if a number N can be expressed as sum of its digits raised to the power of the count of digits ; Stores the number of digits ; Stores the resultant number ; Return true if both the numbers are same ; Function to precompute and store for all numbers whether they can be expressed ; Mark all the index which are plus perfect number ; If true , then update the value at this index ; Compute prefix sum of the array ; Function to count array elements that can be expressed as the sum of digits raised to the power of count of digits ; Precompute the results ; Traverse the queries ; Print the resultant count ; Driver Code ; Function call
R = 100005 NEW_LINE arr = [ 0 for i in range ( R ) ] NEW_LINE def canExpress ( N ) : NEW_LINE INDENT temp = N NEW_LINE n = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT N //= 10 NEW_LINE n += 1 NEW_LINE DEDENT N = temp NEW_LINE sum = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT sum += pow ( N % 10 , n ) NEW_LINE N //= 10 NEW_LINE DEDENT return ( sum == temp ) NEW_LINE DEDENT def precompute ( ) : NEW_LINE INDENT for i in range ( 1 , R , 1 ) : NEW_LINE INDENT if ( canExpress ( i ) ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , R , 1 ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE DEDENT DEDENT def countNumbers ( queries , N ) : NEW_LINE INDENT precompute ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT L1 = queries [ i ] [ 0 ] NEW_LINE R1 = queries [ i ] [ 1 ] NEW_LINE print ( ( arr [ R1 ] - arr [ L1 - 1 ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT queries = [ [ 1 , 400 ] , [ 1 , 9 ] ] NEW_LINE N = len ( queries ) NEW_LINE countNumbers ( queries , N ) NEW_LINE DEDENT
Queries to calculate sum of the path from root to a given node in given Binary Tree | Function to find the sum of the path from root to the current node ; Sum of nodes in the path ; Iterate until root is reached ; Update the node value ; Print the resultant sum ; Function to print the path sum for each query ; Traverse the queries ; arraylist to store integers
def sumOfNodeInAPath ( node_value ) : NEW_LINE INDENT sum_of_node = 0 NEW_LINE while ( node_value ) : NEW_LINE INDENT sum_of_node += node_value NEW_LINE node_value //= 2 NEW_LINE DEDENT print ( sum_of_node , end = " ▁ " ) NEW_LINE DEDENT def findSum ( Q ) : NEW_LINE INDENT for i in range ( len ( Q ) ) : NEW_LINE INDENT node_value = Q [ i ] NEW_LINE sumOfNodeInAPath ( node_value ) NEW_LINE print ( end = " " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 5 , 20 , 100 ] NEW_LINE findSum ( arr ) NEW_LINE
Minimum removals required to make frequency of all remaining array elements equal | Python 3 program for the above approach ; Function to count the minimum removals required to make frequency of all array elements equal ; Stores frequency of all array elements ; Traverse the array ; Stores all the frequencies ; Traverse the map ; Sort the frequencies ; Count of frequencies ; Stores the final count ; Traverse the vector ; Count the number of removals for each frequency and update the minimum removals required ; Print the final count ; Driver Code ; Given array ; Size of the array ; Function call to print the minimum number of removals required
from collections import defaultdict NEW_LINE def minDeletions ( arr , N ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT v = [ ] NEW_LINE for z in freq . keys ( ) : NEW_LINE INDENT v . append ( freq [ z ] ) NEW_LINE DEDENT v . sort ( ) NEW_LINE size = len ( v ) NEW_LINE ans = N - ( v [ 0 ] * size ) NEW_LINE for i in range ( 1 , len ( v ) ) : NEW_LINE INDENT if ( v [ i ] != v [ i - 1 ] ) : NEW_LINE INDENT safe = v [ i ] * ( size - i ) NEW_LINE ans = min ( ans , N - safe ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 2 , 5 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE minDeletions ( arr , N ) NEW_LINE DEDENT
Determine whether a universal sink exists in a directed graph | Python3 program to find whether a universal sink exists in a directed graph ; constructor to initialize number of vertices and size of adjacency matrix ; make adjacency_matrix [ i ] [ j ] = 1 if there is an edge from i to j ; if any element in the row i is 1 , it means that there is an edge emanating from the vertex , which means it cannot be a sink ; if any element other than i in the column i is 0 , it means that there is no edge from that vertex to the vertex we are testing and hence it cannot be a sink ; if none of the checks fails , return true ; we will eliminate n - 1 non sink vertices so that we have to check for only one vertex instead of all n vertices ; If the index is 1 , increment the row we are checking by 1 else increment the column ; If i exceeds the number of vertices , it means that there is no valid vertex in the given vertices that can be a sink ; Driver Code ; input set 1 g . insert ( 1 , 6 ) g . insert ( 2 , 6 ) g . insert ( 3 , 6 ) g . insert ( 4 , 6 ) g . insert ( 5 , 6 ) input set 2 ; returns 0 based indexing of vertex . returns - 1 if no sink exits . returns the vertex number - 1 if sink is found
class Graph : NEW_LINE INDENT def __init__ ( self , vertices ) : NEW_LINE INDENT self . vertices = vertices NEW_LINE self . adjacency_matrix = [ [ 0 for i in range ( vertices ) ] for j in range ( vertices ) ] NEW_LINE DEDENT def insert ( self , s , destination ) : NEW_LINE INDENT self . adjacency_matrix [ s - 1 ] [ destination - 1 ] = 1 NEW_LINE DEDENT def issink ( self , i ) : NEW_LINE INDENT for j in range ( self . vertices ) : NEW_LINE INDENT if self . adjacency_matrix [ i ] [ j ] == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if self . adjacency_matrix [ j ] [ i ] == 0 and j != i : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def eliminate ( self ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE while i < self . vertices and j < self . vertices : NEW_LINE INDENT if self . adjacency_matrix [ i ] [ j ] == 1 : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT if i > self . vertices : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif self . issink ( i ) is False : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT number_of_vertices = 6 NEW_LINE number_of_edges = 5 NEW_LINE g = Graph ( number_of_vertices ) NEW_LINE g . insert ( 1 , 6 ) NEW_LINE g . insert ( 2 , 3 ) NEW_LINE g . insert ( 2 , 4 ) NEW_LINE g . insert ( 4 , 3 ) NEW_LINE g . insert ( 5 , 3 ) NEW_LINE vertex = g . eliminate ( ) NEW_LINE if vertex >= 0 : NEW_LINE INDENT print ( " Sink ▁ found ▁ at ▁ vertex ▁ % d " % ( vertex + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Sink " ) NEW_LINE DEDENT DEDENT
Reverse alternate levels of a perfect binary tree | Python3 program to reverse alternate levels of a tree A Binary Tree Node Utility function to create a new tree node ; Base cases ; Swap subtrees if level is even ; Recur for left and right subtrees ( Note : left of root1 is passed and right of root2 in first call and opposite in second call . ; This function calls preorder ( ) for left and right children of root ; Inorder traversal ( used to print initial and modified trees ) ; A utility function to create a new node ; Driver Code
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . key = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def preorder ( root1 , root2 , lvl ) : NEW_LINE INDENT if ( root1 == None or root2 == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( lvl % 2 == 0 ) : NEW_LINE INDENT t = root1 . key NEW_LINE root1 . key = root2 . key NEW_LINE root2 . key = t NEW_LINE DEDENT preorder ( root1 . left , root2 . right , lvl + 1 ) NEW_LINE preorder ( root1 . right , root2 . left , lvl + 1 ) NEW_LINE DEDENT def reverseAlternate ( root ) : NEW_LINE INDENT preorder ( root . left , root . right , 0 ) NEW_LINE DEDENT def printInorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( root . left ) NEW_LINE print ( root . key , end = " ▁ " ) NEW_LINE printInorder ( root . right ) NEW_LINE DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( ' ▁ ' ) NEW_LINE temp . left = temp . right = None NEW_LINE temp . key = key NEW_LINE return temp NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( ' a ' ) NEW_LINE root . left = newNode ( ' b ' ) NEW_LINE root . right = newNode ( ' c ' ) NEW_LINE root . left . left = newNode ( ' d ' ) NEW_LINE root . left . right = newNode ( ' e ' ) NEW_LINE root . right . left = newNode ( ' f ' ) NEW_LINE root . right . right = newNode ( ' g ' ) NEW_LINE root . left . left . left = newNode ( ' h ' ) NEW_LINE root . left . left . right = newNode ( ' i ' ) NEW_LINE root . left . right . left = newNode ( ' j ' ) NEW_LINE root . left . right . right = newNode ( ' k ' ) NEW_LINE root . right . left . left = newNode ( ' l ' ) NEW_LINE root . right . left . right = newNode ( ' m ' ) NEW_LINE root . right . right . left = newNode ( ' n ' ) NEW_LINE root . right . right . right = newNode ( ' o ' ) NEW_LINE print ( " Inorder ▁ Traversal ▁ of ▁ given ▁ tree " ) NEW_LINE printInorder ( root ) NEW_LINE reverseAlternate ( root ) NEW_LINE print ( " Inorder Traversal of modified tree " ) NEW_LINE printInorder ( root ) NEW_LINE DEDENT
Deletion in a Binary Tree | class to create a node with data , left child and right child . ; Inorder traversal of a binary tree ; function to delete the given deepest node ( d_node ) in binary tree ; Do level order traversal until last node ; function to delete element in binary tree ; Do level order traversal to find deepest node ( temp ) and node to be deleted ( key_node ) ; Driver code
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def inorder ( temp ) : NEW_LINE INDENT if ( not temp ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( temp . left ) NEW_LINE print ( temp . data , end = " ▁ " ) NEW_LINE inorder ( temp . right ) NEW_LINE DEDENT def deleteDeepest ( root , d_node ) : NEW_LINE INDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q . pop ( 0 ) NEW_LINE if temp is d_node : NEW_LINE INDENT temp = None NEW_LINE return NEW_LINE DEDENT if temp . right : NEW_LINE INDENT if temp . right is d_node : NEW_LINE INDENT temp . right = None NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT if temp . left : NEW_LINE INDENT if temp . left is d_node : NEW_LINE INDENT temp . left = None NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def deletion ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . left == None and root . right == None : NEW_LINE INDENT if root . key == key : NEW_LINE INDENT return None NEW_LINE DEDENT else : NEW_LINE INDENT return root NEW_LINE DEDENT DEDENT key_node = None NEW_LINE q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q . pop ( 0 ) NEW_LINE if temp . data == key : NEW_LINE INDENT key_node = temp NEW_LINE DEDENT if temp . left : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if temp . right : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT if key_node : NEW_LINE INDENT x = temp . data NEW_LINE deleteDeepest ( root , temp ) NEW_LINE key_node . data = x NEW_LINE DEDENT return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 11 ) NEW_LINE root . left . left = Node ( 7 ) NEW_LINE root . left . right = Node ( 12 ) NEW_LINE root . right = Node ( 9 ) NEW_LINE root . right . left = Node ( 15 ) NEW_LINE root . right . right = Node ( 8 ) NEW_LINE print ( " The ▁ tree ▁ before ▁ the ▁ deletion : " ) NEW_LINE inorder ( root ) NEW_LINE key = 11 NEW_LINE root = deletion ( root , key ) NEW_LINE print ( ) NEW_LINE print ( " The ▁ tree ▁ after ▁ the ▁ deletion ; " ) NEW_LINE inorder ( root ) NEW_LINE DEDENT
Number of sink nodes in a graph | Return the number of Sink NOdes . ; Array for marking the non - sink node . ; Marking the non - sink node . ; Counting the sink nodes . ; Driver Code
def countSink ( n , m , edgeFrom , edgeTo ) : NEW_LINE INDENT mark = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT mark [ edgeFrom [ i ] ] = 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( not mark [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE m = 2 NEW_LINE edgeFrom = [ 2 , 4 ] NEW_LINE edgeTo = [ 3 , 3 ] NEW_LINE print ( countSink ( n , m , edgeFrom , edgeTo ) ) NEW_LINE DEDENT
Largest subset of Graph vertices with edges of 2 or more colors | Number of vertices ; function to calculate max subset size ; set for number of vertices ; loop for deletion of vertex from set ; if subset has only 1 vertex return 0 ; for each vertex iterate and keep removing a vertix while we find a vertex with all edges of same color . ; note down different color values for each vertex ; if only one color is found erase that vertex ( bad vertex ) ; If no vertex was removed in the above loop . ; Driver Code
N = 6 NEW_LINE def subsetGraph ( C ) : NEW_LINE INDENT global N NEW_LINE vertices = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT vertices . add ( i ) NEW_LINE DEDENT while ( len ( vertices ) != 0 ) : NEW_LINE INDENT if ( len ( vertices ) == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT someone_removed = False NEW_LINE for x in vertices : NEW_LINE INDENT values = set ( ) NEW_LINE for y in vertices : NEW_LINE INDENT if ( y != x ) : NEW_LINE INDENT values . add ( C [ x ] [ y ] ) NEW_LINE DEDENT DEDENT if ( len ( values ) == 1 ) : NEW_LINE INDENT vertices . remove ( x ) NEW_LINE someone_removed = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not someone_removed ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return len ( vertices ) NEW_LINE DEDENT C = [ [ 0 , 9 , 2 , 4 , 7 , 8 ] , [ 9 , 0 , 9 , 9 , 7 , 9 ] , [ 2 , 9 , 0 , 3 , 7 , 6 ] , [ 4 , 9 , 3 , 0 , 7 , 1 ] , [ 7 , 7 , 7 , 7 , 0 , 7 ] , [ 8 , 9 , 6 , 1 , 7 , 0 ] ] NEW_LINE print ( subsetGraph ( C ) ) NEW_LINE
Count number of edges in an undirected graph | Adjacency list representation of graph ; add edge to graph ; Returns count of edge in undirected graph ; traverse all vertex ; add all edge that are linked to the current vertex ; The count of edge is always even because in undirected graph every edge is connected twice between two vertices ; Driver Code ; making above uhown graph
class Graph : NEW_LINE INDENT def __init__ ( self , V ) : NEW_LINE INDENT self . V = V NEW_LINE self . adj = [ [ ] for i in range ( V ) ] NEW_LINE DEDENT def addEdge ( self , u , v ) : NEW_LINE INDENT self . adj [ u ] . append ( v ) NEW_LINE self . adj [ v ] . append ( u ) NEW_LINE DEDENT def countEdges ( self ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( self . V ) : NEW_LINE INDENT Sum += len ( self . adj [ i ] ) NEW_LINE DEDENT return Sum // 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 9 NEW_LINE g = Graph ( V ) NEW_LINE g . addEdge ( 0 , 1 ) NEW_LINE g . addEdge ( 0 , 7 ) NEW_LINE g . addEdge ( 1 , 2 ) NEW_LINE g . addEdge ( 1 , 7 ) NEW_LINE g . addEdge ( 2 , 3 ) NEW_LINE g . addEdge ( 2 , 8 ) NEW_LINE g . addEdge ( 2 , 5 ) NEW_LINE g . addEdge ( 3 , 4 ) NEW_LINE g . addEdge ( 3 , 5 ) NEW_LINE g . addEdge ( 4 , 5 ) NEW_LINE g . addEdge ( 5 , 6 ) NEW_LINE g . addEdge ( 6 , 7 ) NEW_LINE g . addEdge ( 6 , 8 ) NEW_LINE g . addEdge ( 7 , 8 ) NEW_LINE print ( g . countEdges ( ) ) NEW_LINE DEDENT
Two Clique Problem ( Check if Graph can be divided in two Cliques ) | Python3 program to find out whether a given graph can be converted to two Cliques or not . ; This function returns true if subgraph reachable from src is Bipartite or not . ; Create a queue ( FIFO ) of vertex numbers and enqueue source vertex for BFS traversal ; Run while there are vertices in queue ( Similar to BFS ) ; Dequeue a vertex from queue ; Find all non - colored adjacent vertices ; An edge from u to v exists and destination v is not colored ; Assign alternate color to this adjacent v of u ; An edge from u to v exists and destination v is colored with same color as u ; If we reach here , then all adjacent vertices can be colored with alternate color ; Returns true if a Graph G [ ] [ ] is Bipartite or not . Note that G may not be connected . ; Create a color array to store colors assigned to all veritces . Vertex number is used as index in this array . The value ' - 1' of colorArr [ i ] is used to indicate that no color is assigned to vertex ' i ' . The value 1 is used to indicate first color is assigned and value 0 indicates second color is assigned . ; One by one check all not yet colored vertices . ; Returns true if G can be divided into two Cliques , else false . ; Find complement of G [ ] [ ] All values are complemented except diagonal ones ; Return true if complement is Bipartite else false . ; Driver Code
from queue import Queue NEW_LINE def isBipartiteUtil ( G , src , colorArr ) : NEW_LINE INDENT global V NEW_LINE colorArr [ src ] = 1 NEW_LINE q = Queue ( ) NEW_LINE q . put ( src ) NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT u = q . get ( ) NEW_LINE for v in range ( V ) : NEW_LINE INDENT if ( G [ u ] [ v ] and colorArr [ v ] == - 1 ) : NEW_LINE INDENT colorArr [ v ] = 1 - colorArr [ u ] NEW_LINE q . put ( v ) NEW_LINE DEDENT elif ( G [ u ] [ v ] and colorArr [ v ] == colorArr [ u ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def isBipartite ( G ) : NEW_LINE INDENT global V NEW_LINE colorArr = [ - 1 ] * V NEW_LINE for i in range ( V ) : NEW_LINE INDENT if ( colorArr [ i ] == - 1 ) : NEW_LINE INDENT if ( isBipartiteUtil ( G , i , colorArr ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT def canBeDividedinTwoCliques ( G ) : NEW_LINE INDENT global V NEW_LINE GC = [ [ None ] * V for i in range ( V ) ] NEW_LINE for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT GC [ i ] [ j ] = not G [ i ] [ j ] if i != j else 0 NEW_LINE DEDENT DEDENT return isBipartite ( GC ) NEW_LINE DEDENT V = 5 NEW_LINE G = [ [ 0 , 1 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 0 , 0 ] , [ 1 , 1 , 0 , 0 , 0 ] , [ 0 , 1 , 0 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 ] ] NEW_LINE if canBeDividedinTwoCliques ( G ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Check whether given degrees of vertices represent a Graph or Tree | Function returns true for tree false for graph ; Find sum of all degrees ; It is tree if sum is equal to 2 ( n - 1 ) ; main
def check ( degree , n ) : NEW_LINE INDENT deg_sum = sum ( degree ) NEW_LINE if ( 2 * ( n - 1 ) == deg_sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 5 NEW_LINE degree = [ 2 , 3 , 1 , 1 , 1 ] ; NEW_LINE if ( check ( degree , n ) ) : NEW_LINE INDENT print " Tree " NEW_LINE DEDENT else : NEW_LINE INDENT print " Graph " NEW_LINE DEDENT