@Override public Graph<V, E> create() { return new SparseGraph<V, E>(); } };
public boolean isSource(V vertex, E edge) { if (!containsVertex(vertex) || !containsEdge(edge)) return false; V source = getSource(edge); if (source != null) return source.equals(vertex); else return false; }
@Override public boolean isDest(V vertex, E edge) { if (!containsVertex(vertex) || !containsEdge(edge)) { return false; } V dest = getDest(edge); if (dest != null) { return dest.equals(vertex); } return false; }
public boolean removeVertex(V vertex) { if (!containsVertex(vertex)) return false; // copy to avoid concurrent modification in removeEdge Collection<E> incident = new ArrayList<E>(getIncidentEdges(vertex)); for (E edge : incident) removeEdge(edge); vertex_maps.remove(vertex); return true; }
@Override public void updateLayout(Graph graph) { final Layout graphLayout = graph.getLayout(); SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>(); Collection<? extends Vertex> vertices = graph.getDisplayVertices(); for(Vertex v : vertices) { jungGraph.addVertex(v); } Collection<? extends Edge> edges = graph.getDisplayEdges(); for(Edge e : edges) { jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex()); } Dimension size = selectLayoutSize(graph); Dimension paddedSize = new Dimension((int)(size.getWidth()*.75), (int)(size.getHeight()*.75)); doISOMLayout(graphLayout, jungGraph, size); doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION); doFRLayout(graphLayout, jungGraph, paddedSize, (int)(size.getWidth()/8.0), (int)(size.getHeight()/8.0)); doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION); }
@Override public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints); if (new_endpoints == null) return false; E connection = findEdge(v1, v2); if (connection != null && getEdgeType(connection) == edgeType) return false; if (!containsVertex(v1)) this.addVertex(v1); if (!containsVertex(v2)) this.addVertex(v2);
@Override public E findEdge(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) return null; E edge = vertex_maps.get(v1)[OUTGOING].get(v2); if (edge == null) edge = vertex_maps.get(v1)[INCIDENT].get(v2); return edge; }
@Override public boolean removeEdge(E edge) { if (!containsEdge(edge)) { return false; } Pair<V> endpoints = getEndpoints(edge); V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); // remove edge from incident vertices' adjacency maps if (getEdgeType(edge) == EdgeType.DIRECTED) { vertex_maps.get(v1)[OUTGOING].remove(v2); vertex_maps.get(v2)[INCOMING].remove(v1); directed_edges.remove(edge); } else { vertex_maps.get(v1)[INCIDENT].remove(v2); vertex_maps.get(v2)[INCIDENT].remove(v1); undirected_edges.remove(edge); } return true; }
public V getSource(E directed_edge) { if (getEdgeType(directed_edge) == EdgeType.DIRECTED) return directed_edges.get(directed_edge).getFirst(); else return null; }
@Override public void updateLayout(Graph graph) { final Layout graphLayout = graph.getLayout(); SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>(); Collection<? extends Vertex> vertices = graph.getDisplayVertices(); for(Vertex v : vertices) { jungGraph.addVertex(v); } Collection<? extends Edge> edges = graph.getDisplayEdges(); for(Edge e : edges) { jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex()); } Dimension size = selectLayoutSize(graph); Dimension paddedSize = new Dimension((int)(size.getWidth()*.75), (int)(size.getHeight()*.75)); doISOMLayout(graphLayout, jungGraph, size); doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION); doFRLayout(graphLayout, jungGraph, paddedSize, (int)(size.getWidth()/8.0), (int)(size.getHeight()/8.0)); doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION); }
@Override public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) { Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints); if (new_endpoints == null) { return false; E connection = findEdge(v1, v2); if (connection != null && getEdgeType(connection) == edgeType) { return false; if (!containsVertex(v1)) { this.addVertex(v1); if (!containsVertex(v2)) { this.addVertex(v2);
@Override public boolean removeVertex(V vertex) { if (!containsVertex(vertex)) { return false; } // copy to avoid concurrent modification in removeEdge Collection<E> incident = new ArrayList<E>(getIncidentEdges(vertex)); for (E edge : incident) { removeEdge(edge); } vertex_maps.remove(vertex); return true; }
@Override public E findEdge(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) { return null; } E edge = vertex_maps.get(v1)[OUTGOING].get(v2); if (edge == null) { edge = vertex_maps.get(v1)[INCIDENT].get(v2); } return edge; }
public boolean removeEdge(E edge) { if (!containsEdge(edge)) return false; Pair<V> endpoints = getEndpoints(edge); V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); // remove edge from incident vertices' adjacency maps if (getEdgeType(edge) == EdgeType.DIRECTED) { vertex_maps.get(v1)[OUTGOING].remove(v2); vertex_maps.get(v2)[INCOMING].remove(v1); directed_edges.remove(edge); } else { vertex_maps.get(v1)[INCIDENT].remove(v2); vertex_maps.get(v2)[INCIDENT].remove(v1); undirected_edges.remove(edge); } return true; }
public V getDest(E directed_edge) { if (getEdgeType(directed_edge) == EdgeType.DIRECTED) return directed_edges.get(directed_edge).getSecond(); else return null; }
@Override public void updateLayout(final Graph graph) { final Layout graphLayout = graph.getLayout(); SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>(); Collection<Vertex> vertices = graph.getDisplayVertices(); for(Vertex v : vertices) { jungGraph.addVertex(v); } Collection<Edge> edges = graph.getDisplayEdges(); for(Edge e : edges) { jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex()); } TopoFRLayout<VertexRef, EdgeRef> layout = new TopoFRLayout<VertexRef, EdgeRef>(jungGraph); // Initialize the vertex positions to the last known positions from the layout Dimension size = selectLayoutSize(graph); layout.setInitializer(initializer(graphLayout, (int)size.getWidth()/2, (int)size.getHeight()/2)); // Resize the graph to accommodate the number of vertices layout.setSize(size); while(!layout.done()) { layout.step(); } // Store the new positions in the layout for(Vertex v : vertices) { graphLayout.setLocation(v, new Point(layout.getX(v) - (size.getWidth()/2), (int)layout.getY(v) - (size.getHeight()/2))); } }
public Graph<V,E> get() { return new SparseGraph<V,E>(); } };
@Override public Collection<E> getOutEdges(V vertex) { if (!containsVertex(vertex)) { return null; } // combine directed outedges and undirected Collection<E> out = new HashSet<E>( vertex_maps.get(vertex)[OUTGOING].values()); out.addAll(vertex_maps.get(vertex)[INCIDENT].values()); return Collections.unmodifiableCollection(out); }
@Override public boolean isSource(V vertex, E edge) { if (!containsVertex(vertex) || !containsEdge(edge)) { return false; } V source = getSource(edge); if (source != null) { return source.equals(vertex); } return false; }
public boolean isDest(V vertex, E edge) { if (!containsVertex(vertex) || !containsEdge(edge)) return false; V dest = getDest(edge); if (dest != null) return dest.equals(vertex); else return false; }