本文整理汇总了Java中org.jgrapht.Graphs.addEdge方法的典型用法代码示例。如果您正苦于以下问题:Java Graphs.addEdge方法的具体用法?Java Graphs.addEdge怎么用?Java Graphs.addEdge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jgrapht.Graphs
的用法示例。
在下文中一共展示了Graphs.addEdge方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createSimpleConnectedWeightedGraph
import org.jgrapht.Graphs; //导入方法依赖的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;
}
示例2: testGraphWithOneEdge
import org.jgrapht.Graphs; //导入方法依赖的package包/类
/**
* Computes the min s-t cut in a graph with 2 vertices and one directed edge between source and sink
*/
public void testGraphWithOneEdge(){
DirectedGraph<Integer, DefaultWeightedEdge> graph=new DefaultDirectedWeightedGraph <Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
Integer[] vertices={0,1};
for(Integer i: vertices)
graph.addVertex(i);
DefaultWeightedEdge e01=Graphs.addEdge(graph, 0, 1, 2);
MinSourceSinkCut<Integer, DefaultWeightedEdge> mc=new MinSourceSinkCut<Integer, DefaultWeightedEdge>(graph);
mc.computeMinCut(0, 1);
assertEquals(0, mc.getCurrentSource(), 0);
assertEquals(1, mc.getCurrentSink(), 0);
//Test the source and sink partitions
List<Integer> l1 = Arrays.asList(new Integer[] {0});
List<Integer> l2 = Arrays.asList(new Integer[] {1});
Set<Integer> partition1 = new HashSet<Integer>(l1);
Set<Integer> partition2 = new HashSet<Integer>(l2);
assertEquals(partition1, mc.getSourcePartition());
assertEquals(partition2, mc.getSinkPartition());
//Test the cut weight
assertEquals(2, mc.getCutWeight(),0);
//Reverse the source and sink
mc.computeMinCut(1, 0);
assertEquals(1, mc.getCurrentSource(), 0);
assertEquals(0, mc.getCurrentSink(), 0);
assertEquals(0, mc.getCutWeight(),0);
}
示例3: testDisconnectedGraph
import org.jgrapht.Graphs; //导入方法依赖的package包/类
/**
* Computes the min s-t cut in a disconnected graph
*/
public void testDisconnectedGraph(){
DirectedGraph<Integer, DefaultWeightedEdge> graph=new DefaultDirectedWeightedGraph <Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
Integer[] vertices={0,1,2};
for(Integer i: vertices)
graph.addVertex(i);
DefaultWeightedEdge e01=Graphs.addEdge(graph, 1, 2, 2);
MinSourceSinkCut<Integer, DefaultWeightedEdge> mc=new MinSourceSinkCut<Integer, DefaultWeightedEdge>(graph);
mc.computeMinCut(0, 2);
assertEquals(0, mc.getCurrentSource(), 0);
//Test the current sink
assertEquals(2, mc.getCurrentSink(), 0);
//Test the source and sink partitions
List<Integer> l1 = Arrays.asList(new Integer[] {0});
List<Integer> l2 = Arrays.asList(new Integer[] {1,2});
Set<Integer> partition1 = new HashSet<Integer>(l1);
Set<Integer> partition2 = new HashSet<Integer>(l2);
assertEquals(partition1, mc.getSourcePartition());
assertEquals(partition2, mc.getSinkPartition());
//Test the cut weight
assertEquals(0, mc.getCutWeight(),0);
assertEquals(Collections.EMPTY_SET, mc.getCutEdges());
}
示例4: createSimpleDisconnectedWeightedGraph
import org.jgrapht.Graphs; //导入方法依赖的package包/类
protected Graph<String, DefaultWeightedEdge> createSimpleDisconnectedWeightedGraph() {
Graph<String, DefaultWeightedEdge> g =
new SimpleWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
/**
*
* A -- B E -- F
* | | | |
* C -- D G -- H
*
*/
g.addVertex(A);
g.addVertex(B);
g.addVertex(C);
g.addVertex(D);
AB = Graphs.addEdge(g, A, B, 5);
AC = Graphs.addEdge(g, A, C, 10);
BD = Graphs.addEdge(g, B, D, 15);
CD = Graphs.addEdge(g, C, D, 20);
g.addVertex(E);
g.addVertex(F);
g.addVertex(G);
g.addVertex(H);
EF = Graphs.addEdge(g, E, F, 20);
EG = Graphs.addEdge(g, E, G, 15);
GH = Graphs.addEdge(g, G, H, 10);
FH = Graphs.addEdge(g, F, H, 5);
return g;
}
示例5: testRealGraph
import org.jgrapht.Graphs; //导入方法依赖的package包/类
/**
* Constructs a small realistic graph and computes the min s-t cut
*/
public void testRealGraph() {
DirectedGraph<Integer, DefaultWeightedEdge> graph=new DefaultDirectedWeightedGraph <Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
Integer[] vertices={0,1,2,3,4,5,6};
for(Integer i: vertices)
graph.addVertex(i);
DefaultWeightedEdge e01=Graphs.addEdge(graph, 0, 1, 2);
DefaultWeightedEdge e02=Graphs.addEdge(graph, 0, 2, 6);
DefaultWeightedEdge e03=Graphs.addEdge(graph, 0, 3, 3);
DefaultWeightedEdge e12=Graphs.addEdge(graph, 1, 2, 4);
DefaultWeightedEdge e32=Graphs.addEdge(graph, 3, 2, 2);
DefaultWeightedEdge e14=Graphs.addEdge(graph, 1, 4, 1);
DefaultWeightedEdge e35=Graphs.addEdge(graph, 3, 5, 6);
DefaultWeightedEdge e24=Graphs.addEdge(graph, 2, 4, 2);
DefaultWeightedEdge e26=Graphs.addEdge(graph, 2, 6, 1);
DefaultWeightedEdge e52=Graphs.addEdge(graph, 5, 2, 4);
DefaultWeightedEdge e46=Graphs.addEdge(graph, 4, 6, 8);
DefaultWeightedEdge e56=Graphs.addEdge(graph, 5, 6, 7);
MinSourceSinkCut<Integer, DefaultWeightedEdge> mc=new MinSourceSinkCut<Integer, DefaultWeightedEdge>(graph);
mc.computeMinCut(0, 6);
//Test the current source
assertEquals(0, mc.getCurrentSource(), 0);
//Test the current sink
assertEquals(6, mc.getCurrentSink(), 0);
//Test the source and sink partitions
List<Integer> l1 = Arrays.asList(new Integer[] {0, 1, 2});
List<Integer> l2 = Arrays.asList(new Integer[] {3, 4, 5, 6});
Set<Integer> partition1 = new HashSet<Integer>(l1);
Set<Integer> partition2 = new HashSet<Integer>(l2);
assertEquals(partition1, mc.getSourcePartition());
assertEquals(partition2, mc.getSinkPartition());
//Test the cut weight
assertEquals(7, mc.getCutWeight(),0);
//Test the cut edge set
List<DefaultWeightedEdge> l3=Arrays.asList(new DefaultWeightedEdge[] {e03, e26, e24, e14});
Set<DefaultWeightedEdge> cutEdges=new HashSet<DefaultWeightedEdge>(l3);
assertEquals(cutEdges, mc.getCutEdges());
cutEdges=mc.getCutEdges();
double weight=0;
for(DefaultWeightedEdge e: cutEdges)
weight+=graph.getEdgeWeight(e);
assertEquals(7.0, weight, 0.0);
}
示例6: testRealGraph2
import org.jgrapht.Graphs; //导入方法依赖的package包/类
/**
* Test on a graph with fractional edge weights
*/
public void testRealGraph2() {
DirectedGraph<Integer, DefaultWeightedEdge> graph=new DefaultDirectedWeightedGraph <Integer, DefaultWeightedEdge>(DefaultWeightedEdge.class);
Integer[] vertices={0,1,2,3,4,5,6};
for(Integer i: vertices)
graph.addVertex(i);
DefaultWeightedEdge e01=Graphs.addEdge(graph, 0, 1, 1.0/2);
DefaultWeightedEdge e02=Graphs.addEdge(graph, 0, 2, 1.0/6);
DefaultWeightedEdge e03=Graphs.addEdge(graph, 0, 3, 1.0/3);
DefaultWeightedEdge e12=Graphs.addEdge(graph, 1, 2, 1.0/4);
DefaultWeightedEdge e32=Graphs.addEdge(graph, 3, 2, 1.0/2);
DefaultWeightedEdge e14=Graphs.addEdge(graph, 1, 4, 1.0/1);
DefaultWeightedEdge e35=Graphs.addEdge(graph, 3, 5, 1.0/6);
DefaultWeightedEdge e24=Graphs.addEdge(graph, 2, 4, 1.0/2);
DefaultWeightedEdge e26=Graphs.addEdge(graph, 2, 6, 1.0/1);
DefaultWeightedEdge e52=Graphs.addEdge(graph, 5, 2, 1.0/4);
DefaultWeightedEdge e46=Graphs.addEdge(graph, 4, 6, 1.0/8);
DefaultWeightedEdge e56=Graphs.addEdge(graph, 5, 6, 1.0/7);
MinSourceSinkCut<Integer, DefaultWeightedEdge> mc=new MinSourceSinkCut<Integer, DefaultWeightedEdge>(graph);
mc.computeMinCut(0, 6);
//Test the current source
assertEquals(0, mc.getCurrentSource(), 0);
//Test the current sink
assertEquals(6, mc.getCurrentSink(), 0);
//Test the source and sink partitions
List<Integer> l1 = Arrays.asList(new Integer[] {0, 1, 4});
List<Integer> l2 = Arrays.asList(new Integer[] {2, 3, 5, 6});
Set<Integer> partition1 = new HashSet<Integer>(l1);
Set<Integer> partition2 = new HashSet<Integer>(l2);
assertEquals(partition1, mc.getSourcePartition());
assertEquals(partition2, mc.getSinkPartition());
//Test the cut weight
assertEquals(0.875, mc.getCutWeight(),0);
//Test the cut edge set
List<DefaultWeightedEdge> l3=Arrays.asList(new DefaultWeightedEdge[] {e03, e02, e12, e46});
Set<DefaultWeightedEdge> cutEdges=new HashSet<DefaultWeightedEdge>(l3);
assertEquals(cutEdges, mc.getCutEdges());
cutEdges=mc.getCutEdges();
double weight=0;
for(DefaultWeightedEdge e: cutEdges)
weight+=graph.getEdgeWeight(e);
assertEquals(0.875, weight,0.000000001);
}
示例7: SubtourSeparator
import org.jgrapht.Graphs; //导入方法依赖的package包/类
/**
* This method instantiates the Subtour Separator. The input can be any type of graph: directed, undirected, or mixed,
* complete or incomplete, weighted or without weights. Internally, this class converts the given graph to a undirected graph.
* Multiple edges between two vertices i,j, for example two direct arc (i,j) and (j,i)) are aggregated into in undirected edge (i,j).
* WARNING: if the input graph is modified, i.e. edges or vertices are added/removed then the behavior of this class is undefined!
* A new instance should of this class should be made if this happens!
* @param inputGraph input graph
*/
public SubtourSeparator(Graph<V,E> inputGraph){
this.inputGraph=inputGraph;
this.workingGraph=new SimpleWeightedGraph<>(DefaultWeightedEdge.class);
Graphs.addAllVertices(workingGraph, inputGraph.vertexSet());
for(E edge : inputGraph.edgeSet())
Graphs.addEdge(workingGraph, inputGraph.getEdgeSource(edge), inputGraph.getEdgeTarget(edge),0);
}