Created
April 1, 2020 16:03
-
-
Save feehe21/d7993f1ac656c9343784593a7d1e515c to your computer and use it in GitHub Desktop.
Graphs!!!!
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 ArrayLists | |
{ | |
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] + ", "); | |
} | |
} | |
} |
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
public class graphRunner | |
{ | |
public graphRunner(){ | |
Graph g = new Graph(5); | |
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment