Question

write programme in java , Implement the Dijkstra’s method

write programme in java , Implement the Dijkstra’s method

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.*;

public class Dijkstra {

    public int[][] graph;
    public Node[] nodes;

    public static class Node {
        public Node parent;
        public int cost;
        public int id;

        public Node(Node parent, int cost, int id) {
            this.parent = parent;
            this.cost = cost;
            this.id = id;
        }
    }

    public Dijkstra() {
        graph = new int[][]{{0, 4, 0, 0, 0, 0, 0, 8, 0},
                {4, 0, 8, 0, 0, 0, 0, 11, 0},
                {0, 8, 0, 7, 0, 4, 0, 0, 2},
                {0, 0, 7, 0, 9, 14, 0, 0, 0},
                {0, 0, 0, 9, 0, 10, 0, 0, 0},
                {0, 0, 4, 14, 10, 0, 2, 0, 0},
                {0, 0, 0, 0, 0, 2, 0, 1, 6},
                {8, 11, 0, 0, 0, 0, 1, 0, 7},
                {0, 0, 2, 0, 0, 0, 6, 7, 0}
        };
        nodes = new Node[graph.length];
    }

    public static void main(String[] args) {

        Dijkstra dijkstra = new Dijkstra();
        for(int i = 0; i < dijkstra.nodes.length; i++)
            dijkstra.nodes[i] = new Node(null, Integer.MAX_VALUE, i);
        int source = 0;
        int destination = 4;
        dijkstra.shortestPath(source, destination);
        System.out.println("Shortest Distance from " + source + " to " + destination + "  is " + dijkstra.nodes[destination].cost);
        Node temp = dijkstra.nodes[destination];
        System.out.println("Path is ");
        while(temp.parent != null) {
            System.out.print(temp.id + " <--- ");
            temp = temp.parent;
        }
        System.out.println(temp.id);
    }

    public void shortestPath(int source, int destination) {
        Set<Node> visited = new HashSet<>();
        PriorityQueue<Node> pQueue = new PriorityQueue<>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        nodes[source].cost = 0;
        pQueue.add(nodes[source]);
        while(!pQueue.isEmpty()) {
            Node currVertex = pQueue.poll();
            for(int i = 0; i < graph.length; i++) {
                if(graph[currVertex.id][i]!=0 && !visited.contains(nodes[i]) ) {
                    if(!pQueue.contains(nodes[i])) {
                        nodes[i].cost = currVertex.cost + graph[currVertex.id][i];
                        nodes[i].parent = currVertex;
                        pQueue.add(nodes[i]);
                    }
                    else {
                        nodes[i].cost = Math.min(nodes[i].cost, currVertex.cost + graph[currVertex.id][i]);
                        if(nodes[i].cost == currVertex.cost + graph[currVertex.id][i])
                            nodes[i].parent = currVertex;
                    }
                }
            }
            visited.add(currVertex);
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
write programme in java , Implement the Dijkstra’s method
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT