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

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

ultimo commit

File size: 1.8 KB
Line 
1
2package data;
3
4import GraphType.DirectedDenseGraph;
5import clustering.Edge;
6import edu.uci.ics.jung.graph.DirectedSparseGraph;
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
20public class GraphBuilder {
21
22    Graph<String,Integer> graph;
23
24    public GraphBuilder(boolean directed){
25        if (directed)
26            graph = new DirectedDenseGraph<String, Integer>();
27           //graph = new DirectedSparseGraph<String, Integer>();
28
29        else
30            graph = new SparseGraph<String, Integer>();
31    }
32
33    public Graph<String, Integer> getGraph() {
34        return graph;
35    }
36
37    public void buildGraphFromCVS(String path, int maxreadline){
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;
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]);
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.