Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
22
Task/Closest-pair-problem/C-sharp/closest-pair-problem-1.cs
Normal file
22
Task/Closest-pair-problem/C-sharp/closest-pair-problem-1.cs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Segment
|
||||
{
|
||||
public Segment(PointF p1, PointF p2)
|
||||
{
|
||||
P1 = p1;
|
||||
P2 = p2;
|
||||
}
|
||||
|
||||
public readonly PointF P1;
|
||||
public readonly PointF P2;
|
||||
|
||||
public float Length()
|
||||
{
|
||||
return (float)Math.Sqrt(LengthSquared());
|
||||
}
|
||||
|
||||
public float LengthSquared()
|
||||
{
|
||||
return (P1.X - P2.X) * (P1.X - P2.X)
|
||||
+ (P1.Y - P2.Y) * (P1.Y - P2.Y);
|
||||
}
|
||||
}
|
||||
11
Task/Closest-pair-problem/C-sharp/closest-pair-problem-2.cs
Normal file
11
Task/Closest-pair-problem/C-sharp/closest-pair-problem-2.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Segment Closest_BruteForce(List<PointF> points)
|
||||
{
|
||||
int n = points.Count;
|
||||
var result = Enumerable.Range( 0, n-1)
|
||||
.SelectMany( i => Enumerable.Range( i+1, n-(i+1) )
|
||||
.Select( j => new Segment( points[i], points[j] )))
|
||||
.OrderBy( seg => seg.LengthSquared())
|
||||
.First();
|
||||
|
||||
return result;
|
||||
}
|
||||
50
Task/Closest-pair-problem/C-sharp/closest-pair-problem-3.cs
Normal file
50
Task/Closest-pair-problem/C-sharp/closest-pair-problem-3.cs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
public static Segment MyClosestDivide(List<PointF> points)
|
||||
{
|
||||
return MyClosestRec(points.OrderBy(p => p.X).ToList());
|
||||
}
|
||||
|
||||
private static Segment MyClosestRec(List<PointF> pointsByX)
|
||||
{
|
||||
int count = pointsByX.Count;
|
||||
if (count <= 4)
|
||||
return Closest_BruteForce(pointsByX);
|
||||
|
||||
// left and right lists sorted by X, as order retained from full list
|
||||
var leftByX = pointsByX.Take(count/2).ToList();
|
||||
var leftResult = MyClosestRec(leftByX);
|
||||
|
||||
var rightByX = pointsByX.Skip(count/2).ToList();
|
||||
var rightResult = MyClosestRec(rightByX);
|
||||
|
||||
var result = rightResult.Length() < leftResult.Length() ? rightResult : leftResult;
|
||||
|
||||
// There may be a shorter distance that crosses the divider
|
||||
// Thus, extract all the points within result.Length either side
|
||||
var midX = leftByX.Last().X;
|
||||
var bandWidth = result.Length();
|
||||
var inBandByX = pointsByX.Where(p => Math.Abs(midX - p.X) <= bandWidth);
|
||||
|
||||
// Sort by Y, so we can efficiently check for closer pairs
|
||||
var inBandByY = inBandByX.OrderBy(p => p.Y).ToArray();
|
||||
|
||||
int iLast = inBandByY.Length - 1;
|
||||
for (int i = 0; i < iLast; i++ )
|
||||
{
|
||||
var pLower = inBandByY[i];
|
||||
|
||||
for (int j = i + 1; j <= iLast; j++)
|
||||
{
|
||||
var pUpper = inBandByY[j];
|
||||
|
||||
// Comparing each point to successivly increasing Y values
|
||||
// Thus, can terminate as soon as deltaY is greater than best result
|
||||
if ((pUpper.Y - pLower.Y) >= result.Length())
|
||||
break;
|
||||
|
||||
if (Segment.Length(pLower, pUpper) < result.Length())
|
||||
result = new Segment(pLower, pUpper);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
11
Task/Closest-pair-problem/C-sharp/closest-pair-problem-4.cs
Normal file
11
Task/Closest-pair-problem/C-sharp/closest-pair-problem-4.cs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
var randomizer = new Random(10);
|
||||
var points = Enumerable.Range( 0, 10000).Select( i => new PointF( (float)randomizer.NextDouble(), (float)randomizer.NextDouble())).ToList();
|
||||
Stopwatch sw = Stopwatch.StartNew();
|
||||
var r1 = Closest_BruteForce(points);
|
||||
sw.Stop();
|
||||
Debugger.Log(1, "", string.Format("Time used (Brute force) (float): {0} ms", sw.Elapsed.TotalMilliseconds));
|
||||
Stopwatch sw2 = Stopwatch.StartNew();
|
||||
var result2 = Closest_Recursive(points);
|
||||
sw2.Stop();
|
||||
Debugger.Log(1, "", string.Format("Time used (Divide & Conquer): {0} ms",sw2.Elapsed.TotalMilliseconds));
|
||||
Assert.Equal(r1.Length(), result2.Length());
|
||||
21
Task/Closest-pair-problem/C-sharp/closest-pair-problem-5.cs
Normal file
21
Task/Closest-pair-problem/C-sharp/closest-pair-problem-5.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Segment Closest_BruteForce(List<PointF> points)
|
||||
{
|
||||
Trace.Assert(points.Count >= 2);
|
||||
|
||||
int count = points.Count;
|
||||
|
||||
// Seed the result - doesn't matter what points are used
|
||||
// This just avoids having to do null checks in the main loop below
|
||||
var result = new Segment(points[0], points[1]);
|
||||
var bestLength = result.Length();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
for (int j = i + 1; j < count; j++)
|
||||
if (Segment.Length(points[i], points[j]) < bestLength)
|
||||
{
|
||||
result = new Segment(points[i], points[j]);
|
||||
bestLength = result.Length();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
34
Task/Closest-pair-problem/C-sharp/closest-pair-problem-6.cs
Normal file
34
Task/Closest-pair-problem/C-sharp/closest-pair-problem-6.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
Segment Closest(List<PointF> points)
|
||||
{
|
||||
Trace.Assert(points.Count >= 2);
|
||||
|
||||
int count = points.Count;
|
||||
points.Sort((lhs, rhs) => lhs.X.CompareTo(rhs.X));
|
||||
|
||||
var result = new Segment(points[0], points[1]);
|
||||
var bestLength = result.Length();
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
var from = points[i];
|
||||
|
||||
for (int j = i + 1; j < count; j++)
|
||||
{
|
||||
var to = points[j];
|
||||
|
||||
var dx = to.X - from.X;
|
||||
if (dx >= bestLength)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (Segment.Length(from, to) < bestLength)
|
||||
{
|
||||
result = new Segment(from, to);
|
||||
bestLength = result.Length();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue