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.
Input: E = 4, V = 7
Chain = 0 1 Octal equivalent = 1
Chain = 0 0 0 Octal equivalent = 0
Chain = 1 1 Octal equivalent = 3
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 1
Input: E = 6, V = 10
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:
Chain = 0 1 Octal equivalent = 1 Chain = 0 0 0 Octal equivalent = 0 Chain = 1 1 Octal equivalent = 3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.