Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions