Challenge Accepted: Dijkstra's Algorithm

Today, with a friend, we remembered the famous quote by Barney Stinson of How I Met Your Mother:

Challenge Accepted!..


Within the next days, I will challenge myself. The environment is as follows: Given a distance matrix of a connected graph with positive distances, nodes i and j, find the shortest path from i to j, return the minimum total distance. Simple "shortest path" problem and I will use the Dijkstra's Algorithm.

So why is this a challenge? Simple: I will not focus on performance and instead I will focus on writing a "minimal" code. Let's see how much I can succeed with C# using LINQ versus Python.

My primary bet: C# would get this one. If there's one thing in C# that I believe in, it is LINQ. Want a proof? Let's see how I can sum an array of integers into a double:


private static double ClassicalApproach(int[] data)
{
double x = 0;
for (int i = 0; i < data.Length; i++)
x += data[i];
return x;
}



private static double LinqApproach(int[] data)
{
return data.Aggregate<int, double>(0, (current, t) => current + t);}


Do I sound too much like I am too much interested in writing shorter codes? No, actually I'm not, but nowadays this has been quite a hobby for me. Let's see how it will end. By the way, here is the Python version:

sum(x)

Quite intuitive and clear:)

Take good care,
BCS

2 yorum:

bariscsal dedi ki...

Well, here I have to apologize: to convert the sum in Python to double just use [sourcecode language="python"] sum(x) + .0 [/sourcecode]

Turan Murat Güvenç dedi ki...

Challenge Accepted! Let's try to write minimum code for the Djikstra’s Algorithm. I got dibs on C# :)

Yorum Gönderme