protected Graph<String, DefaultWeightedEdge> createWithBias(boolean negate) { Graph<String, DefaultWeightedEdge> g; double bias = 1; if (negate) { // negative-weight edges are being tested, so only a directed graph // makes sense g = new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class); bias = -1; } else { // by default, use an undirected graph g = new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class); } g.addVertex(V1); g.addVertex(V2); g.addVertex(V3); g.addVertex(V4); g.addVertex(V5); e12 = Graphs.addEdge(g, V1, V2, bias * 2); e13 = Graphs.addEdge(g, V1, V3, bias * 3); e24 = Graphs.addEdge(g, V2, V4, bias * 5); e34 = Graphs.addEdge(g, V3, V4, bias * 20); e45 = Graphs.addEdge(g, V4, V5, bias * 5); e15 = Graphs.addEdge(g, V1, V5, bias * 100); return g; }
/** * Generates AdjacencyListGraph based on the values in the input file. * * @requires twitterStream is properly initialized * @param twitterStream initialized input stream for reading twitter file * @return generated graph based on input file */ private static Graph readTwitterFile(FileInputStream twitterStream) { final int U1_INDEX = 0; final int U2_INDEX = 1; try { Graph g = new AdjacencyListGraph(); BufferedReader twitterReader = new BufferedReader(new InputStreamReader(twitterStream)); String line; while ((line = twitterReader.readLine()) != null) { // eliminate any unnecessary whitespace String[] columns = line.trim().replaceAll("\\s+", "").split("->"); // first column is user 1 // second column is user 2 Vertex u1 = new Vertex(columns[U1_INDEX]); Vertex u2 = new Vertex(columns[U2_INDEX]); // System.out.println(columns[0]+","+columns[1]); g.addVertex(u1); g.addVertex(u2); g.addEdge(u1, u2); // System.out.println(line); } twitterReader.close(); return g; } catch (Exception e) { // if something somehow goes wrong throw new RuntimeException(e); } }
public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] input = in.readLine().split(" "); int n = Integer.parseInt(input[0]); int m = Integer.parseInt(input[1]); Graph<Integer> g = new Graph<>(); input = in.readLine().split(" "); int[] cats = new int[n + 1]; for (int i = 1; i <= n; i++) { cats[i] = Integer.parseInt(input[i - 1]); Vertex<Integer> v = new Vertex<>(Integer.toString(i), cats[i]); g.addVertex(v); } for (int i = 1; i < n; i++) { input = in.readLine().split(" "); int x = Integer.parseInt(input[0]); int y = Integer.parseInt(input[1]); Vertex<Integer> from = g.findVertexByName(Integer.toString(x)); Vertex<Integer> to = g.findVertexByName(Integer.toString(y)); g.insertBiEdge(from, to, 0); } BFS_Visit(g.findVertexByName("1")); int count = 0; for (int i = 1; i <= n; i++) { Vertex<Integer> vertex = g.findVertexByName(Integer.toString(i)); if (vertex.getOutgoingEdgeCount() == 1 && i != 1) { int maxi = 0; while (vertex.getParent() != null) { maxi = Math.max(maxi, vertex.getData()); // System.out.print(vertex.getName() + " "); vertex = vertex.getParent(); } // System.out.println(); maxi = Math.max(maxi, vertex.getData()); if (maxi <= m) count++; } } System.out.println(count); }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // Add all the vertices to the set for (int i = 0; i < size; i++) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); } /* * We want two iterators over the vertex set, one fast and one slow. * The slow one will move through the set once. For each vertex, * the fast iterator moves through the set, adding an edge to all * vertices we haven't connected to yet. * * If we have an undirected graph, the second addEdge call will return * nothing; it will not add a second edge. */ Iterator<V> slowI = target.vertexSet().iterator(); Iterator<V> fastI; while (slowI.hasNext()) { // While there are more vertices in the set V latestVertex = slowI.next(); fastI = target.vertexSet().iterator(); // Jump to the first vertex *past* latestVertex while (fastI.next() != latestVertex) {; } // And, add edges to all remaining vertices V temp; while (fastI.hasNext()) { temp = fastI.next(); target.addEdge(latestVertex, temp); target.addEdge(temp, latestVertex); } } }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, final VertexFactory<V> vertexFactory, Map<String, V> resultMap) { if (size < 1) { return; } // A little trickery to intercept the rim generation. This is // necessary since target may be initially non-empty, meaning we can't // rely on its vertex set after the rim is generated. final Collection<V> rim = new ArrayList<V>(); VertexFactory<V> rimVertexFactory = new VertexFactory<V>() { public V createVertex() { V vertex = vertexFactory.createVertex(); rim.add(vertex); return vertex; } }; RingGraphGenerator<V, E> ringGenerator = new RingGraphGenerator<V, E>(size - 1); ringGenerator.generateGraph(target, rimVertexFactory, resultMap); V hubVertex = vertexFactory.createVertex(); target.addVertex(hubVertex); if (resultMap != null) { resultMap.put(HUB_VERTEX, hubVertex); } for (V rimVertex : rim) { if (inwardSpokes) { target.addEdge(rimVertex, hubVertex); } else { target.addEdge(hubVertex, rimVertex); } } }
/** {@inheritDoc} */ public void generateGraph( Graph<V, E> target, VertexFactory<V> vertexFactory, Map<String, V> resultMap) { V lastVertex = null; for (int i = 0; i < size; ++i) { V newVertex = vertexFactory.createVertex(); target.addVertex(newVertex); if (lastVertex == null) { if (resultMap != null) { resultMap.put(START_VERTEX, newVertex); } } else { target.addEdge(lastVertex, newVertex); } lastVertex = newVertex; } if ((resultMap != null) && (lastVertex != null)) { resultMap.put(END_VERTEX, lastVertex); } }
public void addVertex(Vertex<String> v) { super.addVertex(v); vertices.put(v.getWeight(), v); }