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

Np Ms Office 365/Excel 2016 I Ntermed
1st Edition
ISBN:9781337508841
Author:Carey
Publisher:Carey
Chapter3: Performing Calculations With Formulas And Functions
Section: Chapter Questions
Problem 3.5CP
icon
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).

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()
Transcribed Image Text: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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning