Thursday 18 October 2012

Cycle detection with Scala

Is there a nicer way to do cycle detection in Scala than the following?

def isCyclic(graphManager: PricingGraphManager) = graphManager.nodes.exists(hasCycle(graphManager, _, Seq()))

private def hasCycle(graphManager: PricingGraphManager, node:PricingGraphNode, visitedNodes:Seq[PricingGraphNode]) : Boolean = 
  if (visitedNodes.contains(node)) true else node.children.exists(hasCycle(graphManager, _, node +: visitedNodes)

No comments:

Blog Archive