Skip to content

Commit 51c02b1

Browse files
authored
Merge pull request #219 from neha030/dev0
Klee's Algorithm added
2 parents 50a42dc + bed1005 commit 51c02b1

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Description: Given a Point with x and y coordinates,this algorithm finds the union of the
3+
lengths covered by all these line segments.
4+
This algorithm was proposed by Klee in 1977 and is asymptotically the fastest. */
5+
6+
7+
#include<bits/stdc++.h>
8+
#define x first
9+
#define y second
10+
11+
using namespace std;
12+
13+
//Structure to store the point coordinates.
14+
typedef struct points {
15+
int x, y;
16+
}points;
17+
18+
//vector to indicate the points in the plane.
19+
vector<points>plane;
20+
21+
//function to find the union of the lengths.
22+
int find_union(vector<points>line) {
23+
24+
int size = line.size();
25+
26+
//This vector first stores the coordinate and then the bool value.
27+
//Here,the starting coordinate is marked false and the ending coordinate as true in pair.
28+
vector<pair<int, bool>> points(size * 2);
29+
30+
for (int i = 0; i < size; i++)
31+
{
32+
33+
points[i * 2] = make_pair(line[i].x, false);
34+
points[i * 2 + 1] = make_pair(line[i].y, true);
35+
36+
}
37+
38+
//This is to sort all the ending points.
39+
sort(points.begin(), points.end());
40+
41+
//Initializing variable answer which will store the final length and counter which will keep track of the opening and closing segment.
42+
int answer = 0, counter = 0;
43+
44+
//traversing through all the ending points
45+
for (int i = 0; i < size * 2; i++)
46+
{
47+
//for adding the difference of current and previous points to answer.
48+
if (counter)
49+
answer += (points[i].x - points[i - 1].x);
50+
51+
//for the endpoint of the segment, decrements the counter or else increment the counter.
52+
if (points[i].y)
53+
counter--;
54+
else
55+
counter++;
56+
}
57+
58+
return answer;
59+
}
60+
61+
int main()
62+
{
63+
64+
//declaration of the number of points.
65+
int n;
66+
67+
cout << "Enter the desired number of points:\n";
68+
cin >> n;
69+
cout << "Enter the x and y coordinates for the points with a space:\n";
70+
71+
plane.resize(n);
72+
73+
//For input of x and y coordinates of the desired points
74+
for (int i = 0; i < n; i++)
75+
{
76+
cin >> plane[i].x >> plane[i].y;
77+
}
78+
79+
//Outputs the final length of the union the length of segments.
80+
cout << "The union of the length of the line segments is :\n";
81+
82+
int ans = find_union(plane);
83+
84+
cout << ans;
85+
return 0;
86+
87+
}
88+
89+
/* Time Complexity: O(N log N)
90+
Space Complexity: O(N) */
91+
92+
/* Test Case:
93+
*
94+
Sample Input: Enter the desired number of points:
95+
4
96+
Enter the x and y coordinates for the points with a space:
97+
2 4
98+
5 6
99+
3 7
100+
1 6
101+
Sample Output: The union of the length of the line segments is:
102+
6
103+
*/

0 commit comments

Comments
 (0)