Hexadecimal equivalents in Binary Valued Graph | C ++ 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 ; 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 using string stream ; Printing the hexadecimal values ; Driver Program ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void depthFirst ( int v , vector < int > graph [ ] , vector < bool > & visited , vector < int > & storeChain ) { visited [ v ] = true ; storeChain . push_back ( v ) ; for ( auto i : graph [ v ] ) { if ( visited [ i ] == false ) { depthFirst ( i , graph , visited , storeChain ) ; } } } void createMap ( unordered_map < string , char > * um ) { ( * um ) [ "0000" ] = '0' ; ( * um ) [ "0001" ] = '1' ; ( * um ) [ "0010" ] = '2' ; ( * um ) [ "0011" ] = '3' ; ( * um ) [ "0100" ] = '4' ; ( * um ) [ "0101" ] = '5' ; ( * um ) [ "0110" ] = '6' ; ( * um ) [ "0111" ] = '7' ; ( * um ) [ "1000" ] = '8' ; ( * um ) [ "1001" ] = '9' ; ( * um ) [ "1010" ] = ' A ' ; ( * um ) [ "1011" ] = ' B ' ; ( * um ) [ "1100" ] = ' C ' ; ( * um ) [ "1101" ] = ' D ' ; ( * um ) [ "1110" ] = ' E ' ; ( * um ) [ "1111" ] = ' F ' ; } string hexaDecimal ( string bin ) { int l = bin . size ( ) ; int t = bin . find_first_of ( ' . ' ) ; 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' ; } unordered_map < string , char > bin_hex_map ; createMap ( & bin_hex_map ) ; int i = 0 ; string hex = " " ; while ( 1 ) { hex += bin_hex_map [ bin . substr ( i , 4 ) ] ; i += 4 ; if ( i == bin . size ( ) ) break ; if ( bin . at ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } void hexValue ( vector < int > graph [ ] , int vertices , vector < int > values ) { vector < bool > visited ( 10001 , false ) ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; vector < int > storeChain ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . size ( ) ; int chainValues [ sizeChain + 1 ] ; for ( int i = 0 ; i < sizeChain ; i ++ ) { int temp = values [ storeChain [ i ] - 1 ] ; chainValues [ i ] = temp ; } cout << " Chain ▁ = ▁ " ; for ( int i = 0 ; i < sizeChain ; i ++ ) { cout << chainValues [ i ] << " ▁ " ; } cout << " TABSYMBOL " ; stringstream ss ; ss << chainValues [ 0 ] ; string s = ss . str ( ) ; for ( int i = 1 ; i < sizeChain ; i ++ ) { stringstream ss1 ; ss1 << chainValues [ i ] ; string s1 = ss1 . str ( ) ; s . append ( s1 ) ; } cout << " Hexadecimal ▁ " << " equivalent ▁ = ▁ " ; cout << hexaDecimal ( s ) << endl ; } } } int main ( ) { vector < int > graph [ 1001 ] ; int E , V ; E = 4 ; V = 7 ; vector < int > values ; values . push_back ( 0 ) ; values . push_back ( 1 ) ; values . push_back ( 1 ) ; values . push_back ( 1 ) ; values . push_back ( 0 ) ; values . push_back ( 1 ) ; values . push_back ( 1 ) ; graph [ 1 ] . push_back ( 2 ) ; graph [ 2 ] . push_back ( 1 ) ; graph [ 3 ] . push_back ( 4 ) ; graph [ 4 ] . push_back ( 3 ) ; graph [ 4 ] . push_back ( 5 ) ; graph [ 5 ] . push_back ( 4 ) ; graph [ 6 ] . push_back ( 5 ) ; graph [ 5 ] . push_back ( 6 ) ; graph [ 6 ] . push_back ( 7 ) ; graph [ 7 ] . push_back ( 6 ) ; hexValue ( graph , V , values ) ; return 0 ; }