codeparrot/xlcost-text-to-code huggingface dataset:
Count number of edges in an undirected graph | Java program to count number of edge in 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
import java . io . * ; import java . util . * ; class Graph { int V ; Vector < Integer > [ ] adj ; Graph ( int V ) { this . V = V ; this . adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; } void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } int countEdges ( ) { int sum = 0 ; for ( int i = 0 ; i < V ; i ++ ) sum += adj [ i ] . size ( ) ; return sum / 2 ; } } class GFG { public static void main ( String [ ] args ) throws IOException { int V = 9 ; Graph g = new Graph ( V ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 7 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 1 , 7 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 2 , 8 ) ; g . addEdge ( 2 , 5 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 3 , 5 ) ; g . addEdge ( 4 , 5 ) ; g . addEdge ( 5 , 6 ) ; g . addEdge ( 6 , 7 ) ; g . addEdge ( 6 , 8 ) ; g . addEdge ( 7 , 8 ) ; System . out . println ( g . countEdges ( ) ) ; } }
Two Clique Problem ( Check if Graph can be divided in two Cliques ) | Java 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 program to test above function
import java . util . ArrayDeque ; import java . util . Deque ; class GFG { static int V = 5 ; static boolean isBipartiteUtil ( int G [ ] [ ] , int src , int colorArr [ ] ) { colorArr [ src ] = 1 ; Deque < Integer > q = new ArrayDeque < > ( ) ; q . push ( src ) ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . pop ( ) ; for ( int v = 0 ; v < V ; ++ v ) { if ( G [ u ] [ v ] == - 1 && colorArr [ v ] == - 1 ) { colorArr [ v ] = 1 - colorArr [ u ] ; q . push ( v ) ; } else if ( G [ u ] [ v ] == colorArr [ u ] && colorArr [ v ] == colorArr [ u ] ) return false ; } } return true ; } static boolean isBipartite ( int G [ ] [ ] ) { int colorArr [ ] = new int [ V ] ; for ( int i = 0 ; i < V ; ++ i ) colorArr [ i ] = - 1 ; for ( int i = 0 ; i < V ; i ++ ) if ( colorArr [ i ] == - 1 ) if ( isBipartiteUtil ( G , i , colorArr ) == false ) return false ; return true ; } static boolean canBeDividedinTwoCliques ( int G [ ] [ ] ) { int GC [ ] [ ] = new int [ V ] [ V ] ; for ( int i = 0 ; i < V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) GC [ i ] [ j ] = ( i != j ) ? - GC [ i ] [ j ] : 0 ; return isBipartite ( GC ) ; } public static void main ( String [ ] args ) { int 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 } } ; if ( canBeDividedinTwoCliques ( G ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Check whether given degrees of vertices represent a Graph or Tree | Java program to check whether input degree sequence is graph or tree ; Function returns true for tree false for graph ; Find sum of all degrees ; Graph is tree if sum is equal to 2 ( n - 1 ) ; Driver code
class GFG { static boolean check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { deg_sum += degree [ i ] ; } return ( 2 * ( n - 1 ) == deg_sum ) ; } public static void main ( String [ ] args ) { int n = 5 ; int degree [ ] = { 2 , 3 , 1 , 1 , 1 } ; if ( check ( degree , n ) ) { System . out . println ( " Tree " ) ; } else { System . out . println ( " Graph " ) ; } } }
Finding minimum vertex cover size of a graph using binary search | A Java program to find size of minimum vertex cover using Binary Search ; Global array to store the graph Note : since the array is global , all the elements are 0 initially ; Returns true if there is a possible subset of size ' k ' that can be a vertex cover ; Set has first ' k ' bits high initially ; to mark the edges covered in each subset of size ' k ' ; Reset visited array for every subset of vertices ; set counter for number of edges covered from this subset of vertices to zero ; selected vertex cover is the indices where ' set ' has its bit high ; Mark all edges emerging out of this vertex visited ; If the current subset covers all the edges ; Generate previous combination with k bits high set & - set = ( 1 << last bit high in set ) ; Returns answer to graph stored in gr [ ] [ ] ; Binary search the answer ; at the end of while loop both left and right will be equal , / as when they are not , the while loop won 't exit the minimum  size vertex cover = left = right ; Inserts an edge in the graph ; Undirected graph ; Driver code ; 6 / 1 -- -- - 5 vertex cover = { 1 , 2 } / | \ 3 | \  \ | \ 2 4 ; Let us create another graph ; 2 -- -- 4 -- -- 6 / | | 1 | | vertex cover = { 2 , 3 , 4 } \ | | 3 -- -- 5
class GFG { static final int maxn = 25 ; static boolean [ ] [ ] gr = new boolean [ maxn ] [ maxn ] ; static boolean isCover ( int V , int k , int E ) { int set = ( 1 << k ) - 1 ; int limit = ( 1 << V ) ; boolean [ ] [ ] vis = new boolean [ maxn ] [ maxn ] ; ; while ( set < limit ) { for ( int i = 0 ; i < maxn ; i ++ ) { for ( int j = 0 ; j < maxn ; j ++ ) { vis [ i ] [ j ] = false ; } } int cnt = 0 ; for ( int j = 1 , v = 1 ; j < limit ; j = j << 1 , v ++ ) { if ( ( set & j ) != 0 ) { for ( int co = 1 ; co <= V ; co ++ ) { if ( gr [ v ] [ co ] && ! vis [ v ] [ co ] ) { vis [ v ] [ co ] = true ; vis [ co ] [ v ] = true ; cnt ++ ; } } } } if ( cnt == E ) return true ; int co = set & - set ; int ro = set + co ; set = ( ( ( ro ^ set ) >> 2 ) / co ) | ro ; } return false ; } static int findMinCover ( int n , int m ) { int left = 1 , right = n ; while ( right > left ) { int mid = ( left + right ) >> 1 ; if ( isCover ( n , mid , m ) == false ) left = mid + 1 ; else right = mid ; } return left ; } static void insertEdge ( int u , int v ) { gr [ u ] [ v ] = true ; gr [ v ] [ u ] = true ; } public static void main ( String [ ] args ) { int V = 6 , E = 6 ; insertEdge ( 1 , 2 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 1 , 4 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; System . out . print ( " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " + findMinCover ( V , E ) + "NEW_LINE"); for ( int i = 0 ; i < maxn ; i ++ ) { for ( int j = 0 ; j < maxn ; j ++ ) { gr [ i ] [ j ] = false ; } } V = 6 ; E = 7 ; insertEdge ( 1 , 2 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 3 , 5 ) ; insertEdge ( 4 , 5 ) ; insertEdge ( 4 , 6 ) ; System . out . print ( " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " + findMinCover ( V , E ) + "NEW_LINE"); } }
Morris traversal for Preorder | Java program to implement Morris preorder traversal A binary tree node ; Preorder traversal without recursion and without stack ; If left child is null , print the current node data . Move to right child . ; Find inorder predecessor ; If the right child of inorder predecessor already points to this node ; If right child doesn 't point to this node, then print  this node and make right child point to this node ; Function for Standard preorder traversal ; Driver programs to test above functions
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void morrisTraversalPreorder ( ) { morrisTraversalPreorder ( root ) ; } void morrisTraversalPreorder ( Node node ) { while ( node != null ) { if ( node . left == null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } else { Node current = node . left ; while ( current . right != null && current . right != node ) { current = current . right ; } if ( current . right == node ) { current . right = null ; node = node . right ; } else { System . out . print ( node . data + " ▁ " ) ; current . right = node ; node = node . left ; } } } } void preorder ( ) { preorder ( root ) ; } void preorder ( Node node ) { if ( node != null ) { System . out . print ( node . data + " ▁ " ) ; preorder ( node . left ) ; preorder ( node . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . morrisTraversalPreorder ( ) ; System . out . println ( " " ) ; tree . preorder ( ) ; } }
Sum of dependencies in a graph | Java program to find the sum of dependencies ; To add an edge ; find the sum of all dependencies ; just find the size at each vector 's index ; Driver method
import java . util . Vector ; class Test { static void addEdge ( Vector < Integer > adj [ ] , int u , int v ) { adj [ u ] . addElement ( ( v ) ) ; } static int findSum ( Vector < Integer > adj [ ] , int V ) { int sum = 0 ; for ( int u = 0 ; u < V ; u ++ ) sum += adj [ u ] . size ( ) ; return sum ; } public static void main ( String [ ] args ) { int V = 4 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > adj [ ] = new Vector [ V ] ; for ( int i = 0 ; i < adj . length ; i ++ ) { adj [ i ] = new Vector < > ( ) ; } addEdge ( adj , 0 , 2 ) ; addEdge ( adj , 0 , 3 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 2 , 3 ) ; System . out . println ( " Sum ▁ of ▁ dependencies ▁ is ▁ " + findSum ( adj , V ) ) ; } }
Linked List | Set 2 ( Inserting a node ) | This function is in LinkedList class . Inserts a new Node at front of the list . This method is defined inside LinkedList class shown above ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node
public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; }
Linked List | Set 2 ( Inserting a node ) | This function is in LinkedList class . Inserts a new node after the given prev_node . This method is defined inside LinkedList class shown above ; 1. Check if the given Node is null ; 2. Allocate the Node & 3. Put in the data ; 4. Make next of new Node as next of prev_node ; 5. make next of prev_node as new_node
public void insertAfter ( Node prev_node , int new_data ) { if ( prev_node == null ) { System . out . println ( " The ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ null " ) ; return ; } Node new_node = new Node ( new_data ) ; new_node . next = prev_node . next ; prev_node . next = new_node ; }
Iterative Preorder Traversal | Java program to implement iterative preorder traversal ; A binary tree node ; An iterative process to print preorder traversal of Binary tree ; Base Case ; Create an empty stack and push root to it ; Pop all items one by one . Do following for every popped item a ) print it b ) push its right child c ) push its left child Note that right child is pushed first so that left is processed first ; Pop the top item from stack and print it ; Push right and left children of the popped node to stack ; driver program to test above functions
import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void iterativePreorder ( ) { iterativePreorder ( root ) ; } void iterativePreorder ( Node node ) { if ( node == null ) { return ; } Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { Node mynode = nodeStack . peek ( ) ; System . out . print ( mynode . data + " ▁ " ) ; nodeStack . pop ( ) ; if ( mynode . right != null ) { nodeStack . push ( mynode . right ) ; } if ( mynode . left != null ) { nodeStack . push ( mynode . left ) ; } } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 2 ) ; tree . iterativePreorder ( ) ; } }
Find Length of a Linked List ( Iterative and Recursive ) | Linked list Node ; Linked List class ; head of list ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Returns count of nodes in linked list ; Base case ; Count is this node plus rest of the list ; Wrapper over getCountRec ( ) ; Driver program to test above functions . Ideally this function should be in a separate user class . It is kept here to keep code compact ; Start with the empty list
class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } class LinkedList { Node head ; public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public int getCountRec ( Node node ) { if ( node == null ) return 0 ; return 1 + getCountRec ( node . next ) ; } public int getCount ( ) { return getCountRec ( head ) ; } public static void main ( String [ ] args ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 1 ) ; llist . push ( 3 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; System . out . println ( " Count ▁ of ▁ nodes ▁ is ▁ " + llist . getCount ( ) ) ; } }
Iterative Preorder Traversal |  ; A binary tree node ; Iterative function to do Preorder traversal of the tree ; Start from root node ( set curr node to root node ) ; Run till stack is not empty or current is not NULL ; Print left children while exist and keep pushing right into the stack . ; We reach when curr is NULL , so We take out a right child from stack ; Driver code
import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void preorderIterative ( ) { preorderIterative ( root ) ; } void preorderIterative ( Node node ) { if ( node == null ) { return ; } Stack < Node > st = new Stack < Node > ( ) ; Node curr = node ; while ( curr != null || ! st . isEmpty ( ) ) { while ( curr != null ) { System . out . print ( curr . data + " ▁ " ) ; if ( curr . right != null ) st . push ( curr . right ) ; curr = curr . left ; } if ( ! st . isEmpty ( ) ) { curr = st . pop ( ) ; } } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 20 ) ; tree . root . right = new Node ( 30 ) ; tree . root . left . left = new Node ( 40 ) ; tree . root . left . left . left = new Node ( 70 ) ; tree . root . left . right = new Node ( 50 ) ; tree . root . right . left = new Node ( 60 ) ; tree . root . left . left . right = new Node ( 80 ) ; tree . preorderIterative ( ) ; } }
Program for n 'th node from the end of a Linked List |
static void printNthFromLast ( Node head , int n ) { static int i = 0 ; if ( head == null ) return ; printNthFromLast ( head . next , n ) ; if ( ++ i == n ) System . out . print ( head . data ) ; }
Write a function that counts the number of times a given int occurs in a Linked List | Java program to count occurrences in a linked list using recursion ; Represents node of a linkedlist ; global variable for counting frequeancy of given element k ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; allocate node ; link the old list off the new node ; move the head to point to the new node ; Counts the no . of occurrences of a node ( search_for ) in a linked list ( head ) ; Driver Code ; Start with the empty list ; Use push ( ) to construct below list 1 -> 2 -> 1 -> 3 -> 1 ; Check the count function
import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( int val ) { data = val ; next = null ; } } class GFG { static int frequency = 0 ; static Node push ( Node head , int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; return head ; } static int count ( Node head , int key ) { if ( head == null ) return frequency ; if ( head . data == key ) frequency ++ ; return count ( head . next , key ) ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 3 ) ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; System . out . print ( " count ▁ of ▁ 1 ▁ is ▁ " + count ( head , 1 ) ) ; } }
Write a function that counts the number of times a given int occurs in a Linked List | Counts the no . of occurrences of a node ( search_for ) in a linked list ( head )
int count ( Node head , int key ) { if ( head == null ) return 0 ; if ( head . data == key ) return 1 + count ( head . next , key ) ; return count ( head . next , key ) ; }
Detect loop in a linked list | Java program to detect loop in a linked list ; Link list node ; Allocate node ; Put in the data ; Link the old list off the new node ; Move the head to point to the new node ; Returns true if there is a loop in linked list else returns false . ; If this node is already traverse it means there is a cycle ( Because you we encountering the node for the second time ) . ; If we are seeing the node for the first time , mark its flag as 1 ; Driver code ; Start with the empty list ; Create a loop for testing
import java . util . * ; class GFG { static class Node { int data ; Node next ; int flag ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . flag = 0 ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static boolean detectLoop ( Node h ) { while ( h != null ) { if ( h . flag == 1 ) return true ; h . flag = 1 ; h = h . next ; } return false ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 4 ) ; head = push ( head , 15 ) ; head = push ( head , 10 ) ; head . next . next . next . next = head ; if ( detectLoop ( head ) ) System . out . print ( " Loop ▁ found " ) ; else System . out . print ( " No ▁ Loop " ) ; } }
Detect loop in a linked list | Java program to detect loop in a linked list ; head of list ; Linked list Node ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Driver program to test above functions ; Start with the empty list ; Create loop for testing
class LinkedList { Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } void detectLoop ( ) { Node slow_p = head , fast_p = head ; int flag = 0 ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( " Loop ▁ found " ) ; else System . out . println ( " Loop ▁ not ▁ found " ) ; } public static void main ( String args [ ] ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 20 ) ; llist . push ( 4 ) ; llist . push ( 15 ) ; llist . push ( 10 ) ; llist . head . next . next . next . next = llist . head ; llist . detectLoop ( ) ; } }
Detect loop in a linked list | Java program to return first node of loop ; A utility function to print a linked list ; Function to detect first node of loop in a linked list that may contain loop ; Create a temporary node ; This condition is for the case when there is no loop ; Check if next is already pointing to temp ; Store the pointer to the next node in order to get to it in the next step ; Make next point to temp ; Get to the next node in the list ; Driver code ; Create a loop for testing ( 5 is pointing to 3 ) /
class GFG { static class Node { int key ; Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . next = null ; return temp ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . key + " ▁ " ) ; head = head . next ; } System . out . println ( ) ; } static boolean detectLoop ( Node head ) { Node temp = new Node ( ) ; while ( head != null ) { if ( head . next == null ) { return false ; } if ( head . next == temp ) { return true ; } Node nex = head . next ; head . next = temp ; head = nex ; } return false ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = head . next . next ; boolean found = detectLoop ( head ) ; if ( found ) System . out . println ( " Loop ▁ Found " ) ; else System . out . println ( " No ▁ Loop " ) ; } }