-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph3.java.txt
More file actions
260 lines (237 loc) · 7.3 KB
/
Graph3.java.txt
File metadata and controls
260 lines (237 loc) · 7.3 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
package graph;
/**
* Undirected, unweighted simple graph data type
* <p>
* Notes:
* <ul>
* <li> Parallel edges are not allowed
* <li> Self loops are allowed
* </ul>
* <p>
* This Graph class was adapted from
* <a href="http://www.cs.princeton.edu/introcs/45graph/Graph.java">Graph.java</a> by
* by Robert Sedgewick and Kevin Wayne
*/
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
class Graph3 {
private HashMap<Vertex, TreeSet<Vertex>> myAdjList;
private HashMap<String, Vertex> myVertices;
private static final TreeSet<Vertex> EMPTY_SET = new TreeSet<Vertex>();
private int myNumVertices;
private int myNumEdges;
/**
* Construct empty Graph
*/
public Graph3() {
myAdjList = new HashMap<Vertex, TreeSet<Vertex>>();
myVertices = new HashMap<String, Vertex>();
myNumVertices = myNumEdges = 0;
}
/**
* Add a new vertex name with no neighbors (if vertex does not yet exist)
*
* @param name
* vertex to be added
*/
public Vertex addVertex(String name) {
Vertex v;
v = myVertices.get(name);
if (v == null) {
v = new Vertex(name);
myVertices.put(name, v);
myAdjList.put(v, new TreeSet<Vertex>());
myNumVertices += 1;
}
return v;
}
/**
* Returns the Vertex matching v
* @param name a String name of a Vertex that may be in
* this Graph
* @return the Vertex with a name that matches v or null
* if no such Vertex exists in this Graph
*/
public Vertex getVertex(String name) {
return myVertices.get(name);
}
/**
* Returns true iff v is in this Graph, false otherwise
* @param name a String name of a Vertex that may be in
* this Graph
* @return true iff v is in this Graph
*/
public boolean hasVertex(String name) {
return myVertices.containsKey(name);
}
/**
* Is from-to, an edge in this Graph. The graph is
* undirected so the order of from and to does not
* matter.
* @param from the name of the first Vertex
* @param to the name of the second Vertex
* @return true iff from-to exists in this Graph
*/
public boolean hasEdge(String from, String to) {
if (!hasVertex(from) || !hasVertex(to))
return false;
return myAdjList.get(myVertices.get(from)).contains(myVertices.get(to));
}
/**
* Add to to from's set of neighbors, and add from to to's
* set of neighbors. Does not add an edge if another edge
* already exists
* @param from the name of the first Vertex
* @param to the name of the second Vertex
*/
public void addEdge(String from, String to) {
Vertex v, w;
if (hasEdge(from, to))
return;
myNumEdges += 1;
if ((v = getVertex(from)) == null)
v = addVertex(from);
if ((w = getVertex(to)) == null)
w = addVertex(to);
myAdjList.get(v).add(w);
myAdjList.get(w).add(v);
}
/**
* Return an iterator over the neighbors of Vertex named v
* @param name the String name of a Vertex
* @return an Iterator over Vertices that are adjacent
* to the Vertex named v, empty set if v is not in graph
*/
public Iterable<Vertex> adjacentTo(String name) {
if (!hasVertex(name))
return EMPTY_SET;
return myAdjList.get(getVertex(name));
}
/**
* Return an iterator over the neighbors of Vertex v
* @param v the Vertex
* @return an Iterator over Vertices that are adjacent
* to the Vertex v, empty set if v is not in graph
*/
public Iterable<Vertex> adjacentTo(Vertex v) {
if (!myAdjList.containsKey(v))
return EMPTY_SET;
return myAdjList.get(v);
}
/**
* Returns an Iterator over all Vertices in this Graph
* @return an Iterator over all Vertices in this Graph
*/
public Iterable<Vertex> getVertices() {
return myVertices.values();
}
public int numVertices()
{
return myNumVertices;
}
public int numEdges()
{
return myNumEdges;
}
/**
* Sets each Vertices' centrality field to
* the degree of each Vertex (i.e. the number of
* adjacent Vertices)
*/
public void degreeCentrality()
{
// TODO: complete degreeCentrality
}
/**
* Sets each Vertices' centrality field to
* the average distance to every Vertex
*/
public void closenessCentrality()
{
// TODO: complete closenessCentrality
}
/**
* Sets each Vertices' centrality field to
* the proportion of geodesics (shortest paths) that
* this Vertex is on
*/
public void betweennessCentrality()
{
// TODO: complete betweennessCentrality
}
/*
* Returns adjacency-list representation of graph
*/
public String toString() {
String s = "";
for (Vertex v : myVertices.values()) {
s += v + ": ";
for (Vertex w : myAdjList.get(v)) {
s += w + " ";
}
s += "\n";
}
return s;
}
private String escapedVersion(String s) {
return "\'"+s+"\'";
}
public void outputGDF(String fileName)
{
HashMap<Vertex, String> idToName = new HashMap<Vertex, String>();
try {
FileWriter out = new FileWriter(fileName);
int count = 0;
out.write("nodedef> name,label,style,distance INTEGER\n");
// write vertices
for (Vertex v: myVertices.values())
{
String id = "v"+ count++;
idToName.put(v, id);
out.write(id + "," + escapedVersion(v.name));
out.write(",6,"+v.distance+"\n");
}
out.write("edgedef> node1,node2,color\n");
// write edges
for (Vertex v : myVertices.values())
for (Vertex w : myAdjList.get(v))
if (v.compareTo(w) < 0)
{
out.write(idToName.get(v)+","+
idToName.get(w) + ",");
if (v.predecessor == w ||
w.predecessor == v)
out.write("blue");
else
out.write("gray");
out.write("\n");
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Graph3 G = new Graph3();
G.addEdge("A", "B");
G.addEdge("A", "C");
G.addEdge("C", "D");
G.addEdge("D", "E");
G.addEdge("D", "G");
G.addEdge("E", "G");
G.addVertex("H");
System.out.println(G.escapedVersion("Bacon, Kevin"));
// print out graph
System.out.println(G);
// print out graph again by iterating over vertices and edges
for (Vertex v : G.getVertices()) {
System.out.print(v + ": ");
for (Vertex w : G.adjacentTo(v.name)) {
System.out.print(w + " ");
}
System.out.println();
}
G.outputGDF("graph.gdf");
}
}