@Test public void testGetComponentsForMultipleComponent() { String queryString = "" + "(v1)-[e1]->(v2)" + "(v2)<-[e2]-(v3)" + "(v2)-[e3]->(v1)" + "(v3)-[e4]->(v3)" + " " + "(v4)-[e5]->(v5)" + "(v6)-[e6]->(v5)"; QueryHandler query = new QueryHandler(queryString); Map<Integer, Set<String>> components = GraphMetrics.getComponents(query); assertEquals(2, components.size()); assertTrue(components.containsKey(0)); assertTrue(components.containsKey(1)); assertEquals(Sets.newHashSet("v1", "v2", "v3"), components.get(0)); assertEquals(Sets.newHashSet("v4", "v5", "v6"), components.get(1)); }
public static Map<Integer, Set<String>> getComponents(QueryHandler graph) { int nextComponentId = 0; List<Vertex> vertices = new ArrayList<>(graph.getVertices()); Map<Integer, Set<String>> mapping = new HashMap<>(); while (vertices.size() != 0) { int componentId = nextComponentId++; List<Long> nextVertices = Lists.newArrayList(vertices.remove(0).getId()); mapping.put( componentId, Sets.newHashSet(graph.getVertexById(nextVertices.get(0)).getVariable()) ); while (nextVertices.size() != 0) { List<Long> currentVertices = nextVertices; nextVertices = new ArrayList<>(); for (Long vertexId : currentVertices) { nextVertices.addAll( graph.getEdges() .stream() .filter(edge -> edge.getSourceVertexId().equals(vertexId) || edge.getTargetVertexId().equals(vertexId) ) .map(edge -> edge.getSourceVertexId().equals(vertexId) ? edge.getTargetVertexId() : edge.getSourceVertexId() ).filter(id -> vertices.removeIf(v -> v.getId() == id)) .collect(Collectors.toList()) ); } mapping.get(componentId).addAll( nextVertices .stream() .map(id -> graph.getVertexById(id).getVariable()) .collect(Collectors.toList()) ); } } return mapping; }
GraphMetrics { public static Map<Integer, Set<String>> getComponents(QueryHandler graph) { int nextComponentId = 0; List<Vertex> vertices = new ArrayList<>(graph.getVertices()); Map<Integer, Set<String>> mapping = new HashMap<>(); while (vertices.size() != 0) { int componentId = nextComponentId++; List<Long> nextVertices = Lists.newArrayList(vertices.remove(0).getId()); mapping.put( componentId, Sets.newHashSet(graph.getVertexById(nextVertices.get(0)).getVariable()) ); while (nextVertices.size() != 0) { List<Long> currentVertices = nextVertices; nextVertices = new ArrayList<>(); for (Long vertexId : currentVertices) { nextVertices.addAll( graph.getEdges() .stream() .filter(edge -> edge.getSourceVertexId().equals(vertexId) || edge.getTargetVertexId().equals(vertexId) ) .map(edge -> edge.getSourceVertexId().equals(vertexId) ? edge.getTargetVertexId() : edge.getSourceVertexId() ).filter(id -> vertices.removeIf(v -> v.getId() == id)) .collect(Collectors.toList()) ); } mapping.get(componentId).addAll( nextVertices .stream() .map(id -> graph.getVertexById(id).getVariable()) .collect(Collectors.toList()) ); } } return mapping; } }
GraphMetrics { public static Map<Integer, Set<String>> getComponents(QueryHandler graph) { int nextComponentId = 0; List<Vertex> vertices = new ArrayList<>(graph.getVertices()); Map<Integer, Set<String>> mapping = new HashMap<>(); while (vertices.size() != 0) { int componentId = nextComponentId++; List<Long> nextVertices = Lists.newArrayList(vertices.remove(0).getId()); mapping.put( componentId, Sets.newHashSet(graph.getVertexById(nextVertices.get(0)).getVariable()) ); while (nextVertices.size() != 0) { List<Long> currentVertices = nextVertices; nextVertices = new ArrayList<>(); for (Long vertexId : currentVertices) { nextVertices.addAll( graph.getEdges() .stream() .filter(edge -> edge.getSourceVertexId().equals(vertexId) || edge.getTargetVertexId().equals(vertexId) ) .map(edge -> edge.getSourceVertexId().equals(vertexId) ? edge.getTargetVertexId() : edge.getSourceVertexId() ).filter(id -> vertices.removeIf(v -> v.getId() == id)) .collect(Collectors.toList()) ); } mapping.get(componentId).addAll( nextVertices .stream() .map(id -> graph.getVertexById(id).getVariable()) .collect(Collectors.toList()) ); } } return mapping; }  }
GraphMetrics { public static Map<Integer, Set<String>> getComponents(QueryHandler graph) { int nextComponentId = 0; List<Vertex> vertices = new ArrayList<>(graph.getVertices()); Map<Integer, Set<String>> mapping = new HashMap<>(); while (vertices.size() != 0) { int componentId = nextComponentId++; List<Long> nextVertices = Lists.newArrayList(vertices.remove(0).getId()); mapping.put( componentId, Sets.newHashSet(graph.getVertexById(nextVertices.get(0)).getVariable()) ); while (nextVertices.size() != 0) { List<Long> currentVertices = nextVertices; nextVertices = new ArrayList<>(); for (Long vertexId : currentVertices) { nextVertices.addAll( graph.getEdges() .stream() .filter(edge -> edge.getSourceVertexId().equals(vertexId) || edge.getTargetVertexId().equals(vertexId) ) .map(edge -> edge.getSourceVertexId().equals(vertexId) ? edge.getTargetVertexId() : edge.getSourceVertexId() ).filter(id -> vertices.removeIf(v -> v.getId() == id)) .collect(Collectors.toList()) ); } mapping.get(componentId).addAll( nextVertices .stream() .map(id -> graph.getVertexById(id).getVariable()) .collect(Collectors.toList()) ); } } return mapping; }  static int getDiameter(QueryHandler graph); static int getRadius(QueryHandler graph); static Map<Long, Integer> getEccentricity(QueryHandler graph); static Integer getEccentricity(QueryHandler graph, Vertex v); static Map<Integer, Set<String>> getComponents(QueryHandler graph); }
GraphMetrics { public static Map<Integer, Set<String>> getComponents(QueryHandler graph) { int nextComponentId = 0; List<Vertex> vertices = new ArrayList<>(graph.getVertices()); Map<Integer, Set<String>> mapping = new HashMap<>(); while (vertices.size() != 0) { int componentId = nextComponentId++; List<Long> nextVertices = Lists.newArrayList(vertices.remove(0).getId()); mapping.put( componentId, Sets.newHashSet(graph.getVertexById(nextVertices.get(0)).getVariable()) ); while (nextVertices.size() != 0) { List<Long> currentVertices = nextVertices; nextVertices = new ArrayList<>(); for (Long vertexId : currentVertices) { nextVertices.addAll( graph.getEdges() .stream() .filter(edge -> edge.getSourceVertexId().equals(vertexId) || edge.getTargetVertexId().equals(vertexId) ) .map(edge -> edge.getSourceVertexId().equals(vertexId) ? edge.getTargetVertexId() : edge.getSourceVertexId() ).filter(id -> vertices.removeIf(v -> v.getId() == id)) .collect(Collectors.toList()) ); } mapping.get(componentId).addAll( nextVertices .stream() .map(id -> graph.getVertexById(id).getVariable()) .collect(Collectors.toList()) ); } } return mapping; }  static int getDiameter(QueryHandler graph); static int getRadius(QueryHandler graph); static Map<Long, Integer> getEccentricity(QueryHandler graph); static Integer getEccentricity(QueryHandler graph, Vertex v); static Map<Integer, Set<String>> getComponents(QueryHandler graph);  }