Beispiel #1
0
 static private void FindMinWays(VertexDjekstra vertex)
 {
     if (Vertices.Where(item => item.IsVisited != true).Count() != 0)
     {
         VertexDjekstra next = MakeStep(vertex);
         FindMinWays(next);
     }
 }
Beispiel #2
0
 static private async Task FindMinWays(VertexDjekstra vertex, bool isLearingMode)
 {
     if (Vertices.Where(item => item.IsVisited != true).Count() != 0)
     {
         VertexDjekstra next = await MakeStep(vertex, isLearingMode);
         await FindMinWays(next, isLearingMode);
     }
 }
Beispiel #3
0
        static private IEnumerable <VertexDjekstra> FindАdjacentVertexs(VertexDjekstra vertex)
        {
            SortedSet <VertexDjekstra> adjacentVertexs = new SortedSet <VertexDjekstra>();

            foreach (var e in Edges)
            {
                if (e.V1.Number == vertex.Number)
                {
                    adjacentVertexs.Add(e.V2);
                }
            }
            return(adjacentVertexs);
        }
Beispiel #4
0
        private static VertexDjekstra MakeStep(VertexDjekstra vertex)
        {
            foreach (var v in FindАdjacentVertexs(vertex))
            {
                int CurrentMark = Math.Min(v.CurrentMark, vertex.CurrentMark + FindEdge(vertex, v).Distance);
                Vertices.Where(x => x.Number == v.Number).FirstOrDefault().CurrentMark = CurrentMark;
            }
            int            min_dist = Vertices.Where(item => item.IsVisited != true).Select(item => item.CurrentMark).Min();
            VertexDjekstra min_vert = Vertices.Where(item => item.IsVisited != true && item.CurrentMark == min_dist).First();

            min_vert.IsVisited = true;
            _distances.Add(min_vert, min_dist);
            return(min_vert);
        }
Beispiel #5
0
 public EdgeDjekstra(VertexView v1, VertexView v2, int distance)
 {
     V1 = new VertexDjekstra()
     {
         Number = v1.Number,
         Name   = v1.Name
     };
     V2 = new VertexDjekstra()
     {
         Number = v2.Number,
         Name   = v2.Name
     };
     Distance = distance;
 }
Beispiel #6
0
        public static Dictionary <Vertex, int> AlgDjekstra(Vertex startVertex, Graph g)
        {
            _distances = new Dictionary <Vertex, int>();
            Edges      = new List <EdgeDjekstra>();
            Vertices   = new List <VertexDjekstra>();
            foreach (var edge in g.E)
            {
                Edges.Add(new EdgeDjekstra(edge.V1, edge.V2, edge.Distance));
            }
            foreach (var vertex in g.V)
            {
                Vertices.Add(new VertexDjekstra(vertex));
            }
            VertexDjekstra firstVertex = Vertices.Where(item => item.Number == startVertex.Number).First();

            firstVertex.IsVisited   = true;
            firstVertex.CurrentMark = 0;
            _distances.Add(startVertex, 0);
            FindMinWays(firstVertex);
            return(_distances);
        }
Beispiel #7
0
        public async static Task <SortedDictionary <VertexView, int> > AlgDjekstra(int startVert, GraphVisual.Graph g, Canvas graphCanvas, bool isLearingMode)
        {
            VertexView startVertex = g.V.Where(item => item.Number == startVert).First();

            ;            _distances = new SortedDictionary <VertexView, int>();
            Edges    = new List <EdgeDjekstra>();
            Vertices = new List <VertexDjekstra>();
            grCanvas = graphCanvas;
            foreach (var edge in g.E)
            {
                Edges.Add(new EdgeDjekstra(edge.V1, edge.V2, edge.Distance));
            }
            foreach (var vertex in g.V)
            {
                Vertices.Add(new VertexDjekstra(vertex));
            }
            VertexDjekstra firstVertex = Vertices.Where(item => item.Number == startVertex.Number).First();

            firstVertex.CurrentMark = 0;
            await FindMinWays(firstVertex, isLearingMode);

            return(_distances);
        }
Beispiel #8
0
 static private EdgeDjekstra FindEdge(VertexDjekstra v1, VertexDjekstra v2)
 {
     return(Edges.Where(item => item.V1.Number == v1.Number && item.V2.Number == v2.Number).FirstOrDefault());
 }
Beispiel #9
0
 public EdgeDjekstra(Vertex v1, Vertex v2, int distance)
 {
     V1       = new VertexDjekstra(v1.Number, v1.Name, v1.IsVisited);
     V2       = new VertexDjekstra(v2.Number, v2.Name, v2.IsVisited);
     Distance = distance;
 }
Beispiel #10
0
 public EdgeDjekstra(VertexDjekstra v1, VertexDjekstra v2, int distance)
 {
     V1       = v1;
     V2       = v2;
     Distance = distance;
 }
Beispiel #11
0
        private static async Task <VertexDjekstra> MakeStep(VertexDjekstra vertex, bool isLearingMode)
        {
            foreach (var v in FindАdjacentVertexs(vertex))
            {
                var vert        = Vertices.Where(x => x.Number == v.Number).FirstOrDefault();
                int CurrentMark = Math.Min(vert.CurrentMark, vertex.CurrentMark + FindEdge(vertex, vert).Distance);
                Vertices.Where(x => x.Number == vert.Number).FirstOrDefault().CurrentMark = CurrentMark;
                if (isLearingMode == false)
                {
                    await Task.Delay(1000);

                    foreach (var element in grCanvas.Children)
                    {
                        if (element is Line line)
                        {
                            int v1 = int.Parse(line.Tag.ToString().Split()[0]);
                            int v2 = int.Parse(line.Tag.ToString().Split()[1]);
                            if (vertex.Number == v1 && v.Number == v2)
                            {
                                line.Stroke = new SolidColorBrush(Color.FromRgb(140, 0, 140));
                            }
                        }
                        else if (element is TextBlock text)
                        {
                            if (text.Tag.ToString().Split().Length == 2)
                            {
                                if (text.Tag.ToString().Split()[1] == "Dj")
                                {
                                    int vertexNum = int.Parse(text.Tag.ToString().Split()[0]);
                                    if (vertexNum == v.Number)
                                    {
                                        text.Text = CurrentMark.ToString();
                                    }
                                }
                            }
                        }
                    }
                }
            }
            int            min_dist = Vertices.Where(item => item.IsVisited != true).Select(item => item.CurrentMark).Min();
            VertexDjekstra min_vert = Vertices.Where(item => item.IsVisited != true && item.CurrentMark == min_dist).First();

            Vertices.Where(item => item.IsVisited != true && item.CurrentMark == min_dist).First().IsVisited = true;
            _distances.Add(min_vert, min_dist);
            if (isLearingMode == false)
            {
                foreach (var element in grCanvas.Children)
                {
                    if (element is TextBlock text)
                    {
                        if (text.Tag.ToString().Split().Length == 2)
                        {
                            if (text.Tag.ToString().Split()[1] == "Dj")
                            {
                                int vertexNum = int.Parse(text.Tag.ToString().Split()[0]);
                                if (vertexNum == min_vert.Number)
                                {
                                    text.Text       = min_dist.ToString();
                                    text.Foreground = new SolidColorBrush(Color.FromRgb(0, 140, 0));
                                }
                            }
                        }
                    }
                }
            }
            return(min_vert);
        }