Advertisement
Guest User

Dijkstra

a guest
Jan 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Dijkstra {
  5.    private static final Graph.Edge[] GRAPH = {
  6.       new Graph.Edge("a", "b", 7),
  7.       new Graph.Edge("a", "c", 9),
  8.       new Graph.Edge("a", "f", 14),
  9.       new Graph.Edge("b", "c", 10),
  10.       new Graph.Edge("b", "d", 15),
  11.       new Graph.Edge("c", "d", 11),
  12.       new Graph.Edge("c", "f", 2),
  13.       new Graph.Edge("d", "e", 6),
  14.       new Graph.Edge("e", "f", 9),
  15.    };
  16.    private static final String START = "a";
  17.    private static final String END = "e";
  18.  
  19.    public static void main(String[] args) {
  20.       Graph g = new Graph(GRAPH);
  21.       g.dijkstra(START);
  22.       g.printPath(END);
  23.    }
  24. }
  25.  
  26. class Graph {
  27.    private final Map<String, Vertex> graph;
  28.  
  29.    public static class Edge {
  30.       public final String v1, v2;
  31.       public final int dist;
  32.       public Edge(String v1, String v2, int dist) {
  33.          this.v1 = v1;
  34.          this.v2 = v2;
  35.          this.dist = dist;
  36.       }
  37.    }
  38.  
  39.   public static class Vertex implements Comparable<Vertex>{
  40.     public final String name;
  41.     public int dist = Integer.MAX_VALUE;
  42.     public Vertex previous = null;
  43.     public final Map<Vertex, Integer> neighbours = new HashMap<>();
  44.  
  45.     public Vertex(String name)
  46.     {
  47.         this.name = name;
  48.     }
  49.  
  50.     private void printPath()
  51.     {
  52.         if (this == this.previous)
  53.         {
  54.             System.out.printf("%s", this.name);
  55.         }
  56.         else if (this.previous == null)
  57.         {
  58.             System.out.printf("%s(unreached)", this.name);
  59.         }
  60.         else
  61.         {
  62.             this.previous.printPath();
  63.             System.out.printf(" -> %s(%d)", this.name, this.dist);
  64.         }
  65.     }
  66.  
  67.     public int compareTo(Vertex other)
  68.     {
  69.         if (dist == other.dist)
  70.             return name.compareTo(other.name);
  71.  
  72.         return Integer.compare(dist, other.dist);
  73.     }
  74.  
  75.     @Override public String toString()
  76.     {
  77.         return "(" + name + ", " + dist + ")";
  78.     }
  79. }
  80.  
  81.    public Graph(Edge[] edges) {
  82.       graph = new HashMap<>(edges.length);
  83.  
  84.       for (Edge e : edges) {
  85.          if (!graph.containsKey(e.v1)) graph.put(e.v1, new Vertex(e.v1));
  86.          if (!graph.containsKey(e.v2)) graph.put(e.v2, new Vertex(e.v2));
  87.       }
  88.  
  89.       for (Edge e : edges) {
  90.          graph.get(e.v1).neighbours.put(graph.get(e.v2), e.dist);
  91.       }
  92.    }
  93.  
  94.    public void dijkstra(String startName) {
  95.       if (!graph.containsKey(startName)) {
  96.          System.err.printf("Graf ne sadrži početne točke \"%s\"\n", startName);
  97.          return;
  98.       }
  99.       final Vertex source = graph.get(startName);
  100.       NavigableSet<Vertex> q = new TreeSet<>();
  101.  
  102.       for (Vertex v : graph.values()) {
  103.          v.previous = v == source ? source : null;
  104.          v.dist = v == source ? 0 : Integer.MAX_VALUE;
  105.          q.add(v);
  106.       }
  107.  
  108.       dijkstra(q);
  109.    }
  110.  
  111.    private void dijkstra(final NavigableSet<Vertex> q) {      
  112.       Vertex u, v;
  113.       while (!q.isEmpty()) {
  114.  
  115.          u = q.pollFirst();
  116.          if (u.dist == Integer.MAX_VALUE) break;
  117.  
  118.          for (Map.Entry<Vertex, Integer> a : u.neighbours.entrySet()) {
  119.             v = a.getKey();
  120.  
  121.             final int alternateDist = u.dist + a.getValue();
  122.             if (alternateDist < v.dist) {
  123.                q.remove(v);
  124.                v.dist = alternateDist;
  125.                v.previous = u;
  126.                q.add(v);
  127.             }
  128.          }
  129.       }
  130.    }
  131.  
  132.    public void printPath(String endName) {
  133.       if (!graph.containsKey(endName)) {
  134.          System.err.printf("Graf ne sadrži krajnje točke \"%s\"\n", endName);
  135.          return;
  136.       }
  137.  
  138.       graph.get(endName).printPath();
  139.       System.out.println();
  140.    }
  141.    public void printAllPaths() {
  142.       for (Vertex v : graph.values()) {
  143.          v.printPath();
  144.          System.out.println();
  145.       }
  146.    }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement