本文整理汇总了Java中edu.uci.ics.jung.graph.DirectedGraph.addEdge方法的典型用法代码示例。如果您正苦于以下问题:Java DirectedGraph.addEdge方法的具体用法?Java DirectedGraph.addEdge怎么用?Java DirectedGraph.addEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.uci.ics.jung.graph.DirectedGraph
的用法示例。
在下文中一共展示了DirectedGraph.addEdge方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testBfs
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testBfs() throws Exception {
DirectedGraph<URI, URI> graph = new DirectedSparseGraph<>();
graph.addVertex( YURI );
graph.addVertex( YUGO );
graph.addEdge( YPY, YURI, YUGO, EdgeType.DIRECTED );
Collection<URI> roots = Arrays.asList( YURI );
GraphToTreeConverter.Search search = GraphToTreeConverter.Search.BFS;
Forest<URI, URI> result = GraphToTreeConverter.convert( graph, roots, search );
assertEquals( 1, result.getTrees().size() );
Tree<URI, URI> tree = result.getTrees().iterator().next();
assertEquals( YURI, tree.getRoot() );
assertEquals( YUGO, tree.getChildren( YURI ).iterator().next() );
}
示例2: testDfs
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testDfs() throws Exception {
DirectedGraph<URI, URI> graph = new DirectedSparseGraph<>();
graph.addVertex( YURI );
graph.addVertex( YUGO );
graph.addEdge( YPY, YURI, YUGO, EdgeType.DIRECTED );
Collection<URI> roots = Arrays.asList( YURI );
GraphToTreeConverter.Search search = GraphToTreeConverter.Search.DFS;
Forest<URI, URI> result = GraphToTreeConverter.convert( graph, roots, search );
assertEquals( 1, result.getTrees().size() );
Tree<URI, URI> tree = result.getTrees().iterator().next();
assertEquals( YURI, tree.getRoot() );
assertEquals( YUGO, tree.getChildren( YURI ).iterator().next() );
}
示例3: testPrint
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testPrint() throws Exception {
DirectedGraph<URI, URI> graph = new DirectedSparseGraph<>();
graph.addVertex( YURI );
graph.addVertex( YUGO );
graph.addEdge( YPY, YURI, YUGO, EdgeType.DIRECTED );
Collection<URI> roots = Arrays.asList( YURI );
GraphToTreeConverter.Search search = GraphToTreeConverter.Search.DFS;
Forest<URI, URI> result = GraphToTreeConverter.convert( graph, roots, search );
Logger log = Logger.getLogger( GraphToTreeConverter.class );
StringWriter stringy = new StringWriter();
WriterAppender app = new WriterAppender( new SimpleLayout(), stringy );
log.setLevel( Level.DEBUG );
log.addAppender( app );
GraphToTreeConverter.printForest( result );
String output = stringy.toString().replaceAll( "\\s", "" );
assertEquals( "DEBUG-http://semoss.va.gov/database/T44889381-85ce-43e3-893d-6267fd480660/YuriDEBUG-http://semoss.va.gov/database/T44889381-85ce-43e3-893d-6267fd480660/Yugo", output );
}
示例4: build
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
public static DirectedGraph<GraphNode, GraphEdge> build(
GraphModel graphModel, EdgeMatcher<String> matcher) {
DirectedGraph<GraphNode, GraphEdge> result =
new DirectedSparseMultigraph<GraphNode, GraphEdge>();
Set<GraphNode> includedNodes = graphModel.getNodesSet();
for (GraphEdge edge : graphModel.getEdges()) {
// Filter on nodes first
if (!includedNodes.contains(edge.getHead()))
continue;
if (!includedNodes.contains(edge.getTail()))
continue;
if (matcher.edgeForward(edge)) {
result.addEdge(edge, edge.getHead(), edge.getTail());
}
else if (matcher.edgeReverse(edge)) {
result.addEdge(edge, edge.getTail(), edge.getHead());
}
}
return result;
}
示例5: toDirected
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
public static <V> DirectedGraph<V, WeightedEdge> toDirected(Graph<V, WeightedEdge> graph) {
DirectedGraph<V, WeightedEdge> directedGraph = new DirectedSparseGraph<V, WeightedEdge>();
// Add all vertices first
Collection<V> vertices = graph.getVertices();
for(V vertex : vertices) {
directedGraph.addVertex(vertex);
}
// Add directed edges
for(WeightedEdge edge : graph.getEdges()) {
Pair<V> endpoints = graph.getEndpoints(edge);
directedGraph.addEdge(new WeightedEdge(edge.getWeight()), endpoints.getFirst(), endpoints.getSecond());
directedGraph.addEdge(new WeightedEdge(edge.getWeight()), endpoints.getSecond(), endpoints.getFirst());
}
return directedGraph;
}
示例6: test3
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test for discovery of a strongly connected component with 4 nodes (a cycle).
*/
@Test
public void test3() {
DirectedGraph<Integer, WeightedEdge> dg = new DirectedSparseGraph<Integer, WeightedEdge>();
dg.addEdge(new WeightedEdge(1.0f), 5, 6);
dg.addEdge(new WeightedEdge(1.0f), 6, 7);
dg.addEdge(new WeightedEdge(1.0f), 7, 8);
dg.addEdge(new WeightedEdge(1.0f), 8, 5);
Tarjan<Integer, WeightedEdge> t = new Tarjan<Integer, WeightedEdge>(dg);
List<List<Integer>> sccs = t.tarjan();
assertTrue(sccs.size() == 1);
assertTrue(sccs.get(0).contains(5));
assertTrue(sccs.get(0).contains(6));
assertTrue(sccs.get(0).contains(7));
assertTrue(sccs.get(0).contains(8));
}
示例7: test4
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test for discovery of two binary strongly connected components.
*/
@Test
public void test4() {
DirectedGraph<Integer, WeightedEdge> dg = new DirectedSparseGraph<Integer, WeightedEdge>();
dg.addEdge(new WeightedEdge(1.0f), 5, 6);
dg.addEdge(new WeightedEdge(1.0f), 6, 5);
dg.addEdge(new WeightedEdge(1.0f), 7, 8);
dg.addEdge(new WeightedEdge(1.0f), 8, 7);
Tarjan<Integer, WeightedEdge> t = new Tarjan<Integer, WeightedEdge>(dg);
List<List<Integer>> sccs = t.tarjan();
assertTrue(sccs.size() == 2);
assertTrue(sccs.get(0).contains(5));
assertTrue(sccs.get(0).contains(6));
assertTrue(sccs.get(1).contains(7));
assertTrue(sccs.get(1).contains(8));
}
示例8: testLowerLevelComponents
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testLowerLevelComponents() {
DirectedGraph<Integer, WeightedEdge> dg = new DirectedSparseGraph<>();
dg.addEdge(new WeightedEdge(1.0f), 1, 2);
dg.addEdge(new WeightedEdge(1.0f), 2, 3);
dg.addEdge(new WeightedEdge(1.0f), 2, 4);
dg.addEdge(new WeightedEdge(1.0f), 3, 2);
dg.addEdge(new WeightedEdge(1.0f), 3, 4);
dg.addEdge(new WeightedEdge(1.0f), 4, 3);
// for this graph, all nodes are discovered in one "top-level" strongConnect() call
// (specifically the call to strongConnect(1)) and the actual component is added to
// the components list at a "lower-level" call to strongConnect()
Tarjan<Integer, WeightedEdge> tarjan = new Tarjan<>(dg);
List<List<Integer>> sccs = tarjan.tarjan();
assertTrue(sccs.size() == 1);
assertTrue(sccs.get(0).contains(2));
assertTrue(sccs.get(0).contains(3));
assertTrue(sccs.get(0).contains(4));
}
示例9: testJohnson
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test for detection of two binary cycles.
*
* @throws JohnsonIllegalStateException
*/
@Test
public void testJohnson() throws JohnsonIllegalStateException {
DirectedGraph<Integer, WeightedEdge> dsg = new DirectedSparseGraph<Integer, WeightedEdge>();
dsg.addEdge(new WeightedEdge(1.0f), 1, 3);
dsg.addEdge(new WeightedEdge(1.0f), 3, 1);
dsg.addEdge(new WeightedEdge(1.0f), 3, 2);
dsg.addEdge(new WeightedEdge(1.0f), 2, 3);
Johnson j = new Johnson(dsg);
j.findCircuits();
assertSame(j.circuits.size(), 2);
Stack<Integer> expected1 = new Stack<Integer>();
// TODO: there is no necessity for a node appearing twice within a cycle.
expected1.addAll(Arrays.asList(1, 3, 1));
assertTrue(j.circuits.contains(expected1));
}
示例10: testJohnson2
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test with one ternary cycle.
*
* @throws JohnsonIllegalStateException
*/
@Test
public void testJohnson2() throws JohnsonIllegalStateException {
DirectedGraph<Integer, WeightedEdge> dsg = new DirectedSparseGraph<Integer, WeightedEdge>();
dsg.addEdge(new WeightedEdge(1.0f), 1, 2);
dsg.addEdge(new WeightedEdge(1.0f), 2, 1);
dsg.addEdge(new WeightedEdge(1.0f), 2, 3);
dsg.addEdge(new WeightedEdge(1.0f), 4, 5);
dsg.addEdge(new WeightedEdge(1.0f), 5, 6);
dsg.addEdge(new WeightedEdge(1.0f), 6, 4);
Johnson j = new Johnson(dsg);
j.findCircuits();
assertSame(j.circuits.size(), 2);
assertTrue(j.circuits.get(1).contains(4));
assertTrue(j.circuits.get(1).contains(5));
assertTrue(j.circuits.get(1).contains(6));
}
示例11: testJohnson3
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test for a graph with one binary cycle.
* @throws JohnsonIllegalStateException
*/
@Test
public void testJohnson3() throws JohnsonIllegalStateException {
DirectedGraph<Integer, WeightedEdge> dsg = new DirectedSparseGraph<Integer, WeightedEdge>();
dsg.addEdge(new WeightedEdge(1.0f), 2, 1);
dsg.addEdge(new WeightedEdge(1.0f), 1, 2);
dsg.addEdge(new WeightedEdge(1.0f), 3, 2);
dsg.addEdge(new WeightedEdge(1.0f), 3, 1);
Johnson j = new Johnson(dsg);
j.findCircuits();
assertTrue(j.circuits.size() == 1);
System.err.println(j.circuits.get(0));
assertTrue(j.circuits.get(0).contains(2));
assertTrue(j.circuits.get(0).contains(1));
}
示例12: testTarjan3
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/**
* Test for detection of strongly connected components.
* @throws JohnsonIllegalStateException
*/
@Test
public void testTarjan3() throws JohnsonIllegalStateException {
DirectedGraph<Integer, WeightedEdge> dsg = new DirectedSparseGraph<Integer, WeightedEdge>();
dsg.addEdge(new WeightedEdge(1.0f), 1, 2);
dsg.addEdge(new WeightedEdge(1.0f), 2, 1);
dsg.addEdge(new WeightedEdge(1.0f), 2, 3);
dsg.addEdge(new WeightedEdge(1.0f), 2, 4);
dsg.addEdge(new WeightedEdge(1.0f), 4, 2);
DirectedGraph<Integer, WeightedEdge> leastScc = Johnson.leastSCC(dsg);
System.err.println(leastScc);
assertTrue(leastScc.getVertices().contains(1));
assertTrue(leastScc.getVertices().contains(2));
assertTrue(leastScc.getVertices().contains(4));
assertTrue(leastScc.getVertexCount() == 3);
}
示例13: testLargeGraph
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
@Test
public void testLargeGraph() throws JohnsonIllegalStateException {
DirectedGraph<Integer, WeightedEdge> dg = new DirectedSparseGraph<Integer, WeightedEdge>();
Random r = new Random();
int nodes = 1000;
int edges = 1000;
while (edges > 0) {
int from = r.nextInt(nodes) + 1;
int to = r.nextInt(nodes) + 1;
float weight = r.nextFloat();
if (dg.findEdge(from, to) == null && from != to) {
dg.addEdge(new WeightedEdge(weight), from, to);
edges--;
}
}
System.err.println("edges: " + dg.getEdgeCount());
System.err.println(dg);
new Johnson(dg).findCircuits();
}
示例14: displayNetwork
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
/** Displays the constraint network graph */
public void displayNetwork() {
DirectedGraph<Integer, Integer> g = new DirectedSparseGraph<Integer, Integer>();
HashMap<Integer, String> nodeLabels = new HashMap<Integer, String>();
int edgeId = 0;
for (PHYNode n : edges.keySet()) {
g.addVertex(n.getNodeId());
nodeLabels.put(n.getNodeId(), n.getLabel());
for(PHYNode n2 : edges.get(n)) {
if(!g.containsVertex(n2.getNodeId())) {
g.addVertex(n2.getNodeId());
nodeLabels.put(n2.getNodeId(), n2.getLabel());
}
g.addEdge(edgeId, n.getNodeId(), n2.getNodeId(), EdgeType.DIRECTED);
edgeId++;
}
}
Visualizer.showNetwork(g, nodeLabels);
}
示例15: addSetOfFriends
import edu.uci.ics.jung.graph.DirectedGraph; //导入方法依赖的package包/类
private void addSetOfFriends(
DirectedGraph<Fisher, FriendshipEdge> network, Fisher fisher, Collection<Fisher> newConnections) {
for(Fisher friend : newConnections) {
if(equalOutDegree)
network.addEdge(new FriendshipEdge(), fisher, friend, EdgeType.DIRECTED);
else
network.addEdge(new FriendshipEdge(), friend, fisher, EdgeType.DIRECTED);
}
}