Find same contacts in a list of contacts | Structure for storing contact details . ; A utility function to fill entries in adjacency matrix representation of graph ; Initialize the adjacency matrix ; Traverse through all contacts ; Add mat from i to j and vice versa , if possible . Since length of each contact field is at max some constant . ( say 30 ) so body execution of this for loop takes constant time . ; A recuesive function to perform DFS with vertex i as source ; Finds similar contacrs in an array of contacts ; vector for storing the solution ; Declare 2D adjaceny matrix for mats ; visited array to keep track of visited nodes ; Fill adjacency matrix ; Since , we made a graph with contacts as nodes with fields as links . Two nodes are linked if they represent the same person . So , total number of connected components and nodes in each component will be our answer . ; Add delimeter to separate nodes of one component from other . ; Print the solution ; Driver Code
class contact : NEW_LINE INDENT def __init__ ( self , field1 , field2 , field3 ) : NEW_LINE INDENT self . field1 = field1 NEW_LINE self . field2 = field2 NEW_LINE self . field3 = field3 NEW_LINE DEDENT DEDENT def buildGraph ( arr , n , mat ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT mat [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] . field1 == arr [ j ] . field1 or arr [ i ] . field1 == arr [ j ] . field2 or arr [ i ] . field1 == arr [ j ] . field3 or arr [ i ] . field2 == arr [ j ] . field1 or arr [ i ] . field2 == arr [ j ] . field2 or arr [ i ] . field2 == arr [ j ] . field3 or arr [ i ] . field3 == arr [ j ] . field1 or arr [ i ] . field3 == arr [ j ] . field2 or arr [ i ] . field3 == arr [ j ] . field3 ) : NEW_LINE INDENT mat [ i ] [ j ] = 1 NEW_LINE mat [ j ] [ i ] = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT def DFSvisit ( i , mat , visited , sol , n ) : NEW_LINE INDENT visited [ i ] = True NEW_LINE sol . append ( i ) NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( mat [ i ] [ j ] and not visited [ j ] ) : NEW_LINE INDENT DFSvisit ( j , mat , visited , sol , n ) NEW_LINE DEDENT DEDENT DEDENT def findSameContacts ( arr , n ) : NEW_LINE INDENT sol = [ ] NEW_LINE mat = [ [ None ] * n for i in range ( n ) ] NEW_LINE visited = [ 0 ] * n NEW_LINE buildGraph ( arr , n , mat ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT DFSvisit ( i , mat , visited , sol , n ) NEW_LINE sol . append ( - 1 ) NEW_LINE DEDENT DEDENT for i in range ( len ( sol ) ) : NEW_LINE INDENT if ( sol [ i ] == - 1 ) : NEW_LINE INDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( sol [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ contact ( " Gaurav " , " gaurav @ gmail . com " , " gaurav @ gfgQA . com " ) , contact ( " Lucky " , " lucky @ gmail . com " , " + 1234567" ) , contact ( " gaurav123" , " + 5412312" , " gaurav123 @ skype . com " ) , contact ( " gaurav1993" , " + 5412312" , " gaurav @ gfgQA . com " ) , contact ( " raja " , " + 2231210" , " raja @ gfg . com " ) , contact ( " bahubali " , " + 878312" , " raja " ) ] NEW_LINE n = len ( arr ) NEW_LINE findSameContacts ( arr , n ) NEW_LINE DEDENT