Package com.jedics.algorithms

Source Code of com.jedics.algorithms.Dijkstra

package com.jedics.algorithms;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;

import javax.swing.JOptionPane;

import com.jedics.graph.Edge;
import com.jedics.graph.GraphAlgorithm;
import com.jedics.graph.Vertex;

public class Dijkstra extends GraphAlgorithm {
  private static final Color RED = Color.red.brighter();
  private static final Color GREEN = Color.green.brighter();

  public String getName() {
    return "Dijkstra's";
  }

  @Override
  public synchronized void runAlgorithm() {
    handler.reset();
   
    JOptionPane.showMessageDialog(null, "Please select a starting Vertex");
   
    int selected = waitForVertex();

    final Hashtable<Vertex, Float> distances = new Hashtable<Vertex, Float>();
    Hashtable<Vertex, Vertex> parents = new Hashtable<Vertex, Vertex>();
   
    List<Vertex> queue = new ArrayList<Vertex>();
    List<Vertex> closed = new ArrayList<Vertex>();
   
    Comparator<Vertex> c = new Comparator<Vertex>() {
      public int compare(Vertex u, Vertex v) {
        return (distances.get(u) < distances.get(v)) ? -1 : 1;
      }
    };
   
    for(Integer i : handler.vertices.keySet()) {
      distances.put(handler.vertices.get(i), Float.MAX_VALUE);
      animator.setEastText(i, "inf");
    }
    animator.setEastText(selected, "0");
    distances.put(handler.vertices.get(selected), 0f);
   
    Vertex v;
   
    queue.add(handler.vertices.get(selected));
    while(!queue.isEmpty()) {
      v = queue.remove(0);
      System.out.println("Processing " + v.getId());
      animator.setBackground(v.getId(), GREEN);
      closed.add(v);
      for(Edge e : v.outbound()) {
        if(!closed.contains(e.getEnd())) {
          if(distances.get(v) + e.getWeight() < distances.get(e.getEnd())) {
            distances.put(e.getEnd(), distances.get(v) + e.getWeight());
            parents.put(e.getEnd(), v);
          }
          animator.blink(e.getEnd().getId());
          animator.setEastText(e.getEnd().getId(), distances.get(e.getEnd()) + "");
          animator.setSouthText(e.getEnd().getId(), animator.getData(parents.get(e.getEnd()).getId()));
          animator.blink(e.getEnd().getId());
          if(queue.indexOf(e.getEnd()) == -1) {
            queue.add(e.getEnd());
          }
        }
      }
     
      if(!handler.isDirected()) {
        for(Edge e : v.inbound()) {
          if(!closed.contains(e.getStart())) {
            if(distances.get(v) + e.getWeight() < distances.get(e.getStart())) {
              distances.put(e.getStart(), distances.get(v) + e.getWeight());
              parents.put(e.getStart(), v);
            }
            animator.blink(e.getStart().getId());
            animator.setEastText(e.getStart().getId(), distances.get(e.getStart()) + "");
            animator.setSouthText(e.getStart().getId(), animator.getData(parents.get(e.getStart()).getId()));
            animator.blink(e.getStart().getId());
            if(queue.indexOf(e.getStart()) == -1) {
              queue.add(e.getStart());
            }
          }
        }
      }
     
      handler.repaintCanvas();
     
      animator.sleep();
      animator.sleep();
     
     
      animator.setBackground(v.getId(), RED);
     
      Collections.sort(queue, c);
    }
  }
}
TOP

Related Classes of com.jedics.algorithms.Dijkstra

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.