Hexadecimal equivalents in Binary Valued Graph
Given a binary valued undirected graph with V vertices and E edges, the task is to find the hexadecimal 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 Hexadecimal equivalent = 1
Chain = 0 0 0 Hexadecimal equivalent = 0
Chain = 1 1 Hexadecimal equivalent = 3
In case of the first connected component, the binary chain is [0, 1]
Hence, the binary string = “01” and binary number = 01
So, the hexadecimal equivalent = 1
Input: E = 6, V = 10
Chain = 1 Hexadecimal equivalent = 1
Chain = 0 0 1 0 Hexadecimal equivalent = 2
Chain = 1 1 0 Hexadecimal equivalent = 6
Chain = 1 0 Hexadecimal 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 hexadecimal 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 Hexadecimal equivalent = 1 Chain = 1 1 0 1 1 Hexadecimal equivalent = 1B
Time Complexity: O(V2)
The DFS algorithm requires O(V + E) complexity, where V, E are the vertices and edges of the undirected graph. Further, the hexadecimal equivalent is obtained at each iteration which requires an additional O(V) complexity to compute. Hence, the overall complexity is O(V2).
Auxiliary Space: O(V)
Please Login to comment...