How to remove all edges of a certain vertex iteratively?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to remove all edges of a certain vertex iteratively?

Zhigang Wu

Dear jgrapht users,

 

In my problem, I want to traverse the graph to find a certain vertex, then delete the vertex accompanied with all the edges connected to it. Then I will traverse the remained graph again, find another vertex by some special rules, deleted the vertex and all the connected edges…

 

However, when I translate the procedure into JAVA codes, I always receive the same error message:

 

java.util.ConcurrentModificationException

         at java.util.ArrayList$Itr.checkForComodification(Unknown Source)

         at java.util.ArrayList$Itr.next(Unknown Source)

         at java.util.Collections$UnmodifiableCollection$1.next(Unknown Source)

         at org.jgrapht.graph.AbstractGraph.removeAllEdges(AbstractGraph.java:89)

         ……

 

The following is my JAVA code related to this:

 

                            for (Node myNode : myNodeList) {

                                   // Some codes to find the certain node, the name is myNode

                                   // Some operations

                                   myGraph.removeAllEdges(myGraph.edgesOf(myNode));

                                   myGraph.removeVertex(myNode);

                            }

 

This problem has bothered me for quite a long time, many thanks in advance!

 

Best Regards

 

Zhigang Wu


------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users
Reply | Threaded
Open this post in threaded view
|

Re: How to remove all edges of a certain vertex iteratively?

Joris Kinable
Java doesn't allow you to iterate over a list using a for each statement and modify the list at the same time. That leads to an ConcurrentModificationException. To resolve this issue, you either need to create a separate list of vertices you want to delete and invoke removeVertex on each of these vertices, or you should use an iterator and invoke iterator.remove(); 

Finally, removeVertex also removes all edges incident to that vertex, so you don't need a separate call to remove the edges of that vertex.

br,

Joris




On Fri, Aug 29, 2014 at 1:28 PM, Zhigang Wu <[hidden email]> wrote:

Dear jgrapht users,

 

In my problem, I want to traverse the graph to find a certain vertex, then delete the vertex accompanied with all the edges connected to it. Then I will traverse the remained graph again, find another vertex by some special rules, deleted the vertex and all the connected edges…

 

However, when I translate the procedure into JAVA codes, I always receive the same error message:

 

java.util.ConcurrentModificationException

         at java.util.ArrayList$Itr.checkForComodification(Unknown Source)

         at java.util.ArrayList$Itr.next(Unknown Source)

         at java.util.Collections$UnmodifiableCollection$1.next(Unknown Source)

         at org.jgrapht.graph.AbstractGraph.removeAllEdges(AbstractGraph.java:89)

         ……

 

The following is my JAVA code related to this:

 

                            for (Node myNode : myNodeList) {

                                   // Some codes to find the certain node, the name is myNode

                                   // Some operations

                                   myGraph.removeAllEdges(myGraph.edgesOf(myNode));

                                   myGraph.removeVertex(myNode);

                            }

 

This problem has bothered me for quite a long time, many thanks in advance!

 

Best Regards

 

Zhigang Wu


------------------------------------------------------------------------------
Slashdot TV.
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users



------------------------------------------------------------------------------
Slashdot TV.  
Video for Nerds.  Stuff that matters.
http://tv.slashdot.org/
_______________________________________________
jgrapht-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/jgrapht-users