Created
December 9, 2015 14:23
-
-
Save ad-m/955238aaab8895c2786b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| import java.util.LinkedList; | |
| import java.util.List; | |
| public class Graph implements IWGraph { | |
| Integer[][] matrix; | |
| public Graph() { | |
| this.matrix = new Integer[0][0]; | |
| } | |
| @Override | |
| public void addVertex(int i) { | |
| while (i > 0) { | |
| this.addVertex(); | |
| i--; | |
| } | |
| } | |
| public void addVertex() { | |
| Integer[][] niu = new Integer[this.matrix.length + 1][this.matrix.length + 1]; | |
| for (int i = 0; i < this.matrix.length; i++) { | |
| niu[i] = new Integer[this.matrix.length + 1]; | |
| System.arraycopy(matrix[i], 0, niu[i], 0, matrix[i].length); | |
| } | |
| niu[this.matrix.length] = new Integer[this.matrix.length + 1]; | |
| this.matrix = niu; | |
| } | |
| @Override | |
| public void addEdge(int start, int stop, int weight) { | |
| this.matrix[start][stop] = weight; | |
| } | |
| @Override | |
| public int wCheck(int start, int stop) { | |
| return this.matrix[start][stop] == null ? -1 : this.matrix[start][stop]; | |
| } | |
| @Override | |
| public void writeGraph() { | |
| for (int i = 0; i < this.matrix.length; i++) { | |
| System.out.print(i + ": "); | |
| for (int j = 0; j < this.matrix[i].length; j++) { | |
| System.out.print(this.wCheck(i, j) + "\t"); | |
| } | |
| System.out.print("\n"); | |
| } | |
| } | |
| public List<Integer> siblings(int v) { | |
| LinkedList<Integer> data = new LinkedList<Integer>(); | |
| for (int i = 0; i < this.matrix[v].length; i++) { | |
| if (this.wCheck(v, i) != -1) { | |
| data.add(i); | |
| } | |
| } | |
| return data; | |
| } | |
| public int A(int u, int v) { | |
| return (this.matrix[u][v] == null ? Integer.MAX_VALUE / 2 | |
| : this.matrix[u][v]); | |
| } | |
| public int min(int a, int b) { | |
| return (a > b ? b : a); | |
| } | |
| public void ford(int s) { | |
| int count = 0; | |
| int n = this.matrix.length; | |
| int D[] = new int[this.matrix.length]; | |
| for (int v = 1; v < n; v++) { | |
| D[v] = this.A(s, v); | |
| } | |
| D[s] = 0; | |
| for (int k = 0; k < (n - 2); k++) { | |
| for (int v = 0; v < n; v++) { | |
| if (v == s) | |
| continue; | |
| for (int u = 0; u < n; u++) { | |
| if (v == s) | |
| continue; | |
| D[v] = min(D[v], D[u] + this.A(u, v)); | |
| count++; | |
| } | |
| } | |
| ; | |
| } | |
| for (int i = 0; i < D.length; i++) { | |
| System.out.print("D[" + i + "] = " + D[i] + "\n"); | |
| } | |
| System.out.print("Count:" + count+"\n"); | |
| } | |
| public void floyd(int start) { | |
| int n = this.matrix.length; | |
| for (int u = 1; u < n; u++) { | |
| for (int v = 1; v < n; v++) { | |
| } | |
| ; | |
| } | |
| /* | |
| * for u:=1 to n do for v:=1 to n do D[u,v] := A[u,v]; D[u,u] := 0 ; for | |
| * k:=1 to n do for u:=1 to n do for v:=1 to n do D[u,v] := min(D[u,v], | |
| * D[u,k]+D[k,v]) | |
| */ | |
| } | |
| @Override | |
| public void dijkstra(int v) { | |
| int count = 0; | |
| ArrayList<Integer> S = new ArrayList<Integer>(); | |
| ArrayList<Integer> Q = new ArrayList<Integer>(); | |
| for (int i = 0; i < this.matrix.length; i++) { | |
| Q.add(i); | |
| } | |
| Integer[] d = new Integer[this.matrix.length]; | |
| for (int i = 0; i < d.length; i++) { | |
| d[i] = Integer.MAX_VALUE / 2; | |
| } | |
| d[v] = 0; | |
| Integer[] p = new Integer[this.matrix.length]; | |
| for (int i = 0; i < p.length; i++) { | |
| p[i] = -1; | |
| } | |
| while (!Q.isEmpty()) { | |
| System.out.print("Q:" + Q.toString() + "\n"); | |
| for (int i = 0; i < d.length; i++) { | |
| System.out.print("d[" + i + "] = " + d[i] + "\n"); | |
| } | |
| System.out.print("----\n"); | |
| Integer u = null; | |
| for (int m : Q) { | |
| u = (u == null || d[u] > d[m] ? m : u); | |
| } | |
| Q.remove(u); | |
| S.add(u); | |
| for (int w : this.siblings(u)) { | |
| if (!Q.contains(w)) { | |
| continue; | |
| } | |
| count ++; | |
| if (d[w] > d[u] + this.matrix[u][w]) { | |
| d[w] = d[u] + this.matrix[u][w]; | |
| p[w] = u; | |
| } | |
| } | |
| } | |
| System.out.print("Count:" + count+"\n"); | |
| } | |
| public static void main(String[] args) { | |
| Graph g = new Graph(); | |
| g.addVertex(5); | |
| // clockwise | |
| g.addEdge(0, 1, 10); | |
| g.addEdge(0, 4, 5); | |
| g.addEdge(1, 2, 1); | |
| // rotate | |
| g.addEdge(1, 4, 2); | |
| g.addEdge(2, 3, 4); | |
| g.addEdge(3, 0, 7); | |
| g.addEdge(3, 2, 6); | |
| g.addEdge(4, 1, 3); | |
| g.addEdge(4, 2, 9); | |
| g.addEdge(4, 3, 2); | |
| g.writeGraph(); | |
| g.ford(0); | |
| g.dijkstra(0); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public interface IWGraph { | |
| public void addVertex(int i); // dodaje i wierzchołków do grafu; | |
| public void addEdge(int start, int stop, int weight); // dodawanie krawędzi | |
| // łączącej | |
| // wierzchołki oraz | |
| // wprowadzenie wagi | |
| // dla danej | |
| // krawędzi | |
| public int wCheck(int start, int stop); // sprawdza czy jest krawędź między | |
| // wierzchołkami, jeśli jest to | |
| // zwracamy wagę krawędzi | |
| public void writeGraph(); // wypisuje graf w postaci tablicowej lub listowej | |
| public void ford(int start); // wypisuje najkrótszą drogę od wierzchołka | |
| // start do wszystkich pozostałych | |
| // wykorzystując algorytm Bellmana-Forda; | |
| public void dijkstra(int start); // wypisuje najkrótszą drogę od wierzchołka | |
| // start do wszystkich pozostałych | |
| // wykorzystując algorytm Dijkstry; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment