Advertisement
NHristovski

Untitled

Mar 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.77 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Hashtable;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Objects;
  9. import java.util.PriorityQueue;
  10. import java.util.Queue;
  11. import java.util.Set;
  12.  
  13.  
  14. class Vertex<E extends Comparable<E>> implements Comparable<Vertex<E>>{ // E data
  15. private E data;
  16. private double minDistance;
  17. private Vertex predecessor;
  18.  
  19. public Vertex(E data,double minDistance,Vertex predecessor){
  20. this.data = data;
  21. this.minDistance = minDistance;
  22. this.predecessor = predecessor;
  23. }
  24.  
  25. public Vertex(E data,double minDistance){
  26. this.data = data;
  27. this.minDistance = minDistance;
  28. this.predecessor = null;
  29. }
  30.  
  31. public Vertex(E data){
  32. this.data = data;
  33. this.minDistance = Double.MAX_VALUE;
  34. this.predecessor = null;
  35. }
  36.  
  37. public boolean hasPredecessor(){
  38. return this.predecessor != null;
  39. }
  40.  
  41. public double getMinDistance() {
  42. return minDistance;
  43. }
  44.  
  45. public void setMinDistance(double minDistance) {
  46. this.minDistance = minDistance;
  47. }
  48.  
  49. public Vertex<E> getPredecessor() {
  50. return predecessor;
  51. }
  52.  
  53. public void setPredecessor(Vertex predecessor) {
  54. this.predecessor = predecessor;
  55. }
  56.  
  57. public E getData() {
  58. return data;
  59. }
  60.  
  61. public void setData(E data) {
  62. this.data = data;
  63. }
  64.  
  65. @Override
  66. public int compareTo(Vertex<E> v) {
  67. return Double.compare(minDistance, v.getMinDistance());
  68. }
  69.  
  70. @Override
  71. public int hashCode() {
  72. int hash = 3;
  73. hash = 29 * hash + Objects.hashCode(this.data);
  74. return hash;
  75. }
  76.  
  77. @Override
  78. public boolean equals(Object obj) {
  79. if (this == obj) {
  80. return true;
  81. }
  82. if (obj == null) {
  83. return false;
  84. }
  85. if (getClass() != obj.getClass()) {
  86. return false;
  87. }
  88. final Vertex<E> other = (Vertex<E>) obj;
  89. if (!this.data.equals(other.data)) {
  90. return false;
  91. }
  92. return true;
  93. }
  94.  
  95. @Override
  96. public String toString(){
  97. StringBuilder sb = new StringBuilder();
  98. sb.append("Vertex with data: " + this.data + " minDist: " + this.minDistance + " Pred: " + this.predecessor);
  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: ").append(this.source.toString())
  143. .append("\nTo: ").append(this.destination.toString())
  144. .append("\nWith weight: " + this.weight);
  145. return sb.toString();
  146. }
  147.  
  148.  
  149.  
  150. }
  151. class Graph<E extends Comparable<E>>{
  152.  
  153. List<Vertex<E>> vertices;
  154. List<Edge<E>> edges;
  155. Map<E,Integer> indexes; // Integer : na koe mesto vo listata e
  156.  
  157. public Graph(){
  158. vertices = new ArrayList<>();
  159. edges = new ArrayList<>();
  160. indexes = new HashMap<>();
  161. }
  162.  
  163. public void addVertex(E data){
  164. Vertex<E> vertex = new Vertex<>(data);
  165. vertices.add(vertex);
  166. int index = vertices.size() - 1;
  167. indexes.put(data, index);
  168. }
  169.  
  170. public Vertex<E> getVertex(E data){
  171. return vertices.get(indexes.get(data));
  172. }
  173.  
  174. private boolean addEdge(Vertex<E> source,Vertex<E> destination,double weight){
  175. return edges.add(new Edge<E>(source,destination,weight));
  176. }
  177.  
  178. public boolean addEdge(E source,E destination,double weight){
  179. Vertex<E> s = vertices.get(indexes.get(source));
  180. Vertex<E> d = vertices.get(indexes.get(destination));
  181. return addEdge(s,d,weight);
  182. }
  183. public boolean addEdgeBeetween(E source,E destination,double weight){
  184. Vertex<E> s = vertices.get(indexes.get(source));
  185. Vertex<E> d = vertices.get(indexes.get(destination));
  186.  
  187. return addEdge(s,d,weight) && addEdge(d,s,weight);
  188. }
  189.  
  190. public List<Vertex<E>> getVertices(){
  191. return this.vertices;
  192. }
  193.  
  194. public List<Edge<E>> getEdges(){
  195. return this.edges;
  196. }
  197.  
  198. @Override
  199. public String toString(){
  200. StringBuilder sb = new StringBuilder();
  201. for (Vertex<E> v : this.getVertices()){
  202. sb.append(v.toString() + "\n");
  203. }
  204. return sb.toString();
  205. }
  206.  
  207. public double getMinDistanceBetweenDijkstra(E start,E end){
  208. Vertex<E> source = vertices.get(indexes.get(start));
  209. Vertex<E> destination = vertices.get(indexes.get(end));
  210. Dijkstra<E> dijkstra = new Dijkstra<>();
  211.  
  212. dijkstra.run(this, source);
  213.  
  214. return destination.getMinDistance();
  215. }
  216.  
  217. public double getMinDistanceBetweenBellmanFord(E start,E end){
  218. Vertex<E> source = vertices.get(indexes.get(start));
  219. Vertex<E> destination = vertices.get(indexes.get(end));
  220.  
  221. BellmanFord<E> bellmanFord = new BellmanFord<>();
  222.  
  223. if (bellmanFord.run(this, source))
  224. return destination.getMinDistance();
  225.  
  226. else
  227. return -1;
  228. }
  229. }
  230.  
  231. class Dijkstra<E extends Comparable<E>>{
  232.  
  233. public Dijkstra(){
  234. }
  235.  
  236. private Hashtable<Vertex<E>, List<Edge<E> > > makeHashtable(Graph<E> graph){
  237. Hashtable<Vertex<E>, List<Edge<E> > > table = new Hashtable<>();
  238. List<Edge<E>> lst;
  239. for (Vertex<E> v : graph.getVertices()){
  240. lst = new LinkedList<>();
  241. table.put(v,lst);
  242. }
  243.  
  244. for (Edge<E> e : graph.getEdges()){
  245. table.get(e.getSource()).add(e);
  246. }
  247.  
  248. return table;
  249. }
  250. public void run(Graph<E> graph, Vertex<E> startVertex){
  251.  
  252. if (graph == null || startVertex == null)
  253. return;
  254.  
  255. for (Vertex<E> v : graph.getVertices()){
  256. v.setMinDistance(Double.MAX_VALUE);
  257. v.setPredecessor(null);
  258. }
  259.  
  260. startVertex.setMinDistance(0d);
  261. PriorityQueue<Vertex<E>> queue = new PriorityQueue<>(graph.getVertices());
  262. Hashtable<Vertex<E>, List<Edge<E>>> table = makeHashtable(graph);
  263.  
  264. while (!queue.isEmpty()){
  265. Vertex<E> u = queue.poll();
  266.  
  267. for (Edge<E> e : table.get(u)){
  268. Vertex<E> v = e.getDestination();
  269. if (v.getMinDistance() > u.getMinDistance() + e.getWeight()){
  270. v.setMinDistance(u.getMinDistance() + e.getWeight());
  271. v.setPredecessor(u);
  272. queue.add(v);
  273. }
  274. }
  275. }
  276.  
  277. }
  278.  
  279. }
  280.  
  281. class BellmanFord<E extends Comparable<E>>{
  282.  
  283. private void relax(Edge<E> edge){
  284. if (edge.getDestination().getMinDistance() > edge.getSource().getMinDistance() + edge.getWeight()){
  285. edge.getDestination().setMinDistance(edge.getSource().getMinDistance() + edge.getWeight());
  286. edge.getDestination().setPredecessor(edge.getSource());
  287. }
  288. }
  289. public boolean run(Graph<E> graph, Vertex<E> startVertex){
  290. if (graph == null || startVertex == null)
  291. return false;
  292.  
  293. for (Vertex<E> v : graph.getVertices()){
  294. v.setMinDistance(Double.MAX_VALUE);
  295. v.setPredecessor(null);
  296. }
  297.  
  298. startVertex.setMinDistance(0d);
  299.  
  300. for (int i = 0; i < graph.getVertices().size(); i++){
  301. for (Edge<E> edge : graph.getEdges()){
  302. relax(edge);
  303. }
  304. }
  305.  
  306. for (Edge<E> edge : graph.getEdges()){
  307. if (edge.getDestination().getMinDistance() > edge.getSource().getMinDistance() + edge.getWeight()){
  308. return false;
  309. }
  310. }
  311.  
  312. return true;
  313. }
  314. }
  315. public class Main{
  316.  
  317. public static void main(String[] args){
  318. Graph<String> graph = new Graph<>();
  319. graph.addVertex("A");
  320. graph.addVertex("B");
  321. graph.addVertex("C");
  322. graph.addVertex("D");
  323. graph.addVertex("E");
  324.  
  325.  
  326. graph.addEdge("A","B",4);
  327. graph.addEdge("A","C",2);
  328. graph.addEdge("B","C",3);
  329. graph.addEdge("C","B",1);
  330. graph.addEdge("B","D",2);
  331. graph.addEdge("B","E",3);
  332. graph.addEdge("C","E",5);
  333. graph.addEdge("C","D",4);
  334. graph.addEdge("E","D",1);
  335.  
  336. System.out.println(graph.getMinDistanceBetweenBellmanFord("A", "D"));
  337. Vertex<String> current = graph.getVertex("D");
  338. while(current.hasPredecessor()){
  339. System.out.println(current);
  340. current = current.getPredecessor();
  341. }
  342.  
  343. }
  344.  
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement