Me and my friend have been working on a problem for school. We are traversing a graph with DFS and are counting the number of nodes in each given component. We get widely different results and have identified where the difference lies.
When going into the next recursion, my friend uses the syntax
componentSize += DFS_visit(nextNodeToVisit);
whereas I use
componentSize = 1 + DFS_visit(nextNodeToVisit);
I originally thought these two were the same, so what is the difference? And which one should be used in our case?
a += b
is synonymous witha = a + b
a += b
is not exactly the same asa = a + b
- the first one includes an implicit cast anda
is only evaluated once! JLS 15.26.2. Compound Assignment Operators