Generate an array from given pairs of adjacent elements | Utility function to find original array ; Map to store all neighbors for each element ; Vector to store original elements ; Stotrs which array elements are visited ; A djacency list to store neighbors of each array element ; Find the first corner element ; Stores first element of the original array ; Push it into the original array ; Mark as visited ; Traversing the neighbors and check if the elements are visited or not ; Traverse adjacent elements ; If element is not visited ; Push it into res ; Mark as visited ; Update the next adjacent ; Print original array ; Driver Code ; Given pairs of adjacent elements
def find_original_array ( A ) : NEW_LINE INDENT mp = [ [ ] for i in range ( 6 ) ] NEW_LINE res = [ ] NEW_LINE visited = { } NEW_LINE for it in A : NEW_LINE INDENT mp [ it [ 0 ] ] . append ( it [ 1 ] ) NEW_LINE mp [ it [ 1 ] ] . append ( it [ 0 ] ) NEW_LINE DEDENT start = 0 NEW_LINE for it in range ( 6 ) : NEW_LINE INDENT if ( len ( mp [ it ] ) == 1 ) : NEW_LINE INDENT start = it + 3 NEW_LINE break NEW_LINE DEDENT DEDENT adjacent = start NEW_LINE res . append ( start ) NEW_LINE visited [ start ] = True NEW_LINE while ( len ( res ) != len ( A ) + 1 ) : NEW_LINE INDENT for elements in mp [ adjacent ] : NEW_LINE INDENT if ( elements not in visited ) : NEW_LINE INDENT res . append ( elements ) NEW_LINE visited [ elements ] = True NEW_LINE adjacent = elements NEW_LINE DEDENT DEDENT DEDENT print ( * res ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 5 , 1 ] , [ 3 , 4 ] , [ 3 , 5 ] ] NEW_LINE find_original_array ( A ) NEW_LINE DEDENT