Skip to content

Instantly share code, notes, and snippets.

@abhamra
Created November 13, 2020 02:28
Show Gist options
  • Save abhamra/c2910916192518b2f291337262d39794 to your computer and use it in GitHub Desktop.
Save abhamra/c2910916192518b2f291337262d39794 to your computer and use it in GitHub Desktop.
public static void findShortest(ArrayList<Integer> arr, int source, int destination, int size, int count) {
ArrayList<Integer> temp = arr;
for(int i = 0;i<temp.size();i++) {
if(temp.get(i)==source) {
return;
} else {//end if
temp.add(source);
}
}//end for
if(temp.size()>min) {
return;
}//check if temp.size()>min, then return if that is satisfied (break when too long)
count++;//increment count
ArrayList<Integer> arrEdges = getEdge(source);//getting all values from edge of source
for(int i = 0;i<arrEdges.size();i++) {
if(arrEdges.get(i)==destination) {//if an edge == destination
temp.add(destination);
min=temp.size();
System.out.println(temp);
return;//exit the call
} else {//if you don't find the destination vertex among the edges
if(count==size) {
return;
} else {
findShortest(temp, arrEdges.get(i), destination, size, count);
}
}//end else
}//end for
}//end method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment