/// <summary>
/// .
/// </summary>
class DekstraAlgorim
{
public Point[] points { get ; private set ; }
public Rebro[] rebra { get ; private set ; }
public Point BeginPoint { get ; private set ; }
public DekstraAlgorim(Point[] pointsOfgrath, Rebro[] rebraOfgrath)
{
points = pointsOfgrath;
rebra = rebraOfgrath;
}
/// <summary>
///
/// </summary>
/// <param name="beginp"></param>
public void AlgoritmRun(Point beginp)
{
if ( this .points.Count() == 0 || this .rebra.Count() == 0)
{
throw new DekstraException( " !" );
}
else
{
BeginPoint = beginp;
OneStep(beginp);
foreach (Point point in points)
{
Point anotherP = GetAnotherUncheckedPoint();
if (anotherP != null )
{
OneStep(anotherP);
}
else
{
break ;
}
}
}
}
/// <summary>
/// , .
/// </summary>
/// <param name="beginpoint"></param>
public void OneStep(Point beginpoint)
{
foreach (Point nextp in Pred(beginpoint))
{
if (nextp.IsChecked == false ) //
{
float newmetka = beginpoint.ValueMetka + GetMyRebro(nextp, beginpoint).Weight;
if (nextp.ValueMetka > newmetka)
{
nextp.ValueMetka = newmetka;
nextp.predPoint = beginpoint;
}
else
{
}
}
}
beginpoint.IsChecked = true ; //
}
/// <summary>
/// . .
/// </summary>
/// <param name="currpoint"></param>
/// <returns></returns>
private IEnumerable <Point> Pred(Point currpoint)
{
IEnumerable <Point> firstpoints = from ff in rebra where ff.FirstPoint==currpoint select ff.SecondPoint;
IEnumerable <Point> secondpoints = from sp in rebra where sp.SecondPoint == currpoint select sp.FirstPoint;
IEnumerable <Point> totalpoints = firstpoints.Concat<Point>(secondpoints);
return totalpoints;
}
/// <summary>
/// , 2
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
private Rebro GetMyRebro(Point a, Point b)
{ // 2
IEnumerable <Rebro> myr = from reb in rebra where (reb.FirstPoint == a & reb.SecondPoint == b) || (reb.SecondPoint == a & reb.FirstPoint == b) select reb;
if (myr.Count() > 1 || myr.Count() == 0)
{
throw new DekstraException( " !" );
}
else
{
return myr.First();
}
}
/// <summary>
/// , "" .
/// </summary>
/// <returns></returns>
private Point GetAnotherUncheckedPoint()
{
IEnumerable <Point> pointsuncheck = from p in points where p.IsChecked == false select p;
if (pointsuncheck.Count() != 0)
{
float minVal = pointsuncheck.First().ValueMetka;
Point minPoint = pointsuncheck.First();
foreach (Point p in pointsuncheck)
{
if (p.ValueMetka < minVal)
{
minVal = p.ValueMetka;
minPoint = p;
}
}
return minPoint;
}
else
{
return null ;
}
}
public List <Point> MinPath1(Point end)
{
List <Point> listOfpoints = new List <Point>();
Point tempp = new Point();
tempp = end;
while (tempp != this .BeginPoint)
{
listOfpoints.Add(tempp);
tempp = tempp.predPoint;
}
return listOfpoints;
}
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// ,
/// </summary>
class Rebro
{
public Point FirstPoint { get ; private set ; }
public Point SecondPoint { get ; private set ; }
public float Weight { get ; private set ; }
public Rebro(Point first, Point second, float valueOfWeight)
{
FirstPoint = first;
SecondPoint = second;
Weight = valueOfWeight;
}
}
* This source code was highlighted with Source Code Highlighter .
/// <summary>
/// ,
/// </summary>
class Point
{
public float ValueMetka { get ; set ; }
public string Name { get ; private set ; }
public bool IsChecked { get ; set ; }
public Point predPoint { get ; set ; }
public object SomeObj { get ; set ; }
public Point( int value , bool ischecked)
{
ValueMetka = value ;
IsChecked = ischecked;
predPoint = new Point();
}
public Point( int value , bool ischecked, string name)
{
ValueMetka = value ;
IsChecked = ischecked;
Name = name;
predPoint = new Point();
}
public Point()
{
}
}
* This source code was highlighted with Source Code Highlighter .
// <summary>
///
/// </summary>
static class PrintGrath
{
public static List < string > PrintAllPoints(DekstraAlgorim da)
{
List < string > retListOfPoints = new List < string >();
foreach (Point p in da.points)
{
retListOfPoints.Add( string .Format( "point name={0}, point value={1}, predok={2}" , p.Name, p.ValueMetka, p.predPoint.Name ?? " " ));
}
return retListOfPoints;
}
public static List < string > PrintAllMinPaths(DekstraAlgorim da)
{
List < string > retListOfPointsAndPaths = new List < string >();
foreach (Point p in da.points)
{
if (p != da.BeginPoint)
{
string s = string .Empty;
foreach (Point p1 in da.MinPath1(p))
{
s += string .Format( "{0} " , p1.Name);
}
retListOfPointsAndPaths.Add( string .Format( "Point ={0},MinPath from {1} = {2}" , p.Name, da.BeginPoint.Name, s));
}
}
return retListOfPointsAndPaths;
}
}
* This source code was highlighted with Source Code Highlighter .
class DekstraException:ApplicationException
{
public DekstraException( string message): base (message)
{
}
}
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/63347/
All Articles