Skip to content
This repository was archived by the owner on Feb 8, 2019. It is now read-only.

[WIP] Use scala-graph for internal graph implementation #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix graph copy and topologicalOrder with cycles
manuzhang committed May 8, 2018

Verified

This commit was signed with the committer’s verified signature.
KyleFromNVIDIA Kyle Edwards
commit 2d3151a086a29442d1c035e78617ab00a6fad735
14 changes: 6 additions & 8 deletions core/src/main/scala/org/apache/gearpump/util/Graph.scala
Original file line number Diff line number Diff line change
@@ -188,7 +188,7 @@ class Graph[N, E](private val graph: SGraph[N, LDiEdge]) extends Serializable {
* clone the graph
*/
def copy: Graph[N, E] = {
new Graph[N, E](graph)
new Graph[N, E](SGraph.from(graph.nodes, graph.edges))
}

/**
@@ -233,6 +233,8 @@ class Graph[N, E](private val graph: SGraph[N, LDiEdge]) extends Serializable {
graph.topologicalSort match {
case Right(t) =>
t.map(_.value).toIterator
case Left(_) =>
topologicalOrderWithCirclesIterator
}
}

@@ -302,12 +304,8 @@ class Graph[N, E](private val graph: SGraph[N, LDiEdge]) extends Serializable {
* http://www.drdobbs.com/database/topological-sorting/184410262
*/
def topologicalOrderWithCirclesIterator: Iterator[N] = {
if (hasCycle()) {
val topo = getAcyclicCopy().topologicalOrderIterator
topo.flatMap(_.sortBy(indices).iterator)
} else {
topologicalOrderIterator
}
val topo = getAcyclicCopy().topologicalOrderIterator
topo.flatMap(_.sortBy(indices).iterator)
}

private def getAcyclicCopy(): Graph[mutable.MutableList[N], E] = {
@@ -360,7 +358,7 @@ class Graph[N, E](private val graph: SGraph[N, LDiEdge]) extends Serializable {
graph.topologicalSort match {
case Right(t) =>
t.toLayered.foreach { case (level, nodes) =>
nodes.toList.sortBy(indices).foreach { n =>
nodes.foreach { n =>
map += (n.value -> level)
}
}
4 changes: 3 additions & 1 deletion core/src/test/scala/org/apache/gearpump/util/GraphSpec.scala
Original file line number Diff line number Diff line change
@@ -119,7 +119,7 @@ class GraphSpec extends PropSpec with PropertyChecks with Matchers {
assert(levelMap("C") < levelMap("F"))
}

property("copy should return a immutalbe new Graph") {
property("copy should return an immutable new Graph") {
val graph = Graph.empty[String, String]
val defaultEdge = "edge"
graph.addVertex("A")
@@ -193,6 +193,8 @@ class GraphSpec extends PropSpec with PropertyChecks with Matchers {
val topoWithCircles = graph.topologicalOrderWithCirclesIterator
val trueTopoWithCircles = Iterator[Int](0, 8, 1, 3, 4, 2, 6, 5, 7, 10, 9)

topoWithCircles.foreach(println)

assert(trueTopoWithCircles.zip(topoWithCircles).forall(x => x._1 == x._2))
}