def count_crossings_and_nestings(arcs): Count the number of crossings and nestings between a set of arcs. Parameters: arcs (list): A list of tuples representing arcs. Each tuple contains two integers representing the start and end points of an arc. Returns: tuple: A tuple containing the number of crossings and nestings. || || || crossings = 0 nestings = 0 num_arcs = len(arcs) # Get the number of arcs using the len() function for i in range(num_arcs): for j in range(i+1, num_arcs): arci_start, arci_end = arcs [i] arcj_start, arcj_end: = arcs [j] # Check for crossings if arci_start < arcj_start < arci_end < arcj_end: crossings += 1 elif arcj_start < arci_start < arcj_end < arci_end: crossings += 1 # Check for nestings elif arci_start < arcj_start < arcj_end < arci_end: nestings += 1 elif arcj_start < arci_start < arci_end < arcj_end: nestings += 1 return crossings, nestings # Test the function arcs = [(1, 4), (0, 6), (3, 5), (2, 9), (7, 8)] crossings, nestings = count_crossings_and_nestings (arcs) print (f"Number of crossings: {crossings}") # Expected: 1 print (f"Number of nestings: {nestings}") Number of crossings: 3 Number of nestings: 4 # Expected: 3

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter9: Advanced Array Concepts
Section: Chapter Questions
Problem 2PE
icon
Related questions
Question
100%

Look at my code, What can you say about the computational complexity of count_crossings_and_nestings? Discuss how run time and memory requirement grow as the number n of arcs increases. Give reasons.

def count_crossings_and_nestings(arcs):
Count the number of crossings and nestings between a set of arcs.
Parameters:
arcs (list): A list of tuples representing arcs. Each tuple contains two integers representing the start and end points of an arc.
Returns:
tuple: A tuple containing the number of crossings and nestings.
|| || ||
crossings = 0
nestings
= 0
num_arcs = len(arcs) # Get the number of arcs using the len() function
for i in range(num_arcs):
for j in range(i+1, num_arcs):
arci_start, arci_end = arcs [i]
arcj_start, arcj_end: = arcs [j]
# Check for crossings
if arci_start < arcj_start < arci_end < arcj_end:
crossings += 1
elif arcj_start < arci_start < arcj_end < arci_end:
crossings += 1
# Check for nestings
elif arci_start < arcj_start < arcj_end < arci_end:
nestings += 1
elif arcj_start < arci_start < arci_end < arcj_end:
nestings += 1
return crossings, nestings
# Test the function
arcs = [(1, 4), (0, 6), (3, 5), (2, 9), (7, 8)]
crossings, nestings = count_crossings_and_nestings (arcs)
print (f"Number of crossings: {crossings}") # Expected: 1
print (f"Number of nestings: {nestings}")
Number of crossings: 3
Number of nestings: 4
# Expected: 3
Transcribed Image Text:def count_crossings_and_nestings(arcs): Count the number of crossings and nestings between a set of arcs. Parameters: arcs (list): A list of tuples representing arcs. Each tuple contains two integers representing the start and end points of an arc. Returns: tuple: A tuple containing the number of crossings and nestings. || || || crossings = 0 nestings = 0 num_arcs = len(arcs) # Get the number of arcs using the len() function for i in range(num_arcs): for j in range(i+1, num_arcs): arci_start, arci_end = arcs [i] arcj_start, arcj_end: = arcs [j] # Check for crossings if arci_start < arcj_start < arci_end < arcj_end: crossings += 1 elif arcj_start < arci_start < arcj_end < arci_end: crossings += 1 # Check for nestings elif arci_start < arcj_start < arcj_end < arci_end: nestings += 1 elif arcj_start < arci_start < arci_end < arcj_end: nestings += 1 return crossings, nestings # Test the function arcs = [(1, 4), (0, 6), (3, 5), (2, 9), (7, 8)] crossings, nestings = count_crossings_and_nestings (arcs) print (f"Number of crossings: {crossings}") # Expected: 1 print (f"Number of nestings: {nestings}") Number of crossings: 3 Number of nestings: 4 # Expected: 3
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT