-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-3650][GraphX] Triangle Count handles reverse edges incorrectly #11290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
428fa26
Change bytes to bits in RoutingTablePartition.toMessage
insidedctm cf66402
Merge remote-tracking branch 'upstream/master'
insidedctm 96fcc0a
Improving Triangle Count
jegonzal 1edc09d
fixing bug in unit tests where bi-directed edges lead to duplicate tr…
jegonzal 47673ca
factored out code for canonicalization
jegonzal c6cd747
SPARK-3650 updates to PR 2495 to work with current master
insidedctm c8ad0bd
revert unexpected changes to R/pkg/DESCRIPTION
insidedctm d56ad1a
add closing </li> tags
insidedctm 1e6f5d2
code tidy up of run
insidedctm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,32 +20,55 @@ package org.apache.spark.graphx.lib | |
| import scala.reflect.ClassTag | ||
|
|
||
| import org.apache.spark.graphx._ | ||
| import org.apache.spark.graphx.PartitionStrategy.EdgePartition2D | ||
|
|
||
| /** | ||
| * Compute the number of triangles passing through each vertex. | ||
| * | ||
| * The algorithm is relatively straightforward and can be computed in three steps: | ||
| * | ||
| * <ul> | ||
| * <li>Compute the set of neighbors for each vertex | ||
| * <li>For each edge compute the intersection of the sets and send the count to both vertices. | ||
| * <li> Compute the set of neighbors for each vertex | ||
| * <li> For each edge compute the intersection of the sets and send the count to both vertices. | ||
| * <li> Compute the sum at each vertex and divide by two since each triangle is counted twice. | ||
| * </ul> | ||
| * | ||
| * Note that the input graph should have its edges in canonical direction | ||
| * (i.e. the `sourceId` less than `destId`). Also the graph must have been partitioned | ||
| * using [[org.apache.spark.graphx.Graph#partitionBy]]. | ||
| * There are two implementations. The default `TriangleCount.run` implementation first removes | ||
| * self cycles and canonicalizes the graph to ensure that the following conditions hold: | ||
| * <ul> | ||
| * <li> There are no self edges | ||
| * <li> All edges are oriented src > dst | ||
| * <li> There are no duplicate edges | ||
| * </ul> | ||
| * However, the canonicalization procedure is costly as it requires repartitioning the graph. | ||
| * If the input data is already in "canonical form" with self cycles removed then the | ||
| * `TriangleCount.runPreCanonicalized` should be used instead. | ||
| * | ||
| * {{{ | ||
| * val canonicalGraph = graph.mapEdges(e => 1).removeSelfEdges().canonicalizeEdges() | ||
| * val counts = TriangleCount.runPreCanonicalized(canonicalGraph).vertices | ||
| * }}} | ||
| * | ||
| */ | ||
| object TriangleCount { | ||
|
|
||
| def run[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED]): Graph[Int, ED] = { | ||
| // Remove redundant edges | ||
| val g = graph.groupEdges((a, b) => a).cache() | ||
| // Transform the edge data something cheap to shuffle and then canonicalize | ||
| val canonicalGraph = graph.mapEdges(e => true).removeSelfEdges().convertToCanonicalEdges() | ||
| // Get the triangle counts | ||
| val counters = runPreCanonicalized(canonicalGraph).vertices | ||
| // Join them bath with the original graph | ||
| graph.outerJoinVertices(counters) { (vid, _, optCounter: Option[Int]) => | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: just |
||
| optCounter.getOrElse(0) | ||
| } | ||
| } | ||
|
|
||
|
|
||
| def runPreCanonicalized[VD: ClassTag, ED: ClassTag](graph: Graph[VD, ED]): Graph[Int, ED] = { | ||
| // Construct set representations of the neighborhoods | ||
| val nbrSets: VertexRDD[VertexSet] = | ||
| g.collectNeighborIds(EdgeDirection.Either).mapValues { (vid, nbrs) => | ||
| val set = new VertexSet(4) | ||
| graph.collectNeighborIds(EdgeDirection.Either).mapValues { (vid, nbrs) => | ||
| val set = new VertexSet(nbrs.length) | ||
| var i = 0 | ||
| while (i < nbrs.size) { | ||
| // prevent self cycle | ||
|
|
@@ -56,14 +79,14 @@ object TriangleCount { | |
| } | ||
| set | ||
| } | ||
|
|
||
| // join the sets with the graph | ||
| val setGraph: Graph[VertexSet, ED] = g.outerJoinVertices(nbrSets) { | ||
| val setGraph: Graph[VertexSet, ED] = graph.outerJoinVertices(nbrSets) { | ||
| (vid, _, optSet) => optSet.getOrElse(null) | ||
| } | ||
|
|
||
| // Edge function computes intersection of smaller vertex with larger vertex | ||
| def edgeFunc(ctx: EdgeContext[VertexSet, ED, Int]) { | ||
| assert(ctx.srcAttr != null) | ||
| assert(ctx.dstAttr != null) | ||
| val (smallSet, largeSet) = if (ctx.srcAttr.size < ctx.dstAttr.size) { | ||
| (ctx.srcAttr, ctx.dstAttr) | ||
| } else { | ||
|
|
@@ -80,15 +103,18 @@ object TriangleCount { | |
| ctx.sendToSrc(counter) | ||
| ctx.sendToDst(counter) | ||
| } | ||
|
|
||
| // compute the intersection along edges | ||
| val counters: VertexRDD[Int] = setGraph.aggregateMessages(edgeFunc, _ + _) | ||
| // Merge counters with the graph and divide by two since each triangle is counted twice | ||
| g.outerJoinVertices(counters) { | ||
| (vid, _, optCounter: Option[Int]) => | ||
| val dblCount = optCounter.getOrElse(0) | ||
| // double count should be even (divisible by two) | ||
| assert((dblCount & 1) == 0) | ||
| dblCount / 2 | ||
| graph.outerJoinVertices(counters) { (vid, _, optCounter: Option[Int]) => | ||
| val dblCount = optCounter.getOrElse(0) | ||
| // This algorithm double counts each triangle so the final count should be even | ||
| val isEven = (dblCount & 1) == 0 | ||
| if (!isEven) { | ||
| throw new Exception("Triangle count resulted in an invalid number of triangles.") | ||
|
||
| } | ||
| dblCount / 2 | ||
| } | ||
| } // end of TriangleCount | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the
<li>should be closed if you're writing HTML tags