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).
- Octal equivalents of connected components in Binary valued graph
- Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Difference between sum of even and odd valued nodes in a Binary Tree
- Program to find the largest and smallest ASCII valued characters in a string
- Mutation Algorithms for Real-Valued Parameters (GA)
- Find the most valued alphabet in the String
- Minimum valued node having maximum depth in an N-ary Tree
- Convert a binary number to hexadecimal number
- Program for Hexadecimal to Decimal
- StringStream in C++ for Decimal to Hexadecimal and back
- Convert Hexadecimal value String to ASCII value String
- Count Hexadecimal Number
- Check the divisibility of Hexadecimal numbers
- How to add two Hexadecimal numbers
- Check if a string represents a hexadecimal number or not
- How to validate Hexadecimal Color Code using Regular Expression
- Program to Convert Hexadecimal to Octal
- Convert a string to hexadecimal ASCII values
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.