CS21003 Algorithms I
 CS29003 Algorithms Laboratory
Autumn 2012, L-T-P: 3-1-0 
L-T-P: 0-0-3 

Programming Assignment 2

You are given n closed subintervals [ai,bi] of the real interval [0,1]. Your task is to locate the leftmost region of maximum overlap of the given intervals. The following figure illustrates this problem for ten intervals. In this example, the maximum overlap size is five, and this is achieved twice (shaded by green and pink). Only the leftmost region needs to be reported.

Write a program to do the following. First, read the number n of intervals from the user. Generate n subintervals of [0,1] with the two endpoints chosen uniformly randomly in the interval [0,1]. Report the following three items.

Your program should run in O(n log n) time in the worst case, and be as efficient as possible.

Notes

Consider the following points before you start coding.

Sample output

The above figure is drawn from the following randomly generated data.
   How many intervals? 10
   Interval      0 = [0.784384,0.962058]
   Interval      1 = [0.249457,0.473880]
   Interval      2 = [0.077236,0.294932]
   Interval      3 = [0.391598,0.686297]
   Interval      4 = [0.361750,0.647535]
   Interval      5 = [0.521847,0.989767]
   Interval      6 = [0.145509,0.249701]
   Interval      7 = [0.026383,0.145308]
   Interval      8 = [0.086777,0.577941]
   Interval      9 = [0.293653,0.925077]
   Leftmost maximum overlap is [0.391598,0.473880]
   Number of overlapping intervals is 5
   Indices of overlapping intervals are:
         1       3      4      8      9
   Number of indices printed is 5

Submission Site


Back | Home