dijkstra
This commit is contained in:
		
							parent
							
								
									108e795bd9
								
							
						
					
					
						commit
						c81a209b5a
					
				
					 2 changed files with 62 additions and 8 deletions
				
			
		|  | @ -1,7 +1,14 @@ | |||
| package org.insa.graphs.algorithm.shortestpath; | ||||
| import org.insa.graphs.model.Node; | ||||
| import org.insa.graphs.model.Arc; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| import org.insa.graphs.algorithm.utils.BinaryHeap; | ||||
| 
 | ||||
| public class DijkstraAlgorithm extends ShortestPathAlgorithm { | ||||
| 
 | ||||
| 
 | ||||
|     public DijkstraAlgorithm(ShortestPathData data) { | ||||
|         super(data); | ||||
|     } | ||||
|  | @ -9,8 +16,45 @@ public class DijkstraAlgorithm extends ShortestPathAlgorithm { | |||
|     @Override | ||||
|     protected ShortestPathSolution doRun() { | ||||
|         final ShortestPathData data = getInputData(); | ||||
|         /*nombre de nodes du graphe de data */ | ||||
|         int n=data.getGraph().size(); | ||||
|         ArrayList<Label> tab=new ArrayList<Label>(); | ||||
|         BinaryHeap<Label> tas=new BinaryHeap<Label>(); | ||||
|         ShortestPathSolution solution = null; | ||||
|         // TODO: | ||||
|         /*initialisation */ | ||||
|         for (int i=0;i<n;i++){ | ||||
|             tab.add(new Label(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null)); | ||||
|         } | ||||
|         Label label_origine = tab.get(data.getOrigin().getId()); | ||||
|         label_origine.setCoutmin(0); | ||||
| 
 | ||||
|         Label label_dest =tab.get(data.getDestination().getId()) | ||||
|         /*insertion de label origine dans le tas */ | ||||
|         tas.insert(label_origine); | ||||
|         Label x; | ||||
|         while (label_dest.getMarque()==false && tas.isEmpty()==false) { | ||||
|             x=tas.deleteMin(); | ||||
|             x.setMarque(true); | ||||
|             for (Arc suc:x.getSommet().getSuccessors() ) { | ||||
|                 int index=suc.getDestination().getId(); | ||||
|                 if (!tab.get(index).getMarque()) { | ||||
|                     if (tab.get(index).getCost()>x.getCost()+suc.getLength()) { | ||||
|                         /*on vérifie si présent dans le tas ou pas en utilisant remove */ | ||||
|                         try { | ||||
|                             tas.remove(tab.get(index)); | ||||
|                         } catch (Exception e) { | ||||
|                             System.out.println("l'élement n'est pas dans la tas"); | ||||
|                         } | ||||
|                         tab.get(index).setCoutmin(x.getCost()+suc.getLength()); | ||||
|                         /*insertion dans le tas */ | ||||
|                         tas.insert(tab.get(index)); | ||||
|                     } | ||||
| 
 | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| 
 | ||||
|          | ||||
|         return solution; | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,19 +1,20 @@ | |||
| package org.insa.graphs.algorithm.shortestpath; | ||||
| import org.insa.graphs.model.Node; | ||||
| import org.insa.graphs.model.Arc; | ||||
| 
 | ||||
| public class Label{ | ||||
| public class Label implements Comparable<Label>{ | ||||
|       | ||||
|     /*sommet associé au label */ | ||||
|     private Node sommet; | ||||
|     /*marque, vrai lorsque le coût min de ce sommet est définitivement connu par l'algorithme*/ | ||||
|     private boolean marque; | ||||
|     /*valeur courante du plus court chemin */ | ||||
|     private int coutmin; | ||||
|     private double coutmin; | ||||
|     /*sommet précédent sur le chemin correspondant au plus court chemin courant */ | ||||
|     private Node pere; | ||||
|     private Arc pere; | ||||
| 
 | ||||
|     /*Constructeur */ | ||||
|     public Label(Node sommet, boolean marque, int coutmin, Node pere){ | ||||
|     public Label(Node sommet, boolean marque, double coutmin, Arc pere){ | ||||
|         this.sommet=sommet; | ||||
|         this.marque=marque; | ||||
|         this.coutmin=coutmin; | ||||
|  | @ -22,14 +23,23 @@ public class Label{ | |||
| 
 | ||||
|     /*méthodes */ | ||||
|     /*récupérer le cout */ | ||||
|     public int getCost() { return this.coutmin;} | ||||
|     public double getCost() { return this.coutmin;} | ||||
|     /*récupérer le cout réalisé */ | ||||
|     public int getCout() { return this.coutmin;} | ||||
|     public double getCout() { return this.coutmin;} | ||||
|     /*récupérer le sommet du label */ | ||||
|     public Node getSommet() { return this.sommet;} | ||||
|     /*récupérer la marque */ | ||||
|     public boolean getMarque() { return this.marque;} | ||||
|     /*récupérer le père */ | ||||
|     public Node getPere() { return this.pere;} | ||||
|     public Arc getPere() { return this.pere;} | ||||
| 
 | ||||
|     public void setMarque(boolean m) {this.marque=m;} | ||||
|     public void setCoutmin(double c) {this.coutmin=c;} | ||||
|     public void setPere(Arc p) {this.pere=p;} | ||||
|     public void setSommet(Node s) {this.sommet=s;} | ||||
| 
 | ||||
| 
 | ||||
|     public int compareTo(Label other){ | ||||
|         return Double.compare(getCost(), other.getCost()); | ||||
|     } | ||||
| } | ||||
		Loading…
	
		Reference in a new issue