Given a connected graph with N vertices. The task is to select k(k must be less than or equals to n/2, not necessarily minimum) vertices from the graph such that all these selected vertices are connected to at least one of the non selected vertex. In case of multiple answers print any one of them.
Examples:
Input :
Output : 1 Vertex 1 is connected to all other non selected vertices. Here {1, 2}, {2, 3}, {3, 4}, {1, 3}, {1, 4}, {2, 4} are also the valid answers
Input :
Output : 1 3 Vertex 1, 3 are connected to all other non selected vertices. {2, 4} is also a valid answer.
Efficient Approach : An efficient way is to find vertices which are even level and odd level using simple dfs or bfs function. Then if the vertices at odd level are less than the vertices at even level then print odd level vertices. Otherwise, print even level vertices.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define N 200005
int n, m, vis[N];
vector<int> gr[N];
vector<int> v[2];
void add_edges(int x, int y)
{
gr[x].push_back(y);
gr[y].push_back(x);
}
void dfs(int x, int state)
{
v[state].push_back(x);
vis[x] = 1;
for (auto i : gr[x])
if (vis[i] == 0)
dfs(i, state ^ 1);
}
void Print_vertices()
{
if (v[0].size() < v[1].size()) {
for (auto i : v[0])
cout << i << " ";
}
else {
for (auto i : v[1])
cout << i << " ";
}
}
int main()
{
int n = 4, m = 3;
add_edges(1, 2);
add_edges(2, 3);
add_edges(3, 4);
dfs(1, 0);
Print_vertices();
return 0;
}
Java
import java.util.*;
class GFG
{
static final int N = 200005;
static int n, m;
static int[] vis = new int[N];
static Vector<Integer>[] gr = new Vector[N];
static Vector<Integer>[] v = new Vector[2];
static void add_edges(int x, int y)
{
gr[x].add(y);
gr[y].add(x);
}
static void dfs(int x, int state)
{
v[state].add(x);
vis[x] = 1;
for (int i : gr[x])
{
if (vis[i] == 0)
{
dfs(i, state ^ 1);
}
}
}
static void Print_vertices()
{
if (v[0].size() < v[1].size())
{
for (int i : v[0])
{
System.out.print(i + " ");
}
}
else
{
for (int i : v[1])
{
System.out.print(i + " ");
}
}
}
public static void main(String[] args)
{
n = 4;
m = 3;
for (int i = 0; i < N; i++)
{
gr[i] = new Vector<Integer>();
}
for (int i = 0; i < 2; i++)
{
v[i] = new Vector<Integer>();
}
add_edges(1, 2);
add_edges(2, 3);
add_edges(3, 4);
dfs(1, 0);
Print_vertices();
}
}
Python3
N = 200005
n, m, =0,0
vis=[0 for i in range(N)]
gr=[[] for i in range(N)]
v=[[] for i in range(2)]
def add_edges(x, y):
gr[x].append(y)
gr[y].append(x)
def dfs(x, state):
v[state].append(x)
vis[x] = 1
for i in gr[x]:
if (vis[i] == 0):
dfs(i, state ^ 1)
def Print_vertices():
if (len(v[0]) < len(v[1])):
for i in v[0]:
print(i,end=" ")
else:
for i in v[1]:
print(i,end=" ")
n = 4
m = 3
add_edges(1, 2)
add_edges(2, 3)
add_edges(3, 4)
dfs(1, 0)
Print_vertices()
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int N = 200005;
static int n, m;
static int[] vis = new int[N];
static List<int>[] gr = new List<int>[N];
static List<int>[] v = new List<int>[2];
static void add_edges(int x, int y)
{
gr[x].Add(y);
gr[y].Add(x);
}
static void dfs(int x, int state)
{
v[state].Add(x);
vis[x] = 1;
foreach (int i in gr[x])
{
if (vis[i] == 0)
{
dfs(i, state ^ 1);
}
}
}
static void Print_vertices()
{
if (v[0].Count < v[1].Count)
{
foreach (int i in v[0])
{
Console.Write(i + " ");
}
}
else
{
foreach (int i in v[1])
{
Console.Write(i + " ");
}
}
}
public static void Main(String[] args)
{
n = 4;
m = 3;
for (int i = 0; i < N; i++)
{
gr[i] = new List<int>();
}
for (int i = 0; i < 2; i++)
{
v[i] = new List<int>();
}
add_edges(1, 2);
add_edges(2, 3);
add_edges(3, 4);
dfs(1, 0);
Print_vertices();
}
}
Javascript
<script>
let N = 200005;
let n, m;
let vis = new Array(N);
for(let i = 0; i < N; i++)
{
vis[i] = 0;
}
let gr = new Array(N);
let v = new Array(2);
function add_edges(x, y)
{
gr[x].push(y);
gr[y].push(x);
}
function dfs(x, state)
{
v[state].push(x);
vis[x] = 1;
for(let i = 0; i < gr[x].length; i++)
{
if (vis[gr[x][i]] == 0)
{
dfs(gr[x][i], (state ^ 1));
}
}
}
function Print_vertices()
{
if (v[0].length < v[1].length)
{
for(let i = 0; i < v[0].length; i++)
{
document.write(v[0][i] + " ");
}
}
else
{
for(let i = 0; i < v[1].length; i++)
{
document.write(v[1][i] + " ");
}
}
}
n = 4;
m = 3;
for(let i = 0; i < N; i++)
{
gr[i] = [];
}
for(let i = 0; i < 2; i++)
{
v[i] = [];
}
add_edges(1, 2);
add_edges(2, 3);
add_edges(3, 4);
dfs(1, 0);
Print_vertices();
</script>
PHP
<?php
class GFG {
const N = 200005;
public $n, $m;
public $vis = array();
public $gr = array();
public $v = array();
public function __construct() {
for ($i = 0; $i < self::N; $i++) {
$this->gr[$i] = array();
}
for ($i = 0; $i < 2; $i++) {
$this->v[$i] = array();
}
}
public function add_edges($x, $y) {
array_push($this->gr[$x], $y);
array_push($this->gr[$y], $x);
}
public function dfs($x, $state) {
array_push($this->v[$state], $x);
$this->vis[$x] = 1;
foreach ($this->gr[$x] as $i) {
if ($this->vis[$i] == 0) {
$this->dfs($i, $state ^ 1);
}
}
}
public function Print_vertices() {
if (count($this->v[0]) < count($this->v[1])) {
foreach ($this->v[0] as $i) {
echo $i . " ";
}
}
else {
foreach ($this->v[1] as $i) {
echo $i . " ";
}
}
}
public function main() {
$this->n = 4;
$this->m = 3;
$this->add_edges(1, 2);
$this->add_edges(2, 3);
$this->add_edges(3, 4);
$this->dfs(1, 0);
$this->Print_vertices();
}
}
$gfg = new GFG();
$gfg->main();
?>
Time Complexity : O(V+E) Where V is the number of vertices and E is the number of edges in the graph.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
22 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...