public void JoinTest()
        {
            //Arrange
            int root1Rank = 5;
            Vertex root1 = new Vertex(100, new Point(100, 100));
            root1.Rank = root1Rank = 5; ;
            Vertex root2 = new Vertex(200, new Point(200, 200));
            root2.Rank = 1;

            //Act
            Vertex.Join(root1, root2);

            //Assert
            Assert.AreEqual(root2.Root.Name, root1.Name);

            //Arrange
            root2.Rank = root1Rank = 5; ;

            //Act
            Vertex.Join(root1, root2);

            //Assert
            Assert.AreEqual(root1.Root.Name, root2.Name);
            Assert.AreEqual(root2.Rank, root1Rank + 1);
        }
Example #2
0
 private void Clear()
 {
     _vertices = new List<Vertex>();
     _graph = new List<Edge>();
     _solved = false;
     _firstVertex = _secondVertex = null;
 }
Example #3
0
 public Edge(Vertex v1, Vertex v2, int cost, Point stringPosition)
 {
     this.v1 = v1;
     this.v2 = v2;
     this.cost = cost;
     this.stringPosition = stringPosition;
 }
Example #4
0
        internal Vertex GetRoot()
        {
            if (this.Root != this)// am I my own parent ? (am i the root ?)
            {
                this.Root = this.Root.GetRoot();// No? then get my parent
            }

            return this.Root;
        }
        public void VertexConstructorTest()
        {
            //Act
            Vertex target = new Vertex(_name, _position);

            //Assert
            Assert.AreEqual(target.Name, _name);
            Assert.AreEqual(target.Position.ToString(), _position.ToString());
            Assert.AreEqual(target.Rank, 0);
            Assert.AreEqual(target.Root.Name, _name);
        }
        public void GetRootTest()
        {
            //Arrange
            Vertex target = new Vertex(_name, _position);
            Vertex root = new Vertex(500, new Point());
            target.Root = root;

            //Act
            Vertex actual = target.GetRoot();

            //Assert
            Assert.AreEqual(root.Name, actual.Name);
        }
Example #7
0
 internal static void Join(Vertex root1, Vertex root2)
 {
     if (root2.Rank < root1.Rank)//is the rank of Root2 less than that of Root1 ?
     {
         root2.Root = root1;//yes! then Root1 is the parent of Root2 (since it has the higher rank)
     }
     else //rank of Root2 is greater than or equal to that of Root1
     {
         root1.Root = root2;//make Root2 the parent
         if (root1.Rank == root2.Rank)//both ranks are equal ?
         {
             root2.Rank++;//increment Root2, we need to reach a single root for the whole tree
         }
     }
 }
Example #8
0
        public void EdgeConstructorTest()
        {
            //Arrange
            Vertex v1 = new Vertex(100, new Point(100, 100));
            Vertex v2 = new Vertex(200, new Point(200, 200));
            int cost = 500;
            Point stringPosition = new Point(300,300);

            //Act
            Edge target = new Edge(v1, v2, cost, stringPosition);

            //Assert
            Assert.AreEqual(target.V1.Name, v1.Name);
            Assert.AreEqual(target.V2.Name, v2.Name);
            Assert.AreEqual(target.Cost, cost);
            Assert.AreEqual(target.StringPosition.ToString(), stringPosition.ToString());
        }
        public void QuickSortTest()
        {
            //Arrange
            Kruskal_Accessor target = new Kruskal_Accessor();
            Vertex v = new Vertex(1, new System.Drawing.Point(1, 1));
            IList<Edge> graph = new List<Edge>
            {
                new Edge(v,v,2,v.Position),
                new Edge(v,v,1,v.Position),
                new Edge(v,v,3,v.Position)
            };

            int left = 0;
            int right = graph.Count - 1;

            //Act
            target.QuickSort(graph, left, right);

            //Assert
            Assert.AreEqual(graph[0].Cost, 1);
            Assert.AreEqual(graph[1].Cost, 2);
            Assert.AreEqual(graph[2].Cost, 3);
        }
Example #10
0
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            Point clicked = new Point(e.X - _halfRadius, e.Y - _halfRadius);
            if (Control.ModifierKeys == Keys.Control)//if Ctrl is pressed
            {
                if (!_drawEdge)
                {
                    _firstVertex = GetSelectedVertex(clicked);
                    _drawEdge = true;
                }
                else
                {
                    _secondVertex = GetSelectedVertex(clicked);
                    _drawEdge = false;
                    if (_firstVertex != null && _secondVertex != null && _firstVertex.Name != _secondVertex.Name)
                    {
                        frmCost formCost = new frmCost();
                        formCost.ShowDialog();

                        Point stringPoint = GetStringPoint(_firstVertex.Position, _secondVertex.Position);
                        _graph.Add(new Edge(_firstVertex, _secondVertex, formCost._cost, stringPoint));
                        panel1.Invalidate();
                    }
                }
            }
            else
            {
                _vertices.Add(new Vertex(_vertices.Count, clicked));
                panel1.Invalidate();
            }
        }
 public void Union(Vertex x, Vertex y)
 {
     Link(FindSet(x), FindSet(y));
 }
        public void MakeSet(int id)
        {
            Vertex x = new Vertex();
            x.id = id;
            x.p = id;
            x.rank = 0;

            vertexes[x.id]=x;
        }
 public void Link(Vertex x, Vertex y)
 {
     if (x.rank > y.rank)
     {
         y.p = x.id;
     }
     else
     {
         x.p = y.id;
         if (x.rank == y.rank)
         {
             y.rank++;
         }
     }
 }
 public Vertex FindSet(Vertex x)
 {
     if (x.id != x.p)
     {
         x.p = FindSet(vertexes[x.p]).id;
     }
     return vertexes[x.p];
 }
 public Tree(int size)
 {
     vertexes = new Vertex[size];
 }