dijkstra
This commit is contained in:
		
							parent
							
								
									ce9743650b
								
							
						
					
					
						commit
						e4695e0ae7
					
				
					 4 changed files with 51 additions and 8 deletions
				
			
		|  | @ -1,9 +1,22 @@ | |||
| package org.insa.graphs.algorithm.shortestpath; | ||||
| 
 | ||||
| import java.util.ArrayList; | ||||
| 
 | ||||
| 
 | ||||
| public class AStarAlgorithm extends DijkstraAlgorithm { | ||||
| 
 | ||||
|     public AStarAlgorithm(ShortestPathData data) { | ||||
|         super(data); | ||||
|     } | ||||
| 
 | ||||
|     /*on redéfinit l'initialisation */ | ||||
|     void init(ArrayList<Label> tab){ | ||||
|         /*nombre de nodes du graphe de data */ | ||||
|         int n=data.getGraph().size(); | ||||
|         /*on remplit tab */ | ||||
|         for (int i=0;i<n;i++){ | ||||
|             tab.add(new LabelStar(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null,0.0)); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  |  | |||
|  | @ -11,6 +11,15 @@ import org.insa.graphs.algorithm.utils.BinaryHeap; | |||
| 
 | ||||
| public class DijkstraAlgorithm extends ShortestPathAlgorithm { | ||||
| 
 | ||||
|     /*initialisation */ | ||||
|     void init(ArrayList<Label> tab){ | ||||
|         /*nombre de nodes du graphe de data */ | ||||
|         int n=data.getGraph().size(); | ||||
|         /*on remplit tab */ | ||||
|         for (int i=0;i<n;i++){ | ||||
|             tab.add(new Label(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null)); | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     public DijkstraAlgorithm(ShortestPathData data) { | ||||
|         super(data); | ||||
|  | @ -19,18 +28,12 @@ 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; | ||||
|         /*initialisation */ | ||||
|         for (int i=0;i<n;i++){ | ||||
|             tab.add(new Label(data.getGraph().get(i), false, Double.POSITIVE_INFINITY , null)); | ||||
|         } | ||||
|         init(tab); | ||||
|         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); | ||||
|  |  | |||
|  | @ -32,7 +32,10 @@ public class Label implements Comparable<Label>{ | |||
|     public boolean getMarque() { return this.marque;} | ||||
|     /*récupérer le père */ | ||||
|     public Arc getPere() { return this.pere;} | ||||
|     /*récupérer le cout total (modifié dans LabelStar) */ | ||||
|     public double getTotalCost() { return this.coutmin;} | ||||
| 
 | ||||
|     /*setters */ | ||||
|     public void setMarque(boolean m) {this.marque=m;} | ||||
|     public void setCoutmin(double c) {this.coutmin=c;} | ||||
|     public void setPere(Arc p) {this.pere=p;} | ||||
|  | @ -40,6 +43,6 @@ public class Label implements Comparable<Label>{ | |||
| 
 | ||||
| 
 | ||||
|     public int compareTo(Label other){ | ||||
|         return Double.compare(getCost(), other.getCost()); | ||||
|         return Double.compare(getTotalCost(), other.getTotalCost()); | ||||
|     } | ||||
| } | ||||
|  | @ -0,0 +1,24 @@ | |||
| package org.insa.graphs.algorithm.shortestpath; | ||||
| import org.insa.graphs.model.Node; | ||||
| import org.insa.graphs.model.Arc; | ||||
| 
 | ||||
| 
 | ||||
| public class LabelStar extends Label { | ||||
| 
 | ||||
|     /*cout estimé*/ | ||||
|     private double coutest; | ||||
| 
 | ||||
|     /*constructeur */ | ||||
|     public LabelStar(Node sommet, boolean marque, double coutmin, Arc pere, double coutest){ | ||||
| 
 | ||||
|         super(sommet, marque, coutmin, pere); | ||||
|         this.coutest=coutest; | ||||
|     } | ||||
| 
 | ||||
|     /*méthodes */ | ||||
|     /*récupérer le cout total */ | ||||
|     public double getTotalCost() { return this.getCost()+this.coutest;} | ||||
|      | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
		Loading…
	
		Reference in a new issue