Wednesday, March 22, 2023
HomeSoftware DevelopmentClosest pair of factors utilizing sweep line algorithm

Closest pair of factors utilizing sweep line algorithm


#embody <bits/stdc++.h>

utilizing namespace std;

  

lengthy closestPair(vector<pair<int, int> > coordinates, int n)

{

    int i;

    

    vector<pair<int, int> > v;

    for (i = 0; i < n; i++)

        v.push_back({ coordinates[i].first,

                      coordinates[i].second });

  

    

    

    kind(v.start(), v.finish());

  

    

    

    lengthy d = INT_MAX;

  

    

    

    set<pair<int, int> > st;

    st.insert({ v[0].first, v[0].second });

  

    for (i = 1; i < n; i++) {

        auto l = st.lower_bound(

            { v[i].first - d, v[i].second - d });

        auto r = st.upper_bound(

            { v[i].first, v[i].second + d });

        if (l == st.finish())

            proceed;

  

        for (auto p = l; p != r; p++) {

            pair<int, int> val = *p;

            lengthy dis = (v[i].first - val.first)

                           * (v[i].first - val.first)

                       + (v[i].second - val.second)

                             * (v[i].second - val.second);

  

            

            

            if (d > dis)

                d = dis;

        }

        st.insert({ v[i].first, v[i].second });

    }

  

    return d;

}

  

int fundamental()

{

  

    

    vector<pair<int, int> > P = {

        { 1, 2 }, { 2, 3 }, { 3, 4 }, { 5, 6 }, { 2, 1 }

    };

    int n = P.dimension();

  

    

    cout << "The smallest distance is "

         << closestPair(P, n);

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

nine − 1 =

Most Popular

Recent Comments