Contributed by Asher Moody, Development Support Engineer

When writing an application using IG you will likely run into a situation where you want to see who or what is connected to a particular vertex. For example, you could have a social network and want to see who a person’s friends are.

When looking at the Vertex API you will see a few options:

 List<EdgeHandle>   findEdgesToNeighbor(long neighborId)
          Retrieves a list of all edges that connect this vertex to a vertex with the given id.

 Iterable<EdgeHandle>   getEdges()
          Retrieves an iterator for all edges connected to this vertex.

 Iterable<EdgeHandle>   getEdges(Qualifier edgeQualifier)
          Retrieves an iterator for all edges connected to this vertex that satisfy the provided Qualifier.

 Iterable<Hop>   getNeighborHops()
          Retrieves an iterator for all neighboring hops (edge-vertex pairs).

 Iterable<Hop>   getNeighborHops(Qualifier qualifier)
          Retrieves an iterator for all neighboring hops (edge-vertex pairs) that satisfy the supplied Qualifier.

 Iterable<VertexHandle>   getNeighbors()
          Retrieves an iterator for all neighboring (or adjacent) vertices.

 Iterable<VertexHandle>   getNeighbors(Qualifier qualifier)
          Retrieves an iterator for all neighboring (or adjacent) vertices that satisfy the provided qualifier.

 boolean   isNeighbor(long neighborId)
          Indicates whether a vertex with the given id is a neighbor of (or adjacent to) this vertex.

The easiest choice would be to call getNeighbors to retrieve an iterator of the person’s neighbors. But what if they are also connected to groups, books, places, employers, schools, music, and movies? You aren’t interested in all that other stuff if you are just looking for their friends. You could pass in a Qualifier when you call getNeighbors to only search for neighbors of class Friend. It turns out that this might not always be the best solution.

It all comes down to vertices and edges. Everything in IG is an object. A handle is a lightweight reference to an object. You can use them to get ids or edge weights without pulling the actual vertex or edge into memory. This can be very useful if you store a lot of information in your vertices and edges. But wait, there’s more! Internally, IG is storing information about how these edges and vertices connect. When you call getNeighbors IG needs to pull that internal information, edge information, and vertex information. That can be a lot of data. If you call getEdges with or without a Qualifier IG only needs to access the connector information and edge handle data. This can be a lot more efficient.

So why would you use getNeighbors or what about those other methods? Here are some general guidelines:

findEdgesToNeighbor(long neighborId)
Use this method if you are looking for edge specific information for a particular connection between vertices.

getEdges()
Use this method if you are scanning for information across all or most of the edges for a given vertex.

getEdges(Qualifier edgeQualifier)
Use this method if you are scanning for information across a variable portion of the edges for a given vertex.

getNeighborHops()
Use this method if you are scanning for information across all or most of the edges and vertices connected to a given vertex.

getNeighborHops(Qualifier qualifier)
Use this method if you are scanning for information across a significant portion of the edges and vertices connected to a given vertex.

getNeighbors()
Use this method if you are scanning for information across all or most of the vertices connected to a given vertex.

getNeighbors(Qualifier qualifier)
Use this method if you are scanning for information across a significant portion of vertices connected to a given vertex.

isNeighbor(long neighborId)
Use this method if you want to know if two vertices are connected. This could be a useful check before adding an edge so you do not duplicate edges.

Example: 

All this talk is great but let’s put it into use with a real example. We used the above methods in a demonstration application that connects prescription drug information and patients. The image below shows a specific drug called Aminophylline connected to a few hypothetical patients. Aminophylline contains connections to all the people taking it (which can be hundreds, thousands, or more connections!). It is also connected to its ingredients and other drugs that it negatively interacts with.

Sample graph

If you wanted to access all of the patients taking this drug you could call getNeighbors(patientQualifier). Then you could iterate over them sending out educational information about the drug. If you were only interested in seeing if a particular person was taking this drug you should call isNeighbor(). Finally, if you wanted information about the prescription that is stored on the edge object you could call findEdgesToNeighbor(drugId) from the patient.

Sample graph

If you are interested in gathering information about interactions between drugs you could call getEdges(interactionQualifier) if you only need the information contained in the edge object. From each edge you could get the vertex neighbor information as necessary.

Sample graph

Finally, if you are more interested in allergies to specific ingredients you could call getNeighbors() or getNeighborHops() from the ingredient. This is because ingredients have only a few drugs they are connected to while they are connected to many patients (an ingredient connected to a patient denotes that the patient has an allergy to that ingredient).

Sample graph

SHARE THIS POST
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedIn