1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| public class InputGraph {
public int [][] arc; // Matrice d'adjacense
public Object [] sommet; //objet sommet
public static int count;
public InputGraph (int n){
arc = new int[n][n];
sommet = new Object[n];
}
public int GetSize (){ // récupère la taille du sommet
return sommet.length;
}
public void SetSommet(int node, Object label){ //permet d'ajouter un sommet
sommet[node]=label;
}
public Object GetSommet(int node){ // récupère la valeur d'un sommet
return sommet[node];
}
public void AddArc(int SommetDep, int SommetArr, int dist){ // ajoute un arc
arc [SommetDep][SommetArr]=dist;
}
public int GetPoids(int SommetDep, int SommetArr){ //récupère la distance
return arc [SommetDep][SommetArr];
}
public void AfficheMatrice(){ //affiche la matrice du graphe
for (int j = 0; j < arc.length; j++) {
System.out.println("");
System.out.println( " DE " + sommet[j] +" jusqu'à ");
System.out.println("");
for (int i = 0; i < arc[j].length; i++) {
if (arc[j][i]>0)
System.out.println(sommet[i] + " la distance est de " +arc[j][i] );
}
}
}
public int [] ChercheVoisin(int node){ //construit un tableau des sommets adjacents d'un sommet
count=0;
for (int i = 0; i < arc[node].length; i++) {
if (arc[node][i]>0 ) {count ++; }
}
final int[]rep = new int [count];
count=0;
for (int i = 0; i < arc[node].length; i++){
if(arc[node][i]>0){rep[count++]=i;}
}
return rep;
}
public static void main(String[] args) {
InputGraph ex = new InputGraph (4);
ex.SetSommet(0, "New york");
ex.SetSommet(1, "Paris");
ex.SetSommet(2, "Londres");
ex.SetSommet(3, "New delhi");
ex.AddArc(0, 1, 400);
ex.AddArc(0, 2, 200);
ex.AddArc(1, 2, 300);
ex.AddArc(2, 1, 100);
ex.AddArc(1, 3, 100);
ex.AddArc(2, 3, 500);
ex.AddArc(2, 0, 200);
ex.AfficheMatrice();
System.out.println("");
System.out.println("Les plus court chemin à partir du sommet source sont :");
final int [] pred = Dijkstra.djikstra(ex, 0);
for (int n = 0; n < 4; n++) {
Dijkstra.AfficheChemin(ex,pred, 0, n);
}
}
}
==========================================================
public class Dijkstra {
public static int [] djikstra (InputGraph IG, int Source){
final int [] distancecc = new int [IG.GetSize()];
final int [] pred = new int [IG.GetSize()];
final boolean [] marque = new boolean [IG.GetSize()];
for (int i = 0; i < distancecc.length; i++) {
distancecc[i]=Integer.MAX_VALUE;
}
distancecc[Source]=0;
for (int i = 0; i < distancecc.length; i++) {
final int U= ExtraireMin.ExtraireMin (distancecc, marque);
marque[U]=true;
final int [] V= IG.ChercheVoisin(U);
for (int j = 0; j < V.length; j++) {
final int NV = V [j];
final int d = distancecc[U] + IG.GetPoids(U, NV);
if (d < distancecc[NV]) {
distancecc[NV]=d;
pred[NV]=U;
}
}
}
return pred;
}
public static void AfficheChemin (InputGraph IG, int [] pred, int source, int n){
LinkedList cheminpc =new LinkedList();
int x=n;
while (x!=source){
cheminpc.add(source, IG.GetSommet(x)+"=====>");
x=pred[x];
}
cheminpc.add(source, IG.GetSommet(source)+"=====>");
System.out.println("--------------------------------------------");
System.out.println(cheminpc);
}
}
==========================================================
public class ExtraireMin {
public static int ExtraireMin(int [] distancecc, boolean [] marque){
int x =Integer.MAX_VALUE;
int y=0;
for (int i = 0; i < distancecc.length; i++) {
if (!marque[i] && distancecc[i]< x) {
y=i;
x=distancecc[i];
}
}
return y;
}
} |