Finding nodes with only outer connections

25 views
Skip to first unread message

Milan Cvejic

unread,
Jul 21, 2018, 9:43:40 AM7/21/18
to scala-graph
Hi,
I am currently trying to figure out a way to find just nodes that don't have any inner connections, but only outer. 

I was going over documentation and trying to figure if there is something like that, but if I am not wrong, I would need to get list of all nodes from graph and check if there are some inner connections?

val g = Graph(1->2, 1->3, 4->5)
val containmentNodes = mutable.Set[Int]()
g.nodes.foreach(n => {
if (!g.get(n).hasPredecessors) {
println(n)
containmentNodes.add(n.value._1)
}
})

println(containmentNodes.mkString(", "))

Is there any more efficient way to do that?

Thanks

Peter Empen

unread,
Jul 22, 2018, 8:11:48 AM7/22/18
to scala...@googlegroups.com
Right, this operation will be O(V) if your predicate does not inspect edges, otherwise O(V + log(E)) up to O(V + E). So isIsolated is more efficient. Some examples:

val isolatedNodes = g.nodes.filter(_.isIsolated)

resp.

val isolatedNodes = g.nodes.withFilter(_.isIsolated).map(_.toOuter.<your field>)

For the complete list of predicates like
hasPredecessors see http://www.scala-graph.org/api/core/api/scalax/collection/GraphLike$InnerNode.html
Reply all
Reply to author
Forward
0 new messages