本文整理汇总了Java中org.jgrapht.graph.SimpleWeightedGraph.addVertex方法的典型用法代码示例。如果您正苦于以下问题:Java SimpleWeightedGraph.addVertex方法的具体用法?Java SimpleWeightedGraph.addVertex怎么用?Java SimpleWeightedGraph.addVertex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.jgrapht.graph.SimpleWeightedGraph
的用法示例。
在下文中一共展示了SimpleWeightedGraph.addVertex方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMST
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
private SimpleWeightedGraph<MSTNode<T>, DefaultWeightedEdge> getMST() {
if (graph.vertexSet().size() == 1)
{
return graph; // PrimMinimum
}
SpanningTree<DefaultWeightedEdge> internMst = new KruskalMinimumSpanningTree<>(graph)
.getSpanningTree();
Set<DefaultWeightedEdge> edges = internMst.getEdges();
Set<MSTNode<T>> nodes = graph.vertexSet();
SimpleWeightedGraph<MSTNode<T>, DefaultWeightedEdge> internSWG = new SimpleWeightedGraph<>(
DefaultWeightedEdge.class);
for (MSTNode<T> node : nodes) {
internSWG.addVertex(node);
}
for (DefaultWeightedEdge dwe : edges) {
DefaultWeightedEdge newEdge = internSWG.addEdge(graph.getEdgeSource(dwe),
graph.getEdgeTarget(dwe));
internSWG.setEdgeWeight(newEdge, graph.getEdgeWeight(dwe));
}
return internSWG;
}
示例2: directedGraph2simpleGraph
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
/**
* converts a directed weighted graph into a simple weighted graph by
* keeping the maximum weight of 2 parallel edges
*/
public static SimpleWeightedGraph<Long, DefaultWeightedEdge> directedGraph2simpleGraph(
DefaultDirectedWeightedGraph<Long, DefaultWeightedEdge> directedG) {
SimpleWeightedGraph<Long, DefaultWeightedEdge> simpleG = new SimpleWeightedGraph<Long, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
// Parcourt les sommets du graphe orienté et les copie dans le graphe
// simple
for (Long v : directedG.vertexSet()) {
simpleG.addVertex(v);
}
// Parcourt l'ensemble des arcs du graphe orienté
for (DefaultWeightedEdge e : directedG.edgeSet()) {
Long nodeIni = directedG.getEdgeSource(e);
Long nodeFin = directedG.getEdgeTarget(e);
double w = directedG.getEdgeWeight(e);
if (!simpleG.containsEdge(nodeIni, nodeFin)) {
simpleG.addEdge(nodeIni, nodeFin, e);
} else {
// On prend le poids max de deux arcs parallèles
DefaultWeightedEdge e1 = simpleG.getEdge(nodeFin, nodeIni);
if (simpleG.getEdgeWeight(e1) < w)
simpleG.setEdgeWeight(e1, w);
}
}
System.out.println("Graphe simple créé : " + simpleG.vertexSet().size() + " sommets et "
+ simpleG.edgeSet().size() + " arcs");
// for (DefaultWeightedEdge e : simpleG.edgeSet()) {
// if (simpleG.getEdgeSource(e) == (long) 210173)
// System.out.println(
// "Source 210173 - Target " + simpleG.getEdgeTarget(e) + " - Poids " +
// simpleG.getEdgeWeight(e));
// }
return simpleG;
}
示例3: monoplex
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
public static SimpleWeightedGraph<Long, DefaultWeightedEdge> monoplex(
ArrayList<double[][]> multiplexAdjacencyMatrix, HashMap<Long, Integer> contributorIndex) {
SimpleWeightedGraph<Long, DefaultWeightedEdge> simpleG = new SimpleWeightedGraph<Long, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
// Parcourt les sommets du graphe orienté et les copie dans le graphe
// simple
for (Long v : contributorIndex.keySet()) {
simpleG.addVertex(v);
}
// Add edges
int nbContributors = multiplexAdjacencyMatrix.get(0).length;
int dim = multiplexAdjacencyMatrix.size();
System.out.println("nb contributors = " + nbContributors + "; nb dimensions = " + dim);
for (int i = 0; i < nbContributors - 1; i++) {
Long uid1 = (Long) contributorIndex.keySet().toArray()[i];
for (int j = i + 1; j < nbContributors; j++) {
Long uid2 = (Long) contributorIndex.keySet().toArray()[j];
int sum = 0;
for (int d = 0; d < dim; d++) {
sum += multiplexAdjacencyMatrix.get(d)[i][j];
}
if (sum == 0)
continue;
DefaultWeightedEdge e = simpleG.addEdge(uid1, uid2);
simpleG.setEdgeWeight(e, (double) sum);
}
}
return simpleG;
}
示例4: extractNetwork
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
protected WeightedGraph<Figure, DefaultWeightedEdge> extractNetwork(JCas jcas, Annotation range) {
SimpleWeightedGraph<Figure, DefaultWeightedEdge> graph = new SimpleWeightedGraph<Figure, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
for (Figure figure : JCasUtil.select(jcas, Figure.class)) {
if (!graph.containsVertex(figure)) {
graph.addVertex(figure);
}
}
for (Scene scene : JCasUtil.select(jcas, Scene.class)) {
List<Speaker> speakers = JCasUtil.selectCovered(Speaker.class, scene);
for (Speaker s1 : speakers) {
Figure gf1 = s1.getFigure();
if (gf1 != null)
for (Speaker s2 : speakers) {
Figure gf2 = s2.getFigure();
if (gf2 != null)
if (graph.containsEdge(gf1, gf2)) {
DefaultWeightedEdge edge = graph.getEdge(gf1, gf2);
double w = graph.getEdgeWeight(edge);
graph.setEdgeWeight(edge, w + 1.0);
} else {
if (gf1 != gf2)
graph.addEdge(gf1, gf2);
}
}
}
;
}
return graph;
}
示例5: createCoLocationGraph
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
public static SimpleWeightedGraph<Long, DefaultWeightedEdge> createCoLocationGraph(
HashMap<Long, OSMContributor> myOSMContributors, List<Double> bbox, List<String> timespan, double threshold)
throws Exception {
SimpleWeightedGraph<Long, DefaultWeightedEdge> g = new SimpleWeightedGraph<Long, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
// Remplit pour chaque OSMContributor l'attribut ActivityAreas
assignActivityAreas(myOSMContributors, bbox, timespan, threshold);
List<OSMContributor> myContributorList = new ArrayList<OSMContributor>();
for (OSMContributor contributor : myOSMContributors.values()) {
myContributorList.add(contributor);
}
// Parcourt la liste des contributeurs
for (int i = 0; i < myContributorList.size() - 1; i++) {
IFeatureCollection<DefaultFeature> areaCurrentContributor = myContributorList.get(i).getActivityAreas();
if (areaCurrentContributor == null)
continue;
if (!g.containsVertex((long) myContributorList.get(i).getId()))
g.addVertex((long) myContributorList.get(i).getId());
// Parcourt les contributeurs suivants de la liste
for (int j = i + 1; j < myOSMContributors.size(); j++) {
if (!g.containsVertex((long) myContributorList.get(j).getId()))
g.addVertex((long) myContributorList.get(j).getId());
IFeatureCollection<DefaultFeature> areaNextContributor = myContributorList.get(j).getActivityAreas();
if (areaNextContributor == null)
continue;
double totalDistance = 0;
// Parcourt les zones d'activité du contributeur courant
for (int u = 0; u < areaCurrentContributor.size(); u++) {
IGeometry currentZone = areaCurrentContributor.get(u).getGeom();
// Parcourt les zones d'activité du contributeur suivant
for (int v = 0; v < areaCurrentContributor.size(); v++) {
IGeometry nextZone = areaCurrentContributor.get(v).getGeom();
// Calcul de la distance surfacique s'il y a
// intersection
if (currentZone.intersects(nextZone)) {
double union = currentZone.union(nextZone).area();
double intersection = currentZone.intersection(nextZone).area();
double distance = 1 - intersection / union;
totalDistance += distance;
}
}
}
if (totalDistance > 0) {
// double union =
// areaCurrentContributor.union(areaNextContributor).area();
// double intersection =
// areaCurrentContributor.intersection(areaNextContributor).area();
// double distance = 1 - intersection / union;
DefaultWeightedEdge e = g.addEdge((long) myContributorList.get(i).getId(),
(long) myContributorList.get(j).getId());
g.setEdgeWeight(e, 1 / totalDistance);
// if (distance > 0)
// g.setEdgeWeight(e, 1 / distance);
// else
// g.setEdgeWeight(e, Double.POSITIVE_INFINITY);
}
}
}
return g;
}
示例6: generateNdPointGraph
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
private static SimpleWeightedGraph<NdPoint, DefaultWeightedEdge> generateNdPointGraph(NdPoint start,
Collection<Zone> allZones, Collection<BoundedMoveableObject> obstacles, int botSize) {
SimpleWeightedGraph<NdPoint, DefaultWeightedEdge> graph = new SimpleWeightedGraph<NdPoint, DefaultWeightedEdge>(
DefaultWeightedEdge.class);
List<NdPoint> vertices = returnVacantPoints(allZones, obstacles, botSize / 2);
vertices.add(start);
// Margin is botsize / 2, since the points move under the center of the bot.
sanitizeVertices(allZones, vertices, obstacles, botSize / 2);
// Add all the vertices.
for (NdPoint p : vertices) {
graph.addVertex(p);
}
/*
Create the edges
Each vertex can have 4 edges in GRID space. Since we won't be considering
diagonal travel.
*/
for (NdPoint vertex : vertices) {
NdPoint[] neighbours = {
new NdPoint(vertex.getX(), vertex.getY() + 1),
new NdPoint(vertex.getX(), vertex.getY() - 1),
new NdPoint(vertex.getX() - 1, vertex.getY()),
new NdPoint(vertex.getX() + 1, vertex.getY())
};
for (NdPoint neighbour : neighbours) {
if (vertices.contains(neighbour)) {
// Default edge distance is 1. So we're good.
graph.addEdge(vertex, neighbour);
}
}
}
return graph;
}
示例7: toGraph
import org.jgrapht.graph.SimpleWeightedGraph; //导入方法依赖的package包/类
public static UnmodifiableGraph<Coordinate, DefaultWeightedEdge> toGraph(GridMap map) {
SimpleWeightedGraph<Coordinate, DefaultWeightedEdge> graph =
new SimpleWeightedGraph<Coordinate, DefaultWeightedEdge>(DefaultWeightedEdge.class);
int dims = map.dims();
int length = map.length();
int[] sizes = map.sizes();
int[] idx = new int[dims];
// Add every vertex that is not an obstacle
for (int v = 0; v < length; v++) {
if (map.get(idx) >= 0) {
graph.addVertex(new IntCoord(idx));
}
for (int i = 0; i < dims; i++) {
if (idx[i] < sizes[i] - 1) {
idx[i]++;
break;
} else {
idx[i] = 0;
}
}
}
// Add every non-obstacle edge
for (int v = 0; v < length; v++) {
for (int i = 0; i < dims; i++) {
if (idx[i] < sizes[i] - 1) {
int[] idx1 = Arrays.copyOf(idx, idx.length);
idx1[i]++;
if (map.get(idx) >= 0 && map.get(idx1) >= 0) {
DefaultWeightedEdge e = graph.addEdge(new IntCoord(idx), new IntCoord(idx1));
graph.setEdgeWeight(e, ((double)map.get(idx) + (double)map.get(idx1))/2.0 + 1.0);
}
}
}
for (int i = 0; i < dims; i++) {
if (idx[i] < sizes[i] - 1) {
idx[i]++;
break;
} else {
idx[i] = 0;
}
}
}
return new UnmodifiableGraph<Coordinate, DefaultWeightedEdge>(graph);
}