Add code to transpose graph.
This commit is contained in:
parent
95b68f3e31
commit
19220dc8a4
@ -1,5 +1,6 @@
|
||||
package org.insa.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@ -71,8 +72,28 @@ public class Graph {
|
||||
* @return Transpose graph of this graph.
|
||||
*/
|
||||
public Graph transpose() {
|
||||
// TODO:
|
||||
return null;
|
||||
ArrayList<Node> trNodes = new ArrayList<>(nodes.size());
|
||||
for (Node node: nodes) {
|
||||
trNodes.add(new Node(node.getId(), node.getPoint()));
|
||||
}
|
||||
for (Node node: nodes) {
|
||||
Node orig = trNodes.get(node.getId());
|
||||
for (Arc arc: node.getSuccessors()) {
|
||||
if (arc.getRoadInformation().isOneWay()) {
|
||||
Node dest = trNodes.get(arc.getDestination().getId());
|
||||
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
|
||||
arc.getRoadInformation(), arc.getPoints())));
|
||||
}
|
||||
else if (arc instanceof ArcForward) {
|
||||
Node dest = trNodes.get(arc.getDestination().getId());
|
||||
Arc newArc = new ArcForward(orig, dest, arc.getLength(),
|
||||
arc.getRoadInformation(), arc.getPoints());
|
||||
dest.addSuccessor(new ArcBackward(newArc));
|
||||
orig.addSuccessor(newArc);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new Graph("R/" + mapId, mapName, trNodes, graphStatistics);
|
||||
}
|
||||
|
||||
}
|
||||
|
128
src/test/org/insa/graph/GraphTest.java
Normal file
128
src/test/org/insa/graph/GraphTest.java
Normal file
@ -0,0 +1,128 @@
|
||||
package org.insa.graph;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.insa.graph.RoadInformation.RoadType;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GraphTest {
|
||||
|
||||
// Small graph use for tests
|
||||
private static Graph graph;
|
||||
|
||||
// List of nodes
|
||||
private static Node[] nodes;
|
||||
|
||||
@BeforeClass
|
||||
public static void initAll() throws IOException {
|
||||
|
||||
// Create nodes
|
||||
nodes = new Node[5];
|
||||
for (int i = 0; i < nodes.length; ++i) {
|
||||
nodes[i] = new Node(i, null);
|
||||
}
|
||||
|
||||
Node.linkNodes(nodes[0], nodes[1], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[1], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[4], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
|
||||
graph = new Graph("ID", "", Arrays.asList(nodes), null);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return List of arcs between a and b.
|
||||
*/
|
||||
private List<Arc> getArcsBetween(Node a, Node b) {
|
||||
List<Arc> arcs = new ArrayList<>();
|
||||
for (Arc arc: a.getSuccessors()) {
|
||||
if (arc.getDestination().equals(b)) {
|
||||
arcs.add(arc);
|
||||
}
|
||||
}
|
||||
return arcs;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTranspose() {
|
||||
Graph transpose = graph.transpose();
|
||||
|
||||
// Basic asserts...
|
||||
assertEquals("R/" + graph.getMapId(), transpose.getMapId());
|
||||
assertEquals(graph.getNodes().size(), transpose.getNodes().size());
|
||||
|
||||
final int expNbSucc[] = { 4, 2, 2, 4, 2 };
|
||||
for (int i = 0; i < expNbSucc.length; ++i) {
|
||||
assertEquals(expNbSucc[i], transpose.getNodes().get(i).getSuccessors().size());
|
||||
}
|
||||
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(1)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(2)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(3)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(4)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(0)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(2)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(3)).size(), 0);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(4)).size(), 0);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(0)).size(), 1);
|
||||
assertEquals(
|
||||
getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(1)).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);
|
||||
|
||||
}
|
||||
}
|
77
src/test/org/insa/graph/NodeTest.java
Normal file
77
src/test/org/insa/graph/NodeTest.java
Normal file
@ -0,0 +1,77 @@
|
||||
package org.insa.graph;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import org.insa.graph.RoadInformation.RoadType;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class NodeTest {
|
||||
|
||||
// List of nodes
|
||||
private Node[] nodes;
|
||||
|
||||
@Before
|
||||
public void initAll() throws IOException {
|
||||
|
||||
// Create nodes
|
||||
nodes = new Node[5];
|
||||
for (int i = 0; i < nodes.length; ++i) {
|
||||
nodes[i] = new Node(i, null);
|
||||
}
|
||||
|
||||
Node.linkNodes(nodes[0], nodes[1], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[0], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[1], nodes[2], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[2], nodes[3], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
|
||||
new ArrayList<>());
|
||||
Node.linkNodes(nodes[3], nodes[4], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
Node.linkNodes(nodes[4], nodes[0], 0,
|
||||
new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The first arc between from a to b, or null.
|
||||
*/
|
||||
private Arc getFirstArcBetween(Node a, Node b) {
|
||||
for (Arc arc: a.getSuccessors()) {
|
||||
if (arc.getDestination().equals(b)) {
|
||||
return arc;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLinkNodes() {
|
||||
final int[] expNbSucc = { 4, 2, 5, 2, 1 };
|
||||
assertEquals(nodes.length, expNbSucc.length);
|
||||
for (int i = 0; i < expNbSucc.length; ++i) {
|
||||
assertEquals(nodes[i].getSuccessors().size(), expNbSucc[i]);
|
||||
}
|
||||
|
||||
assertEquals(getFirstArcBetween(nodes[0], nodes[1]).getRoadInformation(),
|
||||
getFirstArcBetween(nodes[1], nodes[0]).getRoadInformation());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user