-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'release/0.7.0'. fixes #20.
- Loading branch information
Showing
10 changed files
with
400 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package graph | ||
|
||
import groovy.transform.PackageScope | ||
|
||
/** | ||
* The results from classifyEdges in Graph. | ||
*/ | ||
class EdgeClassification { | ||
Graph forrest = [] | ||
List<Edge> backEdges = [] | ||
List<Edge> treeEdges = [] | ||
List<Edge> forwardEdges = [] | ||
List<Edge> crossEdges = [] | ||
|
||
/** | ||
* The resulting edge type. Used in addEdge to notify the action closure. | ||
*/ | ||
enum EdgeType { | ||
/** | ||
* When the followed edge is GREY | ||
*/ | ||
BACK_EDGE, | ||
/** | ||
* When the followed edge is WHITE | ||
*/ | ||
TREE_EDGE, | ||
/** | ||
* When the followed edge is BLACK and the connecting vertex is not in the forrest | ||
*/ | ||
FORWARD_EDGE, | ||
/** | ||
* When the followed edge is BLACK and the connecting vertex is in the forreest | ||
*/ | ||
CROSS_EDGE | ||
} | ||
|
||
/** | ||
* Adds an edge calling action with the classification. | ||
* @param graph | ||
* @param edge | ||
* @param from | ||
* @param to | ||
* @param toColor | ||
* @param action | ||
*/ | ||
@PackageScope | ||
void addEdge(Graph graph, Edge edge, String from, String to, Graph.TraversalColor toColor, Closure action) { | ||
def edgeType | ||
switch(toColor) { | ||
case Graph.TraversalColor.WHITE: | ||
forrest.addVertex(graph.vertex(from)) | ||
forrest.addVertex(graph.vertex(to)) | ||
forrest.addEdge(edge) | ||
treeEdges << edge | ||
edgeType = EdgeClassification.EdgeType.TREE_EDGE | ||
break | ||
|
||
case Graph.TraversalColor.GREY: | ||
backEdges << edge | ||
edgeType = EdgeClassification.EdgeType.BACK_EDGE | ||
break | ||
|
||
case Graph.TraversalColor.BLACK: | ||
if(forrest.vertices[to]) { | ||
crossEdges << edge | ||
edgeType = EdgeClassification.EdgeType.CROSS_EDGE | ||
} else { | ||
forwardEdges << edge | ||
edgeType = EdgeClassification.EdgeType.FORWARD_EDGE | ||
} | ||
break | ||
|
||
default: | ||
throw new IllegalStateException("Edge from $from to $to needs to be WHITE, GREY, or BLACK.") | ||
} | ||
action(from, to, edgeType) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package graph | ||
|
||
import spock.lang.Specification | ||
|
||
import static graph.Graph.TraversalColor.* | ||
import static graph.EdgeClassification.EdgeType.* | ||
|
||
class EdgeClassificationSpec extends Specification { | ||
def graph = new Graph() | ||
def edgeClassification = new EdgeClassification() | ||
|
||
def setup() { | ||
graph.with { | ||
vertex 'A' | ||
vertex 'B' | ||
edge 'A', 'B' | ||
} | ||
} | ||
|
||
def 'test from and to in addEdge'() { | ||
setup: | ||
def fromCheck | ||
def toCheck | ||
|
||
when: | ||
edgeClassification.addEdge(graph, graph.edge('A', 'B'), 'A', 'B', WHITE) { from, to, type -> | ||
fromCheck = from | ||
toCheck = to | ||
} | ||
|
||
then: | ||
fromCheck == 'A' | ||
toCheck == 'B' | ||
} | ||
|
||
def 'test WHITE edge in addEdge'() { | ||
setup: | ||
def typeCheck | ||
|
||
when: | ||
edgeClassification.addEdge(graph, graph.edge('A', 'B'), 'A', 'B', WHITE) { from, to, type -> | ||
typeCheck = type | ||
} | ||
|
||
then: | ||
typeCheck == TREE_EDGE | ||
edgeClassification.forrest.vertices['A'] == graph.vertex('A') | ||
edgeClassification.forrest.vertices['B'] == graph.vertex('B') | ||
edgeClassification.forrest.vertices.size() == 2 | ||
edgeClassification.forrest.edge('A', 'B') == graph.edge('A', 'B') | ||
edgeClassification.forrest.edges.size() == 1 | ||
edgeClassification.treeEdges.contains(graph.edge('A', 'B')) | ||
} | ||
|
||
def 'test GREY edge in addEdge'() { | ||
setup: | ||
def fromCheck | ||
def toCheck | ||
def typeCheck | ||
|
||
when: | ||
edgeClassification.addEdge(graph, graph.edge('A', 'B'), 'A', 'B', GREY) { from, to, type -> | ||
fromCheck = from | ||
toCheck = to | ||
typeCheck = type | ||
} | ||
|
||
then: | ||
fromCheck == 'A' | ||
toCheck == 'B' | ||
typeCheck == BACK_EDGE | ||
edgeClassification.backEdges.contains(graph.edge('A', 'B')) | ||
} | ||
|
||
def 'test BLACK forward edge in addEdge'() { | ||
setup: | ||
def fromCheck | ||
def toCheck | ||
def typeCheck | ||
|
||
when: | ||
edgeClassification.addEdge(graph, graph.edge('A', 'B'), 'A', 'B', BLACK) { from, to, type -> | ||
fromCheck = from | ||
toCheck = to | ||
typeCheck = type | ||
} | ||
|
||
then: | ||
fromCheck == 'A' | ||
toCheck == 'B' | ||
typeCheck == FORWARD_EDGE | ||
edgeClassification.forwardEdges.contains(graph.edge('A', 'B')) | ||
} | ||
|
||
def 'test BLACK cross edge in addEdge'() { | ||
setup: | ||
def fromCheck | ||
def toCheck | ||
def typeCheck | ||
edgeClassification.forrest.addVertex(graph.vertex('B')) | ||
|
||
when: | ||
edgeClassification.addEdge(graph, graph.edge('A', 'B'), 'A', 'B', BLACK) { from, to, type -> | ||
fromCheck = from | ||
toCheck = to | ||
typeCheck = type | ||
} | ||
|
||
then: | ||
fromCheck == 'A' | ||
toCheck == 'B' | ||
typeCheck == CROSS_EDGE | ||
edgeClassification.crossEdges.contains(graph.edge('A', 'B')) | ||
} | ||
} |
Oops, something went wrong.