Created
April 7, 2020 11:35
-
-
Save feehe21/65b769cefe2468161d2d8f9e95698936 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class ArrayLists implements Iterable<Integer> | |
{ | |
int[] alist; | |
int size; | |
public ArrayLists() | |
{ | |
alist = new int[0]; | |
size = 0; | |
} | |
public void add(int value){ | |
int[] temp = new int[size+1]; | |
for(int i = 0; i<size; i++){ | |
temp[i] = alist[i]; | |
} | |
temp[size] = value; | |
alist = temp; | |
size += 1; | |
} | |
public void add(int spot, int value){ | |
int[] temp = new int[size+1]; | |
for(int i = 0; i<size+1; i++){ | |
if(i == spot){ | |
temp[i] = value; | |
}else if(i < spot){ | |
temp[i] = alist[i]; | |
}else{ | |
temp[i] = alist[i-1]; | |
} | |
} | |
alist = temp; | |
size += 1; | |
} | |
public void set(int spot, int value){ | |
alist[spot] = value; | |
} | |
public int size(){ | |
return size; | |
} | |
public int get(int spot){ | |
return alist[spot]; | |
} | |
public int remove(int spot){ | |
int[] temp = new int[size-1]; | |
int hold = -1; | |
for(int i = 0; i<size; i++){ | |
if(i < spot){ | |
temp[i] = alist[i]; | |
}else if(i>spot){ | |
temp[i-1] = alist[i]; | |
}else{ | |
hold = alist[i]; | |
} | |
} | |
alist = temp; | |
size -= 1; | |
return hold; | |
} | |
public int find(int value){ | |
for(int i = 0; i < size; i++){ | |
if(alist[i] == value){ | |
return i; | |
} | |
} | |
return -1; | |
} | |
public void print(){ | |
for(int i = 0; i < size; i++){ | |
System.out.print(alist[i] + ", "); | |
} | |
} | |
@Override | |
public Iterator<Integer> iterator() { | |
return new Iterator<Integer>() { | |
int idx = 0; | |
@Override | |
public Integer next() { | |
if (idx >= size) | |
throw new IndexOutOfBoundsException(); | |
return alist[idx++]; | |
} | |
@Override | |
public boolean hasNext() { | |
return idx < size; | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Graph | |
{ | |
GraphNode nodes[]; | |
int size; | |
public Graph(int s){ | |
size = s; | |
nodes = new GraphNode[size]; | |
for(int i = 0; i < size; i++){ | |
nodes[i] = new GraphNode(i); | |
} | |
} | |
public void addEdge(int start, int end){ | |
if(nodes[start].connections.find(end) == -1){ | |
nodes[start].connections.add(end); | |
} | |
if(nodes[end].connections.find(start) == -1){ | |
nodes[end].connections.add(start); | |
} | |
} | |
public ArrayLists getEdge(int value){ | |
return nodes[value].connections; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GraphNode | |
{ | |
int value; | |
ArrayLists connections; | |
public GraphNode(int v){ | |
value = v; | |
connections = new ArrayLists(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class GraphRunner | |
{ | |
ArrayList<String> list = new ArrayList<String>(); | |
Graph g; | |
public GraphRunner(){ | |
g = new Graph(8); | |
/*g.addEdge(0,1); | |
g.addEdge(0,4); | |
g.addEdge(1,2); | |
g.addEdge(1,3); | |
g.addEdge(1,4); | |
g.addEdge(2,3); | |
g.addEdge(3,4); | |
for(int i = 0; i < g.size; i++){ | |
System.out.print(i + ": "); | |
g.getEdge(i).print(); | |
System.out.println(); | |
}*/ | |
g.addEdge(0, 1); | |
g.addEdge(0, 3); | |
g.addEdge(1, 2); | |
g.addEdge(3, 4); | |
g.addEdge(3, 7); | |
g.addEdge(4, 5); | |
g.addEdge(4, 6); | |
g.addEdge(4, 7); | |
g.addEdge(5, 6); | |
g.addEdge(6, 7); | |
for(int i = 0; i < g.size; i++){ | |
System.out.print(i + ": "); | |
g.getEdge(i).print(); | |
System.out.println(); | |
} | |
int source = 0, dest = 7; | |
int path = printShortestPath(source, dest); | |
System.out.println("Shortest Length: " + path); | |
System.out.println("Optinal Paths:"); | |
for (String p : list) | |
if (p.length() == path + 1) | |
System.out.println(p); | |
} | |
public int printShortestPath(int source, int dest){ | |
return rec( dest, 1, source, "", source); | |
} | |
public int rec(int dest, int length, int spot, String s, int past){ | |
int a = g.nodes[spot].connections.size(); | |
int smallest = Integer.MAX_VALUE; | |
int hold; | |
for(int i = 0; i < a; i++){ | |
if(g.nodes[spot].connections.get(i) == dest){ | |
System.out.println(s + g.nodes[spot].value + dest); | |
list.add(s + g.nodes[spot].value + dest); | |
return length; | |
} | |
} | |
for (int connection : g.nodes[spot].connections) { | |
boolean go = true; | |
if (s.contains(connection + "")) | |
go = false; | |
if(connection == past){ | |
}else if(!go){ | |
}else if(connection == dest){ | |
System.out.println(s + " " + g.nodes[spot].value); | |
return length; | |
}else{ | |
hold = rec( dest, length+1, connection, s + g.nodes[spot].value, spot); | |
if(hold < smallest){ | |
smallest = hold; | |
} | |
} | |
} | |
return smallest; | |
} | |
//if get back next to source - terminate | |
//if only connection is back - terminate | |
//if connection is found in its string already - terminate | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment