Package edu.ucla.sspace.graph

Examples of edu.ucla.sspace.graph.SparseDirectedGraph$SubgraphAdaptor


    @Override public DirectedGraph<DirectedEdge> readDirected(File f, Indexer<String> vertexIndexer)
            throws IOException {       

        BufferedReader br = new BufferedReader(new FileReader(f));
        DirectedGraph<DirectedEdge> g = new SparseDirectedGraph();
        int lineNo = 0;
        for (String line = null; (line = br.readLine()) != null; ) {
            ++lineNo;
            line = line.trim();
            if (line.startsWith("#"))
                continue;
            else if (line.length() == 0)
                continue;
            String[] arr = line.split("\\s+");
            if (arr.length < 2) {
                throw new IOException("Missing vertex on line " + lineNo);
            }
            int v1 = vertexIndexer.index(arr[0]);
            int v2 = vertexIndexer.index(arr[1]);
            g.add(new SimpleDirectedEdge(v1, v2));
            if (lineNo % 100000 == 0)
                veryVerbose(LOGGER, "read %d lines from %s", lineNo, f);
        }
        verbose(LOGGER, "Read directed graph with %d vertices and %d edges",
                g.order(), g.size());
        return g;
    }
View Full Code Here


    public PajekIO(Indexer<String> vertexIndex) {
        this.vertexIndex = vertexIndex;
    }

    public static Graph<DirectedEdge> readPajek(File f) throws IOException {
        Graph<DirectedEdge> g = new SparseDirectedGraph();
        int lineNo = 0;
        boolean seenVertices = false;
        boolean seenEdges = false;
        Map<String,Integer> labelToVertex = new HashMap<String,Integer>();

        for (String line : new LineReader(f)) {
            ++lineNo;
            // Skip comments and blank lines
            if (line.matches("\\s*%.*") || line.matches("\\s+"))
                continue;
            else if (line.startsWith("*vertices")) {
                if (seenVertices) {
                    throw new IOException("Duplicate vertices definiton on " +
                                          "line " + lineNo);
                }
                String[] arr = line.split("\\s+");
                if (arr.length < 2)
                    throw new IOException("Missing specification of how many " +
                                          "vertices");
                int numVertices = -1;
                try {
                    numVertices = Integer.parseInt(arr[1]);
                } catch (NumberFormatException nfe) {
                    throw new IOException("Invalid number of vertices: " +
                                          arr[1], nfe);
                }
                if (numVertices < 1)
                    throw new IOException("Must have at least one vertex");

                // Add the vertices to the graph
                for (int i = 0; i < numVertices; ++i)
                    g.add(i);

                seenVertices = true;
            }
            else if (line.startsWith("*edges")
                     || line.startsWith("*arcs")) {
                if (!seenVertices)
                    throw new IOException("Must specify vertices before edges");
                if (seenEdges)
                    throw new IOException("Duplicate edges definition on line"
                                          + lineNo);
                seenEdges = true;
            }
            // If the edges flag is true all subsequent lines should be an edge
            // specifaction
            else if (seenEdges) {
                String[] arr = line.split("\\s+");
                if (arr.length < 2)
                    throw new IOException("Missing vertex declaration(s) for " +
                                          "edge definition: " + line);
                int v1 = -1;
                int v2 = -1;
                try {
                    v1 = Integer.parseInt(arr[0]);
                    v2 = Integer.parseInt(arr[1]);
                } catch (NumberFormatException nfe) {
                    throw new IOException("Invalid vertex value: " + line, nfe);
                }
                g.add(new SimpleDirectedEdge(v1, v2));
            }
            else if (seenVertices) {
                // Handle labels here?
            }
            else
View Full Code Here

    public Graph readGraph(File dotFile) {
        return null;
    }

    public DirectedGraph<DirectedEdge> readDirectedGraph(File dotFile) {
        DirectedGraph<DirectedEdge> g = new SparseDirectedGraph();
        // REMINDER: add a lot more error checking
        for (String line : new LineReader(dotFile)) {
            // System.out.println(line);
            Matcher m = DIRECTED_EDGE.matcher(line);
            while (m.find()) {
                String from = m.group(1);
                String to = m.group(2);
                //String meta = m.group(3);
                // System.out.printf("%s -> %s (%s)%n", from, to, meta);
                g.add(new SimpleDirectedEdge(indexer.index(from),
                                             indexer.index(to)));
            }
        }
        return g;
    }
View Full Code Here

TOP

Related Classes of edu.ucla.sspace.graph.SparseDirectedGraph$SubgraphAdaptor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.