-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExploredGraph.java
More file actions
154 lines (135 loc) · 4.07 KB
/
Copy pathExploredGraph.java
File metadata and controls
154 lines (135 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Stack;
import java.util.function.Function;
/**
*
*/
/**
* @author your name(s) here.
* Extra Credit Options Implemented, if any: (mention them here.)
*
* Solution to Assignment 5 in CSE 373, Autumn 2016
* University of Washington.
*
* (Based on starter code v1.3. By Steve Tanimoto.)
*
* Java version 8 or higher is recommended.
*
*/
// Here is the main application class:
public class ExploredGraph {
Set<Vertex> Ve; // collection of explored vertices
Set<Edge> Ee; // collection of explored edges
public ExploredGraph() {
Ve = new LinkedHashSet<Vertex>();
Ee = new LinkedHashSet<Edge>();
}
public void initialize() {
// Implement this
}
public int nvertices() {return 0;} // Implement this.
public int nedges() {return 0;} // Implement this.
public void idfs(Vertex vi, Vertex vj) {} // Implement this. (Iterative Depth-First Search)
public void bfs(Vertex vi, Vertex vj) {} // Implement this. (Breadth-First Search)
public ArrayList<Vertex> retrievePath(Vertex vi) {return null;} // Implement this.
public ArrayList<Vertex> shortestPath(Vertex vi, Vertex vj) {return null;} // Implement this.
public Set<Vertex> getVertices() {return Ve;}
public Set<Edge> getEdges() {return Ee;}
/**
* @param args
*/
public static void main(String[] args) {
ExploredGraph eg = new ExploredGraph();
// Test the vertex constructor:
Vertex v0 = eg.new Vertex("[[4,3,2,1],[],[]]");
System.out.println(v0);
// Add your own tests here.
// The autograder code will be used to test your basic functionality later.
}
class Vertex {
ArrayList<Stack<Integer>> pegs; // Each vertex will hold a Towers-of-Hanoi state.
// There will be 3 pegs in the standard version, but more if you do extra credit option A5E1.
// Constructor that takes a string such as "[[4,3,2,1],[],[]]":
public Vertex(String vString) {
String[] parts = vString.split("\\],\\[");
pegs = new ArrayList<Stack<Integer>>(3);
for (int i=0; i<3;i++) {
pegs.add(new Stack<Integer>());
try {
parts[i]=parts[i].replaceAll("\\[","");
parts[i]=parts[i].replaceAll("\\]","");
List<String> al = new ArrayList<String>(Arrays.asList(parts[i].split(",")));
System.out.println("ArrayList al is: "+al);
Iterator<String> it = al.iterator();
while (it.hasNext()) {
String item = it.next();
if (!item.equals("")) {
System.out.println("item is: "+item);
pegs.get(i).push(Integer.parseInt(item));
}
}
}
catch(NumberFormatException nfe) { nfe.printStackTrace(); }
}
}
public String toString() {
String ans = "[";
for (int i=0; i<3; i++) {
ans += pegs.get(i).toString().replace(" ", "");
if (i<2) { ans += ","; }
}
ans += "]";
return ans;
}
}
class Edge {
Vertex endpoint1;
Vertex endpoint2;
public Edge(Vertex vi, Vertex vj) {
// Add whatever you need to here.
endpoint1 = vi;
endpoint2 = vj;
}
public String toString() {
return "Edge from " + endpoint1.toString() + " to " + endpoint2.toString();
}
public Vertex getEndpoint1() {
return endpoint1;
}
public Vertex getEndpoint2() {
return endpoint2;
}
}
class Operator {
private int i, j;
public Operator(int i, int j) { // Constructor for operators.
this.i = i;
this.j = j;
}
public boolean precondition(Vertex v) {
Integer iVar = v.pegs.get(i).peek();
Integer jVar = v.pegs.get(j).peek();
if (iVar > jVar || iVar == null) {
return false;
} else {
return true;
}
}
public Vertex transition(Vertex v) {
Stack<Integer> iStack = v.pegs.get(i);
Stack<Integer> jStack = v.pegs.get(j);
jStack.push(iStack.pop());
return v;
}
public String toString() {
// TODO: Add code to return a string good enough
// to distinguish different operators
return "Moving disk from " + i + " to " + j;
}
}
}