d087: Collecting Treasures
Po-Lung Chen
Team Dont Block Me, National Taiwan University
Po-Lung Chen (Dont block me) d087: Collecting Treasures 1 / 31
Problem Description
Given M open intervals as platforms.
One can move from platform A to platform B if A and B overlap.
Travel from one starting platform toevery other platformsexactly onceand back.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 2 / 31
Problem Description
Given M open intervals as platforms.
One can move from platform A to platform B if A and B overlap.
Travel from one starting platform toevery other platformsexactly onceand back.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 2 / 31
Problem Description
Given M open intervals as platforms.
One can move from platform A to platform B if A and B overlap.
Travel from one starting platform toevery other platformsexactly onceand back.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 2 / 31
Problem Description
Given M open intervals as platforms.
One can move from platform A to platform B if A and B overlap.
Travel from one starting platform toevery other platformsexactly onceand back.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 2 / 31
Notes
We may construct aninterval graph for the problem.
The problem is equivalent to find aHamilton cycleon this interval graph.
However, finding a Hamilton cycle on the general graph is NP-complete.
But in this “special” graph we can solve it by agreedy algorithm.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 3 / 31
Notes
We may construct aninterval graph for the problem.
The problem is equivalent to find aHamilton cycleon this interval graph.
However, finding a Hamilton cycle on the general graph is NP-complete.
But in this “special” graph we can solve it by agreedy algorithm.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 3 / 31
Notes
We may construct aninterval graph for the problem.
The problem is equivalent to find aHamilton cycleon this interval graph.
However, finding a Hamilton cycle on the general graph is NP-complete.
But in this “special” graph we can solve it by agreedy algorithm.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 3 / 31
Notes
We may construct aninterval graph for the problem.
The problem is equivalent to find aHamilton cycleon this interval graph.
However, finding a Hamilton cycle on the general graph is NP-complete.
But in this “special” graph we can solve it by agreedy algorithm.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 3 / 31
The Algorithm
Keeping two paths starting from the leftmost interval.
Each time we consider the path with smallerendpoint.
Among all intervals which overlap with this path, we pick the interval with smallestendpoint.
If there is no interval overlap with the path, the answer is “no”.
Finally we will get two paths, check if the last interval at both paths overlap.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 4 / 31
The Algorithm
Keeping two paths starting from the leftmost interval.
Each time we consider the path with smallerendpoint.
Among all intervals which overlap with this path, we pick the interval with smallestendpoint.
If there is no interval overlap with the path, the answer is “no”.
Finally we will get two paths, check if the last interval at both paths overlap.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 4 / 31
The Algorithm
Keeping two paths starting from the leftmost interval.
Each time we consider the path with smallerendpoint.
Among all intervals which overlap with this path, we pick the interval with smallestendpoint.
If there is no interval overlap with the path, the answer is “no”.
Finally we will get two paths, check if the last interval at both paths overlap.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 4 / 31
The Algorithm
Keeping two paths starting from the leftmost interval.
Each time we consider the path with smallerendpoint.
Among all intervals which overlap with this path, we pick the interval with smallestendpoint.
If there is no interval overlap with the path, the answer is “no”.
Finally we will get two paths, check if the last interval at both paths overlap.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 4 / 31
Illustration
1- 1-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 5 / 31
Illustration
1- 1-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 6 / 31
Illustration
1-2- 1-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 7 / 31
Illustration
1-2- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 8 / 31
Illustration
1-2- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 9 / 31
Illustration
1-2- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 10 / 31
Illustration
1-2-3- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 11 / 31
Illustration
1-2-3- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 12 / 31
Illustration
1-2-3- 1-9-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 13 / 31
Illustration
1-2-3- 1-9-8-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 14 / 31
Illustration
1-2-3- 1-9-8-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 15 / 31
Illustration
1-2-3- 1-9-8-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 16 / 31
Illustration
1-2-3- 1-9-8-4-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 17 / 31
Illustration
1-2-3- 1-9-8-4-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 18 / 31
Illustration
1-2-3- 1-9-8-4-7-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 19 / 31
Illustration
1-2-3- 1-9-8-4-7-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 20 / 31
Illustration
1-2-3-5- 1-9-8-4-7-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 21 / 31
Illustration
1-2-3-5- 1-9-8-4-7-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 22 / 31
Illustration
1-2-3-5- 1-9-8-4-7-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 23 / 31
Illustration
1-2-3-5- 1-9-8-4-7-6-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 24 / 31
Illustration
1-2-3-5- 1-9-8-4-7-6-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 25 / 31
Illustration
1-2-3-5 1-9-8-4-7-6-
Po-Lung Chen (Dont block me) d087: Collecting Treasures 26 / 31
Illustration (Finished!)
1-2-3-5 1-9-8-4-7-6
Now we have to check whether 5 and 6 overlap or not.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 27 / 31
Illustration (Finished!)
1-2-3-5 1-9-8-4-7-6
Now we have to check whether 5 and 6 overlap or not.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 27 / 31
Naïve Implementation
For each time, we scan onall intervals and find the interval we want.
This cost O(n) per iteration.
Total time complexity is O(n2).
Can we solve this problem faster?
Po-Lung Chen (Dont block me) d087: Collecting Treasures 28 / 31
Naïve Implementation
For each time, we scan onall intervals and find the interval we want.
This cost O(n) per iteration.
Total time complexity is O(n2).
Can we solve this problem faster?
Po-Lung Chen (Dont block me) d087: Collecting Treasures 28 / 31
Naïve Implementation
For each time, we scan onall intervals and find the interval we want.
This cost O(n) per iteration.
Total time complexity is O(n2).
Can we solve this problem faster?
Po-Lung Chen (Dont block me) d087: Collecting Treasures 28 / 31
Naïve Implementation
For each time, we scan onall intervals and find the interval we want.
This cost O(n) per iteration.
Total time complexity is O(n2).
Can we solve this problem faster?
Po-Lung Chen (Dont block me) d087: Collecting Treasures 28 / 31
Carefully Implementation
From the view of thecandidateintervals,
each interval will goes into the set once, and be picked out once, too.
There are a total of O(n) add/remove operations to the set of candidate intervals.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 29 / 31
Carefully Implementation
From the view of thecandidateintervals,
each interval will goes into the set once, and be picked out once, too.
There are a total of O(n) add/remove operations to the set of candidate intervals.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 29 / 31
Carefully Implementation
From the view of thecandidateintervals,
each interval will goes into the set once, and be picked out once, too.
There are a total of O(n) add/remove operations to the set of candidate intervals.
Po-Lung Chen (Dont block me) d087: Collecting Treasures 29 / 31
Carefully Implementation (Contd.)
The interval we want in the candidate set is the one withsmallest endpoint.
If we use a heaporsegment tree, we can update the candidate set in O(log n) time per operation.
The overall time complexity is O(n log n).
Po-Lung Chen (Dont block me) d087: Collecting Treasures 30 / 31
Carefully Implementation (Contd.)
The interval we want in the candidate set is the one withsmallest endpoint.
If we use a heaporsegment tree, we can update the candidate set in O(log n) time per operation.
The overall time complexity is O(n log n).
Po-Lung Chen (Dont block me) d087: Collecting Treasures 30 / 31
Carefully Implementation (Contd.)
The interval we want in the candidate set is the one withsmallest endpoint.
If we use a heaporsegment tree, we can update the candidate set in O(log n) time per operation.
The overall time complexity is O(n log n).
Po-Lung Chen (Dont block me) d087: Collecting Treasures 30 / 31
Thank you for your listening!
Po-Lung Chen (Dont block me) d087: Collecting Treasures 31 / 31