本文整理匯總了Java中org.jgrapht.Graph.addVertex方法的典型用法代碼示例。如果您正苦於以下問題:Java Graph.addVertex方法的具體用法?Java Graph.addVertex怎麽用?Java Graph.addVertex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jgrapht.Graph
的用法示例。
在下文中一共展示了Graph.addVertex方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getGraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
* Convert an FNSS Topology to a JGraphT graph.
*
* @param topology FNSS Topology object
* @return A JGraphT graph
*/
public static Graph<String, Edge> getGraph(Topology topology) {
Graph<String, Edge> graph = null;
if (topology.isDirected()) {
graph = new DefaultDirectedGraph<String, Edge>(Edge.class);
} else {
graph = new SimpleGraph<String, Edge>(Edge.class);
}
for(String node : topology.getAllNodes()) {
graph.addVertex(node);
}
for(Pair<String, String> endpoints : topology.getAllEdges()) {
Edge edge = topology.getEdge(endpoints);
graph.addEdge(endpoints.getU(), endpoints.getV(), edge);
}
return graph;
}
示例2: testExecute
import org.jgrapht.Graph; //導入方法依賴的package包/類
@Test
public void testExecute() throws Exception {
// GIVEN
final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 1l).build());
final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 2l).build());
final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
ImmutableMap.<String, Object>builder().put("id", 3l).build());
final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
graph.addVertex(n1);
graph.addVertex(n2);
graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);
// WHEN
operation.execute(connection, graph);
// THEN
final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
verify(operation).executeQuery(eq(connection), queryCaptor.capture());
final String query = queryCaptor.getValue();
assertThat(query, containsString("CREATE (n1:A {id:1}),(n2:A {id:2}),(n1)-[e1:E {id:3}]->(n2)"));
}
示例3: testExecute
import org.jgrapht.Graph; //導入方法依賴的package包/類
@Test
public void testExecute() throws Exception {
// GIVEN
final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 1l).build());
final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 2l).build());
final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"), Collections.emptyMap());
final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
graph.addVertex(n1);
graph.addVertex(n2);
graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);
// WHEN
operation.execute(connection, graph);
// THEN
final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture());
final String query = queryCaptor.getValue();
assertThat(query, containsString("MATCH (n:A) DETACH DELETE n"));
}
示例4: testExecute
import org.jgrapht.Graph; //導入方法依賴的package包/類
@Test
public void testExecute() throws Exception {
// GIVEN
final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 1l).put("value", "A").build());
final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 2l).build());
final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
ImmutableMap.<String, Object>builder().put("id", 3l).put("value", "C").build());
final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
graph.addVertex(n1);
graph.addVertex(n2);
graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);
// WHEN
operation.execute(connection, graph);
// THEN
final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
verify(operation, times(2)).executeQuery(eq(connection), queryCaptor.capture());
final List<String> queries = queryCaptor.getAllValues();
assertThat(queries.get(0), containsString("MATCH (n1:A {id:1}) SET n1.value=\"A\""));
assertThat(queries.get(1), containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) MERGE (n1)-[e1:E]->(n2) SET e1.id=3,e1.value=\"C\""));
}
示例5: testExecute
import org.jgrapht.Graph; //導入方法依賴的package包/類
@Test
public void testExecute() throws Exception {
// GIVEN
final Node n1 = graphElementFactory.createNode("n1", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 1l).put("value", "A").build());
final Node n2 = graphElementFactory.createNode("n2", Arrays.asList("A"),
ImmutableMap.<String, Object>builder().put("id", 2l).build());
final Edge e1 = graphElementFactory.createEdge(n1, n2, "e1", Arrays.asList("E"),
ImmutableMap.<String, Object>builder().put("id", 3l).put("value", "C").build());
final Graph<Node, Edge> graph = new DefaultDirectedGraph<>(new ClassBasedEdgeFactory<>(Edge.class));
graph.addVertex(n1);
graph.addVertex(n2);
graph.addEdge(e1.getSourceNode(), e1.getTargetNode(), e1);
// WHEN
operation.execute(connection, graph);
// THEN
final ArgumentCaptor<String> queryCaptor = ArgumentCaptor.forClass(String.class);
verify(operation, times(3)).executeQuery(eq(connection), queryCaptor.capture());
final List<String> queries = queryCaptor.getAllValues();
assertThat(queries.get(0), containsString("MERGE (n1:A {id:1}) SET n1.value=\"A\""));
assertThat(queries.get(1), containsString("MERGE (n2:A {id:2})"));
assertThat(queries.get(2), containsString("MATCH (n1:A {id:1}),(n2:A {id:2}) MERGE (n1)-[e1:E]->(n2) SET e1.id=3,e1.value=\"C\""));
}
示例6: build
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
* Builds a simple weighted graph
* @param data The input list of tuples each holds two indices and distance between them
*/
public static Graph<Integer, DefaultWeightedEdge> build(List<Tuple3<Integer, Integer, Double>> data) {
// initialize a weighted graph
Graph<Integer, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
// add vertices and edges to the weighted graph
for (Tuple3<Integer, Integer, Double> t : data) {
int i = t._1();
int j = t._2();
double score = t._3();
graph.addVertex(i);
graph.addVertex(j);
DefaultWeightedEdge e = graph.addEdge(i, j);
((AbstractBaseGraph<Integer, DefaultWeightedEdge>) graph).setEdgeWeight(e, score);
}
return graph;
}
示例7: getMinimumSpanningTree
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
* Builds a graph that contains a Minimum Spanning Tree
* @param graph The input weighted graph
*/
public static Graph<Integer, DefaultWeightedEdge> getMinimumSpanningTree(Graph<Integer, DefaultWeightedEdge> graph) {
// get the edges of a Minimum Spanning Tree generated for a weighted graph
PrimMinimumSpanningTree<Integer, DefaultWeightedEdge> tree = new PrimMinimumSpanningTree<Integer, DefaultWeightedEdge>(graph);
Set<DefaultWeightedEdge> mspEdges = tree.getMinimumSpanningTreeEdgeSet();
// build a new graph from the MST edges
Graph<Integer, DefaultWeightedEdge> mspGraph = new SimpleWeightedGraph<Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
for (DefaultWeightedEdge e : mspEdges) {
int sourceV = graph.getEdgeSource(e);
int targetV = graph.getEdgeTarget(e);
mspGraph.addVertex(sourceV);
mspGraph.addVertex(targetV);
DefaultWeightedEdge edge = graph.getEdge(sourceV, targetV);
double w = graph.getEdgeWeight(edge);
mspGraph.addEdge(sourceV, targetV, edge);
((AbstractBaseGraph<Integer, DefaultWeightedEdge>) mspGraph).setEdgeWeight(edge, w);
}
return mspGraph;
}
示例8: tryAdd
import org.jgrapht.Graph; //導入方法依賴的package包/類
private boolean tryAdd(Set<? extends T> roots, Graph<T, Pair> graph, Set<T> knownNodes,
CycleDetector<T, Pair> cycleDetector, Deque<T> nodePath)
{
for (T node : roots) {
nodePath.addLast(node);
graph.addVertex(node);
if (knownNodes.add(node)) {
Set<? extends T> nodesFrom = nodesFrom(node);
for (T from : nodesFrom) {
graph.addVertex(from);
Pair edge = new Pair(from, node);
graph.addEdge(from, node, edge);
nodePath.addLast(from);
if (cycleDetector.detectCycles())
return false;
nodePath.removeLast();
}
if (!tryAdd(nodesFrom, graph, knownNodes, cycleDetector, nodePath))
return false;
}
nodePath.removeLast();
}
return true;
}
示例9: buildRandomically
import org.jgrapht.Graph; //導入方法依賴的package包/類
public static void buildRandomically(Graph<String, Edge> graph,
int nVertices, int nEdges) {
Map<Integer, String> vertices = new HashMap<Integer, String>();
for (int i = 0; i < nVertices; i++) {
String vStr = Integer.toString(i);
String v = null;
v = vertices.get(vStr);
if (v == null) {
v = new String(vStr);
vertices.put(i, v);
graph.addVertex(v);
}
}
for (int j = 0; j < nEdges; j++) {
int from = Utils.randInt(0, vertices.size() - 1);
int to = Utils.randInt(0, vertices.size() - 1);
graph.addEdge(vertices.get(from), vertices.get(to), new Edge());
}
}
示例10: toSubgraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
* Creates the subgraph of g1 containing all the edges from the edge product in the vertices of this EdgeProductGraph
* @param edgeProductVertices if (and only if) these vertices induce a complete subgraph in this EdgeProductGraph, then the result
* will be the a common subgraph of g1 and g2.
* @return a subgraph of g1
*/
public Graph<V,E> toSubgraph(Set<EdgeProduct<E>> edgeProductVertices){
Graph<V,E> result;
if(g1 instanceof DirectedGraph){
result = new DirectedMultigraph<V,E>(g1.getEdgeFactory());
} else {
result = new Multigraph<V,E>(g1.getEdgeFactory());
}
//Add the left Edge (including vertices) from all the EdgeProducts in vertices
for(EdgeProduct<E> ep: edgeProductVertices){
E edge = ep.getLeft();
V vSource = g1.getEdgeSource(edge);
V vTarget = g1.getEdgeTarget(edge);
result.addVertex(vSource);
result.addVertex(vTarget);
result.addEdge(vSource, vTarget, edge);
}
return result;
}
示例11: createSimpleConnectedWeightedGraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
protected Graph<String, DefaultWeightedEdge> createSimpleConnectedWeightedGraph() {
Graph<String, DefaultWeightedEdge> g =
new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
double bias = 1;
g.addVertex(A);
g.addVertex(B);
g.addVertex(C);
g.addVertex(D);
g.addVertex(E);
AB = Graphs.addEdge(g, A, B, bias * 2);
AC = Graphs.addEdge(g, A, C, bias * 3);
BD = Graphs.addEdge(g, B, D, bias * 5);
CD = Graphs.addEdge(g, C, D, bias * 20);
DE = Graphs.addEdge(g, D, E, bias * 5);
AE = Graphs.addEdge(g, A, E, bias * 100);
return g;
}
示例12: replaceVertex
import org.jgrapht.Graph; //導入方法依賴的package包/類
public static <T extends Object> boolean replaceVertex(Graph<T, DefaultEdge> completeGraph, T oldVertex, T newVertex) {
if ((oldVertex == null) || (newVertex == null)) {
return false;
}
final Set<DefaultEdge> relatedEdges = completeGraph.edgesOf(oldVertex);
completeGraph.addVertex(newVertex);
T sourceVertex;
T targetVertex;
for (DefaultEdge e : relatedEdges) {
sourceVertex = completeGraph.getEdgeSource(e);
targetVertex = completeGraph.getEdgeTarget(e);
if (sourceVertex.equals(oldVertex)
&& targetVertex.equals(oldVertex)) {
completeGraph.addEdge(newVertex, newVertex);
} else {
if (sourceVertex.equals(oldVertex)) {
completeGraph.addEdge(newVertex, targetVertex);
} else {
completeGraph.addEdge(sourceVertex, newVertex);
}
}
}
completeGraph.removeVertex(oldVertex);
return true;
}
示例13: stringGraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
* @return
*/
public static Graph<String, DefaultEdge> stringGraph()
{
final Graph<String, DefaultEdge> g = new ListenableDirectedGraph<>(
DefaultEdge.class);
g.addVertex("v1");
g.addVertex("v2");
g.addVertex("v3");
g.addVertex("v4");
g.addEdge("v1", "v2");
g.addEdge("v2", "v3");
g.addEdge("v3", "v1");
g.addEdge("v4", "v3");
return g;
}
示例14: addToGraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
private void addToGraph(Graph<GraphNode, DefaultEdge> dst, Graph<GraphNode, DefaultEdge> source) {
for(GraphNode node : source.vertexSet()) {
GraphNode toAdd = node.clone();
toAdd.setGraph(dst);
dst.addVertex(toAdd);
}
for(DefaultEdge edge : source.edgeSet()) {
GraphNode src = graph.getEdgeSource(edge).clone();
GraphNode target = graph.getEdgeTarget(edge).clone();
for(GraphNode cur : dst.vertexSet()) {
if(cur.equals(src)) {
src = cur;
} else if(cur.equals(target)) {
target = cur;
}
}
dst.addEdge(src, target);
}
}
示例15: getAST
import org.jgrapht.Graph; //導入方法依賴的package包/類
public Graph<ASTNode, DefaultEdge> getAST() {
Graph<ASTNode, DefaultEdge> ast = new ListenableDirectedGraph<>(DefaultEdge.class);
ast.addVertex(this);
body.forEach(node -> {
node.addDependency(ast);
ast.addEdge(this, node);
});
return ast;
}