Skip to content

Commit 8230945

Browse files
committed
Added closest pair problem
1 parent 17ab4ba commit 8230945

15 files changed

+361
-32
lines changed

closest_pair_problem/bruteForce.c

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
PairedPoints* bruteForce(Point* points, int left, int right) {
44

5-
int minDistance = INT_MAX;
6-
double calculatedDistance;
5+
float minDistance = FLT_MAX;
6+
float calculatedDistance;
77
PairedPoints* closestPaired = (PairedPoints*)malloc(sizeof(PairedPoints));
8+
closestPaired->distance = FLT_MAX;
89

9-
for (int i = left; i <= right; i++) {
10+
for (int i = left; i <= right-1; i++) {
1011
for (int j = i + 1; j <= right; j++) {
1112
calculatedDistance = calculateDistance(points[i], points[j]);
1213
if (calculatedDistance < minDistance) {
1314
closestPaired->p1 = points[i];
1415
closestPaired->p2 = points[j];
1516
closestPaired->distance = calculatedDistance;
17+
minDistance = calculatedDistance;
1618
}
1719
}
1820
}

closest_pair_problem/calculateDistance.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "header_files/declarations.h"
22

3-
int calculateDistance(Point p1, Point p2) {
3+
//A utility function to find Euclidian distance
4+
//between two points
5+
float calculateDistance(Point p1, Point p2) {
46
int diffX = p1.x - p2.x;
57
int diffY = p1.y - p2.y;
68
return sqrt(pow(diffX, 2) + pow(diffY, 2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
<ProjectConfiguration Include="Debug|x64">
13+
<Configuration>Debug</Configuration>
14+
<Platform>x64</Platform>
15+
</ProjectConfiguration>
16+
<ProjectConfiguration Include="Release|x64">
17+
<Configuration>Release</Configuration>
18+
<Platform>x64</Platform>
19+
</ProjectConfiguration>
20+
</ItemGroup>
21+
<PropertyGroup Label="Globals">
22+
<VCProjectVersion>16.0</VCProjectVersion>
23+
<ProjectGuid>{4D17D138-150C-461B-8FF0-37994FBC09A0}</ProjectGuid>
24+
<RootNamespace>closestpairproblem</RootNamespace>
25+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
26+
</PropertyGroup>
27+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
28+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
29+
<ConfigurationType>Application</ConfigurationType>
30+
<UseDebugLibraries>true</UseDebugLibraries>
31+
<PlatformToolset>v142</PlatformToolset>
32+
<CharacterSet>MultiByte</CharacterSet>
33+
</PropertyGroup>
34+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
35+
<ConfigurationType>Application</ConfigurationType>
36+
<UseDebugLibraries>false</UseDebugLibraries>
37+
<PlatformToolset>v142</PlatformToolset>
38+
<WholeProgramOptimization>true</WholeProgramOptimization>
39+
<CharacterSet>MultiByte</CharacterSet>
40+
</PropertyGroup>
41+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
42+
<ConfigurationType>Application</ConfigurationType>
43+
<UseDebugLibraries>true</UseDebugLibraries>
44+
<PlatformToolset>v142</PlatformToolset>
45+
<CharacterSet>MultiByte</CharacterSet>
46+
</PropertyGroup>
47+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
48+
<ConfigurationType>Application</ConfigurationType>
49+
<UseDebugLibraries>false</UseDebugLibraries>
50+
<PlatformToolset>v142</PlatformToolset>
51+
<WholeProgramOptimization>true</WholeProgramOptimization>
52+
<CharacterSet>MultiByte</CharacterSet>
53+
</PropertyGroup>
54+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
55+
<ImportGroup Label="ExtensionSettings">
56+
</ImportGroup>
57+
<ImportGroup Label="Shared">
58+
</ImportGroup>
59+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
60+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
61+
</ImportGroup>
62+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
63+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
64+
</ImportGroup>
65+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
66+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
67+
</ImportGroup>
68+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
69+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
70+
</ImportGroup>
71+
<PropertyGroup Label="UserMacros" />
72+
<PropertyGroup />
73+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
74+
<ClCompile>
75+
<WarningLevel>Level3</WarningLevel>
76+
<Optimization>Disabled</Optimization>
77+
<SDLCheck>true</SDLCheck>
78+
<ConformanceMode>true</ConformanceMode>
79+
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS</PreprocessorDefinitions>
80+
</ClCompile>
81+
<Link>
82+
<SubSystem>Console</SubSystem>
83+
</Link>
84+
</ItemDefinitionGroup>
85+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
86+
<ClCompile>
87+
<WarningLevel>Level3</WarningLevel>
88+
<Optimization>Disabled</Optimization>
89+
<SDLCheck>true</SDLCheck>
90+
<ConformanceMode>true</ConformanceMode>
91+
</ClCompile>
92+
<Link>
93+
<SubSystem>Console</SubSystem>
94+
</Link>
95+
</ItemDefinitionGroup>
96+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
97+
<ClCompile>
98+
<WarningLevel>Level3</WarningLevel>
99+
<Optimization>MaxSpeed</Optimization>
100+
<FunctionLevelLinking>true</FunctionLevelLinking>
101+
<IntrinsicFunctions>true</IntrinsicFunctions>
102+
<SDLCheck>true</SDLCheck>
103+
<ConformanceMode>true</ConformanceMode>
104+
</ClCompile>
105+
<Link>
106+
<SubSystem>Console</SubSystem>
107+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
108+
<OptimizeReferences>true</OptimizeReferences>
109+
</Link>
110+
</ItemDefinitionGroup>
111+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
112+
<ClCompile>
113+
<WarningLevel>Level3</WarningLevel>
114+
<Optimization>MaxSpeed</Optimization>
115+
<FunctionLevelLinking>true</FunctionLevelLinking>
116+
<IntrinsicFunctions>true</IntrinsicFunctions>
117+
<SDLCheck>true</SDLCheck>
118+
<ConformanceMode>true</ConformanceMode>
119+
</ClCompile>
120+
<Link>
121+
<SubSystem>Console</SubSystem>
122+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
123+
<OptimizeReferences>true</OptimizeReferences>
124+
</Link>
125+
</ItemDefinitionGroup>
126+
<ItemGroup>
127+
<ClInclude Include="header_files\common.h" />
128+
<ClInclude Include="header_files\declarations.h" />
129+
</ItemGroup>
130+
<ItemGroup>
131+
<ClCompile Include="bruteForce.c" />
132+
<ClCompile Include="calculateDistance.c" />
133+
<ClCompile Include="eliminateSamePoints.c" />
134+
<ClCompile Include="findClosestPair.c" />
135+
<ClCompile Include="generateRandomPoints.c" />
136+
<ClCompile Include="lookNearOfMid.c" />
137+
<ClCompile Include="main.c" />
138+
<ClCompile Include="printClosestPoints.c" />
139+
<ClCompile Include="printPoints.c" />
140+
<ClCompile Include="quickSort.c" />
141+
<ClCompile Include="readPointsFromFile.c" />
142+
</ItemGroup>
143+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
144+
<ImportGroup Label="ExtensionTargets">
145+
</ImportGroup>
146+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hh;hpp;hxx;hm;inl;inc;ipp;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
</ItemGroup>
17+
<ItemGroup>
18+
<ClInclude Include="header_files\common.h">
19+
<Filter>Header Files</Filter>
20+
</ClInclude>
21+
<ClInclude Include="header_files\declarations.h">
22+
<Filter>Header Files</Filter>
23+
</ClInclude>
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ClCompile Include="generateRandomPoints.c">
27+
<Filter>Source Files</Filter>
28+
</ClCompile>
29+
<ClCompile Include="main.c">
30+
<Filter>Source Files</Filter>
31+
</ClCompile>
32+
<ClCompile Include="quickSort.c">
33+
<Filter>Source Files</Filter>
34+
</ClCompile>
35+
<ClCompile Include="eliminateSamePoints.c">
36+
<Filter>Source Files</Filter>
37+
</ClCompile>
38+
<ClCompile Include="printPoints.c">
39+
<Filter>Source Files</Filter>
40+
</ClCompile>
41+
<ClCompile Include="bruteForce.c">
42+
<Filter>Source Files</Filter>
43+
</ClCompile>
44+
<ClCompile Include="findClosestPair.c">
45+
<Filter>Source Files</Filter>
46+
</ClCompile>
47+
<ClCompile Include="calculateDistance.c">
48+
<Filter>Source Files</Filter>
49+
</ClCompile>
50+
<ClCompile Include="lookNearOfMid.c">
51+
<Filter>Source Files</Filter>
52+
</ClCompile>
53+
<ClCompile Include="readPointsFromFile.c">
54+
<Filter>Source Files</Filter>
55+
</ClCompile>
56+
<ClCompile Include="printClosestPoints.c">
57+
<Filter>Source Files</Filter>
58+
</ClCompile>
59+
</ItemGroup>
60+
</Project>

closest_pair_problem/eliminateSamePoints.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ int eliminateSamePoints(Point* arr, int N) {
99
for (int j = i + 1; j < N; ) {
1010

1111
//increment j variable after shifting
12-
if ((arr[i].x == arr[j].x) && (arr[i].y == arr[j].x)) {
12+
if ((arr[i].x == arr[j].x) && (arr[i].y == arr[j].y)) {
1313

1414
for (int k = j; k < N; k++) {
1515
arr[k] = arr[k + 1];

closest_pair_problem/findClosestPair.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
11
#include "header_files/declarations.h"
22

3+
34
PairedPoints findClosestPair(Point* points, int left, int right) {
45

6+
//the number of elements in the subarray
57
int numOfPoints = (right - left) + 1;
8+
9+
//Termination condition for the recursive function
10+
//Calling brute force If the subarray has 3 or fewer members
611
if (numOfPoints <= 3)
712
return *bruteForce(points, left, right);
813

14+
//Finding the middle point
915
int mid = (left + right) / 2;
1016

17+
//minimum paired points on the left of the mid (minPPL)
1118
PairedPoints minPPL = findClosestPair(points, left, mid);
19+
//minimum paired points on the right of the mid (minPPL)
1220
PairedPoints minPPR = findClosestPair(points, mid + 1, right);
1321

22+
//defining minimum paired point(minPP)
23+
//after finding minPPL and minPPR
1424
PairedPoints minPP;
1525
if (minPPL.distance < minPPR.distance)
1626
minPP = minPPL;
1727
else
1828
minPP = minPPR;
1929

30+
//Stores all element their distance is less than minPP.distance to points[mid]
2031
Point split[MAX];
2132
int j = 0;
2233
for (int i = left; i <= right; i++) {
@@ -26,9 +37,11 @@ PairedPoints findClosestPair(Point* points, int left, int right) {
2637
}
2738
}
2839

40+
//Now, we inspect split pair points
2941
PairedPoints PPM = lookNearOfMid(split, j, minPP.distance);
3042

31-
if (PPM.distance != INT_MIN)
43+
//
44+
if (PPM.distance != FLT_MAX)
3245
return PPM;
3346
else
3447
return minPP;

closest_pair_problem/generateRandomPoints.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//A function that returns a random point array
44
//that will be used to test the algorithm.
5-
Point* generateRandomPoints(int N) {
5+
Point* generateRandomPoints(int N, int max) {
66

77
//Crating an point array with "N" size
88
Point* points = (Point*) malloc(sizeof(Point) * N);
@@ -15,8 +15,8 @@ Point* generateRandomPoints(int N) {
1515
//with random numbers
1616
//Also, subtract N/2 to allow negative points
1717
for (int i = 0; i < N; i++) {
18-
points[i].x = (rand() % N) - (N / 2);
19-
points[i].y = (rand() % N) - (N / 2);
18+
points[i].x = (rand() % max) - (max / 2);
19+
points[i].y = (rand() % max) - (max / 2);
2020
}
2121

2222
return points;

closest_pair_problem/header_files/common.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <stdio.h>
44
#include <stdlib.h>
55
#include <math.h>
6+
#include <float.h>
7+
#include <string.h>
68

79
#define TRUE 1
810
#define FALSE 0
@@ -21,6 +23,6 @@ typedef struct {
2123
typedef struct {
2224
Point p1;
2325
Point p2;
24-
double distance;
26+
float distance;
2527
}PairedPoints;
2628

closest_pair_problem/header_files/declarations.h

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
#include "common.h"
44

5-
Point* generateRandomPoints(int N);
5+
Point* generateRandomPoints(int N, int max);
66
int eliminateSamePoints(Point* arr, int N);
77
void printPoints(Point* points, int N);
88
void quickSort(Point arr[], int first, int last, int axis);
99
int partition(Point arr[], int first, int last, int axis);
1010
int swap_x_axis(Point arr[], int index1, int index2);
1111
int swap_y_axis(Point arr[], int index1, int index2);
1212
PairedPoints* bruteForce(Point* points, int left, int right);
13-
int calculateDistance(Point p1, Point p2);
14-
PairedPoints lookNearOfMid(Point split[], int size, int minDistance);
15-
PairedPoints findClosestPair(Point* points, int left, int right);
13+
float calculateDistance(Point p1, Point p2);
14+
PairedPoints lookNearOfMid(Point split[], int size, float minDistance);
15+
PairedPoints findClosestPair(Point* points, int left, int right);
16+
int readPointsFromFile(Point points[]);
17+
void printClosestPoints(PairedPoints pairedPoints);

0 commit comments

Comments
 (0)