Given an integer N, representing the number of nodes present in an undirected graph, with each node valued from 1 to **N, **and a 2D array Edges[][], representing the pair of vertices connected by an edge, the task is to find a set of at most N/2 nodes such that nodes that are not present in the set, are connected adjacent to any one of the nodes present in the set.
Examples :
Input:_ N = 4, Edges[][2] = {{2, 3}, {1, 3}, {4, 2}, {1, 2}}_
Output:_ 3 2_
Explanation:_ Connections specified in the above graph are as follows:_
_ 1_
_ / _
2 – 3
|
4
Selecting the set {2, 3} satisfies the required conditions.
Input:_ N = 5, Edges[][2] = {{2, 1}, {3, 1}, {3, 2}, {4, 1}, {4, 2}, {4, 3}, {5, 1}, {5, 2}, {5, 3}, {5, 4}}_
Output:_ 1_
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
**Approach: **The given problem can be solved based on the following observations:
Follow the steps below to solve the problem:
#graph #hash #mathematical #tree #node #node.js