Advertisement
NHristovski

Untitled

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