Skip to content

Instantly share code, notes, and snippets.

@feehe21
Created April 15, 2020 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feehe21/79f89a51f59ee5577247505da750e139 to your computer and use it in GitHub Desktop.
Save feehe21/79f89a51f59ee5577247505da750e139 to your computer and use it in GitHub Desktop.
import java.util.*;
public class WeightedGraph
{
int verticies;
List<WGraphEdge>[] a;
public WeightedGraph(int v){
verticies = v;
a = new ArrayList[verticies];
for(int i = 0; i < verticies; i++){
a[i] = new ArrayList<WGraphEdge>();
}
}
public void addEdge(int source, int dest, int length){
WGraphEdge x = new WGraphEdge(length, source, dest);
a[source].add(x);
a[dest].add(x);
}
public void print(){
WGraphEdge e;
for(int i = 0; i < verticies; i++){
System.out.println("");
System.out.print(i + " | ");
for(int j = 0; j < a[i].size(); j++){
e = a[i].get(j);
if(e.source == i){
System.out.print(e.dest);
}else{
System.out.print(e.source);
}
System.out.print(" weight: " + e.length + " | ");
}
}
System.out.println("");
}
public ArrayList connectionsOf(int vertex){//return dest, dist, dest, dist, ect
ArrayList<Integer> connect = new ArrayList<Integer>();
WGraphEdge edge;
for(int i = 0; i < a[vertex].size(); i++){
edge = a[vertex].get(i);
if(edge.source == vertex){
connect.add(edge.dest);
}else{
connect.add(edge.source);
}
connect.add(edge.length);
}
return connect;
}
}
public class WGraphEdge
{
int length;
int source;
int dest;
public WGraphEdge(int l, int s, int d){
length = l;
source = s;
dest = d;
}
}
import java.util.*;
public class WGraphRunner2
{
WeightedGraph g;
int[] shortestLength;
int[] previousNode;
ArrayList<Integer> unexamined;
public WGraphRunner2(){
int size = 5;
g = new WeightedGraph(size);
shortestLength = new int[size];
previousNode = new int[size];
unexamined = new ArrayList<Integer>();
for(int i = 0; i < size; i++){
shortestLength[i] = Integer.MAX_VALUE;
previousNode[i] = -1;
unexamined.add(i);
}
g.addEdge(0, 1, 3);
g.addEdge(0, 3, 7);
g.addEdge(0, 4, 2);
g.addEdge(4, 3, 3);
g.addEdge(1, 3, 4);
g.addEdge(1, 2, 1);
g.addEdge(2, 3, 2);
g.print();
dijkstra(0);
for(int i = 0; i < size; i++){
System.out.println("Vertex:"+i+" Length:"+shortestLength[i]+" prev:"+previousNode[i]);
}
}
public void dijkstra(int source){
int focus = source;
shortestLength[source] = 0;
ArrayList<Integer> connections = new ArrayList<Integer>();
while(unexamined.size() > 0){
int q = unexamined.indexOf(focus);
unexamined.remove(q);
connections = g.connectionsOf(focus);
//System.out.println(focus+"Connections" + connections);
for(int i = 0; i < connections.size(); i+=2){
int testLength = shortestLength[focus] + connections.get(i+1);
if(testLength < shortestLength[connections.get(i)]){
shortestLength[connections.get(i)] = testLength;
previousNode[connections.get(i)] = focus;
}
}
if(unexamined.size() > 0){
focus = smallestUnexamined();
}
}
}
public int smallestUnexamined(){//returns node with shortest
int test;
int lowest = Integer.MAX_VALUE;
int lowPos = -1;
for(int i = 0; i < unexamined.size(); i++){
test = unexamined.get(i);
if(shortestLength[test] < lowest){
lowest = shortestLength[test];
lowPos = i;
}
}
if(lowPos == -1){
lowPos = 0;
}
return unexamined.get(lowPos);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment