ps1num3 - 3. Algorithm: The input S is a set of intervals...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
3. Algorithm : The input S is a set of intervals associated with the students. 01. function disjoint(S): 02. order S by increasing finish time 03. R = {} 04. while S is not empty: 05. A = pop the first element off of S 06. for each element B still in S and ordered still by finish time: 07. if B intersects with A: 08. remove B from S 09. break 10. if no such B exists that intersects with A: 11. discard A and continue 12. add the pair (A, B) into R 13. return R Complexity : Initially, sorting S will take O(n*logn) time. Now each step of the while loop removes at least one item from A, hence even if the input consists of totally disjoint intervals, there will still be at least |S| iterations. And if the input did consist of entirely disjoint intervals, then each of the nested for each loop will loop through the entire set S, which will contain |S|-n elements at the n’th iteration, each with a constant time conditional check. As such, the complexity of the while loop will look like O(n+(n-1)+(n-2)+…+1) which is O((n^2+n)/2) = O(n^2). Coupled with the
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/12/2012 for the course CS 4820 taught by Professor Kleinberg during the Spring '08 term at Cornell.

Page1 / 2

ps1num3 - 3. Algorithm: The input S is a set of intervals...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online