We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
The Skyline Problem using Divide and Conquer algorithm
Given n rectangular buildings in a 2-dimensional city, computes the skyline of these buildings, eliminating hidden lines. The main task is to view buildings from a side and remove all sections that are not visible.
All buildings share common bottom and every building is represented by triplet (left, ht, right)
‘left’: is x coordinated of left side (or wall).
‘right’: is x coordinate of right side
‘ht’: is height of building.
A skyline is a collection of rectangular strips. A rectangular strip is represented as a pair (left, ht) where left is x coordinate of left side of strip and ht is height of strip.
Examples:
Input: Array of buildings
{ (1, 11, 5), (2, 6, 7), (3, 13, 9), (12, 7, 16), (14, 3, 25),
(19, 18, 22), (23, 13, 29), (24, 4, 28) }
Output: Skyline (an array of rectangular strips)
A strip has x coordinate of left side and height
(1, 11), (3, 13), (9, 0), (12, 7), (16, 3), (19, 18),
(22, 3), (25, 0)
Below image is for input 1 :
Consider following as another example when there is only one
building
Input: {(1, 11, 5)}
Output: (1, 11), (5, 0)
A Simple Solution is to initialize skyline or result as empty, then one by one add buildings to skyline. A building is added by first finding the overlapping strip(s). If there are no overlapping strips, the new building adds new strip(s). If overlapping strip is found, then height of the existing strip may increase. Time complexity of this solution is O(n2)
We can find Skyline in Θ(nLogn) time using Divide and Conquer. The idea is similar to Merge Sort, divide the given set of buildings in two subsets. Recursively construct skyline for two halves and finally merge the two skylines.
How to Merge two Skylines?
The idea is similar to merge of merge sort, start from first strips of two skylines, compare x coordinates. Pick the strip with smaller x coordinate and add it to result. The height of added strip is considered as maximum of current heights from skyline1 and skyline2.
Example to show working of merge:
Height of new Strip is always obtained by takin maximum of following
(a) Current height from skyline1, say 'h1'.
(b) Current height from skyline2, say 'h2'
h1 and h2 are initialized as 0. h1 is updated when a strip from
SkyLine1 is added to result and h2 is updated when a strip from
SkyLine2 is added.
Skyline1 = {(1, 11), (3, 13), (9, 0), (12, 7), (16, 0)}
Skyline2 = {(14, 3), (19, 18), (22, 3), (23, 13), (29, 0)}
Result = {}
h1 = 0, h2 = 0
Compare (1, 11) and (14, 3). Since first strip has smaller left x,
add it to result and increment index for Skyline1.
h1 = 11, New Height = max(11, 0)
Result = {(1, 11)}
Compare (3, 13) and (14, 3). Since first strip has smaller left x,
add it to result and increment index for Skyline1
h1 = 13, New Height = max(13, 0)
Result = {(1, 11), (3, 13)}
Similarly (9, 0) and (12, 7) are added.
h1 = 7, New Height = max(7, 0) = 7
Result = {(1, 11), (3, 13), (9, 0), (12, 7)}
Compare (16, 0) and (14, 3). Since second strip has smaller left x,
it is added to result.
h2 = 3, New Height = max(7, 3) = 7
Result = {(1, 11), (3, 13), (9, 0), (12, 7), (14, 7)}
Compare (16, 0) and (19, 18). Since first strip has smaller left x,
it is added to result.
h1 = 0, New Height = max(0, 3) = 3
Result = {(1, 11), (3, 13), (9, 0), (12, 7), (14, 7), (16, 3)}
Since Skyline1 has no more items, all remaining items of Skyline2
are added
Result = {(1, 11), (3, 13), (9, 0), (12, 7), (14, 7), (16, 3),
(19, 18), (22, 3), (23, 13), (29, 0)}
One observation about above output is, the strip (14, 7) is redundant
(There is already an strip of same height). We remove all redundant
strips.
Result = {(1, 11), (3, 13), (9, 0), (12, 7), (16, 3), (19, 18),
(22, 3), (23, 13), (29, 0)}
In below code, redundancy is handled by not appending a strip if the
previous strip in result has same height.
Below is C++ implementation of above idea.
filter_none
edit close
play_arrow
link brightness_4 code
// A divide and conquer based C++
// program to find skyline of given buildings
#include <iostream>
usingnamespacestd;
// A structure for building
structBuilding {
// x coordinate of left side
intleft;
// height
intht;
// x coordinate of right side
intright;
};
// A strip in skyline
classStrip {
// x coordinate of left side
intleft;
// height
intht;
public:
Strip(intl = 0, inth = 0)
{
left = l;
ht = h;
}
friendclassSkyLine;
};
// Skyline: To represent Output(An array of strips)
classSkyLine {
// Array of strips
Strip* arr;
// Capacity of strip array
intcapacity;
// Actual number of strips in array
intn;
public:
~SkyLine() { delete[] arr; }
intcount() { returnn; }
// A function to merge another skyline
// to this skyline
SkyLine* Merge(SkyLine* other);
// Constructor
SkyLine(intcap)
{
capacity = cap;
arr = newStrip[cap];
n = 0;
}
// Function to add a strip 'st' to array
voidappend(Strip* st)
{
// Check for redundant strip, a strip is
// redundant if it has same height or left as previous
This article is contributed Abhay Rathi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.