/
CreateImage.java
371 lines (298 loc) · 9.99 KB
/
CreateImage.java
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import org.jgraph.*;
import org.jgraph.graph.*;
import org.jgraph.graph.DefaultEdge;
import org.jgrapht.graph.*;
import org.jgrapht.ext.*;
import org.jgrapht.alg.*;
public class CreateImage
{
// private static final Color DEFAULT_BG_COLOR = Color.decode("#FAFBFF");
// private static final Dimension DEFAULT_SIZE = new Dimension(1000, 1000);
private static Random randGen = new Random ();
private JGraphModelAdapter<Position,DefaultEdge> m_jgAdapter;
ArrayList<ArrayList<Position>> grid = new ArrayList<ArrayList<Position>>();
ListenableDirectedGraph<Position,DefaultEdge> graph = new ListenableDirectedGraph<Position,DefaultEdge>(DefaultEdge.class);
JGraph jgraph;
ArrayList<ArrayList<Position>> connectedComponents = new ArrayList<ArrayList<Position>>();
private int threads;
public void create()
{
JFrame frame = new JFrame();
frame.getContentPane().add(new JScrollPane(jgraph));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public void parseInput(String file, int threads, int limit) throws FileNotFoundException, InterruptedException
{
//long startParseTime = System.currentTimeMillis();
m_jgAdapter = new JGraphModelAdapter<Position, DefaultEdge>( graph);
jgraph = new JGraph (m_jgAdapter);
this.threads = threads;
Scanner input = new Scanner(new File(file));
try {
for(int r = 0; input.hasNextLine() && r < limit; r++) {
Scanner line = new Scanner(input.nextLine());
try {
ArrayList<Position> row = new ArrayList<Position>();
grid.add(row);
System.out.println ("Row " + r);
for(int c = 0; line.hasNextInt() && c < limit; c++) {
Position position = new Position (r, c, line.nextInt ());
row.add (position);
graph.addVertex(position);
positionVertexAt(position, position.column * 5, position.row * 5);
}
}
finally {
line.close();
}
}
}
finally {
input.close();
}
graphGrid(grid);
//ArrayList<ArrayList<Position>> grid2 = transpose(grid);
//outputGrid(grid2);
}
private void outputGrid(ArrayList<ArrayList<Position>> grid)
{
for(int i = 0; i <grid.size();i++)
{
for (int j = 0; j <grid.get(0).size();j++)
{
System.out.print(grid.get(i).get(j).value + " ");
}
System.out.println("");
}
}
private void graphGrid(ArrayList<ArrayList<Position>> grid) throws InterruptedException
{
System.out.println("Size of file by rows = " + grid.size() + ", columns = " + grid.get(0).size());
final long current= System.currentTimeMillis();
//System.out.println("Time for parsing file: " + ((current - startParseTime) / 1000) + " sec");
//long afterEdge = 0;
//long timeFindEdge = 0;
ArrayList<Integer> ranges = findRanges();
final Cell <Integer> threadsActive = new Cell <Integer> (threads);
class EdgeThread extends Thread {
int startRow = 0;
int endRow = 0;
public EdgeThread(int startRow, int endRow)
{
this.startRow = startRow;
this.endRow = endRow;
}
public void run()
{
System.out.println("Thread: " + this.getId());
findEdges(startRow, endRow);
boolean lastThread;
synchronized (threadsActive) { lastThread = -- threadsActive.object == 0; }
if (lastThread) {
System.out.println("Last Thread: " + this.getId());
long afterEdge= System.currentTimeMillis();
long timeFindEdge = (afterEdge - current) / 1000;
System.out.println("Time for findEdges: " + timeFindEdge + " sec");
colorSCC();
long afterColor = System.currentTimeMillis();
long timeForColor = (afterColor - afterEdge) / 1000;
System.out.println("Time for coloring and find CC: " + timeForColor + " sec");
//findConnectedComponents();
create();
long timeForCreate = (System.currentTimeMillis() - afterColor) / 1000;
System.out.println("Time for creating map: " + timeForCreate + " sec") ;
}
}
}
for(int i = 0; i < threads; i++)
{
EdgeThread thread = new EdgeThread(ranges.get(2*i), ranges.get(2*i + 1));
thread.start();
}
Thread.currentThread().join();
}
private class Cell <a> { public a object; public Cell (a object) { this.object = object; } }
private ArrayList<Integer> findRanges()
{
int rowsPerRange = (int) Math.ceil((grid.size())/ (double)threads);
ArrayList<Integer> ranges = new ArrayList<Integer>();
int startRow =0, endRow = rowsPerRange;
for(int i = 0; i < threads; i++)
{
ranges.add(startRow);
ranges.add(endRow);
startRow = endRow;;
endRow += rowsPerRange;
}
ranges.set(ranges.size()-1, Math.min(grid.size(), ranges.get(ranges.size() - 1)));
System.out.println(ranges);
return ranges;
}
private static <a> ArrayList <ArrayList <a>> transpose (ArrayList <ArrayList <a>> matrix)
{
ArrayList<ArrayList<a>> grid2 = new ArrayList<ArrayList<a>>();
if(matrix.isEmpty())
return grid2;
for(int i = 0; i < matrix.get(0).size();i++)
{
grid2.add(new ArrayList<a> (matrix.size()));
}
System.out.println(grid2);
for(int i = 0; i < matrix.size(); i++)
{
ArrayList<a> row = matrix.get(i);
for(int j = 0; j < row.size(); j++)
{
grid2.get(j).add(row.get(j));//.set(i, row.get(j));
}
}
return grid2;
}
private void findEdges(int startRow, int endRow)
{
int gridSize = grid.size();
int sizeOfArrays = grid.get(0).size();
for(int i = startRow; i < endRow; i++) {
for(int j = 0; j < sizeOfArrays;j++) {
Position currentPosition = grid.get(i).get(j);
int current = currentPosition.value;
int min = current;
Position minP = currentPosition;
if(i-1 >= 0) {
Position north = grid.get(i-1).get(j);
if (north.value < min) {
min = north.value;
minP = north;
}
}
if(i-1 >= 0 && j-1 >= 0) {
Position northWest = grid.get(i-1).get(j-1);
if(northWest.value < min) {
min = northWest.value;
minP = northWest;
}
}
if(j-1 >= 0) {
Position west = grid.get(i).get(j-1);
if(west.value < min) {
min = west.value;
minP = west;
}
}
if(i+1 < gridSize && j-1 >= 0) {
Position southWest = grid.get(i+1).get(j-1);
if(southWest.value < min) {
min = southWest.value;
minP = southWest;
}
}
if(i+1 < gridSize) {
Position south = grid.get(i+1).get(j);
if(south.value < min) {
min = south.value;
minP = south;
}
}
if(i+1 < gridSize && j+1 < sizeOfArrays) {
Position southEast = grid.get(i+1).get(j+1);
if(southEast.value < min) {
min = southEast.value;
minP = southEast;
}
}
if(j+1 < sizeOfArrays) {
Position east = grid.get(i).get(j+1);
if(east.value < min) {
min = east.value;
minP = east;
}
}
if( i-1 >= 0 && j+1 < sizeOfArrays) {
Position northEast = grid.get(i-1).get(j+1);
if(northEast.value< min) {
min = northEast.value;
minP = northEast;
}
}
if(currentPosition.equals (minP)) { }
else {
synchronized(graph) {graph.addEdge(currentPosition, minP);}
}
}
}
}
/* private void colorComponent(Position p, Color color)
{
//System.out.println(p.toString2());
if(p.visited)
return;
p.visited = true;
setVertexColor(p, color);
Set<DefaultEdge> incomingEdges = graph.incomingEdgesOf(p);
Set<DefaultEdge> outgoingEdges = graph.outgoingEdgesOf(p);
for(DefaultEdge inEdge : incomingEdges)
{
colorComponent(graph.getEdgeSource(inEdge), color);
}
for(DefaultEdge outEdge : outgoingEdges)
{
colorComponent(graph.getEdgeTarget(outEdge), color);
}
}
private void findConnectedComponents()
{
java.util.Set<Position> vSet = graph.vertexSet();
int componentIndex = 0;
for(Position p: vSet)
{
if(!p.visited)
{
Color color = new Color(randGen.nextInt());
colorComponent(p, color);
}
}
}*/
// private void colorSCC (ArrayList<Position> vertexSet, Color color)
private void colorSCC ()
{
/*
for (Position p:vertexSet)
{
setVertexColor (p, color);
}*/
java.util.List <Set <Position>> sccs = new ConnectivityInspector<Position, DefaultEdge>(graph).connectedSets ();
for (Set <Position> s:sccs) {
Color color = new Color (randGen.nextInt ());
for (Position p:s) {
setVertexColor (p, color);
}
}
}
private void setVertexColor (Object vertex, Color color)
{
DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
AttributeMap attr = cell.getAttributes( );
GraphConstants.setBackground(attr, color);
AttributeMap cellAttr = new AttributeMap();
cellAttr.put( cell, attr );
m_jgAdapter.edit (cellAttr, null, null, null);
}
private void positionVertexAt( Object vertex, int x, int y )
{
DefaultGraphCell cell = m_jgAdapter.getVertexCell( vertex );
AttributeMap attr = cell.getAttributes( );
Rectangle2D b = GraphConstants.getBounds( attr );
Rectangle2D newBounds = new Rectangle2D.Double(x,y, 5, 5);
GraphConstants.setBounds( attr, newBounds);
AttributeMap cellAttr = new AttributeMap();
cellAttr.put( cell, attr );
m_jgAdapter.edit (cellAttr, null, null, null);
}
}