Counting the number of neighbors of a given type

27 views
Skip to first unread message

José Devezas

unread,
Oct 25, 2017, 8:59:14 AM10/25/17
to HyperGraphDB
Hello,

I am currently trying to use HypergraphDB for the first time and I was wondering what is the best way to implement the following task: given a source node, count the number of nodes of a given type linked to this source node (nevermind direction).

Do I need to extract all neighbors and then iterate over them checking the node type? Or can I do this directly with hg.count somehow?

Best regards,
José Devezas

Borislav Iordanov

unread,
Nov 4, 2017, 8:26:15 PM11/4/17
to hyperg...@googlegroups.com
Hi,

Apologies for the late response!

Every link/edge is reified and HGDB stores the links themselves associated with a node. So in terms of performance you can quickly compute all links of a given type associated with a node, but not the node’s “neighbors”.

However, often what happens is that the type of a link determines the type of the neighbor node. In that case, you can just count the links. So say you are interested in neighbors of type T and those neighbors are only linked through a link of type RelatedToT. You can get the count like this then:

graph.count(hg.and(hg.incident(node), hg.type(RelatedToT)))

Otherwise, you do have to iterate and check the type yes. If that becomes a problem, you have the option of create a TargetToTargetIndexer which allows you to index nodes by their neighbors. You can the join/intersect that index with the by type index. The API is clumsy unfortunately, so let me know if you are going that route and have trouble figuring how to do it.

Cheers,
Boris
> --
> You received this message because you are subscribed to the Google Groups "HyperGraphDB" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to hypergraphdb...@googlegroups.com.
> To post to this group, send email to hyperg...@googlegroups.com.
> Visit this group at https://groups.google.com/group/hypergraphdb.
> For more options, visit https://groups.google.com/d/optout.

José Devezas

unread,
Nov 6, 2017, 8:48:17 AM11/6/17
to HyperGraphDB
Hello,

Thank you for the extensive answer!

So, the graph.count from your example would count all links incident to "node", but not the number of other nodes in the link, right? Unless of course a link is only defined between two nodes, which is not the case.

Let's say that there is a link L1 of type RelatedToT, incident in nodes n1, n2, n3, n4 (i.e., it's a hyperedge with multiple nodes). Let's say that we are in n1 and want to count the number of other nodes in L1 (i.e., 3 nodes). We can only do this by iterating and type checking, right?

I'm trying to do this as efficiently as possible, but, either way, everything I need would be in memory at this point and the cost of counting should be small.

I think you answered my question!

Best regards,
José Devezas

Borislav Iordanov

unread,
Nov 7, 2017, 12:59:08 PM11/7/17
to hyperg...@googlegroups.com
Hi,

On Nov 6, 2017, at 8:48 AM, José Devezas <joselui...@gmail.com> wrote:

Hello,

Thank you for the extensive answer!

So, the graph.count from your example would count all links incident to "node", but not the number of other nodes in the link, right? Unless of course a link is only defined between two nodes, which is not the case.

Let's say that there is a link L1 of type RelatedToT, incident in nodes n1, n2, n3, n4 (i.e., it's a hyperedge with multiple nodes). Let's say that we are in n1 and want to count the number of other nodes in L1 (i.e., 3 nodes). We can only do this by iterating and type checking, right?

Yes, that is correct. The query API is too poor/limited to allow you to do that in a more elegant way. 


I’m trying to do this as efficiently as possible, but, either way, everything I need would be in memory at this point and the cost of counting should be small.

Yes, I wish I could show you a simple expression that does it, but I can’t at the moment :(

Best,
Boris 
Reply all
Reply to author
Forward
0 new messages