-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
297 lines (274 loc) · 8.07 KB
/
Graph.java
File metadata and controls
297 lines (274 loc) · 8.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Main Class File: FollowsAnalysis.java
// File: Graph.java
// Semester: Spring 2011
//
// Author: Erin Rasmussen
// CS Login: rasmusse
// Lecturer's Name: Beck Hasti
// Lab Section: Lecture 2
//////////////////////////// 80 columns wide //////////////////////////////////
import java.util.*;
/**
* The Graph class represents a directed graph with string labels for nodes.
* Node labels are assumed to be unique and it is assumed that nodes do not
* have edges to themselves (i.e., no self edges).
*
* <p>Bugs: none known
*
* @author Erin Rasmussen
*/
public class Graph {
TreeSet<Graphnode> allNodes;
private List<String> names; //names stores all nodes in the graph too
private int numEdges;
Iterator<Graphnode> iter;
LinkedList<String> queue;
/**
* Constructs a new empty graph.
*/
public Graph(){
allNodes = new TreeSet<Graphnode>();
names = new ArrayList<String>();
numEdges = 0;
}
/**
* Add to the graph the specified directed edge from the
* node with the label label1 to the node with the label label2.
* @param label1 the the user node
* @param label2 the follower of the user
* @return true if operation is performed correctly
*/
public boolean addEdge(String label1, String label2){
if (hasEdge(label1, label2)) return false;
if (hasNode(label1)){
if (hasNode(label2)){
getGraphnode(label1).addEdge(label2);
getGraphnode(label2).setInDegree();
setEdges();
return true;
}
else {
addNode(label2);
getGraphnode(label1).addEdge(label2);
getGraphnode(label2).setInDegree();
setEdges();
return true;
}
}
return false;
}
/**
* Adds a node with the given label to the graph. If a node with this label
* is already in the graph, the graph is unchanged and false is returned.
* Otherwise (i.e., if there previously had not been a node with this label
* and a new node with this label is added to the graph), true is returned.
* @param label the name of the node to be added to the graph
* @return true if operation is performed correctly
*/
public boolean addNode(String label){
if (hasNode(label))return false;
else{
Graphnode tmp = new Graphnode(label);
allNodes.add(tmp);
names.add(label);
return true;
}
}
/**
* Return a list of node labels in the order they are visited using
* a breadth-first search starting at the node with the given label.
* @param label the name of the node to be bfs'd
* @return a list of node labels in bfs order
*/
public List<String> bfs(String label){
queue = new LinkedList<String>();
List<String> bfs = new ArrayList<String>();
Graphnode tmp = getGraphnode(label);
tmp.setVisited(true);
queue.addFirst(tmp.getName());
bfs.add(tmp.getName());
while (!queue.isEmpty()) {
String current = queue.removeLast();
for (String k : getGraphnode(current).getSuccessors()) {
if (! getGraphnode(k).getVisited()){
getGraphnode(k).setVisited(true);
queue.addFirst(k);
bfs.add(k);
} // end if k not visited
} // end for every successor k
} // end while queue not empty
iter = allNodes.iterator();
while (iter.hasNext()){
Graphnode tmp2 = iter.next();
tmp2.setVisited(false);
}
return bfs;
}
/**
* Return a list of node labels in the order they are visited using
* a depth-first search starting at the node with the given label.
* @param label the name of the node to be dfs'd
* @return a list of node labels in dfs order
*/
public List<String> dfs(String label){
List<String> dfsList = new ArrayList<String>();
dfsList.add(label);
dfs(label, dfsList);
iter = allNodes.iterator();
while (iter.hasNext()){
iter.next().setVisited(false);
}
return dfsList;
}
/**
* An auxiliary method that allows for recursion in the dfs algorithm.
* @param label the name of the "root" node for each recursion
* @param dfsList the list tracking the order of the dfs
*/
private void dfs(String label, List<String> dfsList){
getGraphnode(label).setVisited(true);
for (String m : getGraphnode(label).getSuccessors()) {
if (! getGraphnode(m).getVisited()) {
dfsList.add(m);
dfs(m, dfsList);
}
}
}
/**
* Return labels of immediate successors of the given node in alphabetical
* order.
* @param label the name of the node to retrieve its successors
* @return a list of immediate successors
*/
public List<String> getSuccessors(String label){
List<String> ordered = new ArrayList<String>();
Graphnode tmp = getGraphnode(label);
while (tmp.getSuccessors().iterator().hasNext()){
ordered.add(tmp.getSuccessors().iterator().next());
}
return ordered;
}
/**
* Return true if and only if the graph contains an edge from the node
* with the label label1 to the node with the label label2.
* @param label1 the user node in the graph
* @param label2 the follower of the user
* @return true if an edge exists between the two nodes
*/
public boolean hasEdge(String label1, String label2){
if (hasNode(label1)){
if (hasNode(label2)){
Graphnode tmp = getGraphnode(label1);
if (tmp.containsSuccessors(label2))return true;
}
}
return false;
}
/**
* Return true if and only if a node with the given label is in the graph.
* @param label the name of the node to determine it's existance
* @return true iff the node is in the graph
*/
public boolean hasNode(String label){
Iterator<String> iter2 = iterator();
String tmp;
while (iter2.hasNext()){
tmp = iter2.next();
if (tmp.equals(label))return true;
}
return false;
}
/**
* Return true if and only if the graph has size 0 (i.e., no nodes or
* edges).
* @return true if graph is empty
*/
public boolean isEmpty(){
return allNodes.size() == 0;
}
/**
* Return an iterator over the node labels in the graph.
* @return an iterator over the labels of the graph
*/
public Iterator<String> iterator(){
return names.iterator();
}
/**
* Return the number of edges in the graph.
* @return the number of edges in a graph
*/
public int numEdges(){
return numEdges;
}
/**
* Find the shortest path from a start node to a finish node.
* @param startLabel the name of first node in the path
* @param finishLabel the name of the last node in the path
* @return a list of the shortest path
*/
public List<String> shortestPath(String startLabel, String finishLabel){
queue = new LinkedList<String>();
List<String> shortestPath = new ArrayList<String>();
Graphnode tmp = getGraphnode(startLabel);
tmp.setVisited(true);
tmp.setDistance(0);
queue.addFirst(tmp.getName());
shortestPath.add(tmp.getName());
while (!queue.isEmpty()) {
String current = queue.removeLast();
if (current.equals(finishLabel)){
shortestPath.add(finishLabel);
iter = allNodes.iterator();
while (iter.hasNext()){
Graphnode tmp2 = iter.next();
tmp2.setVisited(false);
}
return shortestPath;
}
for (String k : getGraphnode(current).getSuccessors()) {
if (!getGraphnode(k).getVisited()){
getGraphnode(k).setVisited(true);
getGraphnode(k).setDistance(getGraphnode(current).
getDistance() + 1);
queue.addFirst(k);
shortestPath.add(k);
} // end if k not visited
} // end for every successor k
} // end while queue not empty
iter = allNodes.iterator();
while (iter.hasNext()){
Graphnode tmp2 = iter.next();
tmp2.setVisited(false);
}
return shortestPath;
}
/**
* Return the number of nodes in the graph.
* @return the number of nodes in a graph
*/
public int size(){
return allNodes.size();
}
/**
* Returns the graphnode with the given label.
* @param g the name of the node to be returned
* @return the graphnode with the given label
*/
private Graphnode getGraphnode(String g){
iter = allNodes.iterator();
Graphnode tmp;
while (iter.hasNext()){
tmp = iter.next();
if (tmp.getName().equals(g))return tmp;
}
return null;
}
/**
*Increases the number of edges by one.
*/
private void setEdges(){
numEdges++;
}
}