本文整理汇总了Java中org.jgrapht.graph.DefaultDirectedGraph.addEdge方法的典型用法代码示例。如果您正苦于以下问题:Java DefaultDirectedGraph.addEdge方法的具体用法?Java DefaultDirectedGraph.addEdge怎么用?Java DefaultDirectedGraph.addEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jgrapht.graph.DefaultDirectedGraph
的用法示例。
在下文中一共展示了DefaultDirectedGraph.addEdge方法的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());
}
}