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()
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...
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()](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F033c0d82-90fb-4c58-9b03-1325bfffdb8d%2Fba7d2b5a-132a-4f12-9a68-1a753063e237%2Fwyrgqed_processed.png&w=3840&q=75)
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

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

Recommended textbooks for you

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage

C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning

EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage

New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning

Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,

Systems Architecture
Computer Science
ISBN:
9781305080195
Author:
Stephen D. Burd
Publisher:
Cengage Learning