Octal equivalents of connected components in Binary valued graph
Given a binary valued undirected graph with V vertices and E edges, the task is to find the octal equivalents of all the connected components of the graph. A binary valued graph can be considered as having only binary numbers (0 or 1) as the vertex values.
Examples:
Input: E = 4, V = 7
Output:
Chain = 0 1 Octal equivalent = 1
Chain = 0 0 0 Octal equivalent = 0
Chain = 1 1 Octal equivalent = 3
Explanation:
In case of the first connected component, the binary chain is [0, 1]
Hence, the binary string = “01” and binary number = 01
Therefore, the octal equivalent is 1Input: E = 6, V = 10
Output:
Chain = 1 Octal equivalent = 1
Chain = 0 0 1 0 Octal equivalent = 2
Chain = 1 1 0 Octal equivalent = 6
Chain = 1 0 Octal equivalent = 2
Approach: The idea is to use Depth First Search Traversal to keep track of the connected components in the undirected graph as explained in this article. For each connected component, the binary string is displayed and the equivalent octal value is calculated from the binary value (as explained in this article) and printed.
Below is the implementation of the above approach:
C++
// C++ implementation to find // octal equivalents of // all connected components #include <bits/stdc++.h> using namespace std; // Function to traverse the undirected // graph using the Depth first traversal void depthFirst( int v, vector< int > graph[], vector< bool >& visited, vector< int >& storeChain) { // Marking the visited // vertex as true visited[v] = true ; // Store the connected chain storeChain.push_back(v); for ( auto i : graph[v]) { if (visited[i] == false ) { // Recursive call to // the DFS algorithm depthFirst(i, graph, visited, storeChain); } } } // Function to create map between binary // number and its equivalent octal value 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' ; } // Function to return octal // equivalent of each connected // component string Octal(string bin) { int l = bin.size(); int t = bin.find_first_of( '.' ); // length of string before '.' int len_left = t != -1 ? t : l; // add min 0's in the beginning to make // left substring length divisible by 3 for ( int i = 1; i <= (3 - len_left % 3) % 3; i++) bin = '0' + bin; // if decimal point exists if (t != -1) { // length of string after '.' int len_right = l - len_left - 1; // add min 0's in the end to make right // substring length divisible by 3 for ( int i = 1; i <= (3 - len_right % 3) % 3; i++) bin = bin + '0' ; } // create map between binary and its // equivalent octal code unordered_map<string, char > bin_oct_map; createMap(&bin_oct_map); int i = 0; string octal = "" ; while (1) { // one by one extract from left, // substring of size 3 and // add its octal code octal += bin_oct_map[bin.substr(i, 3)]; i += 3; if (i == bin.size()) break ; // if '.' is encountered // add it to result if (bin.at(i) == '.' ) { octal += '.' ; i++; } } // required octal number return octal; } // Function to find the octal equivalents // of all connected components void octalValue( vector< int > graph[], int vertices, vector< int > values) { // Initializing boolean array // to mark visited vertices vector< bool > visited(1001, false ); // Following loop invokes DFS algorithm for ( int i = 1; i <= vertices; i++) { if (visited[i] == false ) { // Variable to hold // temporary length int sizeChain; // Container to store each chain vector< int > storeChain; // DFS algorithm depthFirst(i, graph, visited, storeChain); // Variable to hold each chain size sizeChain = storeChain.size(); // Container to store values // of vertices of individual chains int chainValues[sizeChain + 1]; // Storing the values of each chain for ( int i = 0; i < sizeChain; i++) { int temp = values[storeChain[i] - 1]; chainValues[i] = temp; } // Printing binary chain cout << "Chain = " ; for ( int i = 0; i < sizeChain; i++) { cout << chainValues[i] << " " ; } cout << "\t" ; // Converting the array with vertex // values to a binary string // using string stream 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); } // Printing the octal values cout << "Octal equivalent = " ; cout << Octal(s) << endl; } } } // Driver code to test above function int main() { // Initializing graph in the // form of adjacency list vector< int > graph[1001]; // Defining the number // of edges and vertices int E, V; E = 4; V = 7; // Assigning the values for each // vertex of the undirected graph 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); // Constructing the undirected graph 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; } |
Java
// Java implementation to find // octal equivalents of all // connected components import java.io.*; import java.util.*; class GFG{ // Function to traverse the undirected // graph using the Depth first traversal static void depthFirst( int v, List<List<Integer>> graph, boolean [] visited, List<Integer> storeChain) { // Marking the visited // vertex as true visited[v] = true ; // Store the connected chain storeChain.add(v); for ( int i : graph.get(v)) { if (visited[i] == false ) { // Recursive call to // the DFS algorithm depthFirst(i, graph, visited, storeChain); } } } // Function to create map between binary // number and its equivalent hexadecimal static void createMap(Map<String, Character> um) { um.put( "000" , '0' ); um.put( "001" , '1' ); um.put( "010" , '2' ); um.put( "011" , '3' ); um.put( "100" , '4' ); um.put( "101" , '5' ); um.put( "110" , '6' ); um.put( "111" , '7' ); } // Function to return octal // equivalent of each connected // component static String octal(String bin) { int l = bin.length(); int t = bin.indexOf( '.' ); // Length of string before '.' int len_left = t != - 1 ? t : l; // Add min 0's in the beginning to make // left substring length divisible by 3 for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i++) bin = '0' + bin; // If decimal point exists if (t != - 1 ) { // Length of string after '.' int len_right = l - len_left - 1 ; // Add min 0's in the end to make right // substring length divisible by 3 for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i++) bin = bin + '0' ; } // Create map between binary and its // equivalent octal code Map<String, Character> bin_oct_map = new HashMap<String, Character>(); createMap(bin_oct_map); int i = 0 ; String octal = "" ; while ( true ) { // One by one extract from left, // substring of size 3 and // add its octal code octal += bin_oct_map.get( bin.substring(i, i + 3 )); i += 3 ; if (i == bin.length()) break ; // If '.' is encountered // add it to result if (bin.charAt(i) == '.' ) { octal += '.' ; i++; } } // Required octal number return octal; } // Function to find the octal equivalents // of all connected components static void octalValue(List<List<Integer>> graph, int vertices, List<Integer> values) { // Initializing boolean array // to mark visited vertices boolean [] visited = new boolean [ 1001 ]; // Following loop invokes DFS algorithm for ( int i = 1 ; i <= vertices; i++) { if (visited[i] == false ) { // Variable to hold // temporary length int sizeChain; // Container to store each chain List<Integer> storeChain = new ArrayList<Integer>(); // DFS algorithm depthFirst(i, graph, visited, storeChain); // Variable to hold each chain size sizeChain = storeChain.size(); // Container to store values // of vertices of individual chains int [] chainValues = new int [sizeChain + 1 ]; // Storing the values of each chain for ( int j = 0 ; j < sizeChain; j++) { int temp = values.get( storeChain.get(j) - 1 ); chainValues[j] = temp; } // Printing binary chain System.out.print( "Chain = " ); for ( int j = 0 ; j < sizeChain; j++) { System.out.print(chainValues[j] + " " ); } System.out.print( "\t" ); // Converting the array with vertex // values to a binary string String s = "" ; for ( int j = 0 ; j < sizeChain; j++) { String s1 = String.valueOf( chainValues[j]); s += s1; } // Printing the octal values System.out.println( "Octal equivalent = " + octal(s)); } } } // Driver code public static void main(String[] args) { // Initializing graph in the // form of adjacency list @SuppressWarnings ( "unchecked" ) List<List<Integer>> graph = new ArrayList(); for ( int i = 0 ; i < 1001 ; i++) graph.add( new ArrayList<Integer>()); // Defining the number // of edges and vertices int E = 4 , V = 7 ; // Assigning the values for each // vertex of the undirected graph List<Integer> values = new ArrayList<Integer>(); values.add( 0 ); values.add( 1 ); values.add( 0 ); values.add( 0 ); values.add( 0 ); values.add( 1 ); values.add( 1 ); // Constructing the undirected graph 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( 7 ); graph.get( 7 ).add( 6 ); octalValue(graph, V, values); } } // This code is contributed by jithin |
Python3
''' Python implementation to find octal equivalents of all connected components ''' from typing import List from collections import defaultdict from math import ceil # Function to traverse the undirected # graph using the Depth first traversal def depth_first(v: int , graph: List [ List [ int ]], visited: List [ bool ], store_chain: List [ int ]) - > None : # Marking the visited # vertex as true visited[v] = True # Store the connected chain store_chain.append(v) for i in graph[v]: if not visited[i]: # Recursive call to # the DFS algorithm depth_first(i, graph, visited, store_chain) # Function to create map between binary # number and its equivalent octal value def create_map(um: dict ) - > None : um[ "000" ] = '0' um[ "001" ] = '1' um[ "010" ] = '2' um[ "011" ] = '3' um[ "100" ] = '4' um[ "101" ] = '5' um[ "110" ] = '6' um[ "111" ] = '7' # Function to return octal # equivalent of each connected # component def octal(bin_str: str ) - > str : l = len (bin_str) t = bin_str.find( '.' ) # length of string before '.' len_left = t if t ! = - 1 else l # add min 0's in the beginning to make # left substring length divisible by 3 bin_str = ( '0' * (( 3 - len_left % 3 ) % 3 )) + bin_str # if decimal point exists if t ! = - 1 : # length of string after '.' len_right = l - len_left - 1 # add min 0's in the end to make right # substring length divisible by 3 bin_str + = '0' * (( 3 - len_right % 3 ) % 3 ) # create map between binary and its # equivalent octal code bin_oct_map = {} create_map(bin_oct_map) i = 0 octal_str = "" while True : # one by one extract from left, # substring of size 3 and # add its octal code octal_str + = bin_oct_map[bin_str[i:i + 3 ]] i + = 3 if i = = len (bin_str): break # if '.' is encountered # add it to result if bin_str[i] = = '.' : octal_str + = '.' i + = 1 # required octal number return octal_str # Function to find the octal equivalents # of all connected components def octal_value(graph: List [ List [ int ]], vertices: int , values: List [ int ]) - > None : # Initializing boolean array # to mark visited vertices visited = [ False ] * 1001 # Following loop invokes DFS algorithm for i in range ( 1 , vertices + 1 ): if not visited[i]: store_chain = [] depth_first(i, graph, visited, store_chain) # Variable to hold # temporary length size_chain = len (store_chain) chain_values = [values[store_chain[j] - 1 ] for j in range (size_chain)] # Printing binary chain print ( "Chain =" , end = " " ) print ( * chain_values, sep = " " , end = "\t" ) s = ''.join( map ( str , chain_values)) # Printing the octal values print ( " Octal equivalent =" , octal(s)) # Driver code if __name__ = = '__main__' : # Initializing graph in the # form of adjacency list graph = defaultdict( list ) # Defining the number # of edges and vertices E = 4 V = 7 # Assigning the values for each # vertex of the undirected graph values = [ 0 , 1 , 0 , 0 , 0 , 1 , 1 ] # Constructing the undirected graph graph[ 1 ].append( 2 ) graph[ 2 ].append( 1 ) graph[ 3 ].append( 4 ) graph[ 4 ].append( 3 ) graph[ 4 ].append( 5 ) graph[ 5 ].append( 4 ) graph[ 6 ].append( 7 ) graph[ 7 ].append( 6 ) octal_value(graph, V, values) # This code is contributed by Prince Kumar |
C#
// C# implementation to find // octal equivalents of // all connected components using System; using System.Collections.Generic; class GFG { // Function to traverse the undirected // graph using the Depth first traversal static void depthFirst( int v, List<List< int > > graph, bool [] visited, List< int > storeChain) { // Marking the visited // vertex as true visited[v] = true ; // Store the connected chain storeChain.Add(v); foreach ( int i in graph[v]) { if (!visited[i]) { // Recursive call to // the DFS algorithm depthFirst(i, graph, visited, storeChain); } } } // Function to create map between binary // number and its equivalent octal value static void createMap(Dictionary< string , char > um) { um.Add( "000" , '0' ); um.Add( "001" , '1' ); um.Add( "010" , '2' ); um.Add( "011" , '3' ); um.Add( "100" , '4' ); um.Add( "101" , '5' ); um.Add( "110" , '6' ); um.Add( "111" , '7' ); } // Function to return octal // equivalent of each connected // component static string octal( string bin) { int l = bin.Length; int t = bin.IndexOf( '.' ); // length of string before '.' int len_left = t != -1 ? t : l; // add min 0's in the beginning to make // left substring length divisible by 3 for ( int m = 1; m <= (3 - len_left % 3) % 3; m++) bin = '0' + bin; // if decimal point exists if (t != -1) { // length of string after '.' int len_right = l - len_left - 1; // add min 0's in the end to make right // substring length divisible by 3 for ( int p = 1; p <= (3 - len_right % 3) % 3; p++) bin = bin + '0' ; } // create map between binary and its // equivalent octal code Dictionary< string , char > bin_oct_map = new Dictionary< string , char >(); createMap(bin_oct_map); int i = 0; string octal = "" ; while ( true ) { // one by one extract from left, // substring of size 3 and // add its octal code octal += bin_oct_map[bin.Substring(i, 3)]; i += 3; if (i == bin.Length) break ; // if '.' is encountered // add it to result if (bin[i] == '.' ) { octal += '.' ; i++; } } // required octal number return octal; } // Function to find the octal equivalents // of all connected components static void octalValue(List<List< int > > graph, int vertices, List< int > values) { // Initializing boolean array // to mark visited vertices bool [] visited = new bool [1001]; // Following loop invokes DFS algorithm for ( int i = 1; i <= vertices; i++) { if (!visited[i]) { // Variable to hold // temporary length int sizeChain; // Container to store each chain List< int > storeChain = new List< int >(); // DFS algorithm depthFirst(i, graph, visited, storeChain); // Variable to hold each chain size sizeChain = storeChain.Count; // Container to store values // of vertices of individual chains int [] chainValues = new int [sizeChain + 1]; for ( int t = 0; t < sizeChain; t++) { int temp = values[storeChain[t] - 1]; chainValues[t] = temp; } // Printing binary chain Console.Write( "Chain = " ); for ( int j = 0; j < sizeChain; j++) { Console.Write(chainValues[j] + " " ); } Console.Write( "\t" ); // Converting the array with vertex // values to a binary string // using string stream string s = "" ; for ( int k = 0; k < sizeChain; k++) { string s1 = chainValues[k].ToString(); s += s1; } // Printing the octal values Console.WriteLine( "Octal equivalent = " + octal(s)); } } } // Driver code static void Main( string [] args) { // Initializing graph in the // form of adjacency list List<List< int > > graph = new List<List< int > >(); for ( int i = 0; i < 1001; i++) graph.Add( new List< int >()); // Defining the number // of edges and vertices int E = 4, V = 7; // Assigning the values for each // vertex of the undirected graph List< int > values = new List< int >() { 0, 1, 0, 0, 0, 1, 1 }; // Constructing the undirected graph graph[1].Add(2); graph[2].Add(1); graph[3].Add(4); graph[4].Add(3); graph[4].Add(5); graph[5].Add(4); graph[6].Add(7); graph[7].Add(6); octalValue(graph, V, values); } } |
Javascript
/* javascript implementation to find octal equivalents of all connected components */ // Function to traverse the undirected // graph using the Depth first traversal function depth_first(v, graph, visited, store_chain) { // Marking the visited // vertex as true visited[v] = true ; // Store the connected chain store_chain.push(v); for (let i of graph[v]) { if (!visited[i]) { // Recursive call to //the DFS algorithm depth_first(i, graph, visited, store_chain); } } } // Function to create map between binary // number and its equivalent octal value function create_map(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" ; } // Function to return octal // equivalent of each connected // component function octal(bin_str) { let l = bin_str.length; let t = bin_str.indexOf( "." ); // length of string before '.' let len_left = t !== -1 ? t : l; // add min 0's in the beginning to make // left substring length divisible by 3 bin_str = ( "0" .repeat((3 - (len_left % 3)) % 3)) + bin_str; // if decimal point exists if (t !== -1) { // length of string after '.' let len_right = l - len_left - 1; // add min 0's in the end to make right // substring length divisible by 3 bin_str += "0" .repeat((3 - (len_right % 3)) % 3); } // create map between binary and its // equivalent octal code let bin_oct_map = {}; create_map(bin_oct_map); let i = 0; let octal_str = "" ; while ( true ) { // one by one extract from left, // substring of size 3 and // add its octal code octal_str += bin_oct_map[bin_str.slice(i, i + 3)]; i += 3; if (i === bin_str.length) { break ; } // if '.' is encountered // add it to result if (bin_str[i] === "." ) { octal_str += "." ; i += 1; } } // required octal number return octal_str; } // Function to find the octal equivalents // of all connected components function octal_value(graph, vertices, values) { // nitializing boolean array // to mark visited vertices let visited = Array(1001).fill( false ); // Following loop invokes DFS algorithm for (let i = 1; i <= vertices; i++) { if (!visited[i]) { let store_chain = []; depth_first(i, graph, visited, store_chain); // Variable to hold // temporary length let size_chain = store_chain.length; let chain_values = []; for (let j = 0; j < size_chain; j++) { // Printing binary chain chain_values.push(values[store_chain[j] - 1]); } // Printing the octal values console.log( "Chain =" , ...chain_values, "\t" , "Octal equivalent =" , octal(chain_values.join( "" ))); } } } // Driver code // Initializing graph in the // form of adjacency list let graph = {}; // Defining the number // of edges and vertices let E = 4; let V = 7; // Assigning the values for each // vertex of the undirected graph let values = [0, 1, 0, 0, 0, 1, 1]; // Constructing the undirected graph graph[1] = [2]; graph[2] = [1]; graph[3] = [4]; graph[4] = [3, 5]; graph[5] = [4]; graph[6] = [7]; graph[7] = [6]; octal_value(graph, V, values); // This code is contributed by shivhack999 |
Chain = 0 1 Octal equivalent = 1 Chain = 0 0 0 Octal equivalent = 0 Chain = 1 1 Octal equivalent = 3