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
- 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
- Convert a binary number to hexadecimal number
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Finding minimum vertex cover size of a graph using binary search
- Find the most valued alphabet in the String
- Mutation Algorithms for Real-Valued Parameters (GA)
- Minimum valued node having maximum depth in an N-ary Tree
- Program to find the largest and smallest ASCII valued characters in a string
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- How to add two Hexadecimal numbers
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Detect cycle in the graph using degrees of nodes of graph
- Program for Hexadecimal to Decimal
- Count Hexadecimal Number
- StringStream in C++ for Decimal to Hexadecimal and back
- Program to Convert Hexadecimal to Octal
- Check the divisibility of Hexadecimal numbers
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.