|
| 1 | +importjava.util.ArrayList; |
| 2 | +importjava.lang.StringBuilder; |
1 | 3 |
|
2 |
| -/** |
3 |
| - * This class implements the Graph data structure |
4 |
| - * using the classes Graph and Graphs. |
5 |
| - * |
6 |
| - * @author Zachary Jones |
7 |
| - * |
8 |
| - */ |
9 |
| -classGraph { |
| 4 | +classAdjacencyListGraph<EextendsComparable<E>> { |
10 | 5 |
|
11 |
| -} |
| 6 | +ArrayList<Vertex>verticies; |
| 7 | + |
| 8 | +publicAdjacencyListGraph() { |
| 9 | +verticies =newArrayList<>(); |
| 10 | + } |
| 11 | + |
| 12 | +privateclassVertex { |
| 13 | +Edata; |
| 14 | +ArrayList<Vertex>adjacentVerticies; |
| 15 | + |
| 16 | +publicVertex(Edata) { |
| 17 | +adjacentVerticies =newArrayList<>(); |
| 18 | +this.data =data; |
| 19 | + } |
12 | 20 |
|
13 |
| -/** |
14 |
| - * This class is used to test the Graph |
15 |
| - * class above. |
16 |
| - * |
17 |
| - * @author Zachary Jones |
18 |
| - * |
19 |
| - */ |
| 21 | +publicbooleanaddAdjacentVertex(Vertexto) { |
| 22 | +for (Vertexv:adjacentVerticies) { |
| 23 | +if (v.data.compareTo(to.data) ==0) { |
| 24 | +returnfalse;// the edge already exists |
| 25 | + } |
| 26 | + } |
| 27 | +returnadjacentVerticies.add(to);// this will return true; |
| 28 | + } |
| 29 | + |
| 30 | +publicbooleanremoveAdjacentVertex(Eto) { |
| 31 | +// use indexes here so it is possible to |
| 32 | +// remove easily without implementing |
| 33 | +// equals method that ArrayList.remove(Object o) uses |
| 34 | +for (inti =0;i <adjacentVerticies.size();i++) { |
| 35 | +if (adjacentVerticies.get(i).data.compareTo(to) ==0) { |
| 36 | +adjacentVerticies.remove(i); |
| 37 | +returntrue; |
| 38 | + } |
| 39 | + } |
| 40 | +returnfalse; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | +/** |
| 45 | + * this method removes an edge from the graph between two specified |
| 46 | + * verticies |
| 47 | + * |
| 48 | + * @param from the data of the vertex the edge is from |
| 49 | + * @param to the data of the vertex the edge is going to |
| 50 | + * @return returns false if the edge doesn't exist, returns true if the edge exists and is removed |
| 51 | + */ |
| 52 | +publicbooleanremoveEdge(Efrom,Eto) { |
| 53 | +VertexfromV =null; |
| 54 | +for (Vertexv:verticies) { |
| 55 | +if (from.compareTo(v.data) ==0) { |
| 56 | +fromV =v; |
| 57 | +break; |
| 58 | + } |
| 59 | + } |
| 60 | +if (fromV ==null)returnfalse; |
| 61 | +returnfromV.removeAdjacentVertex(to); |
| 62 | + } |
| 63 | +/** |
| 64 | + * this method adds an edge to the graph between two specified |
| 65 | + * verticies |
| 66 | + * |
| 67 | + * @param from the data of the vertex the edge is from |
| 68 | + * @param to the data of the vertex the edge is going to |
| 69 | + * @return returns true if the edge did not exist, return false if it already did |
| 70 | + */ |
| 71 | +publicbooleanaddEdge(Efrom,Eto) { |
| 72 | +VertexfromV =null,toV =null; |
| 73 | +for (Vertexv:verticies) { |
| 74 | +if (from.compareTo(v.data) ==0) {// see if from vertex already exists |
| 75 | +fromV =v; |
| 76 | + }elseif (to.compareTo(v.data) ==0) {// see if to vertex already exists |
| 77 | +toV =v; |
| 78 | + } |
| 79 | +if (fromV !=null &&toV !=null)break;// both nodes exist so stop searching |
| 80 | + } |
| 81 | +if (fromV ==null) { |
| 82 | +fromV =newVertex(from); |
| 83 | +verticies.add(fromV); |
| 84 | + } |
| 85 | +if (toV ==null) { |
| 86 | +toV =newVertex(to); |
| 87 | +verticies.add(toV); |
| 88 | + } |
| 89 | +returnfromV.addAdjacentVertex(toV); |
| 90 | + } |
| 91 | + |
| 92 | +/** |
| 93 | + * this gives a list of verticies in the graph and their adjacencies |
| 94 | + * |
| 95 | + * @return returns a string describing this graph |
| 96 | + */ |
| 97 | +publicStringtoString() { |
| 98 | +StringBuildersb =newStringBuilder(); |
| 99 | +for (Vertexv:verticies) { |
| 100 | +sb.append("Vertex: "); |
| 101 | +sb.append(v.data); |
| 102 | +sb.append("\n"); |
| 103 | +sb.append("Adjacent verticies: "); |
| 104 | +for (Vertexv2:v.adjacentVerticies) { |
| 105 | +sb.append(v2.data); |
| 106 | +sb.append(" "); |
| 107 | + } |
| 108 | +sb.append("\n"); |
| 109 | + } |
| 110 | +returnsb.toString(); |
| 111 | + } |
| 112 | +} |
20 | 113 |
|
21 | 114 | publicclassGraphs {
|
22 | 115 |
|
23 |
| -/** |
24 |
| - * Main method |
25 |
| - * |
26 |
| - * @param args Command line arguments |
27 |
| - */ |
28 | 116 | publicstaticvoidmain(Stringargs[]) {
|
29 |
| - |
30 |
| -} |
| 117 | +AdjacencyListGraph<Integer>graph =newAdjacencyListGraph<>(); |
| 118 | +assertgraph.addEdge(1,2); |
| 119 | +assertgraph.addEdge(1,5); |
| 120 | +assertgraph.addEdge(2,5); |
| 121 | +assert !graph.addEdge(1,2); |
| 122 | +assertgraph.addEdge(2,3); |
| 123 | +assertgraph.addEdge(3,4); |
| 124 | +assertgraph.addEdge(4,1); |
| 125 | +assert !graph.addEdge(2,3); |
| 126 | +System.out.println(graph); |
| 127 | + } |
31 | 128 |
|
32 | 129 | }
|