You are given an undirected connected graph with g nodes and M connections. Your task is to
Question:
You are given an undirected connected graph with g nodes and M connections. Your task is to traverse all nodes at least once and store the order of traversal in an array A. Then, construct an array B using the following algorithm:
For each element Ai in array A:
Initialize a boolean variable found as false.
For each element Bj in array B:
If Ai is equal to Bj, set found to true and break.
If found is still false, add Ai to array B.
Return the resulting array B.
For example:
Consider g_nodes = 5, g_from = [4, 5, 1, 4, 3], and g_to = [5, 1, 4, 3, 2]. There are g_nodes = 5 nodes and M = 5 edges. The connected pairs are: (4, 5), (5, 1), (1, 4), (4, 3), (3, 2).
Traverse the graph and store the order of traversal in array A. Determining the order of traversal is up to you. For this example, the optimal traversal is: 5 -> 4 -> 3 -> 2 -> 3 -> 4 -> 1. So, A = [5, 4, 3, 2, 3, 4, 1].
Construct array B according to the described algorithm: B = [5, 4, 3, 2, 1]. This is the largest lexicographically possible array B.
Function Description
Complete the function coolGraph in the editor below. The function must return an array representing array B.
coolGraph has the following parameters:
int g_nodes: the number of nodes.
int g_from[M]: one end of each connected pair of nodes.
int g_to[M]: the other end of each connected pair of nodes.
Constraints
1 ≤ g_nodes ≤ 105
1 ≤ M ≤ 2 * 105
1 ≤ g_from[i] ≤ 2 * 105 (for each 1 ≤ i ≤ M)
1 ≤ g_to[i] ≤ 2 * 105 (for each 1 ≤ i ≤ M)
Please ensure to follow these constraints and implement the coolGraph function accordingly.
Precalculus Concepts Through Functions A Unit Circle Approach To Trigonometry
ISBN: 9780137945139
5th Edition
Authors: Michael Sullivan