answersLogoWhite

0


Best Answer

import java.util.List;

import java.util.ArrayList;

import java.util.Collections;

class Vertex implements Comparable<Vertex>

{

public final String name;

public Edge[] adjacencies;

public double minDistance = Double.POSITIVE_INFINITY;

public Vertex previous;

public Vertex(String argName) { name = argName; }

public String toString() { return name; }

public int compareTo(Vertex other)

{

return Double.compare(minDistance, other.minDistance);

}

}

class Edge

{

public final Vertex target;

public final double weight;

public Edge(Vertex argTarget, double argWeight)

{ target = argTarget; weight = argWeight; }

}

public class Dijkstra

{

public static void computePaths(Vertex source)

{

source.minDistance = 0.;

PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();

vertexQueue.add(source);

while (!vertexQueue.isEmpty()) {

Vertex u = vertexQueue.poll();

// Visit each edge exiting u

for (Edge e : u.adjacencies)

{

Vertex v = e.target;

double weight = e.weight;

double distanceThroughU = u.minDistance + weight;

if (distanceThroughU < v.minDistance) {

vertexQueue.remove(v);

v.minDistance = distanceThroughU ;

v.previous = u;

vertexQueue.add(v);

}

}

}

}

public static List<Vertex> getShortestPathTo(Vertex target)

{

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

for (Vertex vertex = target; vertex != null; vertex = vertex.previous)

path.add(vertex);

Collections.reverse(path);

return path;

}

public static void main(String[] args)

{

Vertex v0 = new Vertex("Harrisburg");

Vertex v1 = new Vertex("Baltimore");

Vertex v2 = new Vertex("Washington");

Vertex v3 = new Vertex("Philadelphia");

Vertex v4 = new Vertex("Binghamton");

Vertex v5 = new Vertex("Allentown");

Vertex v6 = new Vertex("New York");

v0.adjacencies = new Edge[]{ new Edge(v1, 79.83),

new Edge(v5, 81.15) };

v1.adjacencies = new Edge[]{ new Edge(v0, 79.75),

new Edge(v2, 39.42),

new Edge(v3, 103.00) };

v2.adjacencies = new Edge[]{ new Edge(v1, 38.65) };

v3.adjacencies = new Edge[]{ new Edge(v1, 102.53),

new Edge(v5, 61.44),

new Edge(v6, 96.79) };

v4.adjacencies = new Edge[]{ new Edge(v5, 133.04) };

v5.adjacencies = new Edge[]{ new Edge(v0, 81.77),

new Edge(v3, 62.05),

new Edge(v4, 134.47),

new Edge(v6, 91.63) };

v6.adjacencies = new Edge[]{ new Edge(v3, 97.24),

new Edge(v5, 87.94) };

Vertex[] vertices = { v0, v1, v2, v3, v4, v5, v6 };

computePaths(v0);

for (Vertex v : vertices)

{

System.out.println("Distance to " + v + ": " + v.minDistance);

List<Vertex> path = getShortestPathTo(v);

System.out.println("Path: " + path);

}

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the java program for Dijkstra's algorithm?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Implement the Dijkstras shortest path routing algorithm and submit the code listing with proper documentation?

#include


How do you write algorithms of java programs?

Write a program that graphically demonstrates the shortest path algorithm


How do you write algorithm for java program?

An "algorithm" is a method to solve a problem. These methods are more or less independent of the language. First you think about how you will solve a certain problem, step by step. Then you translate this into a computer program.


Develop an algorithm to display all prime numbers from 2 to 100 Give both the pseudocode version and the flowchart version Convert your pseudocode into a Java program?

Develop an algorithm to display all prime numbers from 2 to 100. Give both the pseudocode version and the flowchart version. Convert your pseudocode into a Java program.


Can you provide a solution to the diamond-square algorithm using Java and recursion?

Yes. It is possible to provide a solution to the diamond-square algorithm using Java and recursion.


Write a program in C language to implement the apriori algorithm?

JavaScript is one program that has been written in C to implement the Apriori algorithm. There are also several other known programs available on the Internet that implement it as well.


How to write a java program that determines the number of prime numbers less than N which is given by the user?

where to start? do you have an algorithm and just want to implement it in java? depends on how big N is, as that will determine which method is most efficient


What is the difference between an algorithm and java code?

In Java programming language, an algorithm refers to a sequence of instructions that have been specified to undertake a particular task within a certain time. An algorithm can take no or several inputs but will generate at least one output.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


What is the difference between an algorithm and a computer program?


What is java program on computers?

#!/usr/bin/perl print 'java program';


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.