本文整理匯總了Java中org.jgrapht.graph.DefaultDirectedGraph.addVertex方法的典型用法代碼示例。如果您正苦於以下問題:Java DefaultDirectedGraph.addVertex方法的具體用法?Java DefaultDirectedGraph.addVertex怎麽用?Java DefaultDirectedGraph.addVertex使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jgrapht.graph.DefaultDirectedGraph
的用法示例。
在下文中一共展示了DefaultDirectedGraph.addVertex方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: computeReverseJGraph
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
/**
* Returns a reverted version of this graph in a jGraph
*
* That is a graph containing exactly the same nodes as this one but for
* each edge from v1 to v2 in this graph the resulting graph will contain an
* edge from v2 to v1 - or in other words the reverted edge
*
* This is used to revert CFGs in order to determine control dependencies
* for example
*
* @return a {@link org.jgrapht.graph.DefaultDirectedGraph} object.
*/
protected DefaultDirectedGraph<V, E> computeReverseJGraph() {
DefaultDirectedGraph<V, E> r = new DefaultDirectedGraph<V, E>(edgeClass);
for (V v : vertexSet())
if (!r.addVertex(v))
throw new IllegalStateException(
"internal error while adding vertices");
for (E e : edgeSet()) {
V src = getEdgeSource(e);
V target = getEdgeTarget(e);
if (r.addEdge(target, src) == null)
throw new IllegalStateException(
"internal error while adding reverse edges");
}
return r;
}
示例2: asGraph
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
private DefaultDirectedGraph<Node, DefaultEdge> asGraph(Set<Triple> triples) {
DefaultDirectedGraph<Node, DefaultEdge> graph = new DefaultDirectedGraph<>(DefaultEdge.class);
for (Triple triple : triples) {
// we have to omit type triples to get connected subgraphs later on
if (!triple.predicateMatches(RDF.type.asNode())) {
graph.addVertex(triple.getSubject());
graph.addVertex(triple.getObject());
graph.addEdge(triple.getSubject(), triple.getObject());
}
}
return graph;
}
示例3: testCycleDetection
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
public void testCycleDetection() {
DefaultDirectedGraph<String, Object> g = new DefaultDirectedGraph<String, Object>(Object.class);
// Add vertices, e.g. equations.
g.addVertex("A");
g.addVertex("B");
g.addEdge("A", "B");
g.addVertex("D");
g.addVertex("A");
g.addEdge("D", "A");
g.addVertex("B");
g.addVertex("C");
g.addEdge("B", "C");
g.addVertex("G");
g.addVertex("H");
g.addEdge("G", "H");
g.addVertex("C");
g.addVertex("D");
g.addEdge("C", "D");
System.out.println(g.toString());
// Are there cycles in the dependencies.
CycleDetector<String, Object> cycleDetector = new CycleDetector<String, Object>(g);
// Cycle(s) detected.
if (cycleDetector.detectCycles()) {
System.out.println("Cycles detected.");
// Get all vertices involved in cycles.
Set<String> cycleVertices = cycleDetector.findCycles();
// Loop through vertices trying to find disjoint cycles.
while (!cycleVertices.isEmpty()) {
System.out.println("Cycle:");
// Get a vertex involved in a cycle.
Iterator<String> iterator = cycleVertices.iterator();
String cycle = iterator.next();
// Get all vertices involved with this vertex.
Set<String> subCycle = cycleDetector.findCyclesContainingVertex(cycle);
for (String sub : subCycle) {
System.out.println(" " + sub);
// Remove vertex so that this cycle is not encountered
// again.
cycleVertices.remove(sub);
}
}
}
// No cycles. Just output properly ordered vertices.
else {
TopologicalOrderIterator<String, Object> orderIterator = new TopologicalOrderIterator<String, Object>(g);
System.out.println("\nOrdering:");
while (orderIterator.hasNext()) {
String v = orderIterator.next();
System.out.println(v);
}
}
}
示例4: link
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
private void link(DefaultDirectedGraph<FStream, IndexedEdge<FStream>> G, FStream node, FStream n) {
// The source should be there, the destination we'll add if necessary.
if (!G.containsVertex(n)) {
G.addVertex(n);
}
index_counter += 1;
IndexedEdge<FStream> e = new IndexedEdge<FStream>(node, n, index_counter);
G.addEdge(node, n, e);
}
示例5: worker
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
private void worker(DefaultDirectedGraph<TaskId, DefaultEdge> graph, Task task, TaskId parentId, ImmutableMap.Builder<TaskId, Task> tasksBuilder, Set<TaskId> usedTasksSet)
{
if ( usedTasksSet.add(task.getTaskId()) )
{
tasksBuilder.put(task.getTaskId(), task);
}
graph.addVertex(task.getTaskId());
if ( parentId != null )
{
graph.addEdge(parentId, task.getTaskId());
}
task.getChildrenTasks().forEach(child -> worker(graph, child, task.getTaskId(), tasksBuilder, usedTasksSet));
}
示例6: testDFS
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
/**
* Rigourous Test :-)
*/
public void testDFS() {
DefaultDirectedGraph<Vertex, Double> graph = new DefaultDirectedGraph<Vertex, Double>(
Double.class);
Vertex v = null;
for (int i = 0; i < this.numOfVertices; i++) {
v = new Vertex("v" + i);
graph.addVertex(v);
}
Vertex lastVertex = v;
Set<Vertex> vertices = graph.vertexSet();
Random rand = new Random();
Double nextEdge = 0.0;
for (Vertex start : vertices)
for (Vertex end : vertices)
if (!start.equals(end) && !graph.containsEdge(start, end)) {
nextEdge = rand.nextDouble();
if (nextEdge > 0.9)
graph.addEdge(start, end, nextEdge);
}
System.out.println("Vertices: " + vertices.size());
System.out.println("Edges: " + graph.edgeSet().size());
DepthFirstIterator<Vertex, Double> dfsIter = new DepthFirstIterator<Vertex, Double>(
graph, lastVertex);
DFS<Double> dfs = new DFS<Double>(graph);
Vertex[] dfsResult = dfs.depthFirstTree(lastVertex);
for (int i = 0; i < this.numOfVertices && dfsIter.hasNext(); i++) {
System.out.println(dfsResult[i] + " " + dfsIter.next());
}
}
示例7: test
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
/**
Test creating a directed graph, checking it for cycles and either
outputting cycle information or ordering.
@param createCycles true - create a directed graph which contains
cycles. false - create a directed graph which does not contain
any cycles.
*/
public static void test(boolean createCycles) {
DefaultDirectedGraph<String, DefaultEdge> g;
g = new DefaultDirectedGraph<String, DefaultEdge>(
DefaultEdge.class);
// Add vertices, e.g. equations.
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
g.addVertex("d");
g.addVertex("e");
// Add edges, e.g. dependencies.
// 2 cycles,
// a = f(b)
// b = f(c)
// c = f(a)
// and
// d = f(e)
// e = f(d)
g.addEdge("b", "a");
g.addEdge("c", "b");
if (createCycles) {
g.addEdge("a", "c");
g.addEdge("a", "d");
}
g.addEdge("e", "d");
if (createCycles) {
g.addEdge("d", "e");
}
System.out.println(g.toString());
displayCyclesInfo(g);
}
示例8: main
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
/**
Generate two cases, one with cycles, this is depencencies and one
without.
@param args Ignored.
*/
public static void main(String [] args) {
// System.out.println("\nCase 1: There are cycles.");
// test(true);
//
// System.out.println("\nCase 2: There are no cycles.");
// test(false);
//
// System.out.println("\nAll done");
// System.exit(0);
DefaultDirectedGraph<String, DefaultEdge> g;
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
// Add vertices, e.g. equations.
g.addVertex("a");
g.addVertex("b");
g.addVertex("c");
g.addVertex("d");
g.addVertex("x");
g.addVertex("y");
g.addVertex("z");
g.addEdge("a", "b");
g.addEdge("b", "c");
g.addEdge("c", "d");
g.addEdge("d", "a");
g.addEdge("a", "x");
g.addEdge("x", "y");
g.addEdge("y", "z");
g.addEdge("z", "a");
System.out.println(g.toString());
displayCyclesInfo(g);
}
示例9: testBFS
import org.jgrapht.graph.DefaultDirectedGraph; //導入方法依賴的package包/類
/**
* Rigourous Test :-)
*/
public void testBFS() {
DefaultDirectedGraph<Vertex, Double> graph = new DefaultDirectedGraph<Vertex, Double>(
Double.class);
Vertex v = null;
for (int i = 0; i < this.numOfVertices; i++) {
v = new Vertex("v" + i);
graph.addVertex(v);
}
// use last vertex as start point of search
Vertex lastVertex = v;
Set<Vertex> vertices = graph.vertexSet();
// add edges to graph
Random rand = new Random();
Double nextEdge = 0.0;
for (Vertex start : vertices)
for (Vertex end : vertices)
if (!start.equals(end) && !graph.containsEdge(start, end)) {
nextEdge = rand.nextDouble();
if (nextEdge > 0.9)
graph.addEdge(start, end, nextEdge);
}
System.out.println("Vertices: " + vertices.size());
System.out.println("Edges: " + graph.edgeSet().size());
BreadthFirstIterator<Vertex, Double> bfsIter = new BreadthFirstIterator<Vertex, Double>(
graph, lastVertex);
BFS<Double> bfs = new BFS<Double>(graph);
Vertex[] bfsResult = bfs.breadthFirstTree(lastVertex);
for (int i = 0; i < this.numOfVertices && bfsIter.hasNext(); i++) {
System.out.println(bfsResult[i] + " " + bfsIter.next());
}
}