### Question:
ControlDag extends Dag { public int trim() { int numRemoved = 0; for (String node : nodes) { Set<Connection> toRemove = new HashSet<>(); for (String nodeInput : getNodeInputs(node)) { if (numPaths(nodeInput, node) > 1) { toRemove.add(new Connection(nodeInput, node)); } } for (Connection conn : toRemove) { removeConnection(conn.getFrom(), conn.getTo()); } numRemoved += toRemove.size(); } return numRemoved; } ControlDag(Collection<Connection> connections);  ControlDag(Dag dag); void flatten(); int trim();  }### Answer:
@Test public void testNoOpTrim() { ControlDag cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n3"), new Connection("n3", "n4"), new Connection("n3", "n5"))); Assert.assertEquals(0, cdag.trim()); cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n3"), new Connection("n2", "n4"), new Connection("n3", "n4"))); Assert.assertEquals(0, cdag.trim()); cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n3"), new Connection("n1", "n4"), new Connection("n2", "n5"), new Connection("n3", "n5"), new Connection("n4", "n5"))); Assert.assertEquals(0, cdag.trim()); cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n3"), new Connection("n1", "n4"), new Connection("n2", "n5"), new Connection("n3", "n5"), new Connection("n4", "n6"))); Assert.assertEquals(0, cdag.trim()); }
@Test public void testTrim() { ControlDag cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n3"), new Connection("n2", "n3"))); Assert.assertEquals(1, cdag.trim()); ControlDag expected = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n2", "n3"))); Assert.assertEquals(expected, cdag); cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n6"), new Connection("n2", "n3"), new Connection("n2", "n4"), new Connection("n3", "n5"), new Connection("n4", "n5"), new Connection("n4", "n7"), new Connection("n6", "n7"), new Connection("n7", "n5"))); Assert.assertEquals(1, cdag.trim()); expected = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n6"), new Connection("n2", "n3"), new Connection("n2", "n4"), new Connection("n3", "n5"), new Connection("n4", "n7"), new Connection("n6", "n7"), new Connection("n7", "n5"))); Assert.assertEquals(expected, cdag); cdag = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n1", "n4"), new Connection("n1", "n6"), new Connection("n2", "n3"), new Connection("n3", "n4"), new Connection("n3", "n5"), new Connection("n3", "n6"), new Connection("n4", "n5"), new Connection("n4", "n6"), new Connection("n5", "n6"))); Assert.assertEquals(5, cdag.trim()); expected = new ControlDag( ImmutableSet.of( new Connection("n1", "n2"), new Connection("n2", "n3"), new Connection("n3", "n4"), new Connection("n4", "n5"), new Connection("n5", "n6"))); Assert.assertEquals(expected, cdag); }