Let's examine a somewhat intricate situation where we have the following array of friendships: friends = [ [ 'A' , 'B' ] , [ 'A' , 'C' ] , [ 'B' , 'D' ] , [ 'C' , 'D' ] , [ 'D' , 'E' ] , [ 'C' , 'F' ] , [ 'E' , 'F' ] , [ 'E' , 'G' ] , [ 'F' , 'H' ] ] We will utilize the same function as previously, but we will enhance it with a nested loop to determine all pairs of friends. def degrees_of_friendship ( friends ) : graph = { f : [ ] for sub in friends for f in sub } for f1 , f2 in friends : graph [ f1 ] . append ( f2 ) graph [ f2 ] . append ( f1 ) def BFS ( graph , start , end ) : visited = set ( ) queue = [ ("("span>start , [ start )] ) ] while queue : ( vertex , path ) = queue . pop ( 0 ) if vertex not in visited : if vertex == end : return len ( path ) - 1 visited . add ( vertex ) for neighbor in graph [ vertex ] : queue . append ( ( neighbor , path + [ neighbor ]) ) all_friends = list ( graph . keys ( ) ) matrix = [ ] for f1 in all_friends : row = [ ] for f2 in all_friends : row . append ( BFS ( graph , f1 , f2 ) ) matrix . append ( row ) return matrix This function will produce a matrix indicating the degree of friendship among all friends.