3.4. Lab 3: Graphs

Due Date: 11:59pm February 6, 2024

Labs should be submitted as a single Jupyter notebook to CourseLink Dropbox

This lab counts for 4% of the final grade

To do in advance of this lab:

  • Make sure you have run the code in Allen Downey’s Chapter 2 notebook (we will review this in lecture)

3.4.1. Exercises

Complete the exercises corresponding to Exercise 2.1 – 2.4 in Think Complexity (version 2):

  1. There are a few short exercises embedded in Chapter 2 notebook. Complete these in-place.

  2. In Section 2.8 of the text, we analyzed the performance of reachable_nodes and classified it in \(O(n+m)\), where \(n\) is the number of nodes and \(m\) is the number of edges. Continuing the analysis, what is the order of growth for is_connected?

    def is_connected(G):
      start = next(G.nodes_iter())
      reachable = reachable_nodes(G, start)
      return len(reachable) == len(G)
    

You can indicate your answer directly in the notebook, using a Markdown cell with LaTeX.

  1. In Allen Downey’s implementation of reachable_nodes, you might be bothered by the apparent inefficiency of adding all neighbours to the stack without checking whether they are already in seen. Write a version of this function that checks the neighbours before adding them to the stack. Does this “optimization” change the order of growth? Does it make the functions faster?

  2. There are actually two kinds of ER graphs. The one we generated in Chapter 2, \(G(n,p)\), is characterized by two parameters, the number of nodes and the probability of an edge between the nodes.

    An alternative definition, denoted \(G(n,m)\), is also characterized by two parameters: the number of nodes, \(n\), and the number of edges, \(m\). Under this definition, the number of edges is fixed, but their location is random.

    Repeat the experiments we did in this chapter using this alternative definition. Here are a few suggestions for how to proceed:

    1. Write a function called m_pairs that takes a list of nodes and the number of edges, \(m\), and returns a random selection of \(m\) edges. A simple way to do that is to generate a list of all possible edges and use random.sample.

    2. Write a function called make_m_graph that takes \(n\) and \(m\) and returns a random graph with \(n\) nodes and \(m\) edges.

    3. Make a version of prob_connected that uses make_m_graph instead of make_random_graph.

    4. Compute the probability of connectivity for a range of values of \(m\).

    How do the results of this experiment compare to the results using the first type of ER graph?

3.4.2. Submission

Please submit a single Jupyter notebook which completes the above exercises, filling in Allen Downey’s Chapter 2 notebook. There are already placeholders in the notebook for your work.