COUNTING number of pairs of intersecting chords [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Consider N chords in a circle, each determined by its endpoints. Describe an O(nlogn) solution for determining the number of pairs of chords that intersect inside the circle.
ASSUMPTION: No two chords share an endpoint.

There exists a general line-segment intersection algorithm which does the job in O(nlogn).
This can be used in your case as two chords can't intersect in the exterior of a circle.
The following link contains the algorithm:
http://www.cs.princeton.edu/~chazelle/pubs/IntersectLineSegments.pdf
P.S.
It requires knowledge of basic computational geometry (line sweeps, range trees).
Hope this helps.

Off the top of my head, sort the chord endpoints by polar angle (this is the O(n log n) part). Then read through the sorted list (which is O(n)) - if two adjacent endpoints belong to the same chord, it has no intersections. Where two adjacent entries in the list belong to different chords, there may be an intersection depending on where the other endpoints for those two chords lie - e.g. if a chord A has endpoints A1 and A2 in their sorted order, and similarly chord B has B1 and B2, finding B2-A1 in the list is not an intersection, because B1 is earlier and A2 is later. However, B1-A2 would be an intersection.
See also biziclop's comment for another, somewhat more carefully constructed, solution.

Related

2D String Matching: Baker-Bird Algorithm [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to find a submatrix in a huge matrix, so I google and find the Baker-Bird Algorithm.
But, unfortunately I cannot understand it very much, and the tutorial about it is rare.
I cannot find some example code to study.
So what I want to ask is there some simple example code or pseudo code that I can study it?
Thx in advance.
Ok, from studying the link Kent Munthe Caspersen gave ( http://www.stringology.org/papers/Zdarek-PhD_thesis-2010.pdf page 30 on), I understand how the Baker-Bird Algorithm works.
For a submatrix to appear in a matrix, its columns must all match individually. You can scan down each column looking for matches, and then scan this post-processed matrix for rows indicating columns consecutively matching at the same spot.
Say we are looking for submatrices of the format
a c a
b b a
c a b
We search down each column for the column-matches 'abc' 'cba' or 'aab' and in a new matrix mark the ends of those complete matches in the corresponding cell - for example with A, B or C. (What the algorithm in the paper does is construct a state machine which transitions to a new state based on the old state number and which letter comes next, and then looks for states that indicate we just matched a column, which is more complex but more efficient as it only has to scan each column once instead of once per different column match we are interested in)
Once we have done this, we scan along each row looking for successive values indicating successive columns matched - in this case, we're looking for the string 'ABC' in a matrix row. If we find it, there was a sub-array match here.
Speedups are attained from using the state machine approach described above, and also from choice of string searching algorithm ( there are many with different time complexities: ( of which there are numerous: http://en.wikipedia.org/wiki/String_searching_algorithm )
(Note that the entire algorithm can, of course, be flipped to do rows first than columns, it's identical.)
What about the example in this PhD thesis p.31-33:
http://www.stringology.org/papers/Zdarek-PhD_thesis-2010.pdf

Area of polygon using c++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How can i calculate the area of a polygon in c++ only by knowing the x and y coordonates of the points which make the polygon?
A simple google search shows the answer provided that you are dealing with non-self-intersecting polygons. The sign of the area is positive if the points on the polygon are arranged in counterclockwise order. This formula does not assume that the polygon is convex.
http://mathworld.wolfram.com/PolygonArea.html
Here, the area is found by summing the determinent of neighboring points. Each determinent computes the area of the parallelogram formed by the vector e.g. (x1,y1) and (x2,y2) (where both vectors stem from the origin (0,0)). The division by 2 gives the area of a triangle. When traveling around the polygon, the triangles will have a positive area if your polygon is convex. Otherwise, negative areas of these triangles will cancel with their positive counterparts for the case of a concave polygon giving you the correct result.
Simple wikipedia search shows the answer:
http://en.wikipedia.org/wiki/Polygon#Area_and_centroid

How to determine what bin a float should be in? C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array of floats Float_t xbins[41] that defines 40 bins i.e. ranges of floats.
E.g. y is in bin 7 if y > xbins[7] && !(y > xbins[8]).
How do I determine what bin a given float should belong to without having 40 if statements?
Please answer in C++ as I don't speak other languages.
If the array is sorted, then do a binary search to locate the correct bin. You'll need a combination of std::sort (if not sorted), then something like std::lower_bound, to locate. You'll need to ensure that operator< is implemented correctly for Float_t.
As it turned out that the bins are not uniformly spaced but have integer bounds, the probably fastest method is to have a (inverse) look up table that apparently has about 100 entries. One needs to make basically two comparisons for the lower & higher bounds.
If the array bounds are derived with a formula, it could be possible to write an inverse formula that outperforms the LUT method.
For a generic case binary search is the way -- and even that can be improved a bit by doing linear interpolation instead of exactly subdividing the range to half. The speed (if the data is not pathological) would be O(loglogn) compared to O(logn) for binary search.

Area of intersecting triangles [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to solve a question, but it's a little hard and I need some help. The question is:
we have 2 triangles, and we have the coordinates of the vertices like (x1, y1), (x2, y2), (x3, y3), (a1, b1), (a2, b2), (a3, b3).
We want to measure the area that two triangles are on each other. It may be 0 or more.
For example, if we have for first triangle (0,0) (3,0) (0,3) and the second (0,0) (3,3) (3,0), the common area will be 2.25.
How should i write a program to solve this?
The problem of intersecting triangles (and convex polygon in general) is way tougher than it seems, especially if you wanna solve it in linear time with respect to the number of edges involved.
You can consider this page to have an idea of a working algorithm for general convex polygons (the algorithm is based on rotating calipers. Indeed, there's some abstract geometry behind, in particular, the geometric Hanh-Banach theorem).
Consider that once you have the intersection polygon, which is convex, evaluating the area is trivial.
Thus, you have two options:
You keep the problem abstract (somehow you consider triangles as convex polygons, and that's it) and a fast solution in C/C++ can be achieved through the GPC library (which is written in C) or, alternatively, for instance, through boost::geometry.
You specialize just for triangles: in this case, my advice is to consider this wonderful paper which details topologically the possible ways of intersecting involved, and gives an efficient implementation of the solution.
I have one more thing to say: when you consider your problem with toy triangles (i.e. low skewness, sizes way larger than machine precision) you can still think to implement your own algorithm and play with it. But, when you have to intersect millions of possibly ill-conditioned triangles per second, you'd better rely on a good and fast library.

C++ Algorithm stability [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I tell whether an algorithm is stable or not?..
Also, how does this algorithm Bucketsort compare to Mergesort, Quicksort, Bubblesort, and Insertionsort
?
At first glance it would seem that if your queues are FIFO then it is stable. However I think there some context from class or other homework that would help you make a more solid determination.
From wikipedia:
Stability
Stable sorting algorithms maintain the relative order of records with equal keys. If all keys are different then this distinction is not necessary. But if there are equal keys, then a sorting algorithm is stable if whenever there are two records (let's say R and S) with the same key, and R appears before S in the original list, then R will always appear before S in the sorted list. When equal elements are indistinguishable, such as with integers, or more generally, any data where the entire element is the key, stability is not an issue. However, assume that the following pairs of numbers are to be sorted by their first component:
http://en.wikipedia.org/wiki/Sorting_algorithm#Stability
As far as comparing to other algorithms. Wikipedia has a concise entry on it:
http://en.wikipedia.org/wiki/Bucket_sort#Comparison_with_other_sorting_algorithms
Also: https://stackoverflow.com/a/7341355/1416221