lab_networkx

Neat Networks with NetworkX

Due: May 08, 23:59 PM

Learning Objectives

  • Practice using the NetworkX package on real-world problems

Submission Instructions

Using the Prairielearn workspace, test and save your solution to the following exercises. (You are welcome to download and work on the files locally but they must be re-uploaded or copied over to the workspace for submission).

Assignment Description

This lab is a ‘bonus’ lab for those interested in either replacing a lab you did not do well in earlier in the semester or simply want to have some practice using NetworkX on example datasets.

The lab is two separate short exercises. The first exercise focuses on data parsing and the creation of a weighted directed graph:

  • build_flight_graph
  • fastest_flight_path

The second exercise begins with a provided graph where each node and edge contains multiple distinct properties:

  • get_atom_set
  • get_bond_set
  • get_mol_weight
  • mol_filter

Part 1: FlightPaths

build_flight_graph()

# INPUT:
# filename: a string representing the path to a CSV file.
#   Each line in the file is in the format: origin,destination,duration
# OUTPUT:
# A networkx.DiGraph representing the flight network with durations as edge weights.
#   These weights should be stored as metadata for edges with the name 'weight'
#   Weights should also be stored as floats, not strings.
def build_flight_graph(filename):

Given a CSV input containing a directed, weighted flightpaths dataset, build a NetworkX graph representation of this dataset.

fastest_flight_path()

# INPUT:
# G: a weighted directed graph returned by build_flight_graph
# origin: a string representing the starting airport
# destination: a string representing the target airport
# OUTPUT:
# A list of airport codes representing the shortest-duration flight path.
# If no path exists, return an empty list.

def fastest_flight_path(G, origin, destination)

Given a NetworkX graph and two labels (origin and destination) find the shortest path from origin to destination. You should use the built-in networkX shortest path function but need to handle cases where there is no valid path from origin to destination.

NOTE: The built-in function when given an origin and destination will produce an error if there is no valid path.

Part 2: Molecular Graph

get_atom_set()

# Function returns a set of atoms that appear in the molecule
# Input: 
# G (graph): molecular graph with nodes as atoms and edges as bonds between atoms
# Output:
# A set containing all the different atoms that appear in the given graph
def get_atom_set(G):

Given a NetworkX graph, return a set containing all atom types in the graph.

get_bond_set()

# Function returns a set of bond types that appear in the molecule
# Input: 
# G (graph): molecular graph with nodes as atoms and edges as bonds between atoms
# Output:
# A set of different bond types that appear in the given graph
def get_bond_set(G):

Given a NetworkX graph, return a set containing all bond types in the graph.

get_mol_weight()

# Input: 
# G (graph): molecular graph with nodes as atoms and edges as bonds between atoms
# Output:
# The molecular weight as a float
def get_mol_weight(G):

Given a NetworkX graph, calculate the molecular weight of the entire graph. Each node has an ‘atomic mass’ as a float.

mol_filter()

# Takes a list of molecular graphs as input and removes all molecules with a molecular weight greater than 300.
# Input:
# dataset: a list of molecular graphs
# Output:
# a new list of molecular graphs with a molecular weight less than 300.
def mol_filter(dataset):

Given a collection (a list) of NetworkX graphs, return a list containing only those graphs whose molecular weight is above a constant (in this case 300).