summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHaidong Ji2021-07-05 14:38:04 -0500
committerHaidong Ji2021-07-05 14:38:04 -0500
commit41efb7277792e9d1502d305cfd706dd514e24663 (patch)
treedf6b7225df83981350f86bc7d48300b4ea3f64dd
parent3929c18515bc46cae10c0e2d66c0c622591eee96 (diff)
Dijkstra shortest path done, using PriorityQueue. Don't give up! Very rewarding!HEADmaster
-rw-r--r--.idea/AlgoGraphJava.iml16
-rw-r--r--src/main/Dijkstra.java64
-rw-r--r--src/main/PlayGround.java13
-rw-r--r--src/test/DijkstraTest.java264
-rw-r--r--src/test/PlayGroundTest.java13
5 files changed, 370 insertions, 0 deletions
diff --git a/.idea/AlgoGraphJava.iml b/.idea/AlgoGraphJava.iml
index f971190..9bf96ce 100644
--- a/.idea/AlgoGraphJava.iml
+++ b/.idea/AlgoGraphJava.iml
@@ -24,5 +24,21 @@
<SOURCES />
</library>
</orderEntry>
+ <orderEntry type="module-library" scope="TEST">
+ <library name="JUnit5.7.0">
+ <CLASSES>
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.7.0/junit-jupiter-5.7.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.7.0/junit-jupiter-api-5.7.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.0/apiguardian-api-1.1.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.7.0/junit-platform-commons-1.7.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.7.0/junit-jupiter-params-5.7.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.7.0/junit-jupiter-engine-5.7.0.jar!/" />
+ <root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.7.0/junit-platform-engine-1.7.0.jar!/" />
+ </CLASSES>
+ <JAVADOC />
+ <SOURCES />
+ </library>
+ </orderEntry>
</component>
</module> \ No newline at end of file
diff --git a/src/main/Dijkstra.java b/src/main/Dijkstra.java
new file mode 100644
index 0000000..5bc31b0
--- /dev/null
+++ b/src/main/Dijkstra.java
@@ -0,0 +1,64 @@
+import java.util.*;
+
+public class Dijkstra {
+ static int distance(ArrayList<Integer>[] adj, ArrayList<Integer>[] cost, int s, int t) {
+ if (s == t)
+ return 0;
+ int[] dist = new int[adj.length];
+ //int[] prev = new int[adj.length];
+ boolean[] visited = new boolean[adj.length];
+ for (int i = 0; i < dist.length; i++) {
+ dist[i] = Integer.MAX_VALUE;
+ //prev[i] = -1;
+ }
+
+ dist[s] = 0;
+
+ PriorityQueue<Integer> vertexQ;
+ vertexQ = new PriorityQueue<Integer>(Comparator.comparingInt(a -> dist[a]));
+ vertexQ.add(s);
+
+ while (!vertexQ.isEmpty()) {
+ int u = vertexQ.remove();
+ if (visited[u]) {
+ continue;
+ }
+ visited[u] = true;
+ if (adj[u].size() > 0) {
+ for (int i = 0; i < adj[u].size(); i++) {
+ if (dist[adj[u].get(i)] > dist[u] + cost[u].get(i)) {
+ dist[adj[u].get(i)] = dist[u] + cost[u].get(i);
+ //prev[adj[u].get(i)] = u;
+ vertexQ.add(adj[u].get(i));
+ }
+ }
+ }
+ }
+ if (dist[t] == Integer.MAX_VALUE)
+ return -1;
+ return dist[t];
+ }
+
+ public static void main(String[] args) {
+ Scanner scanner = new Scanner(System.in);
+ int n = scanner.nextInt();
+ int m = scanner.nextInt();
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[]) new ArrayList[n];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[]) new ArrayList[n];
+ for (int i = 0; i < n; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+ for (int i = 0; i < m; i++) {
+ int x, y, w;
+ x = scanner.nextInt();
+ y = scanner.nextInt();
+ w = scanner.nextInt();
+ adj[x - 1].add(y - 1);
+ cost[x - 1].add(w);
+ }
+ int x = scanner.nextInt() - 1;
+ int y = scanner.nextInt() - 1;
+ System.out.println(distance(adj, cost, x, y));
+ }
+} \ No newline at end of file
diff --git a/src/main/PlayGround.java b/src/main/PlayGround.java
new file mode 100644
index 0000000..14a4c80
--- /dev/null
+++ b/src/main/PlayGround.java
@@ -0,0 +1,13 @@
+import java.util.PriorityQueue;
+import java.util.HashMap;
+public class PlayGround {
+ static int testPriorityQueue() {
+ PriorityQueue<Integer> numbers = new PriorityQueue<>();
+ numbers.add(7);
+ numbers.add(5);
+ numbers.add(8);
+ numbers.add(3);
+ return numbers.poll();
+ }
+
+}
diff --git a/src/test/DijkstraTest.java b/src/test/DijkstraTest.java
new file mode 100644
index 0000000..1afdbb3
--- /dev/null
+++ b/src/test/DijkstraTest.java
@@ -0,0 +1,264 @@
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class DijkstraTest {
+
+ @Test
+ void test1() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[4];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[4];
+
+ for (int i = 0; i < 4; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[0].add(1);
+ cost[0].add(1);
+
+ adj[3].add(0);
+ cost[3].add(2);
+
+ adj[1].add(2);
+ cost[1].add(2);
+
+ adj[0].add(2);
+ cost[0].add(5);
+
+ //assertEquals(0, Dijkstra.distance(adj, cost, 1, 1));
+ assertEquals(3, Dijkstra.distance(adj, cost, 0, 2));
+ }
+
+ @Test
+ void test2() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[5];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[5];
+
+ for (int i = 0; i < 5; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[0].add(1);
+ cost[0].add(4);
+
+ adj[0].add(2);
+ cost[0].add(2);
+
+ adj[1].add(2);
+ cost[1].add(2);
+
+ adj[2].add(1);
+ cost[2].add(1);
+
+ adj[1].add(3);
+ cost[1].add(2);
+
+ adj[2].add(4);
+ cost[2].add(4);
+
+ adj[4].add(3);
+ cost[4].add(1);
+
+ adj[1].add(4);
+ cost[1].add(3);
+
+ adj[2].add(3);
+ cost[2].add(4);
+
+ assertEquals(6, Dijkstra.distance(adj, cost, 0, 4));
+ }
+
+ @Test
+ void test3() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[3];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[3];
+
+ for (int i = 0; i < 3; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[0].add(1);
+ cost[0].add(7);
+
+ adj[0].add(2);
+ cost[0].add(5);
+
+ adj[1].add(2);
+ cost[1].add(2);
+
+ assertEquals(-1, Dijkstra.distance(adj, cost, 2, 1));
+ }
+
+ @Test
+ void test4() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[5];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[5];
+
+ for (int i = 0; i < 5; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[0].add(1);
+ cost[0].add(5);
+
+ adj[1].add(2);
+ cost[1].add(1);
+
+ adj[2].add(3);
+ cost[2].add(1);
+
+ adj[3].add(4);
+ cost[3].add(1);
+
+ adj[2].add(4);
+ cost[2].add(2);
+
+ assertEquals(3, Dijkstra.distance(adj, cost, 1, 4));
+ }
+
+ @Test
+ void test6() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[9];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[9];
+
+ for (int i = 0; i < 9; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[0].add(1);
+ cost[0].add(4);
+
+ adj[1].add(0);
+ cost[1].add(4);
+
+ adj[0].add(7);
+ cost[0].add(8);
+
+ adj[7].add(0);
+ cost[7].add(8);
+
+ adj[1].add(7);
+ cost[1].add(11);
+
+ adj[7].add(1);
+ cost[7].add(11);
+
+ adj[1].add(2);
+ cost[1].add(8);
+
+ adj[2].add(1);
+ cost[2].add(8);
+
+ adj[7].add(8);
+ cost[7].add(7);
+
+ adj[8].add(7);
+ cost[8].add(7);
+
+ adj[7].add(6);
+ cost[7].add(1);
+
+ adj[6].add(7);
+ cost[6].add(1);
+
+ adj[2].add(8);
+ cost[2].add(2);
+
+ adj[8].add(2);
+ cost[8].add(2);
+
+ adj[8].add(6);
+ cost[8].add(6);
+
+ adj[6].add(8);
+ cost[6].add(6);
+
+ adj[2].add(3);
+ cost[2].add(7);
+
+ adj[3].add(2);
+ cost[3].add(7);
+
+ adj[2].add(5);
+ cost[2].add(4);
+
+ adj[5].add(2);
+ cost[5].add(4);
+
+ adj[6].add(5);
+ cost[6].add(2);
+
+ adj[5].add(6);
+ cost[5].add(2);
+
+ adj[3].add(5);
+ cost[3].add(14);
+
+ adj[5].add(3);
+ cost[5].add(14);
+
+ adj[3].add(4);
+ cost[3].add(9);
+
+ adj[4].add(3);
+ cost[4].add(9);
+
+ adj[4].add(5);
+ cost[4].add(10);
+
+ adj[5].add(4);
+ cost[5].add(10);
+
+ assertEquals(21, Dijkstra.distance(adj, cost, 0, 4));
+ }
+
+ @Test
+ void test11() {
+ ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[5];
+ ArrayList<Integer>[] cost = (ArrayList<Integer>[])new ArrayList[5];
+
+ for (int i = 0; i < 5; i++) {
+ adj[i] = new ArrayList<Integer>();
+ cost[i] = new ArrayList<Integer>();
+ }
+
+ adj[2].add(4);
+ cost[2].add(49438153);
+
+ adj[1].add(2);
+ cost[1].add(78072041);
+
+ adj[2].add(3);
+ cost[2].add(12409612);
+
+ adj[0].add(2);
+ cost[0].add(88526216);
+
+ adj[4].add(1);
+ cost[4].add(6876525);
+
+ adj[0].add(3);
+ cost[0].add(28703032);
+
+ adj[3].add(0);
+ cost[3].add(24629785);
+
+ adj[3].add(4);
+ cost[3].add(96300300);
+
+ adj[3].add(2);
+ cost[3].add(317823);
+
+ adj[1].add(4);
+ cost[1].add(22632987);
+
+ assertEquals(56632501, Dijkstra.distance(adj, cost, 3, 1));
+ }
+} \ No newline at end of file
diff --git a/src/test/PlayGroundTest.java b/src/test/PlayGroundTest.java
new file mode 100644
index 0000000..5182c91
--- /dev/null
+++ b/src/test/PlayGroundTest.java
@@ -0,0 +1,13 @@
+import org.junit.jupiter.api.Test;
+
+import java.util.ArrayList;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class PlayGroundTest {
+
+ @Test
+ void test1() {
+ assertEquals(5, PlayGround.testPriorityQueue());
+ }
+} \ No newline at end of file