I am trying to make a closest pair alg using div and conq.
using this base for the alg please show me how I would go about doing this. In C++ obviously.
std::pair<Point<T>, Point<T> > closeP( vector<Point<T> > v){
return std::pair<Point<T>, Point<T> >
(v[0],v[1]);
}
Here is a brute force alg that can be used to create the base
cases for the div and conq alg.
std::pair<Point<T> ,Point<T> > closePBF(
vector<Point<T> > pts){
T min = dist(pts[0],pts[1]);
size_t min1 = 0;
size_t min2 = 1;
for(size_t i = 0; i< pts.size()-1; ++i){
for(size_t j = i+1; j <pts.size(); ++j){
T d = dist(pts[i],pts[j]);
if (d<min) {
min = d;
min1 = i;
min2 = j;
}
}
}
I have a tester so I dont need anything extra to see that it works. Both algs (the brute force and the base of the one I want created) are using a template with typename<T>. Try to keep the alg fairly simple.
C++ code for the above problem is as
follows-
#include <bits/stdc++.h> using namespace std; // A structure to represent a Point in 2D plane class Point { public: int x, y; }; // Needed to sort array of points // according to X coordinate int compareX(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->x - p2->x); } // Needed to sort array of points according to Y coordinate int compareY(const void* a, const void* b) { Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->y - p2->y); } // A utility function to find the // distance between two points float dist(Point p1, Point p2) { return sqrt( (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y) ); } // A Brute Force method to return the // smallest distance between two points // in P[] of size n float bruteForce(Point P[], int n) { float min = FLT_MAX; for (int i = 0; i < n; ++i) for (int j = i+1; j < n; ++j) if (dist(P[i], P[j]) < min) min = dist(P[i], P[j]); return min; } // A utility function to find // minimum of two float values float min(float x, float y) { return (x < y)? x : y; } // A utility function to find the // distance beween the closest points of // strip of given size. All points in // strip[] are sorted accordint to // y coordinate. They all have an upper // bound on minimum distance as d. // Note that this method seems to be // a O(n^2) method, but it's a O(n) // method as the inner loop runs at most 6 times float stripClosest(Point strip[], int size, float d) { float min = d; // Initialize the minimum distance as d qsort(strip, size, sizeof(Point), compareY); // Pick all points one by one and try the next points till the difference // between y coordinates is smaller than d. // This is a proven fact that this loop runs at most 6 times for (int i = 0; i < size; ++i) for (int j = i+1; j < size && (strip[j].y - strip[i].y) < min; ++j) if (dist(strip[i],strip[j]) < min) min = dist(strip[i], strip[j]); return min; } // A recursive function to find the // smallest distance. The array P contains // all points sorted according to x coordinate float closestUtil(Point P[], int n) { // If there are 2 or 3 points, then use brute force if (n <= 3) return bruteForce(P, n); // Find the middle point int mid = n/2; Point midPoint = P[mid]; // Consider the vertical line passing // through the middle point calculate // the smallest distance dl on left // of middle point and dr on right side float dl = closestUtil(P, mid); float dr = closestUtil(P + mid, n - mid); // Find the smaller of two distances float d = min(dl, dr); // Build an array strip[] that contains // points close (closer than d) // to the line passing through the middle point Point strip[n]; int j = 0; for (int i = 0; i < n; i++) if (abs(P[i].x - midPoint.x) < d) strip[j] = P[i], j++; // Find the closest points in strip. // Return the minimum of d and closest // distance is strip[] return min(d, stripClosest(strip, j, d) ); } // The main function that finds the smallest distance // This method mainly uses closestUtil() float closest(Point P[], int n) { qsort(P, n, sizeof(Point), compareX); // Use recursive function closestUtil() // to find the smallest distance return closestUtil(P, n); } // Driver code int main() { Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}}; int n = sizeof(P) / sizeof(P[0]); cout << "The smallest distance is " << closest(P, n); return 0; }
If the answer helped then please upvote, it means a
lot.
And for any queries feel free to comment.
I am trying to make a closest pair alg using div and conq. using this base...
I am trying to write a package in go. I am given two files: I need to modify the function to return the min of an array of ints. do not change the first line of code. Please write in go or I will mark this answer as incorrect. func Min(arr []int) int { } I am also given this tester code: package min import "testing" func TestMin(t *testing.T) { tests := []struct { in []int ...
Can someone please help me fix my code on this assignment that's due in the morning? Please read the question carefully. Using C++, construct a graph class, graph.template, in which the edges are stored in adjacency sets. You need to use graph.h and test_graph.cpp to implement graph.template. The author provides an implementation of a graph using an adjacency matrix. I have started the template file but I cannot get it to compile. I have posted my template file of what...
In C++ I am trying to implement a log base 2 hash table utilizing the shift function. So, the code determines the position of where the value will be stored using log2. But am having issues using the shift part as everything just wants to pile on to zero. //ChainingHash - log2 #include<iostream> #include<vector> #include<iterator> #include<string> #include<cmath> using namespace std; #define BUCKET 10 //no. of buckets class Hash { vector<int>*table; //ptr containing buckets public: Hash();...
C++: Need help debugging my code
I am writing this and using some other examples as reference code
while rewriting it with my own understanding of the material but am
having trouble making it finally compile. Below is a picture of the
error messages.
//main.cpp
//Semester Project
//Created by J---on 5/6/2019
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
void instructions(); //displays program details and
instructions
void openFile();
void takeInput(int*);
void switchBoard(int*);
struct price
{...
I am using xcode Use the following ideas to develop a nonrecursive, linear-time algorithm for the maximum-subarray problem. Start at the left end of the array, and progress toward the right, keeping track of the maximum subarray seen so far. Knowing a maximum subarray of A[1..j], extend the answer to find a maximum subarray ending at index j + 1 by using the following observation: a maximum subarray of A[1..j + 1] is either a maximum subarray of A[1..j] or...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide comments in the main code to describe what is happening? (especially on the bool isNumber) THE MAIN CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false;...
I am having trouble understanding how this code is able to use the contents of the header file. Can someone please provide brief comments in the top code to show what is happening? THE CODE: #include<bits/stdc++.h> #include "MyCartesianPoint.h" #include <math.h> #include <iostream> using namespace std; bool isNumber(string s) { if(!isdigit (s[0])) { if(s[0] != '-') return false; else if(s.length() == 1) return false; } for...
MATLAB only please
I am trying to get my getdatafuntion to work. I am also trying
to get all my x's, y's, and v's to popup in the command window so I
can put in any value and it will find the value for me using
the equation I put in.
function project_9_sjl()
% PROJECT_9_SJL project_9_sjl() is the driver function for the
program.
%
% Name: Scott Lawrence
% Date: 3/27/2019
% Class: CMPSC 200
% Description: Determine the optimal...
I need help modifying this program. How would I make sure that the methods is being called and checked in my main method? Here is what the program needs to run as: GDVEGTA GVCEKST The LCS has length 4 The LCS is GVET This is the error that I'm getting: The LCS has length 4 // I got this right The LCS is //the backtrace is not being called for some reason c++ code: the cpp class: /** * calculate...
In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...