import timeit import random import matplotlib.pyplot as plt # Function to count crossings and nestings def count_crossings_and_nestings (matching): # Your implementation of count_crossings_and_nestings goes here pass # Function to generate random matching def random_matching (length): # Your implementation of random_matching goes here pass # Measure the average run times against the lengths of the matchings x = [n for n in range(1, 51)] # 50 arcs y_matching = [ ] for length in x: avg_runtime = timeit.timeit(lambda: count_crossings_and_nestings (random_matching (length)), number=1000) / 1000 y_matching.append(avg_runtime) # Calculate triangle numbers x_triangle = [j for j in range(1, 51)] y_triangle = [(m ** 2 + m) / 2 for m in range(1, 51)] # Plot both on the same plot plt.plot(x, y_matching, label="Average run times") plt.plot(x_triangle, y_triangle, label="Triangle numbers") plt.xlabel("Length of Matchings / Number of arcs") plt.ylabel("Time / Triangle numbers") plt.title("Average run times vs. Triangle numbers") plt. legend() plt.grid(True) plt.show() Time / Triangle numbers 1200 1000 800 600 400 200 Average run times vs. Triangle numbers Average run times Triangle numbers 0 10 20 30 40 Length of Matchings / Number of arcs 550
import timeit import random import matplotlib.pyplot as plt # Function to count crossings and nestings def count_crossings_and_nestings (matching): # Your implementation of count_crossings_and_nestings goes here pass # Function to generate random matching def random_matching (length): # Your implementation of random_matching goes here pass # Measure the average run times against the lengths of the matchings x = [n for n in range(1, 51)] # 50 arcs y_matching = [ ] for length in x: avg_runtime = timeit.timeit(lambda: count_crossings_and_nestings (random_matching (length)), number=1000) / 1000 y_matching.append(avg_runtime) # Calculate triangle numbers x_triangle = [j for j in range(1, 51)] y_triangle = [(m ** 2 + m) / 2 for m in range(1, 51)] # Plot both on the same plot plt.plot(x, y_matching, label="Average run times") plt.plot(x_triangle, y_triangle, label="Triangle numbers") plt.xlabel("Length of Matchings / Number of arcs") plt.ylabel("Time / Triangle numbers") plt.title("Average run times vs. Triangle numbers") plt. legend() plt.grid(True) plt.show() Time / Triangle numbers 1200 1000 800 600 400 200 Average run times vs. Triangle numbers Average run times Triangle numbers 0 10 20 30 40 Length of Matchings / Number of arcs 550
Chapter3: Performing Calculations With Formulas And Functions
Section: Chapter Questions
Problem 3.5CP
Related questions
Question
Question: The module timeit allows you to compute the time a function call takes. Verify your answer about the time complexity by computing the run time for randomly chosen arc diagrams of suitable sizes and producing an appropriate plot of the average run times against the lengths of the matchings.
Can you help me to check my code and also can you provide an interpretation to my results(the graph).
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps with 1 images
Similar questions
- Recommended textbooks for youNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE LProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningNp Ms Office 365/Excel 2016 I NtermedComputer ScienceISBN:9781337508841Author:CareyPublisher:CengageCOMPREHENSIVE MICROSOFT OFFICE 365 EXCEComputer ScienceISBN:9780357392676Author:FREUND, StevenPublisher:CENGAGE LProgramming with Microsoft Visual Basic 2017Computer ScienceISBN:9781337102124Author:Diane ZakPublisher:Cengage LearningSystems ArchitectureComputer ScienceISBN:9781305080195Author:Stephen D. BurdPublisher:Cengage Learning