Esempio n. 1
0
        public void CreateEdge(Vertex start, Vertex finish)
        {
            var edge = new Edge(start, finish, EdgesAreDirected, EdgesAreWeighted);

            if (EdgesAreDirected)
            {
                if (start.AdjacentNodes.Contains(finish)) return;
                start.AdjacentNodes.Add(finish);
                mainWindow.AdjacencyWindow.SetElement(start,finish,"1");
            }
            else
            {
                if (start.AdjacentNodes.Contains(finish)||finish.AdjacentNodes.Contains(start)) return;
                start.AdjacentNodes.Add(finish);
                finish.AdjacentNodes.Add(start);
                mainWindow.AdjacencyWindow.SetElement(start, finish, "1");
                mainWindow.AdjacencyWindow.SetElement(finish, start, "1");
            }

            start.IncidentEdges.Add(edge);
            finish.IncidentEdges.Add(edge);

            Edges.Add(edge);
            Panel.SetZIndex(edge.Body, -1);
            mainWindow.MainCanvas.Children.Add(edge.Body);
            
            if (EdgesAreWeighted)
                mainWindow.MainCanvas.Children.Add(edge.TbWeight);

            edge.Update();
        }
Esempio n. 2
0
 public void CreateEdge(Vertex source)
 {
     var newEdge = new Edge(source, EdgesAreDirected, EdgesAreWeighted);
     Edges.Add(newEdge);
     Panel.SetZIndex(newEdge.Body, -1);
     mainWindow.MainCanvas.Children.Add(newEdge.Body);
 }
Esempio n. 3
0
        public void FinalizeEdgeCreation(Vertex finish, Edge draggedEdge)
        {
            draggedEdge.B = finish;
            finish.IncidentEdges.Add(draggedEdge);
            draggedEdge.A.IncidentEdges.Add(draggedEdge);

            if (EdgesAreWeighted)
            {
                draggedEdge.CreateWeightBlock();
                mainWindow.MainCanvas.Children.Add(draggedEdge.TbWeight);
                Keyboard.Focus(draggedEdge.TbWeight);
            }

            if (EdgesAreDirected)
            {
                draggedEdge.A.AdjacentNodes.Add(finish);
                mainWindow.AdjacencyWindow.SetElement(draggedEdge.A, finish, "1");
            }
            else
            {
                finish.AdjacentNodes.Add(draggedEdge.A);
                draggedEdge.A.AdjacentNodes.Add(finish);
                mainWindow.AdjacencyWindow.SetElement(draggedEdge.A, finish, "1");
                mainWindow.AdjacencyWindow.SetElement(finish, draggedEdge.A, "1");
            }

            draggedEdge.Update();
        }
Esempio n. 4
0
 public void RemoveEdge(Edge edge)
 {
     Edges.Remove(edge);
     edge.A.IncidentEdges.Remove(edge);
     edge.B.IncidentEdges.Remove(edge);
     if (EdgesAreDirected)
     {
         edge.A.AdjacentNodes.Remove(edge.B);
         mainWindow.AdjacencyWindow.SetElement(edge.A, edge.B, "0");
     }
     else
     {
         edge.A.AdjacentNodes.Remove(edge.B);
         edge.B.AdjacentNodes.Remove(edge.A);
         mainWindow.AdjacencyWindow.SetElement(edge.A, edge.B, "0");
         mainWindow.AdjacencyWindow.SetElement(edge.B, edge.A, "0");
     }
     mainWindow.MainCanvas.Children.Remove(edge.Body);
     if (EdgesAreWeighted)
         mainWindow.MainCanvas.Children.Remove(edge.TbWeight);
 }
Esempio n. 5
0
        private async Task PrimFunc(Vertex node)
        {
            Vertex bestOptionVertex = node;

            Parallel.For((long) 0, NumberOfVerticies, i =>
            {
                distance[i] = double.PositiveInfinity;
                Visited[i] = false;
                predecesor[i] = node;
            });

            distance[NodeDictionary[node]] = 0;
            node.Body.Background = nodeFinalColor;
            predecesor[NodeDictionary[node]] = null;

            while (true)
            {
                var min = double.PositiveInfinity;
                for (var i = 0; i < NumberOfVerticies; ++i)
                {
                    int index = i;
                    var ind = NodeDictionary[MainWindow.VertexHandler.Verticies[index]];
                    if (!Visited[ind])
                    {
                        if(min > distance[ind])
                        {
                            min = distance[ind];
                            bestOptionVertex = MainWindow.VertexHandler.Verticies[index];
                        }
                    }
                }
                if (double.IsInfinity(min)) break;
                var currInd = NodeDictionary[bestOptionVertex];
                bestOptionVertex.Body.Background = nodeFinalColor;
                if (!bestOptionVertex.Equals(node))
                {
                    edge = MainWindow.EdgeHandler.GetEdgeBetween(bestOptionVertex, predecesor[currInd]);
                    edge.Body.Stroke = edgeFinalColor;
                }
                Visited[currInd] = true;
                cost += distance[currInd];
               
                foreach (var nextNode in bestOptionVertex.AdjacentNodes)
                {
                    var weigth = MainWindow.VertexHandler.GetWeightBetween(bestOptionVertex, nextNode);
                    var nextInd = NodeDictionary[nextNode];
                    if (!Visited[nextInd])
                    {
                        if (distance[nextInd] > weigth)
                        {
                            predecesor[nextInd] = bestOptionVertex;
                            distance[nextInd] = weigth;
                        }
                        edge=MainWindow.EdgeHandler.GetEdgeBetween(nextNode, bestOptionVertex);
                        edge.Body.Stroke= edgeTempColor;
                        edge.Body.StrokeThickness = 3;
                    }
                    else
                    {
                        edge = MainWindow.EdgeHandler.GetEdgeBetween(nextNode, bestOptionVertex);
                        if (edge.Body.Stroke.Equals(edgeFinalColor)) continue;
                        edge.Body.Stroke = initColor;
                        edge.Body.StrokeThickness = 1;
                    }
                }
                if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
            }
            MainWindow.MessageTextBlock.Text = cost.ToString(CultureInfo.InvariantCulture);
        }