Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove edge between two vertices?

I want to remove edge between two vertices, so my code in java tinkerpop3 as below

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
        if(g.V(toV).inE(edgeLabel).bothV().hasId(fromV.id()).hasNext()){
            List<Edge> edgeList = g.V(toV).inE(edgeLabel).toList();
            for (Edge edge:edgeList){
                if(edge.outVertex().id().equals(fromV.id())) {
                    TitanGraph().tx();
                    edge.remove();                    
                    TitanGraph().tx().commit();
                    return;//Remove edge ok, now return.
                }
            }
        }
    }

Is there a simpler way to remove edge between two vertices by a direct query to that edge and remove it? Thank for your help.

like image 874
MichaelP Avatar asked Jan 04 '16 10:01

MichaelP


People also ask

How do you remove an edge from a graph?

Example: G = rmedge(G,1,2) removes the edge between node 1 and node 2 from graph G . Example: G = rmedge(G,{'a' 'b'},{'d' 'c'}) removes two edges from graph G , the first of which is between node 'a' and node 'd' .

What is the time complexity of removing an edge from an adjacency matrix given the vertices are integral values?

Both have time complexity O(V + E), where V is the number of vertices and E is the number of edges.

How to dissolve vertices into a single edge?

In the case of two edges, merging them into a single edge. When dissolving vertices into surrounding faces, you can often end up with very large, uneven n-gons. The face split option limits dissolve to only use the corners of the faces connected to the vertex.

What happens if there is no edge between two vertices?

If no edge exists between these two vertices, then g [i] [j] = 0 and g [j] [i] = 0. The graph after removal and adjacency matrix after removal of edge between vertex X and Y:

How to remove an edge while keeping surrounding geometry?

In Edge Select mode (keyboard shortcut 2 ), select the edge you want to get rid of, press the space bar and type dissolve ... then choose Dissolve Edges. Show activity on this post. To remove an edge while keeping surrounding geometry, in Blender terminology you Dissolve it. Delete then Dissolve Edges or...

How do you insert an edge between two vertices?

Inserting an edge: To insert an edge between two vertices suppose i and j, set the corresponding values in the adjacency matrix equal to 1, i.e. g =1 and g =1 if both the vertices i and j exists.


1 Answers

Here's an example of how to drop edges between two vertices (where you just have the ids of those vertices:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[7][1-knows->2]
==>e[8][1-knows->4]

For purpose of the example, let's say we want to drop edges between vertex 1 and vertex 2. We could find those with:

gremlin> g.V(1).bothE().where(otherV().hasId(2))
==>e[7][1-knows->2]

and then remove it with:

gremlin> g.V(1).bothE().where(otherV().hasId(2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

If you have the actual vertices, then you could just do:

gremlin> g.V(v1).bothE().where(otherV().is(v2)).drop()
gremlin> g.V(1).bothE()
==>e[9][1-created->3]
==>e[8][1-knows->4]

You could re-write your function as:

private void removeEdgeOfTwoVertices(Vertex fromV, Vertex toV,String edgeLabel,GraphTraversalSource g){
    g.V(fromV).bothE().hasLabel(edgeLabel).where(__.otherV().is(toV)).drop().iterate();
    g.tx().commit();    
}
like image 137
stephen mallette Avatar answered Oct 29 '22 15:10

stephen mallette