本文整理匯總了Java中org.jgrapht.Graph.getEdgeSource方法的典型用法代碼示例。如果您正苦於以下問題:Java Graph.getEdgeSource方法的具體用法?Java Graph.getEdgeSource怎麽用?Java Graph.getEdgeSource使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.jgrapht.Graph
的用法示例。
在下文中一共展示了Graph.getEdgeSource方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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;
}
示例2: expandConnectedVertices
import org.jgrapht.Graph; //導入方法依賴的package包/類
/**
*
* @param g
* @param newVertex
* @param connectedVertices
*/
public static <V,E> void expandConnectedVertices(Graph<V,E> g, V newVertex, Set<V> connectedVertices){
//for every edge (ignoring direction) connected newVertex
for(E edge : g.edgesOf(newVertex)){
V connectedVertex; //find the other vertex connected to newVertex
if(g.getEdgeSource(edge).equals(newVertex)){
connectedVertex = g.getEdgeTarget(edge);
} else {
connectedVertex = g.getEdgeSource(edge);
}
if(!connectedVertices.contains(connectedVertex)){ //if this vertex is not in connectedVertices
connectedVertices.add(connectedVertex); //add it and search for more connected Vertices
expandConnectedVertices(g, connectedVertex, connectedVertices);
}
}
}
示例3: 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;
}
示例4: buildGraph
import org.jgrapht.Graph; //導入方法依賴的package包/類
public static ListenableGraph<String, MyEdge> buildGraph(double threshold, boolean showEdgeLabels) {
ListenableDirectedWeightedGraph<String, MyEdge> g =
new ListenableDirectedWeightedGraph<String, MyEdge>(MyEdge.class);
Graph<Integer, DefaultWeightedEdge> inGraph = getGraph();
Set<DefaultWeightedEdge> edgeSet = inGraph.edgeSet();
for (DefaultWeightedEdge e : edgeSet) {
int sourceV = inGraph.getEdgeSource(e);
String v1 = names[sourceV];
int targetV = inGraph.getEdgeTarget(e);
String v2 = names[targetV];
g.addVertex(v1);
g.addVertex(v2);
double w = inGraph.getEdgeWeight(e);
if(w>threshold) {
MyEdge edge = g.addEdge(v1, v2);
if(showEdgeLabels){
g.setEdgeWeight(edge, w);
}
}
}
return g;
}
示例5: write
import org.jgrapht.Graph; //導入方法依賴的package包/類
private void write(Path path, Graph<Sense<String>, DefaultWeightedEdge> graph) throws IOException {
try (final BufferedWriter writer = Files.newBufferedWriter(path)) {
for (final DefaultWeightedEdge edge : graph.edgeSet()) {
final IndexedSense<String> source = (IndexedSense<String>) graph.getEdgeSource(edge);
final IndexedSense<String> target = (IndexedSense<String>) graph.getEdgeTarget(edge);
writer.write(String.format(Locale.ROOT, "%s#%d\t%s#%d\t%f\n",
source.get(), source.getSense(),
target.get(), target.getSense(),
graph.getEdgeWeight(edge)));
}
}
}
示例6: getOppositeVertex
import org.jgrapht.Graph; //導入方法依賴的package包/類
public static IShape getOppositeVertex(final Graph<IShape, IShape> g, final IShape e, final IShape v) {
final IShape source = g.getEdgeSource(e);
final IShape target = g.getEdgeTarget(e);
if (v.equals(source)) {
return target;
} else if (v.equals(target)) {
return source;
} else {
return v.euclidianDistanceTo(source) > v.euclidianDistanceTo(target) ? target : source;
}
}
示例7: getPathsFromGraphHopper
import org.jgrapht.Graph; //導入方法依賴的package包/類
private List<Path> getPathsFromGraphHopper(Graph<AutomatVertex, DefaultEdge> completeGraph) {
LOG.info("--- getPathsFromGraphHopper ---");
final Set<AutomatEdge> automatEdges = Sets.newHashSet();
final Iterator<AutomatVertex> graphIterator = new DepthFirstIterator<>(completeGraph);
while (graphIterator.hasNext()) {
final AutomatVertex vertex = graphIterator.next();
Set<DefaultEdge> edges = completeGraph.edgesOf(vertex);
for (DefaultEdge edge : edges) {
AutomatVertex source = completeGraph.getEdgeSource(edge);
AutomatVertex target = completeGraph.getEdgeTarget(edge);
automatEdges.add(new AutomatEdge(source, target));
}
}
final List<Path> paths = Lists.newArrayList();
final int count = automatEdges.size();
int idx = 0;
int errors = 0;
final StopWatch stopWatch = new StopWatch("route");
for (AutomatEdge automatEdge : automatEdges) {
// if (idx == 10) break;
stopWatch.start();
final Path computedPath = graphHopperGetPath(automatEdge.source, automatEdge.target);
if (computedPath != null) {
paths.add(computedPath);
} else {
errors++;
}
stopWatch.stop();
if (idx % 1000 == 0) {
LOG.info(String.format("%.2f %s", (float) idx / (float) count * 100.0f, "%"));
}
idx++;
}
LOG.info(String.format("%.2f %s", (float) idx / (float) count * 100.0f, "%"));
LOG.info(stopWatch.shortSummary());
if (errors > 0) {
LOG.error("Errors count is {} which is {} from all data {}", errors, String.format("%.4f %s", (float) errors / (float) automatEdges.size() * 100.0f, "%"), automatEdges.size());
}
return paths;
}