org.jgrapht.alg.util.Pair Java Examples

The following examples show how to use org.jgrapht.alg.util.Pair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
private void saveShortestPaths(final List<E> edges, final V source, final V target) {
	V s = source;
	final IList<IList<E>> spl = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType()));
	spl.add(GamaListFactory.createWithoutCasting(getGamlType().getContentType(), edges));
	shortestPathComputed.put(new Pair<>(source, target), spl);
	final List<E> edges2 = GamaListFactory.create(graphScope, getGamlType().getContentType(), edges);
	for (int i = 0; i < edges.size(); i++) {
		final E edge = edges2.remove(0);
		if (edges2.isEmpty()) {
			break;
		}
		// DEBUG.LOG("s : " + s + " j : " + j + " i: " + i);
		V nwS = (V) this.getEdgeTarget(edge);
		if (!directed && nwS.equals(s)) {
			nwS = (V) this.getEdgeSource(edge);
		}
		final Pair<V, V> pp = new Pair<>(nwS, target);
		if (!shortestPathComputed.containsKey(pp)) {
			final IList<IList<E>> spl2 = GamaListFactory.create(getGamlType().getContentType());
			spl2.add(GamaListFactory.createWithoutCasting(getGamlType().getContentType(), edges2));
			shortestPathComputed.put(pp, spl2);
		}
		s = nwS;

	}
}
 
Example #2
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public IList<IList<E>> computeKBestRoutesBetween(final IScope scope, final V source, final V target, final int k) {
	final Pair<V, V> pp = new Pair<>(source, target);
	final IList<IList<E>> paths = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType()));
	final IList<IList<E>> sps = shortestPathComputed.get(pp);
	if (sps != null && sps.size() >= k) {
		for (final IList<E> sp : sps) {
			paths.add(GamaListFactory.create(scope, getGamlType().getContentType(), sp));
		}
	} else {
		final KShortestPaths<V, E> kp = new KShortestPaths<>(getProxyGraph(), k);

		final List<GraphPath<V, E>> pathsJGT = kp.getPaths(source, target);
		final IList<IList<E>> el = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType()));
		for (final GraphPath<V, E> p : pathsJGT) {
			paths.add(GamaListFactory.create(scope, getGamlType().getContentType(), p.getEdgeList()));
			if (saveComputedShortestPaths) {
				el.add(GamaListFactory.create(scope, getGamlType().getContentType(), p.getEdgeList()));
			}
		}
		if (saveComputedShortestPaths) {
			shortestPathComputed.put(pp, el);
		}
	}
	return paths;
}
 
Example #3
Source File: RandomWalkEdgeIterator.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Override
public V next() {
    Pair<V, E> nextEdge = nextEdge();
    if (nextEdge == null) {
        return null;
    }
    return nextEdge.getFirst();
}
 
Example #4
Source File: TestUpdateGraph.java    From coordination_oru with GNU General Public License v3.0 5 votes vote down vote up
static void addEdge(int source, int target) {
	HashMap<Pair<Integer,Integer>, Integer> edgesToAdd = new HashMap<Pair<Integer,Integer>, Integer>();
	edgesToAdd.put(new Pair<Integer,Integer>(source,target), 1);
	long startTime = Calendar.getInstance().getTimeInMillis();
	addEdges(edgesToAdd);
	stat1 = stat1 + "\t" + Long.toString(Calendar.getInstance().getTimeInMillis()-startTime);
}
 
Example #5
Source File: TestUpdateGraph.java    From coordination_oru with GNU General Public License v3.0 5 votes vote down vote up
static void deleteEdge(int source, int target) {
	HashMap<Pair<Integer,Integer>, Integer> edgesToDelete = new HashMap<Pair<Integer,Integer>, Integer>();
	edgesToDelete.put(new Pair<Integer,Integer>(source,target), 1);
	long startTime = Calendar.getInstance().getTimeInMillis();
	deleteEdges(edgesToDelete);
	stat2 = stat2 + "\t" + Long.toString(Calendar.getInstance().getTimeInMillis()-startTime);
}
 
Example #6
Source File: FloydWarshallShortestPathsGAMA.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
private GraphPath<V, E> getShortestPathImpl(final V a, final V b) {
	int v_a = vertices.indexOf(a);
	final int v_b = vertices.indexOf(b);
	int prev = v_a;
	final List<E> edges = new ArrayList<>();
	if (matrix != null) {
		v_a = matrix.get(GAMA.getRuntimeScope(), v_b, v_a);
		if (v_a != -1) {
			while (prev != v_b) {
				final Set<E> eds = graph.getAllEdges(vertices.get(prev), vertices.get(v_a));
				if (!eds.isEmpty()) {
					double minW = Double.MAX_VALUE;
					E ed = null;
					for (final E e : eds) {
						final double w = graph.getEdgeWeight(e);
						if (w < minW) {
							minW = w;
							ed = e;
						}
					}
					edges.add(ed);
				} else {
					return null;
				}
				if (prev != v_b) {
					prev = v_a;
					v_a = matrix.get(GAMA.getRuntimeScope(), v_b, v_a);
				}
			}
		}
	} else {
		shortestPathRecur(edges, v_a, v_b);
	}

	// no path, return null
	if (edges.size() < 1) { return null; }
	final GraphWalk<V, E> path = new GraphWalk<>(graph, a, b, edges, edges.size());
	if (graph.isSaveComputedShortestPaths()) {
		final V v_i = vertices.get(v_a);
		final V v_j = vertices.get(v_b);

		paths.put(new Pair<>(v_i, v_j), path);
		nShortestPaths++;
	}
	return path;
}
 
Example #7
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public IList savePaths(final int M[], final IList vertices, final int nbvertices, final Object v1, final int i,
		final int t) {
	IList edgesVertices = GamaListFactory.create(getGamlType().getContentType());
	for (int j = 0; j < nbvertices; j++) {
		final IList<E> edges = GamaListFactory.create(getGamlType().getContentType());
		final V vt = (V) vertices.get(j);
		if (v1 == vt) {
			continue;
		}
		Object vc = vt;
		int previous;
		int next = M[j];
		if (j == next || next == -1) {
			continue;
		}
		do {
			final Object vn = vertices.get(next);

			final Set<E> eds = this.getAllEdges(vn, vc);

			E edge = null;
			for (final E ed : eds) {
				if (edge == null || getEdgeWeight(ed) < getEdgeWeight(edge)) {
					edge = ed;
				}
			}
			if (edge == null) {
				break;
			}
			edges.add(0, edge);
			previous = next;
			next = M[next];
			vc = vn;
		} while (previous != i);
		final Pair vv = new Pair(v1, vt);
		if (!shortestPathComputed.containsKey(vv)) {
			final IList<IList<E>> ssp = GamaListFactory.create(Types.LIST.of(getGamlType().getContentType()));
			ssp.add(edges);
			shortestPathComputed.put(vv, ssp);
		}
		if (j == t) {
			edgesVertices = edges;
		}
	}
	return edgesVertices;
}
 
Example #8
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public Map<Pair<V, V>, IList<IList<E>>> getShortestPathComputed() {
	return shortestPathComputed;
}
 
Example #9
Source File: GamaGraph.java    From gama with GNU General Public License v3.0 4 votes vote down vote up
public IList<E> getShortestPath(final V s, final V t) {
	final Pair<V, V> vp = new Pair<>(s, t);
	final IList<IList<E>> ppc = shortestPathComputed.get(vp);
	if (ppc == null || ppc.isEmpty()) { return null; }
	return ppc.get(0);
}