Question: Use Python and NetworkX. Write a function mutual_friends that takes a graph and two nodes as arguments, and returns a list (or set) of nodes

Use Python and NetworkX.

Write a function mutual_friends that takes a graph and two nodes as arguments, and returns a list (or set) of nodes that are linked to both given nodes. For example, in the graph SG drawn above,

mutual_friends(SG, 'Alice', 'Claire') == ['Frank']

an empty list or set should be returned in the case where two nodes have no mutual friends, e.g. George and Bob in SG drawn above.

Code:

def mutual_friends(G, node_1, node_2):

-

-

SG = nx.read_adjlist('../datasets/friends.adjlist') assert mutual_friends(SG, 'Alice', 'Claire') == ['Frank'] assert mutual_friends(SG, 'George', 'Bob') == [] assert sorted(mutual_friends(SG, 'Claire', 'George')) == ['Dennis', 'Frank']

*************************************************************************************

friends.adjlist = It's a plain text file

George Frank Dennis Dennis Claire Esther Claire Frank Alice Esther Bob Alice Frank Alice Alice Esther Shelly

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!