-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoutineGraph.java
More file actions
150 lines (129 loc) · 3.15 KB
/
RoutineGraph.java
File metadata and controls
150 lines (129 loc) · 3.15 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
/**Graph class built from tutorial, and customized to
* needs for My Routine program. Automatically adds edges
* as vertices are added by tracking the last vertex.
* Hash maps are used to store edges and vertices contained
* within the graph
*/
package MyRoutine;
import java.util.*;
public class RoutineGraph
{
private HashMap<String, Vertex> vertices;
private HashMap<Integer, Edge> edges;
private Vertex last;
public RoutineGraph()
{
this.vertices = new HashMap<String, Vertex>();
this.edges = new HashMap<Integer, Edge>();
}
//add edge with default weight if it does not exist
public boolean addEdge(Vertex one, Vertex two)
{
return addEdge(one, two, 1);//returns true if added
}
//add edge with provided weight
public boolean addEdge(Vertex one,Vertex two,int weight)
{
if (one.equals(two)) { return false; }
//check that edge is not in graph
Edge e = new Edge(one,two,weight);
//future location to increment trips along
//the same path
if (edges.containsKey(e.hashCode()))
{
e.addTrip();//does not work
return false;
}
//check that edge is not incident to either vertex
else if (one.containsNeighbor(e) || two.containsNeighbor(e))
{
return false;
}
edges.put(e.hashCode(), e);
one.addNeighbor(e);
two.addNeighbor(e);
return true;
}
//check if an edge is contained within the graph
public boolean containsEdge(Edge e)
{
if (e.getOne() == null || e.getTwo() == null)
{
return false;
}
return this.edges.containsKey(e.hashCode());
}
//check graph for specified vertex
public boolean containsVertex(Vertex vertex)
{
return this.vertices.get(vertex.getLabel()) != null;
}
//return vertex with specified name
public Vertex getVertex(String place)
{
return vertices.get(place);
}
/**Method to add vertex to the graph, also
* automatically tracks last place and adds edges
* between current and last vertex
* @param vertex
* @return true if vertex was added
*/
public boolean addVertex(Vertex vertex)
{
Vertex current = this.vertices.get(vertex.getLabel());
if (current != null)
{
current.visit();//if vertex exists, increment visits
//add edge between vertices
addEdge(last,current);
//overwrite last vertex
last = current;
return false;//vertex not added
}
vertices.put(vertex.getLabel(), vertex);
//track last vertex to add edge
if(last == null)//first vertex?
{
last = vertex;
}
else
{
//add edge between vertices
addEdge(last,vertex);
//overwrite last vertex
last = vertex;
}
return true;//vertex was added
}
/**@return set of places from graph
*/
public Set<String> vertexKeys()
{
return this.vertices.keySet();
}
/**
* @return set of Edges in graph
*/
public Set<Edge> getEdges()
{
return new HashSet<Edge>(this.edges.values());
}
//print all vertices in graph
public void printRoutine()
{
for (String name: vertices.keySet())
{
String value = vertices.get(name).toString();
System.out.println(value);
}
}
public void printTrips()
{
Iterator<Edge> itr = this.getEdges().iterator();
while(itr.hasNext())
{
System.out.println(itr.next().toString());
}
}
}