Remove access to underlying containers inside Graph and Node.
This commit is contained in:
parent
797a4e1c8c
commit
bdb378c079
@ -24,7 +24,7 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
|||||||
ShortestPathData data = getInputData();
|
ShortestPathData data = getInputData();
|
||||||
Graph graph = data.getGraph();
|
Graph graph = data.getGraph();
|
||||||
|
|
||||||
final int nbNodes = graph.getNodes().size();
|
final int nbNodes = graph.size();
|
||||||
|
|
||||||
// Initialize array of distances.
|
// Initialize array of distances.
|
||||||
double[] distances = new double[nbNodes];
|
double[] distances = new double[nbNodes];
|
||||||
@ -41,8 +41,8 @@ public class BellmanFordAlgorithm extends ShortestPathAlgorithm {
|
|||||||
boolean found = false;
|
boolean found = false;
|
||||||
for (int i = 0; !found && i < nbNodes; ++i) {
|
for (int i = 0; !found && i < nbNodes; ++i) {
|
||||||
found = true;
|
found = true;
|
||||||
for (Node node: graph.getNodes()) {
|
for (Node node: graph) {
|
||||||
for (Arc arc: node.getSuccessors()) {
|
for (Arc arc: node) {
|
||||||
|
|
||||||
// Small test to check allowed roads...
|
// Small test to check allowed roads...
|
||||||
if (!data.isAllowed(arc)) {
|
if (!data.isAllowed(arc)) {
|
||||||
|
@ -60,20 +60,6 @@ public class BinaryHeap<E extends Comparable<E>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return true if the heap is empty, false otherwise.
|
|
||||||
*/
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return this.currentSize == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Current size (number of elements) of this heap.
|
|
||||||
*/
|
|
||||||
public int size() {
|
|
||||||
return this.currentSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Index of the parent of the given index.
|
* @return Index of the parent of the given index.
|
||||||
*/
|
*/
|
||||||
@ -88,17 +74,6 @@ public class BinaryHeap<E extends Comparable<E>> {
|
|||||||
return index * 2 + 1;
|
return index * 2 + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Insert the given element into the heap.
|
|
||||||
*
|
|
||||||
* @param x Item to insert.
|
|
||||||
*/
|
|
||||||
public void insert(E x) {
|
|
||||||
int index = this.currentSize++;
|
|
||||||
this.arraySet(index, x);
|
|
||||||
this.percolateUp(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal method to percolate up in the heap.
|
* Internal method to percolate up in the heap.
|
||||||
*
|
*
|
||||||
@ -151,6 +126,41 @@ public class BinaryHeap<E extends Comparable<E>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if the heap is empty, false otherwise.
|
||||||
|
*/
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return this.currentSize == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Current size (number of elements) of this heap.
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return this.currentSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert the given element into the heap.
|
||||||
|
*
|
||||||
|
* @param x Item to insert.
|
||||||
|
*/
|
||||||
|
public void add(E x) {
|
||||||
|
int index = this.currentSize++;
|
||||||
|
this.arraySet(index, x);
|
||||||
|
this.percolateUp(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tell the binary heap that the given element has been modified and should be
|
||||||
|
* re-positioned inside the heap.
|
||||||
|
*
|
||||||
|
* @param x Item to update.
|
||||||
|
*/
|
||||||
|
public void update(E x) {
|
||||||
|
// TODO:
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the smallest item in the heap.
|
* Find the smallest item in the heap.
|
||||||
*
|
*
|
||||||
@ -160,7 +170,7 @@ public class BinaryHeap<E extends Comparable<E>> {
|
|||||||
*/
|
*/
|
||||||
public E findMin() throws RuntimeException {
|
public E findMin() throws RuntimeException {
|
||||||
if (isEmpty())
|
if (isEmpty())
|
||||||
throw new RuntimeException("Empty binary heap");
|
throw new RuntimeException("Empty binary heap.");
|
||||||
return this.array.get(0);
|
return this.array.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
|
||||||
import org.insa.algo.AbstractAlgorithm;
|
import org.insa.algo.AbstractAlgorithm;
|
||||||
@ -72,14 +71,14 @@ public class WeaklyConnectedComponentsAlgorithm
|
|||||||
* graph.
|
* graph.
|
||||||
*/
|
*/
|
||||||
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
|
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
|
||||||
int nNodes = getInputData().getGraph().getNodes().size();
|
int nNodes = getInputData().getGraph().size();
|
||||||
ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
|
ArrayList<HashSet<Integer>> res = new ArrayList<HashSet<Integer>>(nNodes);
|
||||||
for (int i = 0; i < nNodes; ++i) {
|
for (int i = 0; i < nNodes; ++i) {
|
||||||
res.add(new HashSet<Integer>());
|
res.add(new HashSet<Integer>());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Node node: getInputData().getGraph().getNodes()) {
|
for (Node node: getInputData().getGraph()) {
|
||||||
for (Arc arc: node.getSuccessors()) {
|
for (Arc arc: node) {
|
||||||
res.get(node.getId()).add(arc.getDestination().getId());
|
res.get(node.getId()).add(arc.getDestination().getId());
|
||||||
if (arc.getRoadInformation().isOneWay()) {
|
if (arc.getRoadInformation().isOneWay()) {
|
||||||
res.get(arc.getDestination().getId()).add(node.getId());
|
res.get(arc.getDestination().getId()).add(node.getId());
|
||||||
@ -100,27 +99,27 @@ public class WeaklyConnectedComponentsAlgorithm
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
|
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
|
||||||
List<Node> nodes = getInputData().getGraph().getNodes();
|
Graph graph = getInputData().getGraph();
|
||||||
ArrayList<Node> component = new ArrayList<Node>();
|
ArrayList<Node> component = new ArrayList<Node>();
|
||||||
|
|
||||||
// Using a queue because we are doing a BFS
|
// Using a queue because we are doing a BFS
|
||||||
Queue<Integer> queue = new LinkedList<Integer>();
|
Queue<Integer> queue = new LinkedList<Integer>();
|
||||||
|
|
||||||
// Notify observers about the current component.
|
// Notify observers about the current component.
|
||||||
notifyStartComponent(nodes.get(cur));
|
notifyStartComponent(graph.get(cur));
|
||||||
|
|
||||||
// Add original node and loop until the queue is empty.
|
// Add original node and loop until the queue is empty.
|
||||||
queue.add(cur);
|
queue.add(cur);
|
||||||
marked[cur] = true;
|
marked[cur] = true;
|
||||||
while (!queue.isEmpty()) {
|
while (!queue.isEmpty()) {
|
||||||
Node node = nodes.get(queue.remove());
|
Node node = graph.get(queue.remove());
|
||||||
component.add(node);
|
component.add(node);
|
||||||
|
|
||||||
// Notify observers
|
// Notify observers
|
||||||
notifyNewNodeInComponent(node);
|
notifyNewNodeInComponent(node);
|
||||||
|
|
||||||
for (Integer destId: ugraph.get(node.getId())) {
|
for (Integer destId: ugraph.get(node.getId())) {
|
||||||
Node dest = nodes.get(destId);
|
Node dest = graph.get(destId);
|
||||||
if (!marked[dest.getId()]) {
|
if (!marked[dest.getId()]) {
|
||||||
queue.add(destId);
|
queue.add(destId);
|
||||||
marked[destId] = true;
|
marked[destId] = true;
|
||||||
@ -138,7 +137,7 @@ public class WeaklyConnectedComponentsAlgorithm
|
|||||||
|
|
||||||
Graph graph = getInputData().getGraph();
|
Graph graph = getInputData().getGraph();
|
||||||
ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
|
ArrayList<HashSet<Integer>> ugraph = createUndirectedGraph();
|
||||||
boolean[] marked = new boolean[graph.getNodes().size()];
|
boolean[] marked = new boolean[graph.size()];
|
||||||
Arrays.fill(marked, false);
|
Arrays.fill(marked, false);
|
||||||
|
|
||||||
ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();
|
ArrayList<ArrayList<Node>> components = new ArrayList<ArrayList<Node>>();
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package org.insa.graph;
|
package org.insa.graph;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -11,7 +11,7 @@ import java.util.List;
|
|||||||
* holds a list of nodes and each node holds a list of its successors.
|
* holds a list of nodes and each node holds a list of its successors.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Graph {
|
public class Graph implements Iterable<Node> {
|
||||||
|
|
||||||
// Map identifier.
|
// Map identifier.
|
||||||
private final String mapId;
|
private final String mapId;
|
||||||
@ -48,10 +48,28 @@ public class Graph {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Immutable view of the list of nodes of this graph.
|
* Fetch the node with the given ID.
|
||||||
|
*
|
||||||
|
* Complexity: O(1).
|
||||||
|
*
|
||||||
|
* @param id ID of the node to fetch.
|
||||||
|
*
|
||||||
|
* @return Node with the given ID.
|
||||||
*/
|
*/
|
||||||
public List<Node> getNodes() {
|
public Node get(int id) {
|
||||||
return Collections.unmodifiableList(nodes);
|
return this.nodes.get(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Number of nodes in this graph.
|
||||||
|
*/
|
||||||
|
public int size() {
|
||||||
|
return this.nodes.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Node> iterator() {
|
||||||
|
return this.nodes.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,7 +96,7 @@ public class Graph {
|
|||||||
}
|
}
|
||||||
for (Node node: nodes) {
|
for (Node node: nodes) {
|
||||||
Node orig = trNodes.get(node.getId());
|
Node orig = trNodes.get(node.getId());
|
||||||
for (Arc arc: node.getSuccessors()) {
|
for (Arc arc: node) {
|
||||||
if (arc.getRoadInformation().isOneWay()) {
|
if (arc.getRoadInformation().isOneWay()) {
|
||||||
Node dest = trNodes.get(arc.getDestination().getId());
|
Node dest = trNodes.get(arc.getDestination().getId());
|
||||||
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
|
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
|
||||||
|
@ -2,7 +2,7 @@ package org.insa.graph;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.Iterator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class representing a Node in a {@link Graph}.
|
* Class representing a Node in a {@link Graph}.
|
||||||
@ -13,7 +13,7 @@ import java.util.List;
|
|||||||
* Nodes are comparable based on their ID.
|
* Nodes are comparable based on their ID.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class Node implements Comparable<Node> {
|
public class Node implements Comparable<Node>, Iterable<Arc> {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Link the two given nodes with one or two arcs (depending on roadInformation),
|
* Link the two given nodes with one or two arcs (depending on roadInformation),
|
||||||
@ -94,10 +94,22 @@ public class Node implements Comparable<Node> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Immutable view of the list of successors of this node.
|
* @return Number of successors of this node.
|
||||||
*/
|
*/
|
||||||
public List<Arc> getSuccessors() {
|
public int getNumberOfSuccessors() {
|
||||||
return Collections.unmodifiableList(successors);
|
return this.successors.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return true if this node has at least one successor.
|
||||||
|
*/
|
||||||
|
public boolean hasSuccessors() {
|
||||||
|
return !this.successors.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Iterator<Arc> iterator() {
|
||||||
|
return this.successors.iterator();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,7 +70,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
|
|||||||
* @throws IndexOutOfBoundsException if the node is not in the graph.
|
* @throws IndexOutOfBoundsException if the node is not in the graph.
|
||||||
*/
|
*/
|
||||||
protected Node readNode(Graph graph) throws IOException {
|
protected Node readNode(Graph graph) throws IOException {
|
||||||
return graph.getNodes().get(dis.readInt());
|
return graph.get(dis.readInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -602,8 +602,8 @@ public class MainWindow extends JFrame {
|
|||||||
// name that are right-to-left (e.g. arabic names).
|
// name that are right-to-left (e.g. arabic names).
|
||||||
info += " - " + graph.getMapName() + "\u200e";
|
info += " - " + graph.getMapName() + "\u200e";
|
||||||
}
|
}
|
||||||
info += ", " + graph.getNodes().size() + " nodes, "
|
info += ", " + graph.size() + " nodes, " + graph.getGraphInformation().getArcCount()
|
||||||
+ graph.getGraphInformation().getArcCount() + " arcs.";
|
+ " arcs.";
|
||||||
graphInfoPanel.setText(info);
|
graphInfoPanel.setText(info);
|
||||||
|
|
||||||
drawGraph();
|
drawGraph();
|
||||||
|
@ -65,7 +65,7 @@ public class NodesInputPanel extends JPanel
|
|||||||
public Node findClosestNode(Point point) {
|
public Node findClosestNode(Point point) {
|
||||||
Node minNode = null;
|
Node minNode = null;
|
||||||
double minDis = Double.POSITIVE_INFINITY;
|
double minDis = Double.POSITIVE_INFINITY;
|
||||||
for (Node node: graph.getNodes()) {
|
for (Node node: graph) {
|
||||||
double dlon = point.getLongitude() - node.getPoint().getLongitude();
|
double dlon = point.getLongitude() - node.getPoint().getLongitude();
|
||||||
double dlat = point.getLatitude() - node.getPoint().getLatitude();
|
double dlat = point.getLatitude() - node.getPoint().getLatitude();
|
||||||
double dis = dlon * dlon + dlat * dlat; // No need to square
|
double dis = dlon * dlon + dlat * dlat; // No need to square
|
||||||
@ -308,7 +308,7 @@ public class NodesInputPanel extends JPanel
|
|||||||
*/
|
*/
|
||||||
protected Node getNodeForInput(JTextField textfield) {
|
protected Node getNodeForInput(JTextField textfield) {
|
||||||
try {
|
try {
|
||||||
Node node = graph.getNodes().get(Integer.valueOf(textfield.getText().trim()));
|
Node node = graph.get(Integer.valueOf(textfield.getText().trim()));
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException | IndexOutOfBoundsException ex) {
|
catch (IllegalArgumentException | IndexOutOfBoundsException ex) {
|
||||||
|
@ -614,7 +614,7 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawGraph(Graph graph, GraphPalette palette) {
|
public void drawGraph(Graph graph, GraphPalette palette) {
|
||||||
int repaintModulo = Math.max(1, graph.getNodes().size() / 100);
|
int repaintModulo = Math.max(1, graph.size() / 100);
|
||||||
|
|
||||||
// Initialize the buffered image
|
// Initialize the buffered image
|
||||||
|
|
||||||
@ -625,8 +625,8 @@ public class BasicDrawing extends JPanel implements Drawing {
|
|||||||
this.removeMouseMotionListener(zoomAndPanListener);
|
this.removeMouseMotionListener(zoomAndPanListener);
|
||||||
this.removeMouseWheelListener(zoomAndPanListener);
|
this.removeMouseWheelListener(zoomAndPanListener);
|
||||||
|
|
||||||
for (Node node: graph.getNodes()) {
|
for (Node node: graph) {
|
||||||
for (Arc arc: node.getSuccessors()) {
|
for (Arc arc: node) {
|
||||||
// Draw arcs only if there are one-way arcs or if origin is lower than
|
// Draw arcs only if there are one-way arcs or if origin is lower than
|
||||||
// destination, avoid drawing two-ways arc twice.
|
// destination, avoid drawing two-ways arc twice.
|
||||||
if (arc.getRoadInformation().isOneWay()
|
if (arc.getRoadInformation().isOneWay()
|
||||||
|
@ -11,29 +11,68 @@ import org.junit.Test;
|
|||||||
|
|
||||||
public class BinaryHeapTest {
|
public class BinaryHeapTest {
|
||||||
|
|
||||||
private int[] data1 = IntStream.range(0, 20).toArray();
|
class MutableInteger implements Comparable<MutableInteger> {
|
||||||
private int[] data2 = { 8, 1, 6, 3, 4, 5, 9 };
|
|
||||||
|
|
||||||
private BinaryHeap<Integer> heap1, heap2;
|
// Actual value
|
||||||
|
private int value;
|
||||||
|
|
||||||
|
public MutableInteger(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The integer value stored inside this MutableInteger.
|
||||||
|
*/
|
||||||
|
public int get() {
|
||||||
|
return this.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the integer value stored inside this MutableInteger.
|
||||||
|
*
|
||||||
|
* @param value New value to set.
|
||||||
|
*/
|
||||||
|
public void set(int value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(MutableInteger other) {
|
||||||
|
return Integer.compare(this.value, other.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
// Raw data arrays.
|
||||||
|
private MutableInteger[] data1 = IntStream.range(0, 20).mapToObj(MutableInteger::new)
|
||||||
|
.toArray(MutableInteger[]::new);
|
||||||
|
private MutableInteger[] data2 = Arrays.stream(new int[] { 8, 1, 6, 3, 4, 5, 9 })
|
||||||
|
.mapToObj(MutableInteger::new).toArray(MutableInteger[]::new);
|
||||||
|
|
||||||
|
// Actual heap.
|
||||||
|
private BinaryHeap<MutableInteger> heap1, heap2;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
// Create the range heap
|
// Create the range heap
|
||||||
this.heap1 = new BinaryHeap<Integer>();
|
this.heap1 = new BinaryHeap<>();
|
||||||
this.heap2 = new BinaryHeap<Integer>();
|
this.heap2 = new BinaryHeap<>();
|
||||||
|
|
||||||
for (int v: data1)
|
for (MutableInteger v: data1) {
|
||||||
this.heap1.insert(v);
|
this.heap1.add(v);
|
||||||
for (int v: data2)
|
}
|
||||||
this.heap2.insert(v);
|
|
||||||
|
for (MutableInteger v: data2) {
|
||||||
|
this.heap2.add(v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInsert() {
|
public void testInsert() {
|
||||||
BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
|
BinaryHeap<MutableInteger> heap = new BinaryHeap<>();
|
||||||
int size = 0;
|
int size = 0;
|
||||||
for (int x: data1) {
|
for (MutableInteger x: data1) {
|
||||||
heap.insert(x);
|
heap.add(x);
|
||||||
size += 1;
|
size += 1;
|
||||||
assertEquals(heap.size(), size);
|
assertEquals(heap.size(), size);
|
||||||
}
|
}
|
||||||
@ -41,8 +80,8 @@ public class BinaryHeapTest {
|
|||||||
|
|
||||||
heap = new BinaryHeap<>();
|
heap = new BinaryHeap<>();
|
||||||
size = 0;
|
size = 0;
|
||||||
for (int x: data2) {
|
for (MutableInteger x: data2) {
|
||||||
heap.insert(x);
|
heap.add(x);
|
||||||
size += 1;
|
size += 1;
|
||||||
assertEquals(heap.size(), size);
|
assertEquals(heap.size(), size);
|
||||||
}
|
}
|
||||||
@ -52,11 +91,10 @@ public class BinaryHeapTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testDeleteMin() {
|
public void testDeleteMin() {
|
||||||
// range 1 (sorted)
|
// range 1 (sorted)
|
||||||
int[] range1 = data1;
|
int size = data1.length;
|
||||||
int size = range1.length;
|
|
||||||
assertEquals(heap1.size(), size);
|
assertEquals(heap1.size(), size);
|
||||||
for (int x: range1) {
|
for (MutableInteger x: data1) {
|
||||||
assertEquals(heap1.deleteMin().intValue(), x);
|
assertEquals(heap1.deleteMin(), x);
|
||||||
size -= 1;
|
size -= 1;
|
||||||
assertEquals(heap1.size(), size);
|
assertEquals(heap1.size(), size);
|
||||||
}
|
}
|
||||||
@ -64,12 +102,12 @@ public class BinaryHeapTest {
|
|||||||
assertTrue(heap1.isEmpty());
|
assertTrue(heap1.isEmpty());
|
||||||
|
|
||||||
// range 2 (was not sorted)
|
// range 2 (was not sorted)
|
||||||
int[] range2 = Arrays.copyOf(data2, data2.length);
|
MutableInteger[] range2 = Arrays.copyOf(data2, data2.length);
|
||||||
Arrays.sort(range2);
|
Arrays.sort(range2);
|
||||||
size = range2.length;
|
size = range2.length;
|
||||||
assertEquals(heap2.size(), size);
|
assertEquals(heap2.size(), size);
|
||||||
for (int x: range2) {
|
for (MutableInteger x: range2) {
|
||||||
assertEquals(heap2.deleteMin().intValue(), x);
|
assertEquals(heap2.deleteMin().get(), x.get());
|
||||||
size -= 1;
|
size -= 1;
|
||||||
assertEquals(heap2.size(), size);
|
assertEquals(heap2.size(), size);
|
||||||
}
|
}
|
||||||
@ -77,4 +115,12 @@ public class BinaryHeapTest {
|
|||||||
assertTrue(heap2.isEmpty());
|
assertTrue(heap2.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdate() {
|
||||||
|
MutableInteger newMin = data2[data2.length - 1];
|
||||||
|
newMin.set(0);
|
||||||
|
heap2.update(newMin);
|
||||||
|
assertEquals(heap2.findMin(), newMin);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -62,7 +62,7 @@ public class GraphTest {
|
|||||||
*/
|
*/
|
||||||
private List<Arc> getArcsBetween(Node a, Node b) {
|
private List<Arc> getArcsBetween(Node a, Node b) {
|
||||||
List<Arc> arcs = new ArrayList<>();
|
List<Arc> arcs = new ArrayList<>();
|
||||||
for (Arc arc: a.getSuccessors()) {
|
for (Arc arc: a) {
|
||||||
if (arc.getDestination().equals(b)) {
|
if (arc.getDestination().equals(b)) {
|
||||||
arcs.add(arc);
|
arcs.add(arc);
|
||||||
}
|
}
|
||||||
@ -76,53 +76,33 @@ public class GraphTest {
|
|||||||
|
|
||||||
// Basic asserts...
|
// Basic asserts...
|
||||||
assertEquals("R/" + graph.getMapId(), transpose.getMapId());
|
assertEquals("R/" + graph.getMapId(), transpose.getMapId());
|
||||||
assertEquals(graph.getNodes().size(), transpose.getNodes().size());
|
assertEquals(graph.size(), transpose.size());
|
||||||
|
|
||||||
final int expNbSucc[] = { 4, 2, 2, 4, 2 };
|
final int expNbSucc[] = { 4, 2, 2, 4, 2 };
|
||||||
for (int i = 0; i < expNbSucc.length; ++i) {
|
for (int i = 0; i < expNbSucc.length; ++i) {
|
||||||
assertEquals(expNbSucc[i], transpose.getNodes().get(i).getSuccessors().size());
|
assertEquals(expNbSucc[i], transpose.get(i).getNumberOfSuccessors());
|
||||||
}
|
}
|
||||||
|
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(0), transpose.get(1)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(1)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(0), transpose.get(2)).size(), 1);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(0), transpose.get(3)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(2)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(0), transpose.get(4)).size(), 1);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(1), transpose.get(0)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(3)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(1), transpose.get(2)).size(), 1);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(1), transpose.get(3)).size(), 0);
|
||||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(4)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(1), transpose.get(4)).size(), 0);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(2), transpose.get(0)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(0)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(2), transpose.get(1)).size(), 1);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(2), transpose.get(3)).size(), 0);
|
||||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(2)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(2), transpose.get(4)).size(), 0);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(3), transpose.get(0)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(3)).size(), 0);
|
assertEquals(getArcsBetween(transpose.get(3), transpose.get(1)).size(), 0);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(3), transpose.get(2)).size(), 3);
|
||||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(4)).size(), 0);
|
assertEquals(getArcsBetween(transpose.get(3), transpose.get(4)).size(), 0);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(4), transpose.get(0)).size(), 1);
|
||||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(0)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(4), transpose.get(1)).size(), 0);
|
||||||
assertEquals(
|
assertEquals(getArcsBetween(transpose.get(4), transpose.get(2)).size(), 0);
|
||||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(1)).size(), 1);
|
assertEquals(getArcsBetween(transpose.get(4), transpose.get(3)).size(), 1);
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(3)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(4)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(0)).size(), 1);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(1)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(2)).size(), 3);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(4)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(0)).size(), 1);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(1)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(2)).size(), 0);
|
|
||||||
assertEquals(
|
|
||||||
getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(3)).size(), 1);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,7 @@ public class NodeTest {
|
|||||||
public void initAll() throws IOException {
|
public void initAll() throws IOException {
|
||||||
|
|
||||||
// Create nodes
|
// Create nodes
|
||||||
nodes = new Node[5];
|
nodes = new Node[6];
|
||||||
for (int i = 0; i < nodes.length; ++i) {
|
for (int i = 0; i < nodes.length; ++i) {
|
||||||
nodes[i] = new Node(i, null);
|
nodes[i] = new Node(i, null);
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ public class NodeTest {
|
|||||||
* @return The first arc between from a to b, or null.
|
* @return The first arc between from a to b, or null.
|
||||||
*/
|
*/
|
||||||
private Arc getFirstArcBetween(Node a, Node b) {
|
private Arc getFirstArcBetween(Node a, Node b) {
|
||||||
for (Arc arc: a.getSuccessors()) {
|
for (Arc arc: a) {
|
||||||
if (arc.getDestination().equals(b)) {
|
if (arc.getDestination().equals(b)) {
|
||||||
return arc;
|
return arc;
|
||||||
}
|
}
|
||||||
@ -63,13 +63,25 @@ public class NodeTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testLinkNodes() {
|
public void testGetNumberOfSuccessors() {
|
||||||
final int[] expNbSucc = { 4, 2, 5, 2, 1 };
|
final int[] expNbSucc = { 4, 2, 5, 2, 1, 0 };
|
||||||
assertEquals(nodes.length, expNbSucc.length);
|
assertEquals(nodes.length, expNbSucc.length);
|
||||||
for (int i = 0; i < expNbSucc.length; ++i) {
|
for (int i = 0; i < expNbSucc.length; ++i) {
|
||||||
assertEquals(nodes[i].getSuccessors().size(), expNbSucc[i]);
|
assertEquals(nodes[i].getNumberOfSuccessors(), expNbSucc[i]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testHasSuccessors() {
|
||||||
|
final int[] expNbSucc = { 4, 2, 5, 2, 1, 0 };
|
||||||
|
assertEquals(nodes.length, expNbSucc.length);
|
||||||
|
for (int i = 0; i < expNbSucc.length; ++i) {
|
||||||
|
assertEquals(nodes[i].hasSuccessors(), expNbSucc[i] != 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLinkNodes() {
|
||||||
assertEquals(getFirstArcBetween(nodes[0], nodes[1]).getRoadInformation(),
|
assertEquals(getFirstArcBetween(nodes[0], nodes[1]).getRoadInformation(),
|
||||||
getFirstArcBetween(nodes[1], nodes[0]).getRoadInformation());
|
getFirstArcBetween(nodes[1], nodes[0]).getRoadInformation());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user