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()

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
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
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Systems Architecture
Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning