-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
146 lines (114 loc) · 3.03 KB
/
Graph.java
File metadata and controls
146 lines (114 loc) · 3.03 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
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
public class Graph
{
public int nbNoeud;
public int nbAretes;
public int maxDegree;
public int maxNoeudId;
private int[] from,to;
public int[][] adjacence;
private static final int BUFFER_SIZE = 16384;
public Graph(String fname, int estimNbAretes)
{
nbNoeud = -1;
nbAretes = -1;
maxDegree = -1;
maxNoeudId = -1;
readFile(fname,estimNbAretes);
mem();
this.adjacence = new int[maxNoeudId][];
System.out.println("n="+this.maxNoeudId);
System.out.println("m="+this.nbAretes);
buildAdjacence();
mem();
System.out.println("degmax=" + this.maxDegree);
}
public int distance(int depart, int arrive){
//breadFirstAlgorithm
return -1;
}
public int[] neighbors(int noeud){
return this.adjacence[noeud];
}
private void buildAdjacence(){
for(int i = 0; i < this.nbAretes; i++){
if(this.adjacence[from[i]] == null){
this.adjacence[from[i]] = new int[]{to[i]};
}else{
int[] temp = new int[this.adjacence[from[i]].length + 1];
// copy
int j;
for(j=0; j < this.adjacence[from[i]].length; j++) temp[j] = this.adjacence[from[i]][j];
// add last
temp[j] = to[i];
this.adjacence[from[i]] = temp;
}
if( this.adjacence[from[i]].length > this.maxDegree){
this.maxDegree = this.adjacence[from[i]].length;
}
if(this.adjacence[to[i]] == null){
this.adjacence[to[i]] = new int[]{from[i]};
}else{
int[] temp = new int[this.adjacence[to[i]].length + 1];
// copy
int j;
for(j=0; j < this.adjacence[to[i]].length; j++) temp[j] = this.adjacence[to[i]][j];
// add last
temp[j] = from[i];
this.adjacence[to[i]] = temp;
}
if( this.adjacence[to[i]].length > this.maxDegree){
this.maxDegree = this.adjacence[to[i]].length;
}
}
}
private void readFile(String fname, int estimNbAretes)
{
from = new int[estimNbAretes];
to = new int[estimNbAretes];
try
{
int cpt =0;
//File myObj = new File(fname);
//Scanner myReader = new Scanner(myObj);
BufferedReader reader = new BufferedReader(new FileReader(fname), BUFFER_SIZE);
String line;
while ((line = reader.readLine()) != null && cpt < estimNbAretes ) {
String[] data = line.split("\t+", 2);
if( data[0].contains("#")) {
continue;
}
this.nbAretes++;
from[cpt] = Integer.valueOf(data[0]);
to[cpt] = Integer.valueOf(data[1]);
if(from[cpt] > this.maxNoeudId )
{
this.maxNoeudId = from[cpt];
}
if(to[cpt] > this.maxNoeudId )
{
this.maxNoeudId = to[cpt];
}
cpt ++;
}
this.nbAretes++;
this.maxNoeudId++;
reader.close();
} catch (Exception e)
{
System.out.println("Error");
e.printStackTrace();
}
this.nbNoeud = this.maxNoeudId;
}
public static void mem() {
Runtime rt = Runtime.getRuntime();
rt.gc();
System.err.println("Allocated memory : "+ (rt.totalMemory() - rt.freeMemory()) / 1000000 + " Mb");
System.err.flush();
}
}