From 78e509b373b89290f8acb4a13870423d3a70a413 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Mon, 8 Oct 2018 17:09:50 +0300 Subject: [PATCH 1/9] There was no explanation for the SkylineProblem algorithm. Added and explanation to SkylineProblem. --- SkylineProblem/SkylineProblem.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/SkylineProblem/SkylineProblem.java b/SkylineProblem/SkylineProblem.java index a0b70631..121116e1 100644 --- a/SkylineProblem/SkylineProblem.java +++ b/SkylineProblem/SkylineProblem.java @@ -1,3 +1,9 @@ +/** + * Given n rectangular buildings in a 2-dimensional city, computes the skyline of these buildings, + * eliminating hidden lines. The main task is to view buildings from a side and remove all sections + * that are not visible. + * Source for explanation: https://www.geeksforgeeks.org/the-skyline-problem-using-divide-and-conquer-algorithm/ + */ import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; From c7ca13b9610eeac969dd50d0b7afd2473821c930 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Mon, 8 Oct 2018 19:13:19 +0300 Subject: [PATCH 2/9] Added a new Dijkstra's algorithm. The code and comments should be more readable. --- Others/Dijkstra.java | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 Others/Dijkstra.java diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java new file mode 100644 index 00000000..05914450 --- /dev/null +++ b/Others/Dijkstra.java @@ -0,0 +1,178 @@ +package Others; + +/** + * Dijkstra's algorithm,is a graph search algorithm that solves the single-source + * shortest path problem for a graph with nonnegative edge path costs, producing + * a shortest path tree. + * + * NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph consisting + * of 2 or more nodes, generally represented by an adjacency matrix or list, and a start node. + * + * Original source of code: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java + * Also most of the comments are from RosettaCode. + * + */ + +//import java.io.*; +import java.util.*; +public class Dijkstra { + private static final Graph.Edge[] GRAPH = { + new Graph.Edge("a", "b", 7), //Distance from node "a" to node "b" is 7. In the current Graph there is no way to move the other way (e,g, from "b" to "a"), a new edge would be needed for that + new Graph.Edge("a", "c", 9), + new Graph.Edge("a", "f", 14), + new Graph.Edge("b", "c", 10), + new Graph.Edge("b", "d", 15), + new Graph.Edge("c", "d", 11), + new Graph.Edge("c", "f", 2), + new Graph.Edge("d", "e", 6), + new Graph.Edge("e", "f", 9), + }; + private static final String START = "a"; + private static final String END = "e"; + + /** + * main function + * Will run the code with "GRAPH" that was defined above. + */ + public static void main(String[] args) { + Graph g = new Graph(GRAPH); + g.dijkstra(START); + g.printPath(END); + //g.printAllPaths(); + } +} + +class Graph { + private final Map graph; // mapping of vertex names to Vertex objects, built from a set of Edges + + /** One edge of the graph (only used by Graph constructor) */ + public static class Edge { + public final String v1, v2; + public final int dist; + public Edge(String v1, String v2, int dist) { + this.v1 = v1; + this.v2 = v2; + this.dist = dist; + } + } + + /** One vertex of the graph, complete with mappings to neighbouring vertices */ + public static class Vertex implements Comparable{ + public final String name; + public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity + public Vertex previous = null; + public final Map neighbours = new HashMap<>(); + + public Vertex(String name) + { + this.name = name; + } + + private void printPath() + { + if (this == this.previous) + { + System.out.printf("%s", this.name); + } + else if (this.previous == null) + { + System.out.printf("%s(unreached)", this.name); + } + else + { + this.previous.printPath(); + System.out.printf(" -> %s(%d)", this.name, this.dist); + } + } + + public int compareTo(Vertex other) + { + if (dist == other.dist) + return name.compareTo(other.name); + + return Integer.compare(dist, other.dist); + } + + @Override public String toString() + { + return "(" + name + ", " + dist + ")"; + } +} + + /** Builds a graph from a set of edges */ + public Graph(Edge[] edges) { + graph = new HashMap<>(edges.length); + + //one pass to find all vertices + for (Edge e : edges) { + if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1)); + if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2)); + } + + //another pass to set neighbouring vertices + for (Edge e : edges) { + graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist); + //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph + } + } + + /** Runs dijkstra using a specified source vertex */ + public void dijkstra(String startName) { + if (!graph.containsKey(startName)) { + System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName); + return; + } + final Vertex source = graph.get(startName); + NavigableSet q = new TreeSet<>(); + + // set-up vertices + for (Vertex v : graph.values()) { + v.previous = v == source ? source : null; + v.dist = v == source ? 0 : Integer.MAX_VALUE; + q.add(v); + } + + dijkstra(q); + } + + /** Implementation of dijkstra's algorithm using a binary heap. */ + private void dijkstra(final NavigableSet q) { + Vertex u, v; + while (!q.isEmpty()) { + + u = q.pollFirst(); // vertex with shortest distance (first iteration will return source) + if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable + + //look at distances to each neighbour + for (Map.Entry a : u.neighbours.entrySet()) { + v = a.getKey(); //the neighbour in this iteration + + final int alternateDist = u.dist + a.getValue(); + if (alternateDist < v.dist) { // shorter path to neighbour found + q.remove(v); + v.dist = alternateDist; + v.previous = u; + q.add(v); + } + } + } + } + + /** Prints a path from the source to the specified vertex */ + public void printPath(String endName) { + if (!graph.containsKey(endName)) { + System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName); + return; + } + + graph.get(endName).printPath(); + System.out.println(); + } + /** Prints the path from the source to every vertex (output order is not guaranteed) */ + public void printAllPaths() { + for (Vertex v : graph.values()) { + v.printPath(); + System.out.println(); + } + } +} \ No newline at end of file From 6cfc760e30d513401f718f005b6d221ca067a8f5 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Mon, 8 Oct 2018 19:14:47 +0300 Subject: [PATCH 3/9] Added a new Dijkstra's algorithm. --- Others/Dijkstra.java | 1 - 1 file changed, 1 deletion(-) diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java index 05914450..ae9d35f0 100644 --- a/Others/Dijkstra.java +++ b/Others/Dijkstra.java @@ -12,7 +12,6 @@ package Others; * Also most of the comments are from RosettaCode. * */ - //import java.io.*; import java.util.*; public class Dijkstra { From d2d0b785c37ab3b07631552cab39ebb8674078d8 Mon Sep 17 00:00:00 2001 From: rmakynen Date: Mon, 8 Oct 2018 19:16:15 +0300 Subject: [PATCH 4/9] test --- Others/Dijkstra.java | 177 ------------------------------------------- 1 file changed, 177 deletions(-) delete mode 100644 Others/Dijkstra.java diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java deleted file mode 100644 index ae9d35f0..00000000 --- a/Others/Dijkstra.java +++ /dev/null @@ -1,177 +0,0 @@ -package Others; - -/** - * Dijkstra's algorithm,is a graph search algorithm that solves the single-source - * shortest path problem for a graph with nonnegative edge path costs, producing - * a shortest path tree. - * - * NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph consisting - * of 2 or more nodes, generally represented by an adjacency matrix or list, and a start node. - * - * Original source of code: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java - * Also most of the comments are from RosettaCode. - * - */ -//import java.io.*; -import java.util.*; -public class Dijkstra { - private static final Graph.Edge[] GRAPH = { - new Graph.Edge("a", "b", 7), //Distance from node "a" to node "b" is 7. In the current Graph there is no way to move the other way (e,g, from "b" to "a"), a new edge would be needed for that - new Graph.Edge("a", "c", 9), - new Graph.Edge("a", "f", 14), - new Graph.Edge("b", "c", 10), - new Graph.Edge("b", "d", 15), - new Graph.Edge("c", "d", 11), - new Graph.Edge("c", "f", 2), - new Graph.Edge("d", "e", 6), - new Graph.Edge("e", "f", 9), - }; - private static final String START = "a"; - private static final String END = "e"; - - /** - * main function - * Will run the code with "GRAPH" that was defined above. - */ - public static void main(String[] args) { - Graph g = new Graph(GRAPH); - g.dijkstra(START); - g.printPath(END); - //g.printAllPaths(); - } -} - -class Graph { - private final Map graph; // mapping of vertex names to Vertex objects, built from a set of Edges - - /** One edge of the graph (only used by Graph constructor) */ - public static class Edge { - public final String v1, v2; - public final int dist; - public Edge(String v1, String v2, int dist) { - this.v1 = v1; - this.v2 = v2; - this.dist = dist; - } - } - - /** One vertex of the graph, complete with mappings to neighbouring vertices */ - public static class Vertex implements Comparable{ - public final String name; - public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity - public Vertex previous = null; - public final Map neighbours = new HashMap<>(); - - public Vertex(String name) - { - this.name = name; - } - - private void printPath() - { - if (this == this.previous) - { - System.out.printf("%s", this.name); - } - else if (this.previous == null) - { - System.out.printf("%s(unreached)", this.name); - } - else - { - this.previous.printPath(); - System.out.printf(" -> %s(%d)", this.name, this.dist); - } - } - - public int compareTo(Vertex other) - { - if (dist == other.dist) - return name.compareTo(other.name); - - return Integer.compare(dist, other.dist); - } - - @Override public String toString() - { - return "(" + name + ", " + dist + ")"; - } -} - - /** Builds a graph from a set of edges */ - public Graph(Edge[] edges) { - graph = new HashMap<>(edges.length); - - //one pass to find all vertices - for (Edge e : edges) { - if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1)); - if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2)); - } - - //another pass to set neighbouring vertices - for (Edge e : edges) { - graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist); - //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph - } - } - - /** Runs dijkstra using a specified source vertex */ - public void dijkstra(String startName) { - if (!graph.containsKey(startName)) { - System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName); - return; - } - final Vertex source = graph.get(startName); - NavigableSet q = new TreeSet<>(); - - // set-up vertices - for (Vertex v : graph.values()) { - v.previous = v == source ? source : null; - v.dist = v == source ? 0 : Integer.MAX_VALUE; - q.add(v); - } - - dijkstra(q); - } - - /** Implementation of dijkstra's algorithm using a binary heap. */ - private void dijkstra(final NavigableSet q) { - Vertex u, v; - while (!q.isEmpty()) { - - u = q.pollFirst(); // vertex with shortest distance (first iteration will return source) - if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable - - //look at distances to each neighbour - for (Map.Entry a : u.neighbours.entrySet()) { - v = a.getKey(); //the neighbour in this iteration - - final int alternateDist = u.dist + a.getValue(); - if (alternateDist < v.dist) { // shorter path to neighbour found - q.remove(v); - v.dist = alternateDist; - v.previous = u; - q.add(v); - } - } - } - } - - /** Prints a path from the source to the specified vertex */ - public void printPath(String endName) { - if (!graph.containsKey(endName)) { - System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName); - return; - } - - graph.get(endName).printPath(); - System.out.println(); - } - /** Prints the path from the source to every vertex (output order is not guaranteed) */ - public void printAllPaths() { - for (Vertex v : graph.values()) { - v.printPath(); - System.out.println(); - } - } -} \ No newline at end of file From c9d0aa129048d065e3f8c8e2522d249a65bc5d8f Mon Sep 17 00:00:00 2001 From: rmakynen Date: Mon, 8 Oct 2018 19:17:56 +0300 Subject: [PATCH 5/9] New Dijkstra's algorithm with better comments. --- Others/Dijkstra.java | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100644 Others/Dijkstra.java diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java new file mode 100644 index 00000000..dd671547 --- /dev/null +++ b/Others/Dijkstra.java @@ -0,0 +1,178 @@ +package Others; + + +/** + * Dijkstra's algorithm,is a graph search algorithm that solves the single-source + * shortest path problem for a graph with nonnegative edge path costs, producing + * a shortest path tree. + * + * NOTE: The inputs to Dijkstra's algorithm are a directed and weighted graph consisting + * of 2 or more nodes, generally represented by an adjacency matrix or list, and a start node. + * + * Original source of code: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Java + * Also most of the comments are from RosettaCode. + * + */ +//import java.io.*; +import java.util.*; +public class Dijkstra { + private static final Graph.Edge[] GRAPH = { + new Graph.Edge("a", "b", 7), //Distance from node "a" to node "b" is 7. In the current Graph there is no way to move the other way (e,g, from "b" to "a"), a new edge would be needed for that + new Graph.Edge("a", "c", 9), + new Graph.Edge("a", "f", 14), + new Graph.Edge("b", "c", 10), + new Graph.Edge("b", "d", 15), + new Graph.Edge("c", "d", 11), + new Graph.Edge("c", "f", 2), + new Graph.Edge("d", "e", 6), + new Graph.Edge("e", "f", 9), + }; + private static final String START = "a"; + private static final String END = "e"; + + /** + * main function + * Will run the code with "GRAPH" that was defined above. + */ + public static void main(String[] args) { + Graph g = new Graph(GRAPH); + g.dijkstra(START); + g.printPath(END); + //g.printAllPaths(); + } +} + +class Graph { + private final Map graph; // mapping of vertex names to Vertex objects, built from a set of Edges + + /** One edge of the graph (only used by Graph constructor) */ + public static class Edge { + public final String v1, v2; + public final int dist; + public Edge(String v1, String v2, int dist) { + this.v1 = v1; + this.v2 = v2; + this.dist = dist; + } + } + + /** One vertex of the graph, complete with mappings to neighbouring vertices */ + public static class Vertex implements Comparable{ + public final String name; + public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity + public Vertex previous = null; + public final Map neighbours = new HashMap<>(); + + public Vertex(String name) + { + this.name = name; + } + + private void printPath() + { + if (this == this.previous) + { + System.out.printf("%s", this.name); + } + else if (this.previous == null) + { + System.out.printf("%s(unreached)", this.name); + } + else + { + this.previous.printPath(); + System.out.printf(" -> %s(%d)", this.name, this.dist); + } + } + + public int compareTo(Vertex other) + { + if (dist == other.dist) + return name.compareTo(other.name); + + return Integer.compare(dist, other.dist); + } + + @Override public String toString() + { + return "(" + name + ", " + dist + ")"; + } +} + + /** Builds a graph from a set of edges */ + public Graph(Edge[] edges) { + graph = new HashMap<>(edges.length); + + //one pass to find all vertices + for (Edge e : edges) { + if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1)); + if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2)); + } + + //another pass to set neighbouring vertices + for (Edge e : edges) { + graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist); + //graph.get(e.v2).neighbours.put(graph.get(e.v1), e.dist); // also do this for an undirected graph + } + } + + /** Runs dijkstra using a specified source vertex */ + public void dijkstra(String startName) { + if (!graph.containsKey(startName)) { + System.err.printf("Graph doesn't contain start vertex \"%s\"\n", startName); + return; + } + final Vertex source = graph.get(startName); + NavigableSet q = new TreeSet<>(); + + // set-up vertices + for (Vertex v : graph.values()) { + v.previous = v == source ? source : null; + v.dist = v == source ? 0 : Integer.MAX_VALUE; + q.add(v); + } + + dijkstra(q); + } + + /** Implementation of dijkstra's algorithm using a binary heap. */ + private void dijkstra(final NavigableSet q) { + Vertex u, v; + while (!q.isEmpty()) { + + u = q.pollFirst(); // vertex with shortest distance (first iteration will return source) + if (u.dist == Integer.MAX_VALUE) break; // we can ignore u (and any other remaining vertices) since they are unreachable + + //look at distances to each neighbour + for (Map.Entry a : u.neighbours.entrySet()) { + v = a.getKey(); //the neighbour in this iteration + + final int alternateDist = u.dist + a.getValue(); + if (alternateDist < v.dist) { // shorter path to neighbour found + q.remove(v); + v.dist = alternateDist; + v.previous = u; + q.add(v); + } + } + } + } + + /** Prints a path from the source to the specified vertex */ + public void printPath(String endName) { + if (!graph.containsKey(endName)) { + System.err.printf("Graph doesn't contain end vertex \"%s\"\n", endName); + return; + } + + graph.get(endName).printPath(); + System.out.println(); + } + /** Prints the path from the source to every vertex (output order is not guaranteed) */ + public void printAllPaths() { + for (Vertex v : graph.values()) { + v.printPath(); + System.out.println(); + } + } +} \ No newline at end of file From 66c6353705723932f5e80fdeae185302da843d9e Mon Sep 17 00:00:00 2001 From: rmakynen <43169683+rmakynen@users.noreply.github.com> Date: Tue, 9 Oct 2018 09:05:59 +0300 Subject: [PATCH 6/9] Update Dijkstra.java --- Others/Dijkstra.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java index dd671547..5df11f02 100644 --- a/Others/Dijkstra.java +++ b/Others/Dijkstra.java @@ -63,13 +63,11 @@ class Graph { public Vertex previous = null; public final Map neighbours = new HashMap<>(); - public Vertex(String name) - { + public Vertex(String name){ this.name = name; } - private void printPath() - { + private void printPath(){ if (this == this.previous) { System.out.printf("%s", this.name); @@ -85,16 +83,14 @@ class Graph { } } - public int compareTo(Vertex other) - { + public int compareTo(Vertex other){ if (dist == other.dist) return name.compareTo(other.name); return Integer.compare(dist, other.dist); } - @Override public String toString() - { + @Override public String toString(){ return "(" + name + ", " + dist + ")"; } } @@ -175,4 +171,4 @@ class Graph { System.out.println(); } } -} \ No newline at end of file +} From 57fbbcd83608de76562a4eebded4513809cce15a Mon Sep 17 00:00:00 2001 From: rmakynen <43169683+rmakynen@users.noreply.github.com> Date: Tue, 9 Oct 2018 09:07:56 +0300 Subject: [PATCH 7/9] Updated the format of brackets --- Others/Dijkstra.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java index 5df11f02..4ae2daa5 100644 --- a/Others/Dijkstra.java +++ b/Others/Dijkstra.java @@ -57,7 +57,7 @@ class Graph { } /** One vertex of the graph, complete with mappings to neighbouring vertices */ - public static class Vertex implements Comparable{ + public static class Vertex implements Comparable { public final String name; public int dist = Integer.MAX_VALUE; // MAX_VALUE assumed to be infinity public Vertex previous = null; @@ -67,7 +67,7 @@ class Graph { this.name = name; } - private void printPath(){ + private void printPath() { if (this == this.previous) { System.out.printf("%s", this.name); @@ -83,14 +83,14 @@ class Graph { } } - public int compareTo(Vertex other){ + public int compareTo(Vertex other) { if (dist == other.dist) return name.compareTo(other.name); return Integer.compare(dist, other.dist); } - @Override public String toString(){ + @Override public String toString() { return "(" + name + ", " + dist + ")"; } } From f98e3c06bdf37fa8614b5ebd63ad55fbc39fc1c6 Mon Sep 17 00:00:00 2001 From: rmakynen <43169683+rmakynen@users.noreply.github.com> Date: Tue, 9 Oct 2018 09:09:38 +0300 Subject: [PATCH 8/9] Updated brackets --- Others/Dijkstra.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java index 4ae2daa5..0fe46134 100644 --- a/Others/Dijkstra.java +++ b/Others/Dijkstra.java @@ -63,7 +63,7 @@ class Graph { public Vertex previous = null; public final Map neighbours = new HashMap<>(); - public Vertex(String name){ + public Vertex(String name) { this.name = name; } From efae9fb3179b076d134dba7f82d932f085d0ea8f Mon Sep 17 00:00:00 2001 From: rmakynen <43169683+rmakynen@users.noreply.github.com> Date: Tue, 9 Oct 2018 09:16:00 +0300 Subject: [PATCH 9/9] Updated brackets --- Others/Dijkstra.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/Others/Dijkstra.java b/Others/Dijkstra.java index 0fe46134..b3df65bf 100644 --- a/Others/Dijkstra.java +++ b/Others/Dijkstra.java @@ -68,16 +68,13 @@ class Graph { } private void printPath() { - if (this == this.previous) - { + if (this == this.previous) { System.out.printf("%s", this.name); } - else if (this.previous == null) - { + else if (this.previous == null) { System.out.printf("%s(unreached)", this.name); } - else - { + else { this.previous.printPath(); System.out.printf(" -> %s(%d)", this.name, this.dist); }