Future Vision BIE Future Vision BIE


ONE STOP FOR ALL STUDY MATERIALS & LAB PROGRAMS


E MENU Whatsapp Share Join Telegram, to get Instant Updates
× NOTE! Click on MENU to Browse between Subjects...

Advertisement

DESIGN AND ANALYSIS OF ALGORITHMS LABORATORY

(Effective from the academic year 2018 -2019)

SEMESTER - IV

Course Code 18CSL47

CIE Marks 40

Number of Contact Hours/Week 0:2:2

SEE Marks 60

Total Number of Lab Contact Hours 36

Exam Hours 03



Experiments 1A

Write Java programs to
Implement Travelling Sales Person problem using Dynamic programming.



Advertisement

Advertisement

 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
import java.util.ArrayList;
import java.util.Scanner;

class TSP {
    static int[][] graph;
    static int n, src;

    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter number of cities");
        n = scan.nextInt();

        graph = new int[n][n];

        System.out.println("Enter Adjacency Matrix");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                graph[i][j] = scan.nextInt();
            }
        }

        System.out.println("Enter Source City");
        src = scan.nextInt();
        scan.close();
        
        ArrayList<Integer> set = new ArrayList<Integer>();
        for (int i = 0; i < n; i++) {
            if (i == (src - 1)) {
                continue;
            }
            set.add(i);
        }

        int[] path = new int[n + 1];

        int cost = tsp(src - 1, set, path);
        System.out.println("Total Cost: " + cost);

        path[0] = src - 1;
        path[n] = src - 1;
        System.out.print("Path: ");
        for (int i = n; i >= 0; i--) {
            System.out.print((path[i] + 1) + " ");
        }
        System.out.println();
    }

    static int tsp(int v, ArrayList<Integer> set, int[] path) {
        if (set.isEmpty()) {
            return graph[v][src - 1];
        }
        int size = set.size();
        ArrayList<Integer> subSet;
        int minCost = Integer.MAX_VALUE;
        for (Integer i : set) {
            subSet = new ArrayList<Integer>(set);
            subSet.remove(i);
            int[] tempPath = new int[n+1];
            int cost = graph[v][i] + tsp(i, subSet, tempPath);

            if (cost < minCost) {
                minCost = cost;
                tempPath[size] = i;
                copyCentralArray(path, tempPath, size);
            }
        }
        return minCost;
    }

    static void copyCentralArray(int[] dest, int[] src, int size) {
        for (int i = 1; i <= size; i++) {
            dest[i] = src[i];
        }
    }
}

Advertisement

output
Fig 10.2: Output .

Advertisement
× Note Please Share the website link with Your Friends and known Students...

-ADMIN

× Note Page Number is specified to navigate between Pages...
T = Text book
QB = Question Bank
AS = Amswer Script


-ADMIN

Advertisement