Octal equivalents of connected components in Binary valued graph | C ++ implementation to find octal 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 octal value ; Function to return octal equivalent of each connected component ; length of string before ' . ' ; add min 0 's in the beginning to make  left substring length divisible by 3 ; if decimal point exists ; length of string after ' . ' ; add min 0 's in the end to make right  substring length divisible by 3 ; create map between binary and its equivalent octal code ; one by one extract from left , substring of size 3 and add its octal code ; if ' . ' is encountered add it to result ; required octal number ; Function to find the octal 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 octal values ; Driver code to test above function ; 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 ) [ "000" ] = '0' ; ( * um ) [ "001" ] = '1' ; ( * um ) [ "010" ] = '2' ; ( * um ) [ "011" ] = '3' ; ( * um ) [ "100" ] = '4' ; ( * um ) [ "101" ] = '5' ; ( * um ) [ "110" ] = '6' ; ( * um ) [ "111" ] = '7' ; } string Octal ( string bin ) { int l = bin . size ( ) ; int t = bin . find_first_of ( ' . ' ) ; int len_left = t != -1 ? t : l ; for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = '0' + bin ; if ( t != -1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + '0' ; } unordered_map < string , char > bin_oct_map ; createMap ( & bin_oct_map ) ; int i = 0 ; string octal = " " ; while ( 1 ) { octal += bin_oct_map [ bin . substr ( i , 3 ) ] ; i += 3 ; if ( i == bin . size ( ) ) break ; if ( bin . at ( i ) == ' . ' ) { octal += ' . ' ; i ++ ; } } return octal ; } void octalValue ( vector < int > graph [ ] , int vertices , vector < int > values ) { vector < bool > visited ( 1001 , 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 << " Octal ▁ equivalent ▁ = ▁ " ; cout << Octal ( 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 ( 0 ) ; values . push_back ( 0 ) ; 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 ( 7 ) ; graph [ 7 ] . push_back ( 6 ) ; octalValue ( graph , V , values ) ; return 0 ; }