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..18fccf1 --- /dev/null +++ b/Graph/src/TestGraph.java @@ -0,0 +1,85 @@ +import graph.Graph; +import graph.Path; +import graph.Point; + +public class TestGraph { + static public void main(String[] args) { + + test1(); + test2(); + } + + public static void test1() { + final int GRAPH_SIZE = 10; + + 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"); + 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.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); + + graph.printMinPath(); + graph.printMinLength(); + } +} diff --git a/Graph/src/graph/Graph.java b/Graph/src/graph/Graph.java new file mode 100644 index 0000000..98b2aab --- /dev/null +++ b/Graph/src/graph/Graph.java @@ -0,0 +1,121 @@ +package graph; + +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[][] linkMatrix; + + private int lengths[]; + private int paths[]; + + public Graph(int capacity) { + this.capacity = capacity; + this.size = 0; + + points = new Point[capacity]; + linkMatrix = new int[capacity][capacity]; + } + + public void addPoint(Point point) { + 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) { + int key1 = p1.getKey(); + int key2 = p2.getKey(); + + linkMatrix[key1][key2] = length; + linkMatrix[key2][key1] = length; + } + + public void printMinPath() { + calcMinPathsAndLengths(); + + Stack path = new Stack(); + int key = size-1; + + while(key != 0) { + path.push(getPointByKey(key)); + key = paths[key]; + } + path.push(getPointByKey(key)); + + System.out.print("Min path from " + getPointByKey(0) + " to " + getPointByKey(size-1) + ": "); + while(!path.empty()) { + System.out.print(path.pop() + " "); + } + System.out.println(); + } + + public void printMinLength() { + calcMinPathsAndLengths(); + + System.out.println("Min length: " + lengths[size-1]); + } + + private void calcMinPathsAndLengths() { + Deque deque = new LinkedList(); + boolean[] visited = new boolean[size]; + + paths = new int[size]; + lengths = new int[size]; + for (int i = 1; i < size; i++) { + lengths[i] = Integer.MAX_VALUE; + } + + deque.addLast(points[0]); + + int keyPointFrom = 0; + int length = 0; + int curLength = 0; + + while(!deque.isEmpty()) { + keyPointFrom = deque.peekFirst().getKey(); + + boolean[] processed = new boolean[size]; + for (int keyPointTo = 1; keyPointTo < size; keyPointTo++) { + int minLength = Integer.MAX_VALUE; + int keyMin = 0; + + for (int i = 1; i 0 && length < minLength && !processed[i] && !visited[i]) { + minLength = length; + keyMin = i; + processed[i] = true; + } + } + + if (!visited[keyMin]) { + deque.addLast(points[keyMin]); + + curLength = lengths[keyPointFrom] + minLength; + if (curLength < lengths[keyMin]) { + lengths[keyMin] = curLength; + paths[keyMin] = keyPointFrom; + } + } + } + + deque.removeFirst(); + visited[keyPointFrom] = true; + } + } +} 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; + } +} + +