예제 #1
0
파일: Graph.cs 프로젝트: Nan1t/Graphs
        public static void MakeSpanningTree(GraphNode root)
        {
            Canvas            canvas = MainWindow.GetInstance().CanvasGrid;
            Queue <GraphNode> queue  = new Queue <GraphNode>();

            queue.Enqueue(root);

            double distTop  = 10;
            double distLeft = canvas.ActualWidth / 2;

            while (queue.Count > 0)
            {
                GraphNode current = queue.Dequeue();

                if (!current.IsVisited)
                {
                    Canvas.SetTop(current, distTop);
                    Canvas.SetLeft(current, distLeft);
                    current.IsVisited = true;

                    Console.WriteLine("New level");
                }

                distTop += 64;

                foreach (GraphNode neighbour in Adjency[current])
                {
                    if (!neighbour.IsVisited)
                    {
                        neighbour.IsVisited = true;
                        queue.Enqueue(neighbour);

                        distLeft -= 64;

                        Canvas.SetTop(neighbour, distTop);
                        Canvas.SetLeft(neighbour, distLeft);

                        distLeft += 64 * 2;

                        List <GraphNode> toRemove = new List <GraphNode>();

                        foreach (GraphNode remainNode in Adjency[neighbour])
                        {
                            if (remainNode != current && remainNode.IsVisited)
                            {
                                GraphEdge edge = GetEdgeBetween(remainNode, neighbour);

                                if (edge != null)
                                {
                                    canvas.Children.Remove(edge.EdgeLine);
                                    canvas.Children.Remove(edge.WeightLabel);
                                    toRemove.Add(remainNode);
                                    Adjency[remainNode].Remove(neighbour);
                                    Edges[neighbour].Remove(edge);
                                    Edges[remainNode].Remove(edge);
                                }
                            }
                        }

                        foreach (GraphNode n in toRemove)
                        {
                            Adjency[neighbour].Remove(n);
                        }
                    }
                }
            }
        }
예제 #2
0
        /*
         * Format
         * x,y,index; x,y,index; x,y,index; x,y,index - Vertexes
         * index|index,index,index; index|index,index - Adjency
         */

        public static void Load(string file)
        {
            Graph.Clear();

            StreamReader reader = new StreamReader(@file, true);
            string       line   = null;
            int          index  = 0;

            while ((line = reader.ReadLine()) != null)
            {
                if (index == 0) // Vertexes data
                {
                    string[] arr = line.Split(';');

                    for (int i = 0; i < arr.Length; i++)
                    {
                        string[] data = arr[i].Split('.');

                        if (data.Length < 3)
                        {
                            continue;
                        }

                        GraphNode node = new GraphNode();

                        node.Index = int.Parse(data[2]);
                        node.IndexLabel.Content = node.Index;

                        Canvas.SetLeft(node, double.Parse(data[0]));
                        Canvas.SetTop(node, double.Parse(data[1]));
                        MainWindow.GetInstance().CanvasGrid.Children.Add(node);

                        Canvas.SetZIndex(node, 1);
                        Graph.AddNode(node);
                    }
                }

                if (index == 1) // Adjency data
                {
                    string[] arr = line.Split(';');
                    for (int i = 0; i < arr.Length; i++)
                    {
                        string[] keyData = arr[i].Split('|');

                        if (keyData.Length < 2)
                        {
                            continue;
                        }

                        int       key  = int.Parse(keyData[0]);
                        GraphNode node = Graph.GetNode(key);

                        if (node != null)
                        {
                            string[] neighbours = keyData[1].Split(',');

                            for (int j = 0; j < neighbours.Length - 1; j++)
                            {
                                int       nKey = int.Parse(neighbours[j]);
                                GraphNode n    = Graph.GetNode(nKey);

                                if (n != null)
                                {
                                    GraphEdge edge = Graph.Wire(node, n);

                                    if (edge != null)
                                    {
                                        MainWindow.GetInstance().CanvasGrid.Children.Add(edge.EdgeLine);
                                        Canvas.SetZIndex(edge.EdgeLine, 0);
                                    }
                                }
                            }
                        }
                    }
                }

                index++;
            }
        }
예제 #3
0
파일: Graph.cs 프로젝트: Nan1t/Graphs
        public static void RandEdgesWeight()
        {
            Random     rand = new Random();
            List <int> nums = new List <int>();

            foreach (List <GraphEdge> list in Edges.Values)
            {
                foreach (GraphEdge edge in list)
                {
                    if (!edge.IsVisited)
                    {
                        edge.IsVisited = true;
                        int num = rand.Next(100);

                        while (nums.Contains(num))
                        {
                            num = rand.Next(100);
                        }

                        edge.IsVisited = true;
                        edge.Weigth    = num;
                        nums.Add(num);

                        Label label = new Label();
                        label.Content    = num;
                        label.FontWeight = FontWeights.Bold;
                        label.Foreground = Brushes.Red;

                        MultiBinding bindingX = new MultiBinding();
                        MultiBinding bindingY = new MultiBinding();

                        bindingX.Converter = new WeightPosConverter(edge.EdgeLine, true);
                        bindingY.Converter = new WeightPosConverter(edge.EdgeLine, false);

                        Binding bindPosX1 = new Binding {
                            Source = edge.EdgeLine, Path = new PropertyPath(Line.X1Property)
                        };
                        Binding bindPosX2 = new Binding {
                            Source = edge.EdgeLine, Path = new PropertyPath(Line.X2Property)
                        };
                        Binding bindPosY1 = new Binding {
                            Source = edge.EdgeLine, Path = new PropertyPath(Line.Y1Property)
                        };
                        Binding bindPosY2 = new Binding {
                            Source = edge.EdgeLine, Path = new PropertyPath(Line.Y2Property)
                        };

                        bindingX.Bindings.Add(bindPosX1);
                        bindingX.Bindings.Add(bindPosX2);
                        bindingY.Bindings.Add(bindPosY1);
                        bindingY.Bindings.Add(bindPosY2);

                        label.SetBinding(Canvas.LeftProperty, bindingX);
                        label.SetBinding(Canvas.TopProperty, bindingY);

                        edge.WeightLabel = label;

                        MainWindow.GetInstance().CanvasGrid.Children.Add(label);
                    }
                }
            }
        }