Hexadecimal equivalents in Binary Valued Graph | Java implementation to find hexadecimal equivalents of all connected components ; Function to traverse the undirected graph using the Depth first traversal ; Marking the visited vertex as true ; Store the connected chain ; Recursive call to the DFS algorithm ; Function to create map between binary number and its equivalent hexadecimal ; Function to return hexadecimal equivalent of each connected component ; Length of string before ' . ' ; Add min 0 's in the beginning to make  left substring length divisible by 4 ; If decimal point exists ; Length of string after ' . ' ; Add min 0 's in the end to make right  substring length divisible by 4 ; Create map between binary and its equivalent hex code ; One by one extract from left , substring of size 4 and add its hex code ; If ' . ' is encountered add it to result ; Required hexadecimal number ; Function to find the hexadecimal equivalents of all connected components ; Initializing boolean array to mark visited vertices ; Following loop invokes DFS algorithm ; Variable to hold temporary length ; Container to store each chain ; DFS algorithm ; Variable to hold each chain size ; Container to store values of vertices of individual chains ; Storing the values of each chain ; Printing binary chain ; Converting the array with vertex values to a binary string ; Printing the hexadecimal values ; Driver code ; Initializing graph in the form of adjacency list ; Defining the number of edges and vertices ; Assigning the values for each vertex of the undirected graph ; Constructing the undirected graph
import java . io . * ; import java . util . * ; class GFG { static void depthFirst ( int v , List < List < Integer > > graph , boolean [ ] visited , List < Integer > storeChain ) { visited [ v ] = true ; storeChain . add ( v ) ; for ( int i : graph . get ( v ) ) { if ( visited [ i ] == false ) { depthFirst ( i , graph , visited , storeChain ) ; } } } static void createMap ( Map < String , Character > um ) { um . put ( "0000" , '0' ) ; um . put ( "0001" , '1' ) ; um . put ( "0010" , '2' ) ; um . put ( "0011" , '3' ) ; um . put ( "0100" , '4' ) ; um . put ( "0101" , '5' ) ; um . put ( "0110" , '6' ) ; um . put ( "0111" , '7' ) ; um . put ( "1000" , '8' ) ; um . put ( "1001" , '9' ) ; um . put ( "1010" , ' A ' ) ; um . put ( "1011" , ' B ' ) ; um . put ( "1100" , ' C ' ) ; um . put ( "1101" , ' D ' ) ; um . put ( "1110" , ' E ' ) ; um . put ( "1111" , ' F ' ) ; } static String hexaDecimal ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 4 - len_left % 4 ) % 4 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 4 - len_right % 4 ) % 4 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_hex_map = new HashMap < String , Character > ( ) ; createMap ( bin_hex_map ) ; int i = 0 ; String hex = " " ; while ( true ) { hex += bin_hex_map . get ( bin . substring ( i , i + 4 ) ) ; i += 4 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } static void hexValue ( List < List < Integer > > graph , int vertices , List < Integer > values ) { boolean [ ] visited = new boolean [ 1001 ] ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; List < Integer > storeChain = new ArrayList < Integer > ( ) ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . size ( ) ; int [ ] chainValues = new int [ sizeChain + 1 ] ; for ( int j = 0 ; j < sizeChain ; j ++ ) { int temp = values . get ( storeChain . get ( j ) - 1 ) ; chainValues [ j ] = temp ; } System . out . print ( " Chain ▁ = ▁ " ) ; for ( int j = 0 ; j < sizeChain ; j ++ ) { System . out . print ( chainValues [ j ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " TABSYMBOL " ) ; String s = " " ; for ( int j = 0 ; j < sizeChain ; j ++ ) { String s1 = String . valueOf ( chainValues [ j ] ) ; s += s1 ; } System . out . println ( " Hexadecimal ▁ " + " equivalent ▁ = ▁ " + hexaDecimal ( s ) ) ; } } } public static void main ( String [ ] args ) { @ SuppressWarnings ( " unchecked " ) List < List < Integer > > graph = new ArrayList ( ) ; for ( int i = 0 ; i < 1001 ; i ++ ) graph . add ( new ArrayList < Integer > ( ) ) ; int E = 4 , V = 7 ; List < Integer > values = new ArrayList < Integer > ( ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 1 ) ; values . add ( 1 ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 1 ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 1 ) ; graph . get ( 3 ) . add ( 4 ) ; graph . get ( 4 ) . add ( 3 ) ; graph . get ( 4 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 4 ) ; graph . get ( 6 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 6 ) ; graph . get ( 6 ) . add ( 7 ) ; graph . get ( 7 ) . add ( 6 ) ; hexValue ( graph , V , values ) ; } }