Skip to content

Instantly share code, notes, and snippets.

@zikosw
Last active August 29, 2015 14:08
Show Gist options
  • Save zikosw/42347179881258895e57 to your computer and use it in GitHub Desktop.
Save zikosw/42347179881258895e57 to your computer and use it in GitHub Desktop.
Dijkstras with graph.csv
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
class Vertex implements Comparable<Vertex> {
public final String name;
public ArrayList<Edge> adjacencies;
public int minDistance = Integer.MAX_VALUE;
public Vertex previous;
public Vertex(String name) {
this.name = name;
adjacencies = new ArrayList<Edge>();
}
public String toString() {
return name;
}
public int compareTo(Vertex other) {
return Integer.compare(minDistance, other.minDistance);
}
}
class Edge {
public final Vertex target;
public final int weight;
public Edge(Vertex argTarget, int weight) {
target = argTarget;
this.weight = weight;
}
}
public class Dijkstras {
public static void readCSV(ArrayList<String> vname,ArrayList<ArrayList<Integer>> vAdjacen) {
String csvFile = "graph.csv";
String csvSpliter = ",";
Scanner scanner = null;
try {
scanner = new Scanner(new File(csvFile));
} catch (FileNotFoundException e) {
System.out.println("File graph.csv not found, exit!");
System.exit(1);
}
scanner.useDelimiter(",");
int i = 0, arrCount = 0;
System.out.println("Graph Adjacencies List");
System.out.println("---------------------------------------------");
while (scanner.hasNextLine()) {
String[] c = scanner.nextLine().split(csvSpliter);
if (i != 0) {
System.out.print(vname.get(i - 1) + "|");
} else {
System.out.print(" ");
}
ArrayList<Integer> adjacen = new ArrayList<Integer>();
for (int j = 0; j < c.length; j++) {
if (i == 0) {
vname.add(c[j]);
} else {
int w=Integer.parseInt(c[j]);
adjacen.add(w);
}
System.out.print(c[j] + "|");
}
vAdjacen.add(arrCount, adjacen);
if (i != 0) {
arrCount++;
}
System.out.println();
i++;
}
scanner.close();
System.out.println("---------------------------------------------");
}
public static void addEdge(ArrayList<Vertex> vertices,
ArrayList<String> vname, ArrayList<ArrayList<Integer>> vAdjacen) {
for (int i = 0; i < vname.size(); i++) {
for (int j = 0, k = 0; j < vertices.size() && k < vAdjacen.size(); j++, k++) {
if (i != j) {
if (vAdjacen.get(i).get(k) != 0) {
Edge e = new Edge(vertices.get(j), vAdjacen.get(i).get(
k));
vertices.get(i).adjacencies.add(e);
}
}
}
}
}
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;
int weight = e.weight;
int 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 String makeEndpoint(String pos, ArrayList<String> vname,
String vertices) {
Scanner in = new Scanner(System.in);
String line;
String c = null;
while (true) {
System.out.print("Enter " + pos + " node " + vertices + ": ");
line = in.next();
if (!vname.contains(line)) {
System.out.print("Please enter right node, " + pos
+ " node " + vertices + "\nTry again.\n");
} else {
c = line;
break;
}
}
return c;
}
public static void main(String[] args) {
ArrayList<String> vname = new ArrayList<String>();
ArrayList<ArrayList<Integer>> vAdjacen = new ArrayList<ArrayList<Integer>>();
readCSV(vname, vAdjacen);
ArrayList<Vertex> vertices = new ArrayList<Vertex>();
for (int i = 0; i < vname.size(); i++) {
Vertex v = new Vertex(vname.get(i));
vertices.add(v);
}
addEdge(vertices, vname, vAdjacen);
String start = makeEndpoint("start", vname, vertices.toString());
String end = makeEndpoint("end", vname, vertices.toString());
Vertex vstart = null, vend = null;
for (int x = 0; x < vertices.size(); x++) {
if (start.equals(vertices.get(x).name)) {
vstart = vertices.get(x);
}
}
for (int x = 0; x < vertices.size(); x++) {
if (end.equals(vertices.get(x).name)) {
vend = vertices.get(x);
}
}
computePaths(vstart);
System.out.println("Distance from " + start + " to " + end + " : "
+ vend.minDistance);
List<Vertex> path = getShortestPathTo(vend);
System.out.println("Path: " + path);
}
}
a b c d e f g h i j k z
0 3 8 0 0 0 7 0 0 0 2 0
3 0 0 1 4 5 0 0 0 0 0 0
8 0 0 0 3 0 0 0 0 0 0 0
0 1 0 0 0 7 2 0 0 2 0 0
0 4 3 0 0 1 0 0 0 0 0 0
0 5 0 7 1 0 0 0 0 0 0 2
7 0 0 2 0 0 0 0 0 0 3 0
0 0 0 0 0 0 0 0 7 0 4 0
0 0 0 0 0 0 0 7 0 4 0 3
0 0 0 2 0 0 0 0 4 0 0 0
2 0 0 0 0 0 3 4 0 0 0 0
0 0 0 0 0 2 0 0 3 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment