java djikstra's algorithm
Java
import java.util.*;
public class DPQ {
private int dist[];
private Set<Integer> settled;
private PriorityQueue<Node> pq;
private int V; // Number of vertices
List<List<Node> > adj;
public DPQ(int V)
{
this.V = V;
dist = new int[V];
settled = new HashSet<Integer>();
pq = new PriorityQueue<Node>(V, new Node());
}
// Function for Dijkstra's Algorithm
public void dijkstra(List<List<Node> > adj, int src)
{
this.adj = adj;
for (int i = 0; i < V; i++)
dist[i] = Integer.MAX_VALUE;
// Add source node to the priority queue
pq.add(new Node(src, 0));
// Distance to the source is 0
dist[src] = 0;
while (settled.size() != V) {
// remove the minimum distance node
// from the priority queue
int u = pq.remove().node;
// adding the node whose distance is
// finalized
settled.add(u);
e_Neighbours(u);
}
}
// Function to process all the neighbours
// of the passed node
private void e_Neighbours(int u)
{
int edgeDistance = -1;
int newDistance = -1;
// All the neighbors of v
for (int i = 0; i < adj.get(u).size(); i++) {
Node v = adj.get(u).get(i);
// If current node hasn't already been processed
if (!settled.contains(v.node)) {
edgeDistance = v.cost;
newDistance = dist[u] + edgeDistance;
// If new distance is cheaper in cost
if (newDistance < dist[v.node])
dist[v.node] = newDistance;
// Add the current node to the queue
pq.add(new Node(v.node, dist[v.node]));
}
}
}
// Driver code
public static void main(String arg[])
{
int V = 5;
int source = 0;
// Adjacency list representation of the
// connected edges
List<List<Node> > adj = new ArrayList<List<Node> >();
// Initialize list for every node
for (int i = 0; i < V; i++) {
List<Node> item = new ArrayList<Node>();
adj.add(item);
}
// Inputs for the DPQ graph
adj.get(0).add(new Node(1, 9));
adj.get(0).add(new Node(2, 6));
adj.get(0).add(new Node(3, 5));
adj.get(0).add(new Node(4, 3));
adj.get(2).add(new Node(1, 2));
adj.get(2).add(new Node(3, 4));
// Calculate the single source shortest path
DPQ dpq = new DPQ(V);
dpq.dijkstra(adj, source);
// Print the shortest path to all the nodes
// from the source node
System.out.println("The shorted path from node :");
for (int i = 0; i < dpq.dist.length; i++)
System.out.println(source + " to " + i + " is "
+ dpq.dist[i]);
}
}
// Class to represent a node in the graph
class Node implements Comparator<Node> {
public int node;
public int cost;
public Node()
{
}
public Node(int node, int cost)
{
this.node = node;
this.cost = cost;
}
@Override
public int compare(Node node1, Node node2)
{
if (node1.cost < node2.cost)
return -1;
if (node1.cost > node2.cost)
return 1;
return 0;
}
}
Also in Java:
- Title
- java arraylist in enum
- Category
- Java
- Title
- java log base 2
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- string method example in java
- Category
- Java
- Title
- remove item from arraylist in java
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- get number of items in arraylist java
- Category
- Java
- Title
- reverse a string in java
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- java insert array
- Category
- Java
- Title
- how to convert string to array in java
- Category
- Java
- Title
- how to byheart faster
- Category
- Java
- Title
- java sum of two numbers program intellij
- Category
- Java
- Title
- How to loop through objects in java using streams
- Category
- Java
- Title
- java beginners book
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- java mouseevent
- Category
- Java
- Title
- most common element in list java
- Category
- Java
- Title
- scanner in java
- Category
- Java
- Title
- spigot repeating task
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- java Pair
- Category
- Java
- Title
- How to make a class in Java?
- Category
- Java
- Title
- break for loop java
- Category
- Java
- Title
- java function without return
- Category
- Java
- Title
- how to find the total of the products added to the shopping cart in java program
- Category
- Java
- Title
- java convert string with commas to long
- Category
- Java
- Title
- FileNotFoundException: properties/fortunes.txt (No such file or directory)
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- how to write queries in java
- Category
- Java
- Title
- how to create an array list in java
- Category
- Java
- Title
- android create snackbar
- Category
- Java
- Title
- string array in java methods
- Category
- Java
- Title
- what is java plug-in
- Category
- Java
- Title
- java if a or b
- Category
- Java
- Title
- how to check if an arraylist contains a value in java recursion
- Category
- Java
- Title
- how to play an audio in java
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- java string replace character at position
- Category
- Java
- Title
- string to int java
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- logger in java
- Category
- Java
- Title
- login and logout react native and firebase
- Category
- Java
- Title
- error attribute fabattached not found
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- joptionpane fonctionnement java
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- tower of hanoi program in java using recursion
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- treeset java descending order using comparator
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- how to end a program in an if statement java
- Category
- Java
- Title
- spigot custom join message
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- how to create a draw Rectangle in java
- Category
- Java
- Title
- spigot sounds
- Category
- Java
- Title
- IN APPLet how to disable a button in java
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- kotlin add element to array
- Category
- Java
- Title
- java scanner
- Category
- Java
- Title
- java swing timer sleep
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- how to check the lines in a file java scanner
- Category
- Java
- Title
- how to find the intersection of two rectangles in java
- Category
- Java
- Title
- how to split a string in java
- Category
- Java
- Title
- java tester si un caractere est une lettre
- Category
- Java
- Title
- string to double java
- Category
- Java
- Title
- print hello world in java
- Category
- Java
- Title
- validation list empty java
- Category
- Java
- Title
- java regex replace all characters before
- Category
- Java
- Title
- JavaFX onMouseEntered
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- java hello world
- Category
- Java
- Title
- how to make an arraylist java
- Category
- Java
- Title
- read int from keyboard java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- get today date in java 8
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- java import
- Category
- Java
- Title
- java clear console
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- treemap get order java
- Category
- Java
- Title
- java polymorphism
- Category
- Java
- Title
- how to pass enum in postman body
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- java double format
- Category
- Java
- Title
- jtable get get row
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- java instantiate collection with values
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- switch java 11
- Category
- Java
- Title
- java newinstance alternative
- Category
- Java
- Title
- connecting to h2 database from java
- Category
- Java
- Title
- setbackground java
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- java long to int
- Category
- Java
- Title
- transition java fx
- Category
- Java
- Title
- find duplicate elements in array in java
- Category
- Java