Find palindromic path of given length K in a complete Binary Weighted Graph | C ++ implementation for the above approach ; Function to print the left path ; j -> i -> j -> i -> j -> k -> j -> k -> j ; i -> j -> i -> j -> k -> j -> k ; Function to print the right path ; j -> i -> j -> i -> j -> k -> j -> k -> j ; i -> j -> i -> j -> k -> j -> k ; Function to check that if there exists a palindromic path in a binary graoh ; Create adjacency matrix ; If K is odd then print the path directly by choosing node 1 and 2 repeatedly ; If K is even Try to find an edge such that weight of edge i -> j and j -> i is equal ; Same weight edges are found ; Store their indexes ; Print the path ; If nodes i , j having equal weight on edges i -> j and j -> i can not be found then try to find three nodes i , j , k such that weights of edges i -> j and j -> k are equal ; To store edges with weight '0' ; To store edges with weight '1' ; Try to find edges i -> j and j -> k having weight 0 ; Print left Path ; Print centre ; Print right path ; Try to find edges i -> j and j -> k which having weight 1 ; cout << k ; ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printLeftPath ( int i , int j , int K ) { if ( K & 1 ) { for ( int p = 0 ; p < K ; p ++ ) { if ( p & 1 ) { cout << i << " ▁ " ; } else { cout << j << " ▁ " ; } } } else { for ( int p = 0 ; p < K ; p ++ ) { if ( p & 1 ) { cout << j << " ▁ " ; } else { cout << i << " ▁ " ; } } } } void printRightPath ( int j , int k , int K ) { if ( K & 1 ) { for ( int p = 0 ; p < K ; p ++ ) { if ( p & 1 ) { cout << k << " ▁ " ; } else { cout << j << " ▁ " ; } } } else { for ( int p = 0 ; p < K ; p ++ ) { if ( p & 1 ) { cout << k << " ▁ " ; } else { cout << j << " ▁ " ; } } } } void constructPalindromicPath ( vector < pair < pair < int , int > , char > > edges , int n , int K ) { vector < vector < char > > adj ( n + 1 , vector < char > ( n + 1 ) ) ; for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { adj [ edges [ i ] . first . first ] [ edges [ i ] . first . second ] = edges [ i ] . second ; } if ( K & 1 ) { cout << " YES " << endl ; for ( int i = 1 ; i <= K + 1 ; i ++ ) { cout << ( i & 1 ) + 1 << " ▁ " ; } return ; } bool found = 0 ; int idx1 , idx2 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i == j ) { continue ; } if ( adj [ i ] [ j ] == adj [ j ] [ i ] ) { found = 1 ; idx1 = i , idx2 = j ; } } } if ( found ) { cout << " YES " << endl ; for ( int i = 1 ; i <= K + 1 ; i ++ ) { if ( i & 1 ) { cout << idx1 << " ▁ " ; } else { cout << idx2 << " ▁ " ; } } return ; } else { vector < int > mp1 [ n + 1 ] ; vector < int > mp2 [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i == j ) { continue ; } if ( adj [ i ] [ j ] == '0' ) { mp1 [ i ] . push_back ( j ) ; } else { mp2 [ i ] . push_back ( j ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j == i ) { continue ; } if ( adj [ i ] [ j ] == '0' ) { if ( mp1 [ j ] . size ( ) ) { int k = mp1 [ j ] [ 0 ] ; if ( k == i k == j ) { continue ; } cout << " YES " << endl ; K -= 2 ; K /= 2 ; printLeftPath ( i , j , K ) ; cout << i << " ▁ " << j << " ▁ " << k << " ▁ " ; printRightPath ( j , k , K ) ; return ; } } } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j == i ) { continue ; } if ( adj [ i ] [ j ] == '1' ) { if ( mp1 [ j ] . size ( ) ) { int k = mp1 [ j ] [ 0 ] ; if ( k == i k == j ) { continue ; } cout << " YES " << endl ; K -= 2 ; K /= 2 ; printLeftPath ( i , j , K ) ; cout << i << " ▁ " << j << " ▁ " << k << " ▁ " ; printRightPath ( j , k , K ) ; return ; } } } } } cout << " NO " ; } int main ( ) { int N = 3 , K = 4 ; vector < pair < pair < int , int > , char > > edges = { { { 1 , 2 } , '1' } , { { 1 , 3 } , '1' } , { { 2 , 1 } , '1' } , { { 2 , 3 } , '1' } , { { 3 , 1 } , '0' } , { { 3 , 2 } , '0' } } ; constructPalindromicPath ( edges , N , K ) ; }