public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Sommet nodeA = new Sommet("A");
Sommet nodeB = new Sommet("B");
Sommet nodeC = new Sommet("C");
Sommet nodeD = new Sommet("D");
Arete areteAB = new Arete(nodeA, nodeB, 2);
Arete areteAC = new Arete(nodeA, nodeC, 3);
Arete areteBD = new Arete(nodeB, nodeD, 8);
Arete areteCD = new Arete(nodeC, nodeD, 6);
nodeA.addArete(areteAB);
nodeA.addArete(areteAC);
nodeB.addArete(areteBD);
nodeC.addArete(areteCD);
ArrayList<Sommet> sommets = new ArrayList<>(Arrays.asList(nodeA, nodeB, nodeC, nodeD));
ArrayList<Arete> aretes = new ArrayList<>(Arrays.asList(areteAB, areteAC, areteBD, areteCD));
Graph graph = new Graph(sommets, aretes);
System.out.println("Voici le graphe : ");
graph.displayGraphInfo();
System.out.println("Choisissez le sommet de départ (par ex. A):");
Sommet depart = findSommetByName(sommets, scan.nextLine());
System.out.println("Choisissez le sommet d’arrivée (par ex. D):");
Sommet cible = findSommetByName(sommets, scan.nextLine());
scan.close();
if (depart != null && cible != null) {
runDijkstra(graph, depart, cible);
} else {
System.out.println("L’un des sommets spécifiés n’existe pas.");
}
}
private static Sommet findSommetByName(ArrayList<Sommet> sommets, String nom) {
for (Sommet sommet : sommets) {
if (sommet.getNom().equalsIgnoreCase(nom)) {
return sommet;
}
}
System.out.println("Le sommet " + nom + " n’existe pas dans le graphe");
return null;
}
public static void runDijkstra(Graph graph, Sommet depart, Sommet cible) {
final Map<Sommet, Double> distances = new HashMap<>();
final Map<Sommet, Sommet> predecessors = new HashMap<>();
Set<Sommet> unsettledNodes = new HashSet<>(graph.getSommets());
Set<Sommet> settledNodes = new HashSet<>();
for (Sommet sommet : graph.getSommets()) {
distances.put(sommet, Double.POSITIVE_INFINITY);
}
distances.put(depart, 0.0);
while (!unsettledNodes.isEmpty()) {
Sommet current = getLowestDistanceNode(unsettledNodes, distances);
unsettledNodes.remove(current);
settledNodes.add(current);
for (Arete arete : current.getAretes()) {
Sommet adjacent = arete.getDestination();
if (settledNodes.contains(adjacent)) continue;
double edgeWeight = arete.getPoids();
if (distances.get(current) + edgeWeight < distances.get(adjacent)) {
distances.put(adjacent, distances.get(current) + edgeWeight);
predecessors.put(adjacent, current);
}
}
}
printShortestPath(cible, predecessors, distances);
}
private static Sommet getLowestDistanceNode(Set<Sommet> unsettledNodes, Map<Sommet, Double> distances) {
Sommet lowestDistanceNode = null;
double lowestDistance = Double.POSITIVE_INFINITY;
for (Sommet node : unsettledNodes) {
double nodeDistance = distances.get(node);
if (nodeDistance < lowestDistance) {
lowestDistance = nodeDistance;
lowestDistanceNode = node;
}
}
return lowestDistanceNode;
}
private static void printShortestPath(Sommet target, Map<Sommet, Sommet> predecessors, Map<Sommet, Double> distances) {
LinkedList<Sommet> path = new LinkedList<>();
Sommet current = target;
if (predecessors.get(current) == null && !current.equals(target)) {
System.out.println("Aucun chemin trouvé de " + target.getNom());
return;
}
path.add(current);
while (predecessors.containsKey(current)) {
current = predecessors.get(current);
path.addFirst(current);
}
System.out.println("Chemin le plus court : " + pathToString(path) + ", Distance totale : " + distances.get(target));
}
private static String pathToString(LinkedList<Sommet> path) {
return path.stream().map(Sommet::getNom).collect(Collectors.joining(" -> "));
}