Skip to content

Commit

Permalink
Add the method Graphs.endpoints() that does what you expect: returns …
Browse files Browse the repository at this point in the history
…the endpoints corresponding to each edge in a graph. The whole "we print every edge in an undirected graph's toString() twice, and that's a feature not a bug *wink*" annoyed me. Also there's other cases, such as copyOf() and and getWeaklyConnectedComponents(), where we don't want to process each edge twice. In those particular cases they happen to be no-ops, but for cases where it's not, or where you do something computationally expensive, doubling up on undirected edges is bad :(

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=126884706
  • Loading branch information
Bezier89 authored and cpovirk committed Jul 8, 2016
1 parent 8a2e184 commit 6427916
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 40 deletions.
11 changes: 10 additions & 1 deletion guava-tests/test/com/google/common/graph/GraphsTest.java
Expand Up @@ -31,6 +31,7 @@
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.JUnit4; import org.junit.runners.JUnit4;


import java.util.Collection;
import java.util.Set; import java.util.Set;


/** /**
Expand All @@ -39,6 +40,7 @@
*/ */
@RunWith(JUnit4.class) @RunWith(JUnit4.class)
public class GraphsTest { public class GraphsTest {
private static final Integer N0 = 0;
private static final Integer N1 = 1; private static final Integer N1 = 1;
private static final Integer N2 = 2; private static final Integer N2 = 2;
private static final Integer N3 = 3; private static final Integer N3 = 3;
Expand Down Expand Up @@ -129,7 +131,6 @@ public void roots_multipleRoots() {
assertThat(roots(directedGraph)).isEqualTo(ImmutableSet.of(N1, N2)); assertThat(roots(directedGraph)).isEqualTo(ImmutableSet.of(N1, N2));
} }


@Test
public void parallelEdges_directed() { public void parallelEdges_directed() {
MutableNetwork<Integer, String> directedGraph = MutableNetwork<Integer, String> directedGraph =
NetworkBuilder.directed().allowsParallelEdges(true).build(); NetworkBuilder.directed().allowsParallelEdges(true).build();
Expand Down Expand Up @@ -480,4 +481,12 @@ private static MutableNetwork<Integer, String> buildUndirectedTestNetwork() {


return undirectedGraph; return undirectedGraph;
} }

private static void containsExactlySanityCheck(Collection<?> collection, Object... varargs) {
assertThat(collection).hasSize(varargs.length);
for (Object obj : varargs) {
assertThat(collection).contains(obj);
}
assertThat(collection).containsExactly(varargs);
}
} }
6 changes: 3 additions & 3 deletions guava/src/com/google/common/graph/AbstractGraph.java
Expand Up @@ -34,19 +34,19 @@ public abstract class AbstractGraph<N> implements Graph<N> {


@Override @Override
public int degree(Object node) { public int degree(Object node) {
// only works for non-multigraphs; multigraphs not yet supported // TODO(b/28087289): only works for non-multigraphs; multigraphs not yet supported
return adjacentNodes(node).size(); return adjacentNodes(node).size();
} }


@Override @Override
public int inDegree(Object node) { public int inDegree(Object node) {
// only works for non-multigraphs; multigraphs not yet supported // TODO(b/28087289): only works for non-multigraphs; multigraphs not yet supported
return predecessors(node).size(); return predecessors(node).size();
} }


@Override @Override
public int outDegree(Object node) { public int outDegree(Object node) {
// only works for non-multigraphs; multigraphs not yet supported // TODO(b/28087289): only works for non-multigraphs; multigraphs not yet supported
return successors(node).size(); return successors(node).size();
} }


Expand Down

0 comments on commit 6427916

Please sign in to comment.