[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1261835084 ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,363 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge implements Comparable { +final V destination; +final int capacity; +final int cost; +int residualFlow; +int flow; +Edge counterEdge; + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow) { +Objects.requireNonNull(destination); +this.destination = destination; +this.capacity = capacity; +this.cost = cost; +this.residualFlow = residualFlow; +this.flow = flow; +} + +@Override +public int compareTo(final Edge o) { Review Comment: `compareTo` is to establish an order, right? Why do we order by `(destination,capacity,cost)`; does is matter, or could we use any order as long as deterministic? ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,363 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge implements Comparable { +final V destination; +final int capacity; +final int cost; +int residualFlow; +int flow; +Edge counterEdge; + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow) { +Objects.requireNonNull(destination); +this.destination = destination; +this.capacity = capacity; +this.cost = cost; +this.residualFlow = residualFlow; +this.flow = flow; +} + +@Override +public int compareTo(final Edge o) { +int compare = destination.compareTo(o.destination); +if (compare != 0) { +return compare; +} + +compare = capacity - o.capacity; +if (compare != 0) { +return compare; +} + +return cost - o.cost; +} + +@Override +public boolean equals(final Object other) { +if (this == other) { +return true; +} +if (other == null || other.getClass() != getClass()) { +return false; +} + +final Graph.Edge otherEdge = (Graph.Edge) other; + +return destination.equals(otherEdge.destination) && capacity == otherEdge.capacity +
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1262977617 ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,348 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge { +final V destination; +final int capacity; +final int cost; +int residualFlow; +int flow; +Edge counterEdge; + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow) { +Objects.requireNonNull(destination); +if (capacity < 0) { +throw new IllegalArgumentException("Edge capacity cannot be negative"); +} +if (flow > capacity) { +throw new IllegalArgumentException(String.format("Edge flow %d cannot exceed capacity %d", +flow, capacity)); +} + +this.destination = destination; +this.capacity = capacity; +this.cost = cost; +this.residualFlow = residualFlow; +this.flow = flow; +} + +@Override +public boolean equals(final Object other) { +if (this == other) { +return true; +} +if (other == null || other.getClass() != getClass()) { +return false; +} + +final Graph.Edge otherEdge = (Graph.Edge) other; + +return destination.equals(otherEdge.destination) && capacity == otherEdge.capacity +&& cost == otherEdge.cost && residualFlow == otherEdge.residualFlow && flow == otherEdge.flow; +} + +@Override +public int hashCode() { +return Objects.hash(destination, capacity, cost, residualFlow, flow); +} + +@Override +public String toString() { +return "{destination= " + destination + ", capacity=" + capacity + ", cost=" + cost ++ ", residualFlow=" + residualFlow + ", flow=" + flow; +} +} + +private final SortedMap> adjList = new TreeMap<>(); +private final SortedSet nodes = new TreeSet<>(); +private final boolean isResidualGraph; +private V sourceNode, sinkNode; + +public Graph() { +this(false); +} + +private Graph(final boolean isResidualGraph) { +this.isResidualGraph = isResidualGraph; +} + +public void addEdge(final V u, final V v, final int capacity, final int cost, final int flow) { +addEdge(u, new Edge(v, capacity, cost, capacity - flow, flow)); +} + +public Set nodes() { +return nodes; +} + +public Map edges(final V node) { +return adjList.get(node); +} + +public boolean isResidualGraph() { +return isResidualGraph; +} + +public void setSourceNode(final V node) { +sourceNode = node; +} + +public void setSinkNode(final V node) { +sinkNode = node; +} + +public int totalCost() { +int totalCost = 0; +for (final Map.Entry> nodeEdges : adjList.entrySet()) { +final SortedMap edges = nodeEdges.getValue(); +for (final Entry nodeEdge : edges.entrySet()) { +totalCost += nodeEdge.getValue().cost * nodeEdge.getValue().flow; +} +} +return totalCost; +} + +private void addEdge(final V u, final Edge edge) { +if (!isResidualGraph) { +// Check if there's already an edge from u to v +final Map edgeMap = adjList.get(edge.destination); +if (edgeMap != null && edgeMap.containsKey(u)) { +throw new IllegalArgumentException( +"There is already an edge from " + edge.destination +
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1262978439 ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,348 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge { +final V destination; +final int capacity; +final int cost; +int residualFlow; +int flow; +Edge counterEdge; + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow) { +Objects.requireNonNull(destination); +if (capacity < 0) { +throw new IllegalArgumentException("Edge capacity cannot be negative"); +} +if (flow > capacity) { +throw new IllegalArgumentException(String.format("Edge flow %d cannot exceed capacity %d", +flow, capacity)); +} + +this.destination = destination; +this.capacity = capacity; +this.cost = cost; +this.residualFlow = residualFlow; +this.flow = flow; +} + +@Override +public boolean equals(final Object other) { +if (this == other) { +return true; +} +if (other == null || other.getClass() != getClass()) { +return false; +} + +final Graph.Edge otherEdge = (Graph.Edge) other; + +return destination.equals(otherEdge.destination) && capacity == otherEdge.capacity +&& cost == otherEdge.cost && residualFlow == otherEdge.residualFlow && flow == otherEdge.flow; +} + +@Override +public int hashCode() { +return Objects.hash(destination, capacity, cost, residualFlow, flow); +} + +@Override +public String toString() { +return "{destination= " + destination + ", capacity=" + capacity + ", cost=" + cost ++ ", residualFlow=" + residualFlow + ", flow=" + flow; +} +} + +private final SortedMap> adjList = new TreeMap<>(); +private final SortedSet nodes = new TreeSet<>(); +private final boolean isResidualGraph; +private V sourceNode, sinkNode; + +public Graph() { +this(false); +} + +private Graph(final boolean isResidualGraph) { +this.isResidualGraph = isResidualGraph; +} + +public void addEdge(final V u, final V v, final int capacity, final int cost, final int flow) { +addEdge(u, new Edge(v, capacity, cost, capacity - flow, flow)); +} + +public Set nodes() { +return nodes; +} + +public Map edges(final V node) { +return adjList.get(node); +} + +public boolean isResidualGraph() { +return isResidualGraph; +} + +public void setSourceNode(final V node) { +sourceNode = node; +} + +public void setSinkNode(final V node) { +sinkNode = node; +} + +public int totalCost() { +int totalCost = 0; +for (final Map.Entry> nodeEdges : adjList.entrySet()) { +final SortedMap edges = nodeEdges.getValue(); +for (final Entry nodeEdge : edges.entrySet()) { +totalCost += nodeEdge.getValue().cost * nodeEdge.getValue().flow; +} +} +return totalCost; +} + +private void addEdge(final V u, final Edge edge) { +if (!isResidualGraph) { +// Check if there's already an edge from u to v +final Map edgeMap = adjList.get(edge.destination); +if (edgeMap != null && edgeMap.containsKey(u)) { +throw new IllegalArgumentException( +"There is already an edge from " + edge.destination +
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1265999671 ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,367 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge { +final V destination; +final int capacity; +final int cost; +int residualFlow; +int flow; +Edge counterEdge; +boolean forwardEdge; + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow) { +this(destination, capacity, cost, residualFlow, flow, true); +} + +public Edge(final V destination, final int capacity, final int cost, final int residualFlow, final int flow, +final boolean forwardEdge) { Review Comment: nit: formatting (if it does not fit in one line, we should move each parameter into it's one line to simplify reading) ``` public Edge( final V destination, final int capacity, final int cost, final int residualFlow, final int flow, final boolean forwardEdge ) { ``` ## streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/GraphTest.java: ## @@ -0,0 +1,414 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; + +public class GraphTest { +private Graph graph; + +@Before +public void setUp() { +/* + * Node 0 and 2 are both connected to node 1 and 3. There's a flow of 1 unit from 0 to 1 and 2 to + * 3. The total cost in this case is 5. Min cost should be 2 by flowing 1 unit from 0 to 3 and 2 + * to 1 + */ +graph = new Graph<>(); +graph.addEdge(0, 1, 1, 3, 1); +graph.addEdge(0, 3, 1, 1, 0); +graph.addEdge(2, 1, 1, 1, 0); +graph.addEdge(2, 3, 1, 2, 1); +graph.addEdge(4, 0, 1, 0, 1); +graph.addEdge(4, 2, 1, 0, 1); +graph.addEdge(1, 5, 1, 0, 1); +graph.addEdge(3, 5, 1, 0, 1); +graph.setSourceNode(4); +graph.setSinkNode(5); +} + +@Test +public void testBasic() { +final Set nodes = graph.nodes(); +assertEquals(6, nodes.size()); +assertThat(nodes, contains(0, 1, 2, 3, 4, 5)); + +Map.Edge> edges
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1270057798 ## streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/GraphTest.java: ## @@ -0,0 +1,414 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; + +public class GraphTest { +private Graph graph; + +@Before +public void setUp() { +/* + * Node 0 and 2 are both connected to node 1 and 3. There's a flow of 1 unit from 0 to 1 and 2 to + * 3. The total cost in this case is 5. Min cost should be 2 by flowing 1 unit from 0 to 3 and 2 + * to 1 + */ +graph = new Graph<>(); +graph.addEdge(0, 1, 1, 3, 1); +graph.addEdge(0, 3, 1, 1, 0); +graph.addEdge(2, 1, 1, 1, 0); +graph.addEdge(2, 3, 1, 2, 1); +graph.addEdge(4, 0, 1, 0, 1); +graph.addEdge(4, 2, 1, 0, 1); +graph.addEdge(1, 5, 1, 0, 1); +graph.addEdge(3, 5, 1, 0, 1); +graph.setSourceNode(4); Review Comment: Would it be simpler to use `0` as source? (At least for my mind it's easier to follow what going on, if numbers are "ordered") Or to avoid a lot of re-writing, name the source -1 (and the sink 99) so both a clearly different, and we don't need to update too much code. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1271147536 ## streams/src/main/java/org/apache/kafka/streams/processor/internals/assignment/Graph.java: ## @@ -0,0 +1,377 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.Set; +import java.util.SortedMap; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Collectors; + +public class Graph> { +public class Edge { +final V destination; +final int capacity; +final int cost; +int residualFlow; Review Comment: Ah. Parameter order... Thought it's `, flow, residualFlow,` (not sure why you picked the "reverse" order) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org
[GitHub] [kafka] mjsax commented on a diff in pull request #13996: KAFKA-15022: [2/N] introduce graph to compute min cost
mjsax commented on code in PR #13996: URL: https://github.com/apache/kafka/pull/13996#discussion_r1271148006 ## streams/src/test/java/org/apache/kafka/streams/processor/internals/assignment/GraphTest.java: ## @@ -0,0 +1,414 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + *http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.kafka.streams.processor.internals.assignment; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.junit.Before; +import org.junit.Test; + +public class GraphTest { +private Graph graph; + +@Before +public void setUp() { +/* + * Node 0 and 2 are both connected to node 1 and 3. There's a flow of 1 unit from 0 to 1 and 2 to + * 3. The total cost in this case is 5. Min cost should be 2 by flowing 1 unit from 0 to 3 and 2 + * to 1 + */ +graph = new Graph<>(); +graph.addEdge(0, 1, 1, 3, 1); +graph.addEdge(0, 3, 1, 1, 0); +graph.addEdge(2, 1, 1, 1, 0); +graph.addEdge(2, 3, 1, 2, 1); +graph.addEdge(4, 0, 1, 0, 1); +graph.addEdge(4, 2, 1, 0, 1); +graph.addEdge(1, 5, 1, 0, 1); +graph.addEdge(3, 5, 1, 0, 1); +graph.setSourceNode(4); +graph.setSinkNode(5); +} + +@Test +public void testBasic() { +final Set nodes = graph.nodes(); +assertEquals(6, nodes.size()); +assertThat(nodes, contains(0, 1, 2, 3, 4, 5)); + +Map.Edge> edges = graph.edges(0); +assertEquals(2, edges.size()); +assertEquals(getEdge(1, 1, 3, 0, 1), edges.get(1)); +assertEquals(getEdge(3, 1, 1, 1, 0), edges.get(3)); + +edges = graph.edges(2); +assertEquals(2, edges.size()); +assertEquals(getEdge(1, 1, 1, 1, 0), edges.get(1)); +assertEquals(getEdge(3, 1, 2, 0, 1), edges.get(3)); + +edges = graph.edges(1); +assertEquals(1, edges.size()); +assertEquals(getEdge(5, 1, 0, 0, 1), edges.get(5)); + +edges = graph.edges(3); +assertEquals(1, edges.size()); +assertEquals(getEdge(5, 1, 0, 0, 1), edges.get(5)); + +edges = graph.edges(4); +assertEquals(2, edges.size()); +assertEquals(getEdge(0, 1, 0, 0, 1), edges.get(0)); +assertEquals(getEdge(2, 1, 0, 0, 1), edges.get(2)); + +edges = graph.edges(5); +assertNull(edges); + +assertFalse(graph.isResidualGraph()); +} + +@Test +public void testResidualGraph() { +final Graph residualGraph = graph.residualGraph(); +final Graph residualGraph1 = residualGraph.residualGraph(); +assertSame(residualGraph1, residualGraph); + +final Set nodes = residualGraph.nodes(); +assertEquals(6, nodes.size()); +assertThat(nodes, contains(0, 1, 2, 3, 4, 5)); + +Map.Edge> edges = residualGraph.edges(0); +assertEquals(3, edges.size()); +assertEquals(getEdge(1, 1, 3, 0, 1), edges.get(1)); +assertEquals(getEdge(3, 1, 1, 1, 0), edges.get(3)); +assertEquals(getEdge(4, 1, 0, 1, 0, false), edges.get(4)); + +edges = residualGraph.edges(2); +assertEquals(3, edges.size()); +assertEquals(getEdge(1, 1, 1, 1, 0), edges.get(1)); +assertEquals(getEdge(3, 1, 2, 0, 1), edges.get(3)); +assertEquals(getEdge(4, 1, 0, 1, 0, false), edges.get(4)); + +edges = residualGraph.edges(1); +assertEquals(3, edges.size()); +assertEquals(getEdge(0, 1, -3, 1, 0, false), edges.get(0)); +assertEquals(getEdge(2, 1, -1, 0, 0, false), edges.get(2)); +assertEquals(getEdge(5, 1, 0, 0, 1), edges.get(5)); + +edges = residualGraph.edges(3); +assertEq