source: branches/localSpectral/src/data/GraphBuilder.java @ 38

Last change on this file since 38 was 36, checked in by toshi, 14 years ago

ultimo commit

File size: 1.8 KB
RevLine 
[27]1
2package data;
3
[36]4import GraphType.DirectedDenseGraph;
[32]5import clustering.Edge;
[34]6import edu.uci.ics.jung.graph.DirectedSparseGraph;
[27]7import edu.uci.ics.jung.graph.Graph;
8import edu.uci.ics.jung.graph.SparseGraph;
9import java.io.BufferedReader;
10import java.io.DataInputStream;
11import java.io.FileInputStream;
12import java.io.FileNotFoundException;
13import java.io.IOException;
14import java.io.InputStreamReader;
15import java.util.Arrays;
16import java.util.HashSet;
17import java.util.Set;
18
19
[32]20public class GraphBuilder {
[27]21
[36]22    Graph<String,Integer> graph;
[27]23
[34]24    public GraphBuilder(boolean directed){
25        if (directed)
[36]26            graph = new DirectedDenseGraph<String, Integer>();
27           //graph = new DirectedSparseGraph<String, Integer>();
[27]28
[34]29        else
[36]30            graph = new SparseGraph<String, Integer>();
[27]31    }
32
[36]33    public Graph<String, Integer> getGraph() {
[27]34        return graph;
35    }
36
[36]37    public void buildGraphFromCVS(String path, int maxreadline){
[27]38        try {
39            FileInputStream fstream = new FileInputStream(path);
40            DataInputStream in = new DataInputStream(fstream);
41            BufferedReader br = new BufferedReader(new InputStreamReader(in));
42
43            String read;
44            int edge=0;
[36]45            String[] splitted;
46            while((read = br.readLine()) != null){
47                if(!read.trim().isEmpty()){
48                    splitted = read.trim().split(",");
49                    graph.addEdge(edge, splitted[0], splitted[1]);
[27]50                    edge++;
51                }
52            }
53            br.close();
54            in.close();
55            fstream.close();
56
57        } catch (FileNotFoundException e) {
58            System.out.println("<GraphBuilder> Error: file not found!");
59        } catch(IOException e){
60            System.out.println("<GraphBuilder> Error: closing FileInputStream");
61        }
62    }
63
64}
Note: See TracBrowser for help on using the repository browser.