This project is read-only.

Duplicate Vertex getting added

Nov 26, 2014 at 12:08 PM
Edited Nov 26, 2014 at 2:35 PM
I am a newbie to graphsharp. I have succesfully managed to use Graph# in Windows Form.
I have defined a custom vertex
 public class MyVertex
        public string Name{ get; private set; }

        public MyVertex(string name)
            User = name;
Now when I add vertexes and edges into my graphcontrol instance, which is defined as
 public classMyGraphControl : BidirectionalGraph<MyVertex, MyEdge>

There are duplicate vertex created.

This behaviour is observed when custom vertices are created. However if I do not define custom vertices. the graph is shown without any duplicate

Example :
       graphCtrl.AddVerticesAndEdge(new MyEdge(new MyVertex("A"),
                                                               new MyVertex("B")));

            graphCtrl.AddVerticesAndEdge(new MyEdge(new MyVertex("B"),
                                                              new MyVertex("C")));
The above creates 4 vertices and 2 edges in the graphcontrol (Vertex B is getting duplicated in the control)

A -> B
B -> C

whereas the expected behavior should be 3 vertices and 2 edges

A -> B -> C

Can somebody please suggest how I can overcome this?
Nov 26, 2014 at 12:44 PM
I think that new SysVertexControl("B") != new MyVertex("B") thats why you get 4 vertices instead of 3. You create two different objects, so if you want to create only 3 you should declare smth like var vertex = new MyVertex("B"); before the code presented above and use this vertex variable in your edge creation code.
Nov 26, 2014 at 2:44 PM
Sorry that was a typo error in the post, In my code MyVertex is used , I have edited and made the change in the post. so, the problem remains
Nov 26, 2014 at 3:16 PM
I even tried :
                MyVertex source = new MyVertex(strSource);
                MyVertex Target = new MyVertex (strTarget);

                if (!graphCtrl.ContainsVertex(source))
                if (!graphCtrl.ContainsVertex(Target))

               if (!graphCtrl.ContainsEdge(source, Target))
but duplicate Vertex always get created.
 if (!graphCtrl.ContainsVertex(source))
always returns false even though a given vertex has already been added.
Nov 26, 2014 at 3:29 PM
Edited Nov 26, 2014 at 3:31 PM
Every time you create an object with the new operator a new unique instance of an object is created. if you use cycles make sure you create source, target and all corresponding edges and add them to graphCtrl only once.

The code you provided doesn't make sense as you create new object (source, Target) and try to look for it in the collection using ContainsVertex method (ofcause it isn't there, it's the new one ). I can't see all the logic, but it seems that you create new objects in the cycle. I think you should create all needed vertex and edge connections before trying to add them into ctrl.

PS: If you want you can look at GraphX, it was initialy based on Graph# and far more advanced and user friendly.
Marked as answer by Jude_AJ on 11/30/2014 at 9:47 PM
Nov 26, 2014 at 4:05 PM
Yes you guessed it right. The code mentioned runs in a cycle, Pardon me for not adding the complete code. Does graphX overcome this problem? I will give it a try , thanks for the link.