using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //size of square
int mm = 999; //maxmaximum cost
double t = m / 2;0.75; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //square
int mm = 999; //max cost
double t = m / 2; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //size of square
int mm = 999; //maximum cost
double t = m / 0.75; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //size of square
int mm = 999; //maximummax cost
double t = m / 0.75;2; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //size of square
int mm = 999; //maximum cost
double t = m / 0.75; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dijkstra
{
class Program
{
static void Main(string[] args)
{
int n = 5; //nodes
int m = 100; //square
int mm = 999; //max cost
double t = m / 2; //distance
float[] x, y; //x and y coordinates
x = new float[n + 1];
y = new float[n + 1];
float[,] c; //cost matrix
c = new float[n + 1, n + 1];
int[,] a; //adjacency matrix
a = new int[n + 1, n + 1];
//distance array
float[] d;
d = new float[n + 1];
//span array
int[] sa;
sa = new int[n + 1];
Random r = new Random();
//randomise coordinates
for (int i = 1; i <= n; i++)
{
x[i] = m * (float)r.NextDouble();
y[i] = m * (float)r.NextDouble();
d[i] = mm;
sa[i] = 0;
}
//coordinates
Console.WriteLine("Coordinates");
for (int i = 1; i <= n; i++)
Console.WriteLine(i + ": (" + x[i].ToString("0.00") + " , " + y[i].ToString("0.00") + " )");
Console.WriteLine();
//span array
Console.WriteLine("Spanne array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
//calculate distance costs
for (int i = 1; i <= n; i++)
for (int j = i + 1; j <= n; j++)
{
c[i, j] = (float)Math.Sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
if (c[i, j] > t)
c[i, j] = mm;
a[i, j] = 0;
}
Console.WriteLine("Starting values: ");
Console.WriteLine();
// distances
Console.WriteLine("Cost matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if ((i >= j) || (c[i, j] > t)) //if i is greater than j or is the 999 distance then
Console.Write(" -- ");
else
Console.Write(" " + c[i, j].ToString("00.00"));
Console.WriteLine();
}
Console.WriteLine();
//adjaceny matrics
Console.WriteLine("Adjancency matrix");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
Console.ReadLine();
// starting node
int start = r.Next(1, n + 1);
Console.WriteLine("Start at node " + start + " ...");
Console.WriteLine();
sa[start] = 1;
d[start] = 0;
Console.WriteLine("Span array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
for (int k = 1; k < n; k++)
{
float shortestDistance = mm;
int iShortest = 0, jShortest = 0, spannedShortest = 0, unspannedShortest = 0;
for (int i = 1; i < n; i++)
for (int j = i + 1; j <= n; j++)
if ((sa[i] == 1) && (sa[j] == 0))
{
if (d[i] + c[i, j] < shortestDistance)
{
shortestDistance = d[i] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = i;
unspannedShortest = j;
}
}
else if ((sa[i] == 0) && (sa[j] == 1))
{
if (d[j] + c[i, j] < shortestDistance)
{
shortestDistance = d[j] + c[i, j];
iShortest = i;
jShortest = j;
spannedShortest = j;
unspannedShortest = i;
}
}
a[iShortest, jShortest] = 1;
Console.WriteLine("Joining " + iShortest + " and " + jShortest);
sa[unspannedShortest] = 1;
d[unspannedShortest] = d[spannedShortest] + c[iShortest, jShortest];
Console.WriteLine("Distance to " + unspannedShortest + " is " + d[unspannedShortest].ToString("00.00"));
Console.ReadLine();
}
Console.WriteLine("Spanned array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + sa[i] + " ");
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Adjancey array");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
if (i >= j)
Console.Write(" -");
else
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
Console.WriteLine();
// look at the distance array
Console.WriteLine("Distance array");
for (int i = 1; i <= n; i++)
Console.Write(i + ": " + d[i].ToString("0.00") + " ");
Console.WriteLine();
Console.WriteLine();
Console.ReadLine();
}
}
}
I have implementing a simple version of Dijkstra's algorithm in C#. I found it really confusing, and I'm not 100% if I've implemented it correctly. Could Could this be made more efficient? Does it need to be modified?
I have implementing a simple version of Dijkstra's algorithm in C#. I found it really confusing, and I'm not 100% if I've implemented it correctly. Could this be made more efficient? Does it need to be modified?
I have implementing a simple version of Dijkstra's algorithm in C#. Could this be made more efficient? Does it need to be modified?