From 67d62e0664eb21442510b8c7922e856345164fcb Mon Sep 17 00:00:00 2001 From: ddp Date: Wed, 23 Jul 2003 14:07:18 +0000 Subject: [PATCH] Adding DFS Patch from Tomasz Skutnik Here's patch against HEAD version of DFS algorithm in jakarta-commons-sandbox/graph2 package - it uses less memory, which is important for very large graphs (as in my case). Additionally DFS now can use lazy graph implementations without causing full, in-memory vertex retrieval on search initialization. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/sandbox/graph2/trunk@144643 13f79535-47bb-0310-9956-ffa450edef68 --- .../commons/graph/algorithm/search/DFS.java | 22 +++++-------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/commons/graph/algorithm/search/DFS.java b/src/java/org/apache/commons/graph/algorithm/search/DFS.java index 8fccbc2..3dbbb83 100644 --- a/src/java/org/apache/commons/graph/algorithm/search/DFS.java +++ b/src/java/org/apache/commons/graph/algorithm/search/DFS.java @@ -94,7 +94,8 @@ public class DFS */ public String getColor(Vertex v) { - return (String) colors.get(v); + String color = (String)colors.get(v); + return color != null ? color : WHITE; } @@ -109,7 +110,7 @@ public class DFS Vertex v = graph.getTarget(e); - if (colors.get(v) == WHITE) + if (getColor(v) == WHITE) { visitVertex(graph, v, visitor); } @@ -124,7 +125,6 @@ public class DFS Vertex v, Visitor visitor) { - colors.remove(v); colors.put(v, GRAY); visitor.discoverVertex(v); @@ -138,7 +138,6 @@ public class DFS visitor.finishVertex(v); - colors.remove(v); colors.put(v, BLACK); } @@ -149,12 +148,7 @@ public class DFS Vertex root, Visitor visitor) { - Iterator vertices = graph.getVertices().iterator(); - while (vertices.hasNext()) - { - colors.put(vertices.next(), WHITE); - } - + colors.clear(); visitor.discoverGraph(graph); visitVertex(graph, root, visitor); @@ -166,14 +160,10 @@ public class DFS * visit - Visits all nodes in the graph. */ public void visit( DirectedGraph graph, Visitor visitor ) { - Iterator vertices = graph.getVertices().iterator(); - while (vertices.hasNext()) { - colors.put( vertices.next(), WHITE ); - } - + colors.clear(); visitor.discoverGraph( graph ); - vertices = graph.getVertices().iterator(); + Iterator vertices = graph.getVertices().iterator(); while (vertices.hasNext()) { Vertex v = (Vertex) vertices.next();