tsp bb

 import java.util.Arrays;

import java.util.PriorityQueue;

class TravellingSalesman {

    static class Node implements Comparable<Node> {
        int level;
        int[] path;
        int bound;
        int cost;

        Node(int level, int[] path, int bound, int cost) {
            this.level = level;
            this.path = Arrays.copyOf(path, path.length);
            this.bound = bound;
            this.cost = cost;
        }

        @Override
        public int compareTo(Node o) {
            return Integer.compare(this.bound, o.bound);
        }
    }

   
    static int calculateBound(Node node, int[][] graph, int n) {
        int bound = 0;

       
        for (int i = 0; i < node.level - 1; i++) {
            bound += graph[node.path[i]][node.path[i + 1]];
        }

       
        boolean[] visited = new boolean[n];
        for (int i = 0; i < node.level; i++) {
            visited[node.path[i]] = true;
        }

        for (int i = 0; i < n; i++) {
            if (!visited[i]) {
                int minCost = Integer.MAX_VALUE;
                for (int j = 0; j < n; j++) {
                    if (!visited[j] && i != j && graph[i][j] < minCost) {
                        minCost = graph[i][j];
                    }
                }
                bound += (minCost == Integer.MAX_VALUE) ? 0 : minCost;
            }
        }

        return bound;
    }

   
    public static int solveTSP(int[][] graph) {
        int n = graph.length;
        PriorityQueue<Node> pq = new PriorityQueue<>();

       
        int[] initialPath = new int[n + 1];
        initialPath[0] = 0;
        Node root = new Node(1, initialPath, calculateBound(new Node(1, initialPath, 0, 0), graph, n), 0);
        pq.add(root);

        int minCost = Integer.MAX_VALUE;
        int[] bestPath = null;

        while (!pq.isEmpty()) {
            Node currentNode = pq.poll();

           
            if (currentNode.bound < minCost) {
                for (int i = 1; i < n; i++) {
                    if (!isVisited(i, currentNode.path, currentNode.level)) {
                        Node childNode = new Node(currentNode.level + 1, currentNode.path, 0, currentNode.cost);

                        // Update path and cost
                        childNode.path[currentNode.level] = i;
                        childNode.cost = currentNode.cost + graph[currentNode.path[currentNode.level - 1]][i];

                       
                        childNode.bound = calculateBound(childNode, graph, n);

                       
                        if (childNode.level == n) {
                            childNode.cost += graph[i][0];
                            if (childNode.cost < minCost) {
                                minCost = childNode.cost;
                                bestPath = Arrays.copyOf(childNode.path, n + 1);
                                bestPath[n] = 0;
                            }
                        } else if (childNode.bound < minCost) {
                            pq.add(childNode);
                        }
                    }
                }
            }
        }

        System.out.println("Optimal Path: " + Arrays.toString(bestPath));
        return minCost;
    }

   
    static boolean isVisited(int city, int[] path, int level) {
        for (int i = 0; i < level; i++) {
            if (path[i] == city) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
       
        int[][] graph = {
            {0, 10, 15, 20},
            {10, 0, 35, 25},
            {15, 35, 0, 30},
            {20, 25, 30, 0}
        };

        System.out.println("Minimum cost: " + solveTSP(graph));
    }
}

Comments

Popular posts from this blog

q3part b

Literal table

quicksort 1