distributed_automated/multi_agent_system/src/com/mas/main/Graph.java
<filename>distributed_automated/multi_agent_system/src/com/mas/main/Graph.javapackage com.mas.main;

import java.util.*;

import java.util.*;

class Vertex {
    String name;
    String state;
    Boolean hasPallet = false;
    List<Edge> edges;

    public Vertex(String name, String state) {
        this.name = name;
        this.state = state;
        this.edges = new ArrayList<>();
    }

    public void addEdge(Edge edge) {
        edges.add(edge);
    }
}

class Edge {
    Vertex source;
    Vertex destination;
    int weight;

    public Edge(Vertex source, Vertex destination, int weight) {
        this.source = source;
        this.destination = destination;
        this.weight = weight;
    }
}

class Graph {
    Map<String, Vertex> vertices;

    public Graph() {
        this.vertices = new HashMap<>();
    }

    public void addVertex(String name, String state) {
        Vertex vertex = new Vertex(name, state);
        vertices.put(name, vertex);
    }

    public void addEdge(String sourceName, String destName, int weight) {
        Vertex source = vertices.get(sourceName);
        Vertex destination = vertices.get(destName);
        Edge edge = new Edge(source, destination, weight);
        source.addEdge(edge);
    }
    
    
    /*
    public List<Vertex> shortestPath(String startName, String endName) {
        Map<Vertex, Integer> distances = new HashMap<>();
        Map<Vertex, Vertex> previous = new HashMap<>();
        PriorityQueue<Vertex> queue = new PriorityQueue<>((v1, v2) -> distances.get(v1) - distances.get(v2));

        Vertex start = vertices.get(startName);
        Vertex end = vertices.get(endName);

        for (Vertex vertex : vertices.values()) {
            distances.put(vertex, Integer.MAX_VALUE);
            previous.put(vertex, null);
        }

        distances.put(start, 0);
        queue.add(start);

        while (!queue.isEmpty()) {
            Vertex current = queue.poll();

            if (current == end) {
                break;
            }

            for (Edge edge : current.edges) {
                Vertex neighbor = edge.destination;
                int weight = edge.weight;
                int distanceFromCurrent = distances.get(current) + weight;

                if (distanceFromCurrent < distances.get(neighbor)) {
                    distances.put(neighbor, distanceFromCurrent);
                    previous.put(neighbor, current);
                    queue.remove(neighbor);
                    queue.add(neighbor);
                }
            }
        }

        List<Vertex> path = new ArrayList<>();

        for (Vertex vertex = end; vertex != null; vertex = previous.get(vertex)) {
            path.add(vertex);
        }

        Collections.reverse(path);

        return path;
    }*/
    public List<Vertex> shortestPath(String startName, String endName) {
        Map<Vertex, Integer> distances = new HashMap<>();
        Map<Vertex, Vertex> previous = new HashMap<>();
        PriorityQueue<Vertex> queue = new PriorityQueue<>((v1, v2) -> distances.get(v1) - distances.get(v2));

        Vertex start = vertices.get(startName);
        Vertex end = vertices.get(endName);

        for (Vertex vertex : vertices.values()) {
            distances.put(vertex, Integer.MAX_VALUE);
            previous.put(vertex, null);
        }

        distances.put(start, 0);
        queue.add(start);

        while (!queue.isEmpty()) {
            Vertex current = queue.poll();

            if (current == end) {
                break;
            }

            for (Edge edge : current.edges) {
                Vertex neighbor = edge.destination;
                //System.out.println(neighbor.state+","+neighbor.name);

                // Skip vertices with state "DOWN"
                if (neighbor.state.contentEquals("DOWN")) {

                    continue;
                }

                int weight = edge.weight;
                int distanceFromCurrent = distances.get(current) + weight;
                if (distanceFromCurrent < distances.get(neighbor)) {
                    distances.put(neighbor, distanceFromCurrent);
                    previous.put(neighbor, current);
                    queue.remove(neighbor);
                    queue.add(neighbor);
                }
            }

            // Handle unreachable vertices
            if (distances.get(current) == Integer.MAX_VALUE) {
                break;
            }
        }

        List<Vertex> path = new ArrayList<>();

        for (Vertex vertex = end; vertex != null; vertex = previous.get(vertex)) {
            path.add(vertex);
        }

        Collections.reverse(path);

        return path;
    }

}