@Override public UndirectedGraph<V, E> create() { return new UndirectedSparseGraph<V, E>(); } };
@Override public E findEdge(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) { return null; } return vertices.get(v1).get(v2); }
@Override public Collection<E> findEdgeSet(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) return null; ArrayList<E> edge_collection = new ArrayList<E>(1); // if (!containsVertex(v1) || !containsVertex(v2)) // return edge_collection; E e = findEdge(v1, v2); if (e == null) return edge_collection; edge_collection.add(e); return edge_collection; }
public boolean removeVertex(V vertex) { if (!containsVertex(vertex)) return false; // iterate over copy of incident edge collection for (E edge : new ArrayList<E>(vertices.get(vertex).values())) removeEdge(edge); vertices.remove(vertex); return true; }
/** * Calculate and return the minimum spanning free of the Graph top. * @param topo - the graph. * @return the "pruned" minimal spanning tree. */ private Graph<String, Link> calcMinimumSpanningTree(List<String> fabrics) { Topology topo = this.getFabricTopology(); if (topo == null) { LOG.error("Failed to read Fabric Topology!"); return null; } UndirectedSparseGraph<String, Link> graph = new UndirectedSparseGraph<>(); for (Node node : topo.getNode()) { graph.addVertex(node.getNodeId().getValue()); } if (topo.getLink() != null) for (Link link : topo.getLink()) { graph.addEdge(link, link.getSource().getSourceNode().getValue(), link.getDestination().getDestNode().getValue()); } PrimMinimumSpanningTree<String, Link> alg = new PrimMinimumSpanningTree<>(UndirectedSparseGraph.<String, Link>getFactory()); Graph<String, Link> miniTree = alg.transform(graph); return pruneTree(miniTree, fabrics); }
private List<Link> calcShortestPathOnFabricTopo(NodeId fabrics, NodeId fabricd) { UndirectedSparseGraph<NodeId, Link> g = new UndirectedSparseGraph<>(); Topology topo = this.getFabricTopology(); if (topo == null) { LOG.error("Failed to get fabric topology!"); return Collections.emptyList(); } for (Node node : topo.getNode()) { g.addVertex(node.getNodeId()); } if (topo.getLink() != null) for (Link link : topo.getLink()) { g.addEdge(link, link.getSource().getSourceNode(), link.getDestination().getDestNode()); } return calcShortestPath(fabrics, fabricd, g); }
@Override public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) { this.validateEdgeType(edgeType); Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints); if (new_endpoints == null) return false; V v1 = new_endpoints.getFirst(); V v2 = new_endpoints.getSecond(); if (findEdge(v1, v2) != null) return false; edges.put(edge, new_endpoints); if (!vertices.containsKey(v1)) this.addVertex(v1); if (!vertices.containsKey(v2)) this.addVertex(v2); // map v1 to <v2, edge> and vice versa vertices.get(v1).put(v2, edge); vertices.get(v2).put(v1, edge); return true; }
public boolean removeEdge(E edge) { if (!containsEdge(edge)) return false; Pair<V> endpoints = getEndpoints(edge); V v1 = endpoints.getFirst(); V v2 = endpoints.getSecond(); // remove incident vertices from each others' adjacency maps vertices.get(v1).remove(v2); vertices.get(v2).remove(v1); edges.remove(edge); return true; } }
public Collection<E> getOutEdges(V vertex) { return this.getIncidentEdges(vertex); }
@Override public boolean addEdge(E edge, Pair<? extends V> endpoints, EdgeType edgeType) { this.validateEdgeType(edgeType); Pair<V> new_endpoints = getValidatedEndpoints(edge, endpoints); if (new_endpoints == null) { return false; } V v1 = new_endpoints.getFirst(); V v2 = new_endpoints.getSecond(); if (findEdge(v1, v2) != null) { return false; } edges.put(edge, new_endpoints); if (!vertices.containsKey(v1)) { this.addVertex(v1); } if (!vertices.containsKey(v2)) { this.addVertex(v2); } // map v1 to <v2, edge> and vice versa vertices.get(v1).put(v2, edge); vertices.get(v2).put(v1, edge); return true; }
@Override public boolean removeVertex(V vertex) { if (!containsVertex(vertex)) { return false; } // iterate over copy of incident edge collection for (E edge : new ArrayList<E>(vertices.get(vertex).values())) { removeEdge(edge); } vertices.remove(vertex); return true; }
@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 incident vertices from each others' adjacency maps vertices.get(v1).remove(v2); vertices.get(v2).remove(v1); edges.remove(edge); return true; }
@Override public Collection<E> getInEdges(V vertex) { return this.getIncidentEdges(vertex); }
@Override public UndirectedSparseGraph<V, E> newInstance() { return new UndirectedSparseGraph<V, E>(); }
@Override public E findEdge(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) return null; return vertices.get(v1).get(v2); }
@Override public Collection<E> findEdgeSet(V v1, V v2) { if (!containsVertex(v1) || !containsVertex(v2)) { return null; } ArrayList<E> edge_collection = new ArrayList<E>(1); // if (!containsVertex(v1) || !containsVertex(v2)) // return edge_collection; E e = findEdge(v1, v2); if (e == null) { return edge_collection; } edge_collection.add(e); return edge_collection; }
@Override public Collection<E> getOutEdges(V vertex) { return this.getIncidentEdges(vertex); }
public UndirectedGraph<V,E> get() { return new UndirectedSparseGraph<V,E>(); } };
public Collection<V> getNeighbors(V vertex) { if (!containsVertex(vertex)) return null; return Collections.unmodifiableCollection(vertices.get(vertex).keySet()); }
public Collection<E> getInEdges(V vertex) { return this.getIncidentEdges(vertex); }