Browse Source

Add code to transpose graph.

Holt59 3 years ago
parent
commit
19220dc8a4

+ 23
- 2
src/main/org/insa/graph/Graph.java View File

@@ -1,5 +1,6 @@
1 1
 package org.insa.graph;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Collections;
4 5
 import java.util.List;
5 6
 
@@ -71,8 +72,28 @@ public class Graph {
71 72
      * @return Transpose graph of this graph.
72 73
      */
73 74
     public Graph transpose() {
74
-        // TODO:
75
-        return null;
75
+        ArrayList<Node> trNodes = new ArrayList<>(nodes.size());
76
+        for (Node node: nodes) {
77
+            trNodes.add(new Node(node.getId(), node.getPoint()));
78
+        }
79
+        for (Node node: nodes) {
80
+            Node orig = trNodes.get(node.getId());
81
+            for (Arc arc: node.getSuccessors()) {
82
+                if (arc.getRoadInformation().isOneWay()) {
83
+                    Node dest = trNodes.get(arc.getDestination().getId());
84
+                    dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
85
+                            arc.getRoadInformation(), arc.getPoints())));
86
+                }
87
+                else if (arc instanceof ArcForward) {
88
+                    Node dest = trNodes.get(arc.getDestination().getId());
89
+                    Arc newArc = new ArcForward(orig, dest, arc.getLength(),
90
+                            arc.getRoadInformation(), arc.getPoints());
91
+                    dest.addSuccessor(new ArcBackward(newArc));
92
+                    orig.addSuccessor(newArc);
93
+                }
94
+            }
95
+        }
96
+        return new Graph("R/" + mapId, mapName, trNodes, graphStatistics);
76 97
     }
77 98
 
78 99
 }

+ 128
- 0
src/test/org/insa/graph/GraphTest.java View File

@@ -0,0 +1,128 @@
1
+package org.insa.graph;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+
5
+import java.io.IOException;
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+import java.util.List;
9
+
10
+import org.insa.graph.RoadInformation.RoadType;
11
+import org.junit.BeforeClass;
12
+import org.junit.Test;
13
+
14
+public class GraphTest {
15
+
16
+    // Small graph use for tests
17
+    private static Graph graph;
18
+
19
+    // List of nodes
20
+    private static Node[] nodes;
21
+
22
+    @BeforeClass
23
+    public static void initAll() throws IOException {
24
+
25
+        // Create nodes
26
+        nodes = new Node[5];
27
+        for (int i = 0; i < nodes.length; ++i) {
28
+            nodes[i] = new Node(i, null);
29
+        }
30
+
31
+        Node.linkNodes(nodes[0], nodes[1], 0,
32
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
33
+                new ArrayList<>());
34
+        Node.linkNodes(nodes[0], nodes[2], 0,
35
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
36
+                new ArrayList<>());
37
+        Node.linkNodes(nodes[0], nodes[4], 0,
38
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
39
+        Node.linkNodes(nodes[1], nodes[2], 0,
40
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
41
+                new ArrayList<>());
42
+        Node.linkNodes(nodes[2], nodes[3], 0,
43
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
44
+        Node.linkNodes(nodes[2], nodes[3], 0,
45
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
46
+        Node.linkNodes(nodes[2], nodes[3], 0,
47
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
48
+        Node.linkNodes(nodes[3], nodes[0], 0,
49
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
50
+                new ArrayList<>());
51
+        Node.linkNodes(nodes[3], nodes[4], 0,
52
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
53
+        Node.linkNodes(nodes[4], nodes[0], 0,
54
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
55
+
56
+        graph = new Graph("ID", "", Arrays.asList(nodes), null);
57
+
58
+    }
59
+
60
+    /**
61
+     * @return List of arcs between a and b.
62
+     */
63
+    private List<Arc> getArcsBetween(Node a, Node b) {
64
+        List<Arc> arcs = new ArrayList<>();
65
+        for (Arc arc: a.getSuccessors()) {
66
+            if (arc.getDestination().equals(b)) {
67
+                arcs.add(arc);
68
+            }
69
+        }
70
+        return arcs;
71
+    }
72
+
73
+    @Test
74
+    public void testTranspose() {
75
+        Graph transpose = graph.transpose();
76
+
77
+        // Basic asserts...
78
+        assertEquals("R/" + graph.getMapId(), transpose.getMapId());
79
+        assertEquals(graph.getNodes().size(), transpose.getNodes().size());
80
+
81
+        final int expNbSucc[] = { 4, 2, 2, 4, 2 };
82
+        for (int i = 0; i < expNbSucc.length; ++i) {
83
+            assertEquals(expNbSucc[i], transpose.getNodes().get(i).getSuccessors().size());
84
+        }
85
+
86
+        assertEquals(
87
+                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(1)).size(), 1);
88
+        assertEquals(
89
+                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(2)).size(), 1);
90
+        assertEquals(
91
+                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(3)).size(), 1);
92
+        assertEquals(
93
+                getArcsBetween(transpose.getNodes().get(0), transpose.getNodes().get(4)).size(), 1);
94
+        assertEquals(
95
+                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(0)).size(), 1);
96
+        assertEquals(
97
+                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(2)).size(), 1);
98
+        assertEquals(
99
+                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(3)).size(), 0);
100
+        assertEquals(
101
+                getArcsBetween(transpose.getNodes().get(1), transpose.getNodes().get(4)).size(), 0);
102
+        assertEquals(
103
+                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(0)).size(), 1);
104
+        assertEquals(
105
+                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(1)).size(), 1);
106
+        assertEquals(
107
+                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(3)).size(), 0);
108
+        assertEquals(
109
+                getArcsBetween(transpose.getNodes().get(2), transpose.getNodes().get(4)).size(), 0);
110
+        assertEquals(
111
+                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(0)).size(), 1);
112
+        assertEquals(
113
+                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(1)).size(), 0);
114
+        assertEquals(
115
+                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(2)).size(), 3);
116
+        assertEquals(
117
+                getArcsBetween(transpose.getNodes().get(3), transpose.getNodes().get(4)).size(), 0);
118
+        assertEquals(
119
+                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(0)).size(), 1);
120
+        assertEquals(
121
+                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(1)).size(), 0);
122
+        assertEquals(
123
+                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(2)).size(), 0);
124
+        assertEquals(
125
+                getArcsBetween(transpose.getNodes().get(4), transpose.getNodes().get(3)).size(), 1);
126
+
127
+    }
128
+}

+ 77
- 0
src/test/org/insa/graph/NodeTest.java View File

@@ -0,0 +1,77 @@
1
+package org.insa.graph;
2
+
3
+import static org.junit.Assert.assertEquals;
4
+
5
+import java.io.IOException;
6
+import java.util.ArrayList;
7
+
8
+import org.insa.graph.RoadInformation.RoadType;
9
+import org.junit.Before;
10
+import org.junit.Test;
11
+
12
+public class NodeTest {
13
+
14
+    // List of nodes
15
+    private Node[] nodes;
16
+
17
+    @Before
18
+    public void initAll() throws IOException {
19
+
20
+        // Create nodes
21
+        nodes = new Node[5];
22
+        for (int i = 0; i < nodes.length; ++i) {
23
+            nodes[i] = new Node(i, null);
24
+        }
25
+
26
+        Node.linkNodes(nodes[0], nodes[1], 0,
27
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
28
+                new ArrayList<>());
29
+        Node.linkNodes(nodes[0], nodes[2], 0,
30
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
31
+                new ArrayList<>());
32
+        Node.linkNodes(nodes[0], nodes[4], 0,
33
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
34
+        Node.linkNodes(nodes[1], nodes[2], 0,
35
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
36
+                new ArrayList<>());
37
+        Node.linkNodes(nodes[2], nodes[3], 0,
38
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
39
+        Node.linkNodes(nodes[2], nodes[3], 0,
40
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
41
+        Node.linkNodes(nodes[2], nodes[3], 0,
42
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
43
+        Node.linkNodes(nodes[3], nodes[0], 0,
44
+                new RoadInformation(RoadType.UNCLASSIFIED, null, false, 1, null),
45
+                new ArrayList<>());
46
+        Node.linkNodes(nodes[3], nodes[4], 0,
47
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
48
+        Node.linkNodes(nodes[4], nodes[0], 0,
49
+                new RoadInformation(RoadType.UNCLASSIFIED, null, true, 1, null), new ArrayList<>());
50
+
51
+    }
52
+
53
+    /**
54
+     * @return The first arc between from a to b, or null.
55
+     */
56
+    private Arc getFirstArcBetween(Node a, Node b) {
57
+        for (Arc arc: a.getSuccessors()) {
58
+            if (arc.getDestination().equals(b)) {
59
+                return arc;
60
+            }
61
+        }
62
+        return null;
63
+    }
64
+
65
+    @Test
66
+    public void testLinkNodes() {
67
+        final int[] expNbSucc = { 4, 2, 5, 2, 1 };
68
+        assertEquals(nodes.length, expNbSucc.length);
69
+        for (int i = 0; i < expNbSucc.length; ++i) {
70
+            assertEquals(nodes[i].getSuccessors().size(), expNbSucc[i]);
71
+        }
72
+
73
+        assertEquals(getFirstArcBetween(nodes[0], nodes[1]).getRoadInformation(),
74
+                getFirstArcBetween(nodes[1], nodes[0]).getRoadInformation());
75
+    }
76
+
77
+}

Loading…
Cancel
Save