constructor

constructor

| 공개

Graph(String input1, String input2){

nodeList = new ArrayList<Node>();

try{

/* file I/O part

you open the node_info.txt, edge_info.txt files in java

read the input data line by line, and separate line by split() function.

allocate memory to each Node. input data is from node_info.txt

allocate memory to each Node's adjList. input data is from edge_info.txt

Notice: Be careful if you put data in variable with right data structure.

Example)

HashMap<Integer, Integer> a = new HashMap<Integer, Integer>();

a.put(1,"1"); --> wrong!

The second value seems to be integer, but actually it is String.

You should use parse function to convert String to int.

*/

//TODO: Construction of Graph

//I made <Start>

File file1 = new File(input1);

FileReader fr1 = new FileReader(file1);

BufferedReader br1 = new BufferedReader(fr1);

String line1;

while((line1 = br1.readLine()) != null){

String[] str1 = line1.split("\t");

String nodeName = str1[1];//Column2 is distict name

int nodeId = Integer.parseInt(str1[0]);//Column1 is node ID

Node n = new Node(nodeName,nodeId);//To create nodes(0~24)

nodeList.add(nodeId,n);//Add created nodes to 'nodeList'

}

File file2 = new File(input2);

FileReader fr2 = new FileReader(file2);

BufferedReader br2 = new BufferedReader(fr2);

String line2;

while((line2 = br2.readLine()) != null){

String[] str2 = line2.split("\t");

Node node1 = nodeList.get( Integer.parseInt(str2[0]) );

Node node2 = nodeList.get( Integer.parseInt(str2[1]) );

double dist = Double.parseDouble(str2[2]);

node1.adjList.put(node2.id,dist);

node2.adjList.put(node1.id,dist);

}

//I made <End>

}catch(IOException e){}

}

 

댓글

댓글 본문
graphittie 자세히 보기