Advertisement
NHristovski

Untitled

Mar 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.68 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.Hashtable;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Objects;
  11. import java.util.PriorityQueue;
  12. import java.util.Queue;
  13. import java.util.Scanner;
  14. import java.util.Set;
  15.  
  16.  
  17. class Vertex<E extends Comparable<E>> implements Comparable<Vertex<E>>{
  18. private E data;
  19. private double minDistance;
  20. private Vertex predecessor;
  21.  
  22. public Vertex(E data){
  23. this.data = data;
  24. this.minDistance = Double.MAX_VALUE;
  25. this.predecessor = null;
  26. }
  27.  
  28. public boolean hasPredecessor(){
  29. return this.predecessor != null;
  30. }
  31.  
  32. public double getMinDistance() {
  33. return minDistance;
  34. }
  35.  
  36. public void setMinDistance(double minDistance) {
  37. this.minDistance = minDistance;
  38. }
  39.  
  40. public Vertex<E> getPredecessor() {
  41. return predecessor;
  42. }
  43.  
  44. public void setPredecessor(Vertex predecessor) {
  45. this.predecessor = predecessor;
  46. }
  47.  
  48. public E getData() {
  49. return data;
  50. }
  51.  
  52. public void setData(E data) {
  53. this.data = data;
  54. }
  55.  
  56. @Override
  57. public int compareTo(Vertex<E> v) {
  58. return Double.compare(minDistance, v.getMinDistance());
  59. }
  60.  
  61. @Override
  62. public int hashCode() {
  63. int hash = 3;
  64. hash = 29 * hash + Objects.hashCode(this.data);
  65. return hash;
  66. }
  67.  
  68. @Override
  69. public boolean equals(Object obj) {
  70. if (this == obj) {
  71. return true;
  72. }
  73. if (obj == null) {
  74. return false;
  75. }
  76. if (getClass() != obj.getClass()) {
  77. return false;
  78. }
  79. final Vertex<E> other = (Vertex<E>) obj;
  80. if (!this.data.equals(other.data)) {
  81. return false;
  82. }
  83. return true;
  84. }
  85.  
  86. @Override
  87. public String toString(){
  88. StringBuilder sb = new StringBuilder();
  89. sb.append("Vertex with data: ")
  90. .append(this.data)
  91. .append(" minDist: ")
  92. .append(this.minDistance)
  93. .append(" Pred: ");
  94. if (this.hasPredecessor())
  95. sb.append(this.predecessor.getData());
  96. else{
  97. sb.append("None");
  98. }
  99. return sb.toString();
  100. }
  101.  
  102. }
  103.  
  104. class Edge<E extends Comparable<E>>{
  105. private Vertex<E> source;
  106. private Vertex<E> destination;
  107. private double weight;
  108.  
  109. public Edge(Vertex<E> source, Vertex<E> destination, double weight) {
  110. this.source = source;
  111. this.destination = destination;
  112. this.weight = weight;
  113. }
  114.  
  115. public Vertex<E> getSource() {
  116. return source;
  117. }
  118.  
  119. public void setSource(Vertex<E> source) {
  120. this.source = source;
  121. }
  122.  
  123. public Vertex<E> getDestination() {
  124. return destination;
  125. }
  126.  
  127. public void setDestination(Vertex<E> destination) {
  128. this.destination = destination;
  129. }
  130.  
  131. public double getWeight() {
  132. return weight;
  133. }
  134.  
  135. public void setWeight(double weight) {
  136. this.weight = weight;
  137. }
  138.  
  139. @Override
  140. public String toString(){
  141. StringBuilder sb = new StringBuilder();
  142. sb.append("Edge from: ")
  143. .append(this.source.toString())
  144. .append("\nTo: ")
  145. .append(this.destination.toString())
  146. .append("\nWith weight: " + this.weight);
  147. return sb.toString();
  148. }
  149.  
  150. @Override
  151. public int hashCode() {
  152. int hash = 7;
  153. hash = 11 * hash + Objects.hashCode(this.source);
  154. hash = 11 * hash + Objects.hashCode(this.destination);
  155. return hash;
  156. }
  157.  
  158. @Override
  159. public boolean equals(Object obj) {
  160. if (this == obj) {
  161. return true;
  162. }
  163. if (obj == null) {
  164. return false;
  165. }
  166. if (getClass() != obj.getClass()) {
  167. return false;
  168. }
  169. final Edge<?> other = (Edge<?>) obj;
  170.  
  171. if (!Objects.equals(this.source, other.source)) {
  172. return false;
  173. }
  174. if (!Objects.equals(this.destination, other.destination)) {
  175. return false;
  176. }
  177. return true;
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184. }
  185. class Graph<E extends Comparable<E>>{
  186.  
  187. List<Vertex<E>> vertices;
  188. List<Edge<E>> edges;
  189. Map<E,Integer> indexes; // E( name of the vertex) -> Integer (position in list vertices)
  190.  
  191. public Graph(){
  192. vertices = new ArrayList<>();
  193. edges = new ArrayList<>();
  194. indexes = new HashMap<>();
  195. }
  196.  
  197. public void addVertex(E data){
  198. Vertex<E> vertex = new Vertex<>(data);
  199. vertices.add(vertex);
  200. int index = vertices.size() - 1;
  201. indexes.put(data, index);
  202. }
  203.  
  204. public Vertex<E> getVertex(E data){
  205. return vertices.get(indexes.get(data));
  206. }
  207.  
  208. private boolean addEdge(Vertex<E> source,Vertex<E> destination,double weight){
  209. return edges.add(new Edge<E>(source,destination,weight));
  210. }
  211.  
  212. public boolean addDirectedEdge(E source,E destination,double weight){
  213. Vertex<E> s = vertices.get(indexes.get(source));
  214. Vertex<E> d = vertices.get(indexes.get(destination));
  215. return addEdge(s,d,weight);
  216. }
  217. public boolean addUndirectedEdge(E source,E destination,double weight){
  218. Vertex<E> s = vertices.get(indexes.get(source));
  219. Vertex<E> d = vertices.get(indexes.get(destination));
  220.  
  221. return addEdge(s,d,weight) && addEdge(d,s,weight);
  222. }
  223.  
  224. public List<Vertex<E>> getVertices(){
  225. return this.vertices;
  226. }
  227.  
  228. public List<Edge<E>> getEdges(){
  229. return this.edges;
  230. }
  231.  
  232. @Override
  233. public String toString(){
  234. StringBuilder sb = new StringBuilder();
  235. for (Vertex<E> v : this.getVertices()){
  236. sb.append(v.toString() + "\n");
  237. }
  238. return sb.toString();
  239. }
  240.  
  241. public double getMinDistanceBetweenDijkstra(E start,E end){
  242. Vertex<E> source = vertices.get(indexes.get(start));
  243. Vertex<E> destination = vertices.get(indexes.get(end));
  244. Dijkstra<E> dijkstra = new Dijkstra<>();
  245.  
  246. dijkstra.run(this, source);
  247.  
  248. return destination.getMinDistance();
  249. }
  250.  
  251. public List<List<Double>> getMinimumDistanceJohnsonButSlower(){
  252.  
  253. double minWeight = Double.MAX_VALUE;
  254.  
  255. for (Edge<E> edge : this.getEdges()){
  256. if (edge.getWeight() < minWeight){
  257. minWeight = edge.getWeight();
  258. }
  259. }
  260. minWeight = Math.abs(minWeight);
  261.  
  262. for (Edge<E> edge : this.getEdges()){
  263. edge.setWeight(edge.getWeight() + minWeight);
  264. }
  265.  
  266. List<List<Double>> matrix = new ArrayList<>();
  267. List<Double> resultRow;
  268.  
  269. Dijkstra<E> dijkstra = new Dijkstra<>();
  270.  
  271. for (Vertex<E> source : this.getVertices()){
  272.  
  273. dijkstra.run(this,source);
  274.  
  275. resultRow = new ArrayList<>();
  276.  
  277. for (Vertex<E> vertex : this.getVertices()){
  278. int counter = 0;
  279. Vertex<E> tempVertex = vertex;
  280. while(tempVertex.hasPredecessor()){
  281. counter++;
  282. tempVertex = tempVertex.getPredecessor();
  283. }
  284.  
  285. double result = vertex.getMinDistance() - (counter * minWeight);
  286.  
  287. resultRow.add(result);
  288. }
  289. matrix.add(resultRow);
  290. }
  291.  
  292.  
  293. return matrix;
  294. }
  295.  
  296. public double getMinDistanceBetweenBellmanFord(E start,E end){
  297. Vertex<E> source = vertices.get(indexes.get(start));
  298. Vertex<E> destination = vertices.get(indexes.get(end));
  299.  
  300. BellmanFord<E> bellmanFord = new BellmanFord<>();
  301.  
  302. if (bellmanFord.run(this, source))
  303. return destination.getMinDistance();
  304.  
  305. else{
  306. return -1;
  307. }
  308. }
  309.  
  310. /*
  311. MatrixPair class
  312. */
  313. ////////////////////////////////////////////////////////////////////////////
  314. class Matrix{
  315. double[][] adjMatrix;
  316. Integer[][] parrentMatrix;
  317.  
  318. public Matrix(double[][] adjMatrix, Integer[][] parrentMatrix) {
  319. this.adjMatrix = adjMatrix;
  320. this.parrentMatrix = parrentMatrix;
  321. }
  322.  
  323. public double[][] getAdjMatrix() {
  324. return adjMatrix;
  325. }
  326.  
  327. public Integer[][] getParrentMatrix() {
  328. return parrentMatrix;
  329. }
  330.  
  331. public List<Integer> getPathBetween(int source,int destination){
  332. int start = source - 1;
  333. int finish = destination - 1;
  334. List<Integer> path = new ArrayList<>();
  335. while(parrentMatrix[start][finish] != start){
  336. path.add(parrentMatrix[start][finish] + 1);
  337. finish = parrentMatrix[start][finish];
  338. }
  339. path.add(parrentMatrix[start][finish] + 1);
  340. Collections.reverse(path);
  341. return path;
  342. }
  343.  
  344. public double getDistanceBetween(int source,int destination){
  345. int start = source - 1;
  346. int finish = destination - 1;
  347. return this.adjMatrix[start][finish];
  348. }
  349. }
  350. ////////////////////////////////////////////////////////////////////////////
  351.  
  352. public Matrix getMinDistanceFloydWarshall(double[][] adjMat){
  353. int N = adjMat.length;
  354.  
  355. Integer[][] parentMatrix = new Integer[N][N];
  356.  
  357. for (int i = 0; i < N; i++){
  358. for (int j = 0; j < N; j++){
  359. parentMatrix[i][j] = null;
  360. }
  361. }
  362.  
  363. for (int i = 0; i < N; i++){
  364. for (int j = 0; j < N; j++){
  365. if (adjMat[i][j] != Double.MAX_VALUE / 2 && adjMat[i][j] != 0.0){
  366. parentMatrix[i][j] = i;
  367. }
  368. }
  369. }
  370.  
  371.  
  372. for(int k = 0; k < N; ++k) {
  373. for(int i = 0; i < N; ++i) {
  374. for(int j = 0; j < N; ++j) {
  375. if ( adjMat[i][k] + adjMat[k][j] < adjMat[i][j] ){
  376. parentMatrix[i][j] = parentMatrix[k][j];
  377. }
  378. adjMat[i][j] = Math.min(adjMat[i][j], adjMat[i][k] + adjMat[k][j]);
  379. }
  380. }
  381. }
  382.  
  383. return new Matrix(adjMat,parentMatrix);
  384. }
  385.  
  386.  
  387. public double[][] getAdjMatrix(){
  388. int N = vertices.size();
  389. double[][] matrix = new double[N][N];
  390. for (int i = 0; i < N; i++){
  391. for (int j = 0; j < N; j++){
  392. if (i == j)
  393. continue;
  394. else{
  395. matrix[i][j] = Double.MAX_VALUE / 2.0;
  396. }
  397. }
  398. }
  399. List<Edge<E>> egdes = this.getEdges();
  400. for (Edge<E> e : edges){
  401. matrix[indexes.get(e.getSource().getData())][indexes.get(e.getDestination().getData())] = e.getWeight();
  402. }
  403. return matrix;
  404. }
  405. }
  406.  
  407. class Dijkstra<E extends Comparable<E>>{
  408.  
  409. public Dijkstra(){
  410. }
  411.  
  412. private Hashtable<Vertex<E>, List<Edge<E> > > makeHashtable(Graph<E> graph){
  413. Hashtable<Vertex<E>, List<Edge<E> > > table = new Hashtable<>();
  414. List<Edge<E>> lst;
  415. for (Vertex<E> v : graph.getVertices()){
  416. lst = new LinkedList<>();
  417. table.put(v,lst);
  418. }
  419.  
  420. for (Edge<E> e : graph.getEdges()){
  421. table.get(e.getSource()).add(e);
  422. }
  423.  
  424. return table;
  425. }
  426. public void run(Graph<E> graph, Vertex<E> startVertex){
  427.  
  428. if (graph == null || startVertex == null)
  429. return;
  430.  
  431. for (Vertex<E> v : graph.getVertices()){
  432. v.setMinDistance(Double.MIN_VALUE);
  433. v.setPredecessor(null);
  434. }
  435.  
  436. double result = Double.MAX_VALUE;
  437. for (Edge<E> edge : graph.getEdges()){
  438. if (edge.equals(new Edge<E>(startVertex,startVertex,0))){
  439. result = edge.getWeight();
  440. }
  441. }
  442. startVertex.setMinDistance(result);
  443. PriorityQueue<Vertex<E>> queue = new PriorityQueue<>(graph.getVertices());
  444. Hashtable<Vertex<E>, List<Edge<E>>> table = makeHashtable(graph);
  445.  
  446. while (!queue.isEmpty()){
  447. Vertex<E> u = queue.poll();
  448.  
  449. for (Edge<E> e : table.get(u)){
  450. Vertex<E> v = e.getDestination();
  451. if (v.getMinDistance() < Math.min(u.getMinDistance(),e.getWeight())){
  452. v.setMinDistance(Math.min(u.getMinDistance(),e.getWeight()));
  453. v.setPredecessor(u);
  454. queue.add(v);
  455. }
  456. }
  457. }
  458.  
  459. }
  460.  
  461. }
  462.  
  463. class BellmanFord<E extends Comparable<E>>{
  464.  
  465. private void relax(Edge<E> edge){
  466. if (edge.getDestination().getMinDistance() < Math.min(edge.getSource().getMinDistance(),edge.getWeight())){
  467. edge.getDestination().setMinDistance(Math.min(edge.getSource().getMinDistance(),edge.getWeight()));
  468. edge.getDestination().setPredecessor(edge.getSource());
  469. }
  470. }
  471. public boolean run(Graph<E> graph, Vertex<E> startVertex){
  472. if (graph == null || startVertex == null)
  473. return false;
  474.  
  475. for (Vertex<E> v : graph.getVertices()){
  476. v.setMinDistance(Double.MIN_VALUE);
  477. v.setPredecessor(null);
  478. }
  479.  
  480. startVertex.setMinDistance(Double.MAX_VALUE);
  481.  
  482. for (int i = 0; i < graph.getVertices().size(); i++){
  483. for (Edge<E> edge : graph.getEdges()){
  484. relax(edge);
  485. }
  486. }
  487.  
  488. for (Edge<E> edge : graph.getEdges()){
  489. if (edge.getDestination().getMinDistance() < Math.min(edge.getSource().getMinDistance(),edge.getWeight())){
  490. return false;
  491. }
  492. }
  493.  
  494. return true;
  495. }
  496. }
  497.  
  498. public class Main{
  499.  
  500. public static void main(String[] args){
  501. /*Graph<Integer> g = new Graph<>();
  502. double[][] d = new double[5][5];
  503.  
  504. d[0][0] = 0;
  505. d[0][1] = 3;
  506. d[0][2] = 8;
  507. d[0][3] = Double.MAX_VALUE / 2;
  508. d[0][4] = -4;
  509.  
  510. d[1][0] = Double.MAX_VALUE / 2;;
  511. d[1][1] = 0;
  512. d[1][2] = Double.MAX_VALUE / 2;;
  513. d[1][3] = 1;
  514. d[1][4] = 7;
  515.  
  516. d[2][0] = Double.MAX_VALUE / 2;;
  517. d[2][1] = 4;
  518. d[2][2] = 0;
  519. d[2][3] = Double.MAX_VALUE / 2;;
  520. d[2][4] = Double.MAX_VALUE / 2;;
  521.  
  522. d[3][0] = 2;
  523. d[3][1] = Double.MAX_VALUE / 2;;
  524. d[3][2] = -5;
  525. d[3][3] = 0;
  526. d[3][4] = Double.MAX_VALUE / 2;;
  527.  
  528. d[4][0] = Double.MAX_VALUE / 2;;
  529. d[4][1] = Double.MAX_VALUE / 2;;
  530. d[4][2] = Double.MAX_VALUE / 2;;
  531. d[4][3] = 6;
  532. d[4][4] = 0;
  533.  
  534.  
  535. System.out.println(g.getMinDistanceFloydWarshall(d).getPathBetween(1, 2));
  536. System.out.println(g.getMinDistanceFloydWarshall(d).getDistanceBetween(1, 2));
  537.  
  538.  
  539. for (int i = 1; i <= 5; i++){
  540. g.addVertex(i);
  541. }
  542.  
  543. g.addDirectedEdge(1, 2, 3);
  544. g.addDirectedEdge(1, 5, -4);
  545. g.addDirectedEdge(1, 3, 8);
  546.  
  547. g.addDirectedEdge(2, 4, 1);
  548. g.addDirectedEdge(2, 5, 7);
  549.  
  550. g.addDirectedEdge(3, 2, 4);
  551.  
  552. g.addDirectedEdge(4, 1, 2);
  553. g.addDirectedEdge(4, 3, -5);
  554.  
  555. g.addDirectedEdge(5, 4, 6);
  556.  
  557. System.out.println(g.getMinDistanceFloydWarshall(g.getAdjMatrix()).getPathBetween(1, 2));
  558. System.out.println(g.getMinDistanceFloydWarshall(g.getAdjMatrix()).getDistanceBetween(1, 2));
  559. */
  560. Graph<Integer> graph = new Graph<>();
  561.  
  562. graph.addVertex(1);
  563. graph.addVertex(2);
  564. graph.addVertex(3);
  565.  
  566. graph.addDirectedEdge(1, 1, 2);
  567. graph.addDirectedEdge(1, 2, 1);
  568. graph.addDirectedEdge(1, 3, -1);
  569. graph.addDirectedEdge(2, 1, 1);
  570. graph.addDirectedEdge(2, 2, 2);
  571. graph.addDirectedEdge(2, 3, 2);
  572. graph.addDirectedEdge(3, 1, 2);
  573. graph.addDirectedEdge(3, 2, 1);
  574. graph.addDirectedEdge(3, 3, 2);
  575.  
  576. /*for (List<Double> lst : graph.getMinDistanceBetweenEveryDijkstra()){
  577. System.out.println(lst);
  578. }*/
  579. System.out.println(graph.getMinimumDistanceJohnsonButSlower());
  580.  
  581. }
  582.  
  583. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement