示例#1
0
 public override async void Execute(Vertex start)
 {
     Initialize();
     algTask = Postorder(start);
     await algTask;
     base.Execute();
 }
示例#2
0
 private void PostOrder(Vertex vertex)
 {
     Visited[NodeDictionary[vertex]] = true;
     foreach (var node in vertex.AdjacentNodes.Where(node => !Visited[NodeDictionary[node]]))
         PostOrder(node);
     postorderStack.Push(vertex);
 }
示例#3
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);
 }
示例#4
0
        private async Task dijkstra(Vertex node)
        {
            Vertex bestOptionVertex = node;

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

            distance[NodeDictionary[node]] = 0;
            MainWindow.PathWindow.SetElement(node, MainWindow.VertexHandler.Verticies[NodeDictionary[node]], "0");
            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] && min > distance[ind])
                    {
                        min = distance[ind];
                        bestOptionVertex = MainWindow.VertexHandler.Verticies[index];
                        ColorPath(bestOptionVertex, searchColor, searchColor, searchColor, 3);
                        if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
                        ColorPath(bestOptionVertex, nextColor, mainColor, new SolidColorBrush(Colors.Black), 1);
                    }
                }
                if (!double.IsInfinity(min))
                {
                    var currInd = NodeDictionary[bestOptionVertex];
                    Visited[currInd] = true;
                    ColorPath(bestOptionVertex, tempColor, tempColor, tempColor, 3);
                    if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
                    foreach (var nextNode in bestOptionVertex.AdjacentNodes)
                    {
                        var weigth = MainWindow.VertexHandler.GetWeightBetween(bestOptionVertex, nextNode);
                        var nextInd = NodeDictionary[nextNode];
                        if (!Visited[nextInd] && distance[nextInd] > distance[currInd] + weigth)
                        {
                            distance[nextInd] = distance[currInd] + weigth;
                            MainWindow.PathWindow.SetElement(node, nextNode,
                                distance[nextInd].ToString(CultureInfo.InvariantCulture),true);
                            predecesor[nextInd] = bestOptionVertex;
                            nextNode.Body.Background = nextColor;
                            if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
                        }
                    }
                    ColorPath(bestOptionVertex, mainColor, mainColor, mainColor, 3);
                }
                else
                {
                    ColorPath(bestOptionVertex, mainColor, mainColor, mainColor, 3);
                    break;
                }
            }
        }
示例#5
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();
        }
示例#6
0
 private async Task Bipartite(Vertex node)
 {
     node.Body.Background = tempColor;
     if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
     var ind = NodeDictionary[node];
     var currColorInd = colorUsed[ind] ? 1 : 0;
     for (int i = 0; i < node.AdjacentNodes.Count; i++)
     {
         var vertex = node.AdjacentNodes[i];
         var nextInd = NodeDictionary[vertex];
         var nextColor = !colorUsed[ind];
         if (!Visited[nextInd])
         {
             node.Body.Background = colors[currColorInd];
             colorUsed[nextInd] = nextColor;
             Visited[nextInd] = true;
             await Bipartite(vertex);
             node.Body.Background = tempColor;
             if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
         }
         else
         {
             if (colorUsed[nextInd] != nextColor)
             {
                 var edge = MainWindow.EdgeHandler.GetEdgeBetween(node, vertex);
                 edgeStack.Push(edge);
                 MainWindow.EdgeHandler.RemoveEdge(edge);
                 i -= 1;
                 if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
             }
         }
     }
     node.Body.Background = colors[currColorInd];
 }
示例#7
0
 private void Dfs(Vertex vertex)
 {
     Visited[NodeDictionary[vertex]] = false;
     vertex.Body.Background = color;
     foreach (var node in transposedGraph[NodeDictionary[vertex]].Where(node => Visited[NodeDictionary[node]]))
         Dfs(node);
 }
示例#8
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();
        }
示例#9
0
 public override void Execute(Vertex start)
 {
     while (start.IncidentEdges.Count > 0)
         MainWindow.EdgeHandler.RemoveEdge(start.IncidentEdges[0]);
     MainWindow.MessageTextBlock.Text = "";
     base.Execute();
 }
示例#10
0
        public void RemoveNode(Vertex poorVertex)
        {
            Isolate(poorVertex);

            Verticies.Remove(poorVertex);

            mainWindow.MainCanvas.Children.Remove(poorVertex.Body);
            mainWindow.UpdateTables();
        }
示例#11
0
 public double GetWeightBetween(Vertex a, Vertex b)
 {
     if (!mainWindow.EdgeHandler.EdgesAreDirected)
         foreach (var edge in a.IncidentEdges.Where(edge => edge.B.Equals(b) || edge.A.Equals(b)))
             return edge.Weight;
     else
         foreach (var edge in a.IncidentEdges.Where(edge => edge.B.Equals(b)))
             return edge.Weight;
     return double.PositiveInfinity;
 }
示例#12
0
        public void CreateNode(Point position, string index)
        {
            var node = new Vertex(index);

            Verticies.Add(node);
            node.Center = position;
            
            mainWindow.MainCanvas.Children.Add(node.Body);
            mainWindow.UpdateTables();
        }
示例#13
0
        public override void Execute(Vertex start)
        {
            foreach (var vertex in MainWindow.VertexHandler.Verticies)
                vertex.Center = start.Center;

            foreach (var edge in MainWindow.EdgeHandler.Edges)
                edge.Update();

            base.Execute();
        }
示例#14
0
 private void Dfs(Vertex node)
 {
     Visited[NodeDictionary[node]] = true;
     node.Body.Background = color;
     foreach (
         var vertex in
             from vertex in node.AdjacentNodes let ind = NodeDictionary[vertex] where !Visited[ind] select vertex
         )
         Dfs(vertex);
 }
示例#15
0
 public Edge(Vertex nodeA, bool isOriented, bool isWeighted)
 {
     A = nodeA;
     B = nodeA;
     oriented = isOriented;
     weighted = isWeighted;
     if (oriented)
         CreateArrow();
     else
         CreateEdge();
 }
示例#16
0
        public Edge(Vertex nodeA, Vertex nodeB, bool isOriented, bool isWeighted)
        {
            A = nodeA;
            B = nodeB;
            oriented = isOriented;
            weighted = isWeighted;
            centerPoint = new Point((A.Center.X + B.Center.X) / 2, (A.Center.Y + B.Center.Y) / 2);

            if(weighted)
                CreateWeightBlock();
            if (oriented)
                CreateArrow();
            else
                CreateEdge();
        }
示例#17
0
文件: DFS.cs 项目: daymonc/Draw_Graph
 private async Task Dfs(Vertex node)
  {
      Visited[NodeDictionary[node]] = true;
     
      MainWindow.MessageTextBlock.Text += node.Content + " ";
      node.Body.Background = tempColor;
      if(!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
      foreach (var vertex in from vertex in node.AdjacentNodes let ind = NodeDictionary[vertex] where !Visited[ind] select vertex)
      {
          node.Body.Background = mainColor;
          await Dfs(vertex);
          node.Body.Background = tempColor;
          if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
      }
      node.Body.Background = mainColor;
  }
示例#18
0
        public void ApplyAlgorithm(string algName, Vertex selectedVertex)
        {
            if (requires2Nodes.Contains(algName) && startVertex == null)
            {
                MainWindow.MessageTextBlock.Text = "Please choose the second node!";
                startVertex = selectedVertex;
                startVertex.Body.Background=new SolidColorBrush(Colors.Red);
                return;
            }

            alg = null;

            switch (algName)
            {
                case "BFS":
                    alg = new BFS(Colors.Cyan, Colors.DarkCyan, Colors.LightCyan);
                    break;
                case "DFS":
                    alg = new DFS(Colors.Blue, Colors.Aqua);
                    break;
                case "Post-order":
                    alg = new PostOrder(Colors.Blue, Colors.DodgerBlue, Colors.Aqua);
                    break;
                case "Dijkstra":
                    alg = new Dijkstra(Colors.DarkViolet, Colors.Red, Colors.Gold, Colors.Violet);
                    break;
                case "Bellman-Ford":
                    alg = new BellmanFord(Colors.Firebrick, Colors.Magenta, Colors.OrangeRed);
                    break;
                case "Isolate":
                    alg = new Isolate();
                    break;
                case "A-path-B":
                    alg = new ApathB(startVertex);
                    break;
                case "Prim":
                    alg = new Prim(Colors.DarkGray, Colors.Sienna, Colors.Gold, Colors.ForestGreen);
                    break;
                case "Reposition":
                    alg=new Reposition();
                    break;
            }

            if (alg == null) return;
            MainWindow.ApplicationState = States.AnimationRunning;
            alg.Execute(selectedVertex);
        }
示例#19
0
        private void Dijkstra(Vertex node)
        {
            Vertex bestOptionVertex = node;

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

            distance[NodeDictionary[node]] = 0;
            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] && min > distance[ind])
                    {
                        min = distance[ind];
                        bestOptionVertex = MainWindow.VertexHandler.Verticies[index];
                    }
                }
                if (!double.IsInfinity(min))
                {
                    var currInd = NodeDictionary[bestOptionVertex];
                    Visited[currInd] = true;

                    foreach (var nextNode in bestOptionVertex.AdjacentNodes)
                    {
                        var weigth = MainWindow.VertexHandler.GetWeightBetween(bestOptionVertex, nextNode);
                        var nextInd = NodeDictionary[nextNode];
                        if (!Visited[nextInd] && distance[nextInd] > distance[currInd] + weigth)
                        {
                            distance[nextInd] = distance[currInd] + weigth;
                            predecesor[nextInd] = bestOptionVertex;
                        }
                    }
                }
                else break;
            } 
        }
示例#20
0
 public override void Execute(Vertex finish)
 {
     Initialize();
     Dijkstra(start);
     if (!double.IsPositiveInfinity(distance[NodeDictionary[finish]]))
     {
         ColorPath(finish, new SolidColorBrush(Colors.Red), 3);
         MainWindow.MessageTextBlock.Text =
             distance[NodeDictionary[finish]].ToString(CultureInfo.InvariantCulture);
     }
     else
     {
         finish.Body.Background= new SolidColorBrush(Colors.Red);
         MainWindow.MessageTextBlock.Text = "It's impossible to reach target!";
     }
     base.Execute();
 }
示例#21
0
文件: BFS.cs 项目: daymonc/Draw_Graph
 private async Task Bfs(Vertex start)
 {
     var queue = new Queue<Vertex>();
     queue.Enqueue(start);
     Visited[NodeDictionary[start]] = true;
     MainWindow.MessageTextBlock.Text += start.Content + " ";
     while (queue.Count > 0)
     {
         var node = queue.Dequeue();
         node.Body.Background = tempColor;
         foreach (var vertex in node.AdjacentNodes)
         {
             var ind = NodeDictionary[vertex];
             if (Visited[ind]) continue;
             Visited[ind] = true;
             vertex.Body.Background = nextColor;
             queue.Enqueue(vertex);
             MainWindow.MessageTextBlock.Text += vertex.Content + " ";
             if (!IsAnimationSkiping) await Task.Delay(AnimationSpeed);
             vertex.Body.Background = mainColor;
         }
         node.Body.Background= mainColor;
     }
 }
示例#22
0
 public ApathB(Vertex a)
 {
     start = a;
 }
示例#23
0
 public void Isolate(Vertex target)
 {
     while (target.IncidentEdges.Count > 0)
         mainWindow.EdgeHandler.RemoveEdge(target.IncidentEdges[0]);   
 }
示例#24
0
 private void ColorPath(Vertex node, SolidColorBrush lastVertexColor, SolidColorBrush pathColor, SolidColorBrush lastEdgeColor,
     double lastStrokeThickness)
 {
     node.Body.Background = lastVertexColor;
     var temp = node;
     node = predecesor[NodeDictionary[node]];
     if (node == null) return;
     var edge = MainWindow.EdgeHandler.GetEdgeBetween(node, temp);
     edge.Body.Stroke = lastEdgeColor;
     edge.Body.StrokeThickness = lastStrokeThickness;
     node.Body.Background = pathColor;
     temp = node;
     node = predecesor[NodeDictionary[node]];
     while (node != null)
     {
         edge = MainWindow.EdgeHandler.GetEdgeBetween(node, temp);
         edge.Body.Stroke = pathColor;
         edge.Body.StrokeThickness = 3;
         node.Body.Background = pathColor;
         temp = node;
         node = predecesor[NodeDictionary[node]];
     }
 }
示例#25
0
 public Edge GetEdgeBetween(Vertex a, Vertex b)
 {
     return EdgesAreDirected ? a.IncidentEdges.FirstOrDefault(edge => edge.B.Equals(b)) : a.IncidentEdges.FirstOrDefault(edge => edge.B.Equals(b) || edge.A.Equals(b));
 }
示例#26
0
 private void ColorPath(Vertex node, SolidColorBrush color, double strokeThickness)
 {
     node.Body.Background = color;
     var temp = node;
     node = predecesor[NodeDictionary[node]];
     while (node != null)
     {
         var edge = MainWindow.EdgeHandler.GetEdgeBetween(node, temp);
         edge.Body.Stroke = color;
         edge.Body.StrokeThickness = strokeThickness;
         node.Body.Background = color;
         temp = node;
         node = predecesor[NodeDictionary[node]];
     }
 }
示例#27
0
 public override async void Execute(Vertex start)
 {
     Initialize();
     await (algTask = PrimFunc(start));
     base.Execute();
 }
示例#28
0
 public virtual void Execute(Vertex start)
 {
 }
示例#29
0
        public void ApplyAlgorithm(string algName)
        {
            if (MainWindow.VertexHandler.Verticies.Count == 0)
            {
                MainWindow.MessageTextBlock.Text = "Graph is missing!";
                return;
            }

            startVertex = null;
            if (!MainWindow.EdgeHandler.EdgesAreWeighted)
                if (requiresWeight.Contains(algName))
                {
                    MainWindow.MessageTextBlock.Text = "Graph shall be weighted!";
                    return;
                }
            if (MainWindow.EdgeHandler.EdgesAreDirected)
                if (requiresUndirected.Contains(algName))
                {
                    MainWindow.MessageTextBlock.Text = "Graph shall be undirected!";
                    return;
                }
            if (requiresNode.Contains(algName))
            {
                MainWindow.ApplicationState = States.WaitingForNodeSelection;
                MainWindow.MessageTextBlock.Text = "Please choose a node!";
                return;
            }

            alg = null;

            switch (algName)
            {
                case "Bipartization":
                    alg = new Bipartitzation(Colors.DarkOrange, Colors.DarkOrchid, Colors.Aqua);
                    break;
                case "Do MaxHeap":
                    alg=new DoMaxHeap();
                    break;
                case "Do MinHeap":
                    alg = new DoMinHeap();
                    break;
                case "Complete":
                    alg = new Complete();
                    break;
                case "Connectivity":
                    alg = new Connectivity();
                    break;
                case "S Connectivity" :
                    alg = new SConnectivity();
                    break;
                case "Floyd-Warshall":
                    alg = new FloydWarshall();
                    break;
                case "Kruskal":
                    alg = new Kruskal(Colors.Sienna, Colors.DarkGray, Colors.Red);
                    break;
            }

            if (alg == null) return;
            MainWindow.ApplicationState=States.AnimationRunning;
            alg.Execute();
        }
示例#30
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);
        }