Skip to content

Instantly share code, notes, and snippets.

@a-r-d
Created September 27, 2015 20:30
Show Gist options
  • Save a-r-d/0d7398d4b9406b115296 to your computer and use it in GitHub Desktop.
Save a-r-d/0d7398d4b9406b115296 to your computer and use it in GitHub Desktop.
Shortest path problem on hacker rank using Dijkstra algorithm
1
8 7
1 2
1 3
3 6
2 4
4 5
5 6
2 7
1
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
public class BfsShortestDijkstra {
public static class Node implements Comparable<Node>{
int val = 0;
long minDistance = Long.MAX_VALUE;
boolean isStart = false;
List<Edge> adjacencies = new ArrayList<>();
Node prevNode;
@Override
public String toString() {
return val + "";
}
@Override
public int compareTo(Node o) {
return Long.compare(this.minDistance, o.minDistance);
}
}
public static class Edge {
int x = 0;
int y = 0;
Node start;
Node target;
long weight = 6;
@Override
public String toString() {
return x + "," + y;
}
}
public static Node getNode(List<Node> nodes, int val) {
for(Node n : nodes) {
if(n.val == val) {
return n;
}
}
return null;
}
public static void computePaths(Node start) {
start.minDistance = 0;
PriorityQueue<Node> queue = new PriorityQueue<>();
queue.add(start);
while(!queue.isEmpty()) {
Node current = queue.poll();
for(Edge e: current.adjacencies) {
// proposed min distance for this node.
Node nextNode = e.target;
long mindistance = e.weight + current.minDistance;
// is the new min distance less than the previously recorded min distance?
if(mindistance < nextNode.minDistance) {
// maybe it is not in here, we don;t know, just want to update
queue.remove(nextNode);
nextNode.prevNode = current;
nextNode.minDistance = mindistance;
queue.add(nextNode);
}
}
}
}
public static List<Node> getShortestPath(Node target) {
List<Node> path = new ArrayList<>();
while(target.prevNode != null) {
path.add(target);
target = target.prevNode;
}
return path;
}
public static void main(String[] args) throws Exception {
FileInputStream fis = new FileInputStream(new File("bfs-shortest.txt"));
Scanner s = new Scanner(fis);
// num tests
int n = s.nextInt();
for(int i =0; i < n; i++) {
// # nodes
int nodes = s.nextInt();
// Node values do not count from zero...
List<Node> nodeList = new ArrayList<>();
for(int j=0; j < nodes; j++) {
Node node = new Node();
node.val = j + 1;
nodeList.add(node);
}
// # edges
int edges = s.nextInt();
// list of edges x, and y positions.
for(int e = 0; e < edges; e++) {
int x = s.nextInt();
int y = s.nextInt();
// the graph is undirected so this causes so much damn pain
Edge edge1 = new Edge();
edge1.x = x;
edge1.y = y;
Node n1 = getNode(nodeList, x);
Node n2 = getNode(nodeList, y);
n1.adjacencies.add(edge1);
edge1.start = n1;
edge1.target = n2;
// reverse the edge because it is undirected.
Edge edge2 = new Edge();
edge2.x = y;
edge2.y = x;
edge2.start = n2;
edge2.target = n1;
n2.adjacencies.add(edge2);
}
// start position
int start = s.nextInt();
Node startNode = getNode(nodeList, start);
computePaths(startNode);
for(Node node : nodeList) {
if(node.val == start) {
continue;
}
if(node.minDistance == Long.MAX_VALUE) {
System.out.print(-1 + " ");
} else {
System.out.print(node.minDistance + " ");
}
}
System.out.println();
// help GC
nodeList = null;
startNode = null;
}
s.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment