From e0c1e9124c57cd757bb8e1438490a7831d400921 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 29 Oct 2019 17:14:06 +1000 Subject: [PATCH 1/2] - --- .gitignore | 4 + Graph/src/TestGraph.java | 33 +++++++++ Graph/src/graph/Graph.java | 148 +++++++++++++++++++++++++++++++++++++ Graph/src/graph/Path.java | 9 +++ Graph/src/graph/Point.java | 24 ++++++ 5 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 Graph/src/TestGraph.java create mode 100644 Graph/src/graph/Graph.java create mode 100644 Graph/src/graph/Path.java create mode 100644 Graph/src/graph/Point.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d7fe42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.iml +Graph/out/ +Graph/.idea/ + diff --git a/Graph/src/TestGraph.java b/Graph/src/TestGraph.java new file mode 100644 index 0000000..c33de3c --- /dev/null +++ b/Graph/src/TestGraph.java @@ -0,0 +1,33 @@ +import graph.Graph; +import graph.Path; +import graph.Point; + +public class TestGraph { + static public void main(String[] args) { + final int GRAPH_SIZE = 6; + + Graph graph = new Graph(GRAPH_SIZE); + Point a = new Point("A"); + Point b = new Point("B"); + Point c = new Point("C"); + Point d = new Point("D"); + Point e = new Point("E"); + + graph.addPoint(a); + graph.addPoint(b); + graph.addPoint(c); + graph.addPoint(d); + graph.addPoint(e); + + graph.addLink(a, b, 2); + graph.addLink(b, c, 3); + graph.addLink(c, e, 1); + graph.addLink(a, d, 5); + graph.addLink(d, e, 6); + + // graph.getAllPath(); + + //Path minPath = graph.getMinLengthPath(); + graph.getMinPath(); + } +} diff --git a/Graph/src/graph/Graph.java b/Graph/src/graph/Graph.java new file mode 100644 index 0000000..da63f36 --- /dev/null +++ b/Graph/src/graph/Graph.java @@ -0,0 +1,148 @@ +package graph; + +import java.util.Deque; +import java.util.LinkedList; +import java.util.List; + +public class Graph { + private int size; + private Point[] points; + private int count; + private int[][] linkMatrix; + + public Graph(int size) { + this.size = size; + + points = new Point[size]; + linkMatrix = new int[size][size]; + } + + public void addPoint(Point point) { + point.setKey(count); + points[count] = point; + count++; + } + + public void addLink(Point p1, Point p2, int length) { + int key1 = p1.getKey(); + int key2 = p2.getKey(); + + linkMatrix[key1][key2] = length; + linkMatrix[key2][key1] = length; + } + + //public Path getMinPath() { + public void getMinPath() { + Path[] paths = new Path[size]; + Tree pathTree = new Tree(); + + //Deque deque = new LinkedList(); + Deque dequeNode = new LinkedList(); + + //deque.addLast(points[0]); + + Node root = new Node(points[0]); + pathTree.setRoot(root); + dequeNode.addLast(root); + + //while(!deque.isEmpty()) { + while ((!dequeNode.isEmpty())) { + //Point pointFrom = deque.peekFirst(); + //int keyPointFrom = pointFrom.getKey(); + + Node parentNode = dequeNode.peekFirst(); + int keyPointFrom = parentNode.getPoint().getKey(); + + for (int keyPointTo = keyPointFrom + 1; keyPointTo < size; keyPointTo++) { + int length = linkMatrix[keyPointFrom][keyPointTo]; + if (length > 0) { + Point pointTo = points[keyPointTo]; + + // deque.addLast(pointTo); + + Node childNode = new Node(pointTo); + + dequeNode.addLast(childNode); + pathTree.addNode(parentNode, childNode); + } + } + + //Point point = deque.removeFirst(); + //System.out.println(point); + + dequeNode.removeFirst(); + } + + //return path; + System.out.println(); + System.out.println("tree:"); + pathTree.display(); + } +} + +class Node { + private Point point; + private Node parent; + private LinkedList chields; + + public Node(Point point) { + this.point = point; + this.chields = new LinkedList(); + } + + public void setParent(Node node) { + parent = node; + } + + public Point getPoint() { + return point; + } + + public Node getParent() { + return parent; + } + + public void addChield(Node node) { + chields.add(node); + } + + public LinkedList getChields() { + return chields; + } + + public String toString() { + return String.format("%s", point); + } +} + +class Tree { + private Node root; + + public Tree() { + root = null; + } + + public void setRoot(Node node) { + root = node; + } + + public void addNode(Node parent, Node child) { + child.setParent(parent); + parent.addChield(child); + } + + public void display() { + display0(root, ""); + } + + private void display0(Node node, String path) { + if (node.getChields().size() == 0) { + System.out.println(path + "-" + node); + return; + } + + for (Node child: node.getChields()) { + display0(child, path + "-" + node); + } + } +} diff --git a/Graph/src/graph/Path.java b/Graph/src/graph/Path.java new file mode 100644 index 0000000..9f70f3d --- /dev/null +++ b/Graph/src/graph/Path.java @@ -0,0 +1,9 @@ +package graph; + +public class Path { + private Point[] path; + + public void push(Point point) { + + } +} diff --git a/Graph/src/graph/Point.java b/Graph/src/graph/Point.java new file mode 100644 index 0000000..75e572c --- /dev/null +++ b/Graph/src/graph/Point.java @@ -0,0 +1,24 @@ +package graph; + +public class Point { + private String label; + private int key; + + public Point(String label) { + this.label = label; + } + + void setKey(int key) { + this.key = key; + } + + int getKey() { + return key; + } + + public String toString() { + return label; + } +} + + From 77934cc79112fa8fe5c87166ce4e2d52df408c6f Mon Sep 17 00:00:00 2001 From: Sleeper32 Date: Tue, 29 Oct 2019 23:27:55 +1000 Subject: [PATCH 2/2] implemented Dijkstra's algorithm --- Graph/src/TestGraph.java | 60 ++++++++++++- Graph/src/graph/Graph.java | 175 ++++++++++++++++--------------------- 2 files changed, 130 insertions(+), 105 deletions(-) diff --git a/Graph/src/TestGraph.java b/Graph/src/TestGraph.java index c33de3c..18fccf1 100644 --- a/Graph/src/TestGraph.java +++ b/Graph/src/TestGraph.java @@ -4,7 +4,13 @@ public class TestGraph { static public void main(String[] args) { - final int GRAPH_SIZE = 6; + + test1(); + test2(); + } + + public static void test1() { + final int GRAPH_SIZE = 10; Graph graph = new Graph(GRAPH_SIZE); Point a = new Point("A"); @@ -12,22 +18,68 @@ static public void main(String[] args) { Point c = new Point("C"); Point d = new Point("D"); Point e = new Point("E"); + Point f = new Point("F"); + Point g = new Point("G"); + Point h = new Point("H"); + Point i = new Point("I"); + Point k = new Point("K"); graph.addPoint(a); graph.addPoint(b); graph.addPoint(c); graph.addPoint(d); graph.addPoint(e); + graph.addPoint(f); + graph.addPoint(g); + graph.addPoint(h); + graph.addPoint(i); + graph.addPoint(k); graph.addLink(a, b, 2); graph.addLink(b, c, 3); graph.addLink(c, e, 1); graph.addLink(a, d, 5); graph.addLink(d, e, 6); + graph.addLink(e, f, 7); + graph.addLink(e, g, 1); + graph.addLink(f, i, 6); + graph.addLink(g, h, 2); + graph.addLink(h, i, 3); + graph.addLink(i, k, 1); + + graph.printMinPath(); + graph.printMinLength(); + } + + public static void test2() { + int size = 6; + Graph graph = new Graph(size); + + Point p1 = new Point("1"); + Point p2 = new Point("2"); + Point p3 = new Point("3"); + Point p4 = new Point("4"); + Point p5 = new Point("5"); + Point p6 = new Point("6"); + + graph.addPoint(p1); + graph.addPoint(p2); + graph.addPoint(p3); + graph.addPoint(p4); + graph.addPoint(p5); + graph.addPoint(p6); - // graph.getAllPath(); + graph.addLink(p1, p2, 7); + graph.addLink(p1, p3, 9); + graph.addLink(p1, p6, 14); + graph.addLink(p2, p4, 15); + graph.addLink(p2, p3, 10); + graph.addLink(p3, p4, 11); + graph.addLink(p3, p6, 2); + graph.addLink(p6, p5, 9); + graph.addLink(p5, p4, 6); - //Path minPath = graph.getMinLengthPath(); - graph.getMinPath(); + graph.printMinPath(); + graph.printMinLength(); } } diff --git a/Graph/src/graph/Graph.java b/Graph/src/graph/Graph.java index da63f36..98b2aab 100644 --- a/Graph/src/graph/Graph.java +++ b/Graph/src/graph/Graph.java @@ -3,24 +3,38 @@ import java.util.Deque; import java.util.LinkedList; import java.util.List; +import java.util.Stack; public class Graph { + private int capacity; private int size; + private Point[] points; - private int count; private int[][] linkMatrix; - public Graph(int size) { - this.size = size; + private int lengths[]; + private int paths[]; + + public Graph(int capacity) { + this.capacity = capacity; + this.size = 0; - points = new Point[size]; - linkMatrix = new int[size][size]; + points = new Point[capacity]; + linkMatrix = new int[capacity][capacity]; } public void addPoint(Point point) { - point.setKey(count); - points[count] = point; - count++; + point.setKey(size); + points[size] = point; + size++; + } + + public Point getPointByKey(int key) { + for (int i = 0; i < size; i++) { + if (points[i].getKey() == key) return points[i]; + } + + return null; } public void addLink(Point p1, Point p2, int length) { @@ -31,118 +45,77 @@ public void addLink(Point p1, Point p2, int length) { linkMatrix[key2][key1] = length; } - //public Path getMinPath() { - public void getMinPath() { - Path[] paths = new Path[size]; - Tree pathTree = new Tree(); - - //Deque deque = new LinkedList(); - Deque dequeNode = new LinkedList(); - - //deque.addLast(points[0]); - - Node root = new Node(points[0]); - pathTree.setRoot(root); - dequeNode.addLast(root); - - //while(!deque.isEmpty()) { - while ((!dequeNode.isEmpty())) { - //Point pointFrom = deque.peekFirst(); - //int keyPointFrom = pointFrom.getKey(); + public void printMinPath() { + calcMinPathsAndLengths(); - Node parentNode = dequeNode.peekFirst(); - int keyPointFrom = parentNode.getPoint().getKey(); + Stack path = new Stack(); + int key = size-1; - for (int keyPointTo = keyPointFrom + 1; keyPointTo < size; keyPointTo++) { - int length = linkMatrix[keyPointFrom][keyPointTo]; - if (length > 0) { - Point pointTo = points[keyPointTo]; - - // deque.addLast(pointTo); - - Node childNode = new Node(pointTo); - - dequeNode.addLast(childNode); - pathTree.addNode(parentNode, childNode); - } - } - - //Point point = deque.removeFirst(); - //System.out.println(point); - - dequeNode.removeFirst(); + while(key != 0) { + path.push(getPointByKey(key)); + key = paths[key]; } + path.push(getPointByKey(key)); - //return path; + System.out.print("Min path from " + getPointByKey(0) + " to " + getPointByKey(size-1) + ": "); + while(!path.empty()) { + System.out.print(path.pop() + " "); + } System.out.println(); - System.out.println("tree:"); - pathTree.display(); - } -} - -class Node { - private Point point; - private Node parent; - private LinkedList chields; - - public Node(Point point) { - this.point = point; - this.chields = new LinkedList(); } - public void setParent(Node node) { - parent = node; - } + public void printMinLength() { + calcMinPathsAndLengths(); - public Point getPoint() { - return point; + System.out.println("Min length: " + lengths[size-1]); } - public Node getParent() { - return parent; - } + private void calcMinPathsAndLengths() { + Deque deque = new LinkedList(); + boolean[] visited = new boolean[size]; - public void addChield(Node node) { - chields.add(node); - } + paths = new int[size]; + lengths = new int[size]; + for (int i = 1; i < size; i++) { + lengths[i] = Integer.MAX_VALUE; + } - public LinkedList getChields() { - return chields; - } + deque.addLast(points[0]); - public String toString() { - return String.format("%s", point); - } -} + int keyPointFrom = 0; + int length = 0; + int curLength = 0; -class Tree { - private Node root; + while(!deque.isEmpty()) { + keyPointFrom = deque.peekFirst().getKey(); - public Tree() { - root = null; - } + boolean[] processed = new boolean[size]; + for (int keyPointTo = 1; keyPointTo < size; keyPointTo++) { + int minLength = Integer.MAX_VALUE; + int keyMin = 0; - public void setRoot(Node node) { - root = node; - } + for (int i = 1; i 0 && length < minLength && !processed[i] && !visited[i]) { + minLength = length; + keyMin = i; + processed[i] = true; + } + } - public void addNode(Node parent, Node child) { - child.setParent(parent); - parent.addChield(child); - } + if (!visited[keyMin]) { + deque.addLast(points[keyMin]); - public void display() { - display0(root, ""); - } - - private void display0(Node node, String path) { - if (node.getChields().size() == 0) { - System.out.println(path + "-" + node); - return; - } + curLength = lengths[keyPointFrom] + minLength; + if (curLength < lengths[keyMin]) { + lengths[keyMin] = curLength; + paths[keyMin] = keyPointFrom; + } + } + } - for (Node child: node.getChields()) { - display0(child, path + "-" + node); + deque.removeFirst(); + visited[keyPointFrom] = true; } } }