import numpy as np import matplotlib.pyplot as plt def random_matching (n): permutation = np.random. permutation (2 * n) matching = [(min (permutation [i], permutation [i + n]), max(permutation [i], permutation [i + n])) for i in range(n)] return matching def count_crossings (matching): crossings = 0 for i in range (len (matching)): for j in range(i + 1, len (matching)): if matching [i] [0] < matching [j] [0] < matching [i] [1] < matching [j] [1] or matching [j] [0] < matching[i][0] < matching [j] [1] < matching[i][1]: crossings += 1 return crossings def count_nestings (matching): nestings = 0 for i in range (len (matching)): for j in range(i + 1, len (matching)): if matching [i][0] < matching [j] [0] and matching [j] [1] < matching [i] [1] : nestings + 1 elif matching [j] [0] < matching [i] [0] and matching [i] [1] < matching [j] [1] : nestings += 1 return nestings # Generate data crossings_list = [] nestings_list = [] for in range(10): m = random_matching (100) crossings _list.append(count_crossings (m)) nestings_list.append(count_nestings(m)) # Create histograms fig, axs = plt.subplots(2, 1, figsize=(10, 10)) axs[0].hist (crossings_list, bins-10, color='blue', alpha=0.7, label='Crossings') axs[1].hist (nestings_list, bins-10, color='green', alpha=0.7, label='Nestings') axs[0].set_title('Histogram of Crossings') axs [1].set_title('Histogram of Nestings') for ax in axs: ax.set_xlabel('Count') ax.set_ylabel('Frequency') ax. legend() # Two-dimensional histogram plt.figure(figsize=(10, 8)) plt.hist2d(crossings_list, nestings_list, bins-10, cmap='Reds') plt.colorbar ( ) plt.xlabel('Crossings') plt.ylabel('Nestings') plt.title('Joint Distribution of Crossings and Nestings') plt.show()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Can you run the code and provide an interpretation to all the graphs.

import numpy as np
import matplotlib.pyplot as plt
def random_matching (n):
permutation = np.random. permutation (2 * n)
matching = [(min (permutation [i], permutation [i + n]), max(permutation [i],
permutation [i + n])) for i in range(n)]
return matching
def count_crossings (matching):
crossings = 0
for i in range (len (matching)):
for j in range(i + 1, len (matching)):
if matching [i] [0] < matching [j] [0] < matching [i] [1] < matching [j] [1] or
matching [j] [0] < matching[i][0] < matching [j] [1] < matching[i][1]:
crossings += 1
return crossings
def count_nestings (matching):
nestings = 0
for i in range (len (matching)):
for j in range(i + 1, len (matching)):
if matching [i][0] < matching [j] [0] and matching [j] [1] < matching [i] [1] :
nestings + 1
elif matching [j] [0] < matching [i] [0] and matching [i] [1] < matching [j] [1] :
nestings += 1
return nestings
# Generate data
crossings_list = []
nestings_list = []
for in range(10):
m = random_matching (100)
crossings _list.append(count_crossings (m))
nestings_list.append(count_nestings(m))
# Create histograms
fig, axs = plt.subplots(2, 1, figsize=(10, 10))
axs[0].hist (crossings_list, bins-10, color='blue', alpha=0.7, label='Crossings')
axs[1].hist (nestings_list, bins-10, color='green', alpha=0.7, label='Nestings')
axs[0].set_title('Histogram of Crossings')
axs [1].set_title('Histogram of Nestings')
for ax in axs:
ax.set_xlabel('Count')
ax.set_ylabel('Frequency')
ax. legend()
# Two-dimensional histogram
plt.figure(figsize=(10, 8))
plt.hist2d(crossings_list, nestings_list, bins-10, cmap='Reds')
plt.colorbar ( )
plt.xlabel('Crossings')
plt.ylabel('Nestings')
plt.title('Joint Distribution of Crossings and Nestings')
plt.show()
Transcribed Image Text:import numpy as np import matplotlib.pyplot as plt def random_matching (n): permutation = np.random. permutation (2 * n) matching = [(min (permutation [i], permutation [i + n]), max(permutation [i], permutation [i + n])) for i in range(n)] return matching def count_crossings (matching): crossings = 0 for i in range (len (matching)): for j in range(i + 1, len (matching)): if matching [i] [0] < matching [j] [0] < matching [i] [1] < matching [j] [1] or matching [j] [0] < matching[i][0] < matching [j] [1] < matching[i][1]: crossings += 1 return crossings def count_nestings (matching): nestings = 0 for i in range (len (matching)): for j in range(i + 1, len (matching)): if matching [i][0] < matching [j] [0] and matching [j] [1] < matching [i] [1] : nestings + 1 elif matching [j] [0] < matching [i] [0] and matching [i] [1] < matching [j] [1] : nestings += 1 return nestings # Generate data crossings_list = [] nestings_list = [] for in range(10): m = random_matching (100) crossings _list.append(count_crossings (m)) nestings_list.append(count_nestings(m)) # Create histograms fig, axs = plt.subplots(2, 1, figsize=(10, 10)) axs[0].hist (crossings_list, bins-10, color='blue', alpha=0.7, label='Crossings') axs[1].hist (nestings_list, bins-10, color='green', alpha=0.7, label='Nestings') axs[0].set_title('Histogram of Crossings') axs [1].set_title('Histogram of Nestings') for ax in axs: ax.set_xlabel('Count') ax.set_ylabel('Frequency') ax. legend() # Two-dimensional histogram plt.figure(figsize=(10, 8)) plt.hist2d(crossings_list, nestings_list, bins-10, cmap='Reds') plt.colorbar ( ) plt.xlabel('Crossings') plt.ylabel('Nestings') plt.title('Joint Distribution of Crossings and Nestings') plt.show()
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education