Get All Edges For a Given Node

79 views
Skip to first unread message

Jake True

unread,
Apr 22, 2014, 8:05:15 PM4/22/14
to
Hello All,

What is the best way to get all incoming edges for a given node, when the edge type is unknown?  If the edge type is known it is simple but what if I don't know/care about the edge type?

The only way I've devised is highly inefficient.  Any suggestions?  The example below is in C# .NET. 

       public TypeList GetIncomingEdgesFromGateNode(long gateNode)
        {
            // Empty TypeList: Add edges later
            TypeList edges = new TypeList();
 
            // Get ALL edge types
            TypeList allEdgesList = g.FindEdgeTypes();
 
            using (TypeListIterator allEdgesIterator = allEdgesList.Iterator())
            {
                // Iterate over ALL edge types
                while (allEdgesIterator.HasNext())
                {
                    // Get edge type
                    int nextEdge = allEdgesIterator.Next();
 
                    // Any neighbors?
                    Objects neighbors = g.Neighbors(gateNodenextEdgeEdgesDirection.Ingoing);
                    
                    // If neighbors exist
                    if (neighbors.Count() > 0)
                    {
                        // Add edge to edges TypeList
                        edges.Add(nextEdge);
                    }
 
                    // Get rid of neighbors
                    neighbors.Close();
                    neighbors.Dispose();
                }
 
            }
 
            // Done with allEdgesList
            allEdgesList.Dispose();
 
            return edges;
        }

Thanks in advance,
Jake






c3po.ac

unread,
Apr 23, 2014, 6:06:48 AM4/23/14
to spar...@googlegroups.com
Hi,

Your example is fine if you are interested in knowing the edge TYPES of the incoming edges (not the edges) for the given node and the edge types were created with the neighbors materialized.

If the neighbors are not indexed or you want to find the edges, then it may be better to use Explode instead of Neighbors.

public TypeList GetIncomingEdgesFromGateNode(long gateNode)
       
{
           
// Empty TypeList: Add edge TYPES later
           
TypeList edgeTypes = new TypeList();
 
           
// Get ALL edge types
           
TypeList allEdgeTypesList = g.FindEdgeTypes();
 
           
// Empty Objecs: All the incoming edges (not types)
           
// Not needed if you really only want the edge types
           
Objects edges = session.NewObjects();
 
           
using (TypeListIterator allEdgeTypesList = allEdgeTypesList.Iterator())
           
{
               
// Iterate over ALL edges
               
while (allEdgeTypesList.HasNext())
               
{
                   
// Get edge TYPE ID
                   
int nextEdgeType = allEdgeTypesList.Next();
 
                   
// Get the incoming edges of this type
                   
Objects nextEdges = g.Explode(gateNode, nextEdgeType, EdgesDirection.Ingoing);
 
                   
// If incoming edges exist
                   
if (nextEdges.Count() > 0)
                   
{
                       
// Add edge TYPE to edges TypeList
                        edgeTypes
.Add(nextEdgeType)
                       
// Keep all the edges
                        edges
.Union(nextEdges); // only if you want to find the edges, not only the types
                   
}
 
                   
// Get rid of nextEdges
                    nextEdges
.Close();
               
}
           
}
 
           
// Done with allEdgeTypesList
            allEdgeTypesList
.Dispose();
 
           
// Return the types of the incoming edges (edgeTypes)
           
// Or return the incoming edges (edges)
            edges
.Close(); // If you only want the edge TYPES, the edges Objects was not really needed
           
return edgeTypes;
       
}

Best regards.


El dimecres 23 d’abril de 2014 1:49:44 UTC+2, Jake True va escriure:

Jake True

unread,
Apr 23, 2014, 11:56:53 AM4/23/14
to spar...@googlegroups.com
Thank you for the quick reply.  Your input is greatly appreciated.

I understand your confusion, I should have been more clear.  I am only interested in the Edge Types for this particular function.  The TypeList of Edge Types obtained from this function is an input for a Traversal elsewhere in the program.  I will rename this function to: GetIncomingEdgeTypesFromGateNode.

That being said, there are other parts of the program where I will use your example of getting the edges themselves.

Thanks again,
Jake
Reply all
Reply to author
Forward
0 new messages