@Test(expected = IllegalStateException.class) public void depthRequiresADirectedGraph() { Graph<String> undirectedGraph = GraphBuilder.undirected().allowsSelfLoops(false).build(); GraphUtil.depth(undirectedGraph, "", 0); }
public static <T> int depth(Graph<T> graph, T node, int initialDepth) { checkNotNull(graph); checkNotNull(node); checkState(graph.isDirected(), "unable to compute depth for an undirected graph"); checkState(!Graphs.hasCycle(graph), "unable to compute depth for a graph with cycles"); int currentDepth = initialDepth + 1; return graph.successors(node).stream() .map(successor -> depth(graph, successor, currentDepth)) .max(comparing(Integer::intValue)) .orElse(currentDepth); }
GraphUtil { public static <T> int depth(Graph<T> graph, T node, int initialDepth) { checkNotNull(graph); checkNotNull(node); checkState(graph.isDirected(), "unable to compute depth for an undirected graph"); checkState(!Graphs.hasCycle(graph), "unable to compute depth for a graph with cycles"); int currentDepth = initialDepth + 1; return graph.successors(node).stream() .map(successor -> depth(graph, successor, currentDepth)) .max(comparing(Integer::intValue)) .orElse(currentDepth); } }
GraphUtil { public static <T> int depth(Graph<T> graph, T node, int initialDepth) { checkNotNull(graph); checkNotNull(node); checkState(graph.isDirected(), "unable to compute depth for an undirected graph"); checkState(!Graphs.hasCycle(graph), "unable to compute depth for a graph with cycles"); int currentDepth = initialDepth + 1; return graph.successors(node).stream() .map(successor -> depth(graph, successor, currentDepth)) .max(comparing(Integer::intValue)) .orElse(currentDepth); }  }
GraphUtil { public static <T> int depth(Graph<T> graph, T node, int initialDepth) { checkNotNull(graph); checkNotNull(node); checkState(graph.isDirected(), "unable to compute depth for an undirected graph"); checkState(!Graphs.hasCycle(graph), "unable to compute depth for a graph with cycles"); int currentDepth = initialDepth + 1; return graph.successors(node).stream() .map(successor -> depth(graph, successor, currentDepth)) .max(comparing(Integer::intValue)) .orElse(currentDepth); }  static int depth(Graph<T> graph, T node, int initialDepth); }
GraphUtil { public static <T> int depth(Graph<T> graph, T node, int initialDepth) { checkNotNull(graph); checkNotNull(node); checkState(graph.isDirected(), "unable to compute depth for an undirected graph"); checkState(!Graphs.hasCycle(graph), "unable to compute depth for a graph with cycles"); int currentDepth = initialDepth + 1; return graph.successors(node).stream() .map(successor -> depth(graph, successor, currentDepth)) .max(comparing(Integer::intValue)) .orElse(currentDepth); }  static int depth(Graph<T> graph, T node, int initialDepth);  }