Find if possible to visit every nodes in given Graph exactly once based on given conditions
Given a Graph having N+1 nodes with 2*N-1 edges and a boolean array arr[ ], the task is to find if one can visit every nodes exactly once and print any one possible path. It’s also known that N-1 edges go i-th to (i+1)-th node and rest edges go i-th to (n+1)-th node if arr[i] is 0 otherwise (n+1)-th to i-th.
Examples:
Input: N = 3, arr = {0, 1, 0}
Output: [1, 2, 3, 4]
Explanation:
from the given data, the edges will:
1 -> 2
2 -> 3
1 -> 4
4 -> 2
3 -> 4
Therefore, one can follow the path: 1 -> 2 -> 3 -> 4
Input: N = 4, arr = {0, 1, 1, 1}
Output: [1, 4, 2, 3]
Approach: This problem can be solved using greedy approach with some observations. The observations are given below:
- If arr[1] is 1, then follow the path [N+1 -> 1 -> 2……..-> N].
- If arr[N] is 0, then follow the path [1 -> 2 ->……….-> N -> N+1].
- If arr[1] is 0 and arr[N] is 1, there must exists an integer i (1 ≤ i< N) where arr[i] =0 and arr[i+1] = 1, then the path [1 -> 2 -> ⋯ -> i -> N+1 -> i+1 -> i+2 -> ⋯N] is valid.
- Previous step proves that there always exists an Hamiltonian path in this graph.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findpath( int N, int a[])
{
if (a[0]) {
cout << " " << N + 1;
for ( int i = 1; i <= N; i++)
cout << " " << i;
return ;
}
for ( int i = 0; i < N - 1; i++) {
if (!a[i] && a[i + 1]) {
for ( int j = 1; j <= i; j++)
cout << " " << j;
cout << " " << N + 1;
for ( int j = i + 1; j <= N; j++)
cout << " " << j;
return ;
}
}
for ( int i = 1; i <= N; i++)
cout << " " << i;
cout << " " << N + 1;
}
int main()
{
int N = 3, arr[] = { 0, 1, 0 };
findpath(N, arr);
}
|
C
#include <bits/stdc++.h>
using namespace std;
void findpath( int N, int a[])
{
if (a[0]) {
printf ( "%d " , N + 1);
for ( int i = 1; i <= N; i++)
printf ( "%d " , i);
return ;
}
for ( int i = 0; i < N - 1; i++) {
if (!a[i] && a[i + 1]) {
for ( int j = 1; j <= i; j++)
printf ( "%d " , j);
printf ( "%d " , N + 1);
for ( int j = i + 1; j <= N; j++)
printf ( "%d " , j);
return ;
}
}
for ( int i = 1; i <= N; i++)
printf ( "%d " , i);
printf ( "%d " , N + 1);
}
int main()
{
int N = 3, arr[] = { 0, 1, 0 };
findpath(N, arr);
}
|
Java
import java.io.*;
class GFG {
static void findpath( int N, int a[])
{
if (a[ 0 ] == 1 ) {
System.out.print((N + 1 ) + " " );
for ( int i = 1 ; i <= N; i++)
System.out.print((i) + " " );
return ;
}
for ( int i = 0 ; i < N - 1 ; i++) {
if (a[i] == 0 && a[i + 1 ] == 1 ) {
for ( int j = 1 ; j <= i; j++)
System.out.print((j) + " " );
System.out.print((N + 1 ) + " " );
for ( int j = i + 1 ; j <= N; j++)
System.out.print((j) + " " );
return ;
}
}
for ( int i = 1 ; i <= N; i++)
System.out.print((i) + " " );
System.out.print((N + 1 ) + " " );
}
public static void main(String[] args)
{
int N = 3 , arr[] = { 0 , 1 , 0 };
findpath(N, arr);
}
}
|
Python3
def findpath(N, a):
if (a[ 0 ]):
print (N + 1 )
for i in range ( 1 ,N + 1 , 1 ):
print (i,end = " " )
return
for i in range (N - 1 ):
if (a[i] = = 0 and a[i + 1 ]):
for j in range ( 1 ,i + 1 , 1 ):
print (j,end = " " )
print (N + 1 ,end = " " );
for j in range (i + 1 ,N + 1 , 1 ):
print (j,end = " " )
return
for i in range ( 1 ,N + 1 , 1 ):
print (i,end = " " )
print (N + 1 ,end = " " )
if __name__ = = '__main__' :
N = 3
arr = [ 0 , 1 , 0 ]
findpath(N, arr)
|
C#
using System;
class GFG{
static void findpath( int N, int []a)
{
if (a[0]!=0) {
Console.Write(N + 1);
for ( int i = 1; i <= N; i++)
Console.Write(i+ " " );
return ;
}
for ( int i = 0; i < N - 1; i++) {
if (a[i]==0 && a[i + 1] !=0) {
for ( int j = 1; j <= i; j++)
Console.Write(j+ " " );
Console.Write(N + 1 + " " );
for ( int j = i + 1; j <= N; j++)
Console.Write(j + " " );
return ;
}
}
for ( int i = 1; i <= N; i++)
Console.Write(i+ " " );
Console.Write(N + 1 + " " );
}
public static void Main()
{
int N = 3;
int []arr = { 0, 1, 0 };
findpath(N, arr);
}
}
|
Javascript
<script>
function findpath(N, a) {
if (a[0]) {
document.write(N + 1);
for (let i = 1; i <= N; i++)
document.write(i);
return ;
}
for (let i = 0; i < N - 1; i++) {
if (!a[i] && a[i + 1]) {
for (let j = 1; j <= i; j++)
document.write(j+ " " );
document.write(N + 1+ " " );
for (let j = i + 1; j <= N; j++)
document.write(j+ " " );
return ;
}
}
for (let i = 1; i <= N; i++)
document.write(i+ " " );
document.write(N + 1+ " " );
}
let N = 3, arr = [0, 1, 0];
findpath(N, arr);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
13 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...