Repository navigation
Expand file tree
/
Copy pathReadGraphClass.java
More file actions
148 lines (113 loc) · 3.69 KB
/
Copy pathReadGraphClass.java
File metadata and controls
148 lines (113 loc) · 3.69 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
package application;
import java.io.*;
class ColEdge2 {
int u;
int v;
}
public class ReadGraphClass {
public final static boolean DEBUG = true;
public final static String COMMENT = "//";
public static String args;
public static int vertices;
public static int edges;
public ReadGraphClass(String file) {
args = file;
}
public int getVertices() {
return vertices;
}
public int getEdges() {
return edges;
}
public static int[][] ReadFile() {
if( args.length() < 1 ) {
System.out.println("Error! No filename specified.");
System.exit(0);
}
String inputfile = args;
boolean seen[] = null;
// n is the number of vertices in the graph
int n = -1;
// m is the number of edges in the graph
int m = -1;
// e will contain the edges of the graph
ColEdge2 e[] = null;
try {
FileReader fr = new FileReader(inputfile);
BufferedReader br = new BufferedReader(fr);
String record = new String();
// THe first few lines of the file are allowed to be comments, staring with a // symbol.
// These comments are only allowed at the top of the file.
// -----------------------------------------
while ((record = br.readLine()) != null) {
if( record.startsWith("//") ) {
continue;
}
break; // Saw a line that did not start with a comment -- time to start reading the data in!
}
if( record.startsWith("VERTICES = ") ) {
n = Integer.parseInt( record.substring(11) );
//if(DEBUG) System.out.println(COMMENT + " Number of vertices = "+n);
vertices=n;
}
seen = new boolean[n+1];
record = br.readLine();
if( record.startsWith("EDGES = ") ) {
m = Integer.parseInt( record.substring(8) );
//if(DEBUG) System.out.println(COMMENT + " Expected number of edges = "+m);
edges=m;
}
e = new ColEdge2[m];
for( int d=0; d<m; d++) {
//if(DEBUG) System.out.println(COMMENT + " Reading edge "+(d+1));
record = br.readLine();
String data[] = record.split(" ");
if( data.length != 2 ) {
System.out.println("Error! Malformed edge line: " + record);
System.exit(0);
}
e[d] = new ColEdge2();
e[d].u = Integer.parseInt(data[0]);
e[d].v = Integer.parseInt(data[1]);
seen[ e[d].u ] = true;
seen[ e[d].v ] = true;
//if(DEBUG) System.out.println(COMMENT + " Edge: "+ e[d].u +" "+e[d].v);
}
br.close();
}
catch (IOException ex) {
// catch possible io errors from readLine()
System.out.println("Error! Problem reading file " + inputfile);
System.exit(0);
}
for( int x=1; x<=n; x++ ) {
if( seen[x] == false ) {
if(DEBUG) System.out.println(COMMENT + " Warning: vertex "+x+" didn't appear in any edge : it will be considered a disconnected vertex on its own.");
}
}
/*
* At this point e[0] will be the first edge, with e[0].u referring to one endpoint and e[0].v to the other
* e[1] will be the second edge...
* (and so on)
* e[m-1] will be the last edge
*
* there will be n vertices in the graph, numbered 1 to ns
*/
int[] connect = new int[n];
for (int i = 0; i < m; i++) {
connect[e[i].u-1]++;
connect[e[i].v-1]++;
}
int[][] graph = new int[n][n];
fillMatrix(graph,e,n,m);
return graph;
}
private static void fillMatrix(int[][] matrix, ColEdge2 e[], int n, int m) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n ;j++) {
matrix[e[i].u-1][e[i].v-1] = 1;
matrix[e[i].v-1][e[i].u-1] = 1;
}
}
}
}