
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Klee’s Algorithm (Length Of Union Of Segments of a line) in C++
In this tutorial, we are going to write a program that finds the length of union of segments of a line.
We are given starting and ending points of the line segment and we need to find the length of union of segments of the line.
The algorithm that we are going to use is called klee's algorithm.
Let's see the steps to solve the problem.
- Initialise the array with coordinates of all the segments.
- Initialise a vector called points with double the size of segments array.
- Iterate over the segments array.
- Fill the values of points array at index i * 2 with the first point of the current segment and false.
- Fill the values of points array at index i * 2 + 1 with the second point of the current segment and false.
- Sort the points array.
- Iterate over the points array with a counter variable.
- If the counter is greater than 0, then add the first points of i and i - 1 to the result.
- Decrement the counter if there is second point else increment it.
- Return the result.
Example
Let's see the code.
#include<bits/stdc++.h> using namespace std; int segmentUnionLength(const vector<pair <int,int>> &segments) { int n = segments.size(); vector<pair<int, bool>> points(n * 2); for (int i = 0; i < n; i++) { points[i*2] = make_pair(segments[i].first, false); points[i*2 + 1] = make_pair(segments[i].second, true); } sort(points.begin(), points.end()); int result = 0, count = 0; for (int i = 0; i < n * 2; i++){ if (count) { result += points[i].first - points[i-1].first; } points[i].second ? count-- : count++; } return result; } int main() { vector<pair<int,int>> segments; segments.push_back(make_pair(1, 3)); segments.push_back(make_pair(2, 7)); segments.push_back(make_pair(6, 12)); segments.push_back(make_pair(13, 5)); cout << segmentUnionLength(segments) << endl; return 0; }
Output
If you run the above code, then you will get the following result.
6
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Maximum length of segments of 0’s and 1’s in C++
- Maximize the number of segments of length p, q and r in C++
- Maximum possible intersection by moving centers of line segments in C++
- Justify with reasons:i) Line Segments of length 10cm, 2cm, and 12 cm can't form a triangle.ii) Line Segments of 9cm, 10cm, and 11cm can form a right triangle.
- Number of Segments in a String in C++
- C++ Program to get maximum area of rectangle made from line segments
- Union of SortedSet to a collection in C#
- Union of two HashSet in C#
- Find all disjointed intersections in a set of vertical line segments in JavaScript
- Maximum number of segments of lengths a, b and c in C++
- Find the number of line segments in the given figure."\n
- Program to find number of sets of k-non-overlapping line segments in Python
- C++ program to find the number of triangles amongst horizontal and vertical line segments
- How do I find the intersection of two line segments in Matplotlib?

Advertisements