提交 d2af0fbc 编写于 作者: M Mathieu Bastian

Update the AlgorithmsPlugin to the new Graph API

上级 431fc592
......@@ -14,10 +14,6 @@
<packaging>nbm</packaging>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>dynamic-api</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>graph-api</artifactId>
......
......@@ -45,7 +45,6 @@ import java.awt.Color;
import java.util.HashMap;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.NodeData;
/**
*
......@@ -53,27 +52,27 @@ import org.gephi.graph.api.NodeData;
*/
public abstract class AbstractShortestPathAlgorithm {
protected final HashMap<NodeData, Color> colors;
protected final HashMap<NodeData, Double> distances;
protected final HashMap<Node, Color> colors;
protected final HashMap<Node, Double> distances;
protected final Node sourceNode;
protected double maxDistance = 0;
public AbstractShortestPathAlgorithm(Node sourceNode) {
this.sourceNode = sourceNode;
colors = new HashMap<NodeData, Color>();
distances = new HashMap<NodeData, Double>();
colors = new HashMap<Node, Color>();
distances = new HashMap<Node, Double>();
}
protected boolean relax(Edge edge) {
Node source = edge.getSource();
Node target = edge.getTarget();
double distSource = distances.get(source.getNodeData());
double distTarget = distances.get(target.getNodeData());
double distSource = distances.get(source);
double distTarget = distances.get(target);
double weight = edgeWeight(edge);
double sourceWeight = distSource + weight;
if (sourceWeight < distTarget) {
distances.put(target.getNodeData(), sourceWeight);
distances.put(target, sourceWeight);
maxDistance = Math.max(maxDistance, sourceWeight);
return true;
} else {
......@@ -91,7 +90,7 @@ public abstract class AbstractShortestPathAlgorithm {
public abstract Edge getPredecessorIncoming(Node node);
public HashMap<NodeData, Double> getDistances() {
public HashMap<Node, Double> getDistances() {
return distances;
}
......
/*
Copyright 2008-2010 Gephi
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
Website : http://www.gephi.org
This file is part of Gephi.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2011 Gephi Consortium. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://gephi.org/about/legal/license-notice/
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
specific language governing permissions and limitations under the
License. When distributing the software, include this License Header
Notice in each file and include the License files at
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"
If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 3, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 3] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 3 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 3 code and therefore, elected the GPL
Version 3 license, then the option applies only if the new code is
made subject to such option by the copyright holder.
Contributor(s):
Portions Copyrighted 2011 Gephi Consortium.
Copyright 2008-2010 Gephi
Authors : Mathieu Bastian <mathieu.bastian@gephi.org>
Website : http://www.gephi.org
This file is part of Gephi.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2011 Gephi Consortium. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://gephi.org/about/legal/license-notice/
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
specific language governing permissions and limitations under the
License. When distributing the software, include this License Header
Notice in each file and include the License files at
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"
If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 3, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 3] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 3 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 3 code and therefore, elected the GPL
Version 3 license, then the option applies only if the new code is
made subject to such option by the copyright holder.
Contributor(s):
Portions Copyrighted 2011 Gephi Consortium.
*/
package org.gephi.algorithms.shortestpath;
import java.util.HashMap;
import org.gephi.data.attributes.type.TimeInterval;
import org.gephi.dynamic.DynamicUtilities;
import org.gephi.dynamic.api.DynamicController;
import org.gephi.graph.api.DirectedGraph;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.NodeData;
import org.openide.util.Lookup;
/**
*
......@@ -58,19 +53,15 @@ import org.openide.util.Lookup;
public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgorithm {
protected final DirectedGraph graph;
protected final HashMap<NodeData, Edge> predecessors;
protected TimeInterval timeInterval;
protected final HashMap<Node, Edge> predecessors;
public BellmanFordShortestPathAlgorithm(DirectedGraph graph, Node sourceNode) {
super(sourceNode);
this.graph = graph;
predecessors = new HashMap<NodeData, Edge>();
DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
if (dynamicController != null) {
timeInterval = DynamicUtilities.getVisibleInterval(dynamicController.getModel(graph.getGraphModel().getWorkspace()));
}
predecessors = new HashMap<Node, Edge>();
}
@Override
public void compute() {
graph.readLock();
......@@ -78,10 +69,10 @@ public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgori
//Initialize
int nodeCount = 0;
for (Node node : graph.getNodes()) {
distances.put(node.getNodeData(), Double.POSITIVE_INFINITY);
distances.put(node, Double.POSITIVE_INFINITY);
nodeCount++;
}
distances.put(sourceNode.getNodeData(), 0d);
distances.put(sourceNode, 0d);
//Relax edges repeatedly
......@@ -92,7 +83,7 @@ public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgori
Node target = edge.getTarget();
if (relax(edge)) {
relaxed = true;
predecessors.put(target.getNodeData(), edge);
predecessors.put(target, edge);
}
}
if (!relaxed) {
......@@ -103,7 +94,7 @@ public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgori
//Check for negative-weight cycles
for (Edge edge : graph.getEdges()) {
if (distances.get(edge.getSource().getNodeData()) + edgeWeight(edge) < distances.get(edge.getTarget().getNodeData())) {
if (distances.get(edge.getSource()) + edgeWeight(edge) < distances.get(edge.getTarget())) {
graph.readUnlock();
throw new RuntimeException("The Graph contains a negative-weighted cycle");
}
......@@ -114,16 +105,14 @@ public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgori
@Override
protected double edgeWeight(Edge edge) {
if (timeInterval != null) {
return edge.getWeight(timeInterval.getLow(), timeInterval.getHigh());
}
return edge.getWeight();
}
@Override
public Node getPredecessor(Node node) {
Edge edge = predecessors.get(node.getNodeData());
Edge edge = predecessors.get(node);
if (edge != null) {
if (edge.getSource().getNodeData() != node.getNodeData()) {
if (edge.getSource() != node) {
return edge.getSource();
} else {
return edge.getTarget();
......@@ -132,7 +121,8 @@ public class BellmanFordShortestPathAlgorithm extends AbstractShortestPathAlgori
return null;
}
@Override
public Edge getPredecessorIncoming(Node node) {
return predecessors.get(node.getNodeData());
return predecessors.get(node);
}
}
......@@ -44,104 +44,93 @@ package org.gephi.algorithms.shortestpath;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import org.gephi.data.attributes.type.TimeInterval;
import org.gephi.dynamic.DynamicUtilities;
import org.gephi.dynamic.api.DynamicController;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Graph;
import org.gephi.graph.api.Node;
import org.gephi.graph.api.NodeData;
import org.openide.util.Lookup;
/**
*
* @author Mathieu Bastian
*/
public class DijkstraShortestPathAlgorithm extends AbstractShortestPathAlgorithm {
protected final Graph graph;
protected final HashMap<NodeData, Edge> predecessors;
protected TimeInterval timeInterval;
protected final HashMap<Node, Edge> predecessors;
public DijkstraShortestPathAlgorithm(Graph graph, Node sourceNode) {
super(sourceNode);
this.graph = graph;
predecessors = new HashMap<NodeData, Edge>();
DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class);
if (dynamicController != null) {
timeInterval = DynamicUtilities.getVisibleInterval(dynamicController.getModel(graph.getGraphModel().getWorkspace()));
}
predecessors = new HashMap<Node, Edge>();
}
@Override
public void compute() {
graph.readLock();
Set<Node> unsettledNodes = new HashSet<Node>();
Set<NodeData> settledNodes = new HashSet<NodeData>();
Set<Node> settledNodes = new HashSet<Node>();
//Initialize
for (Node node : graph.getNodes()) {
distances.put(node.getNodeData(), Double.POSITIVE_INFINITY);
distances.put(node, Double.POSITIVE_INFINITY);
}
distances.put(sourceNode.getNodeData(), 0d);
distances.put(sourceNode, 0d);
unsettledNodes.add(sourceNode);
while (!unsettledNodes.isEmpty()) {
// find node with smallest distance value
Double minDistance = Double.POSITIVE_INFINITY;
Node minDistanceNode = null;
for (Node k : unsettledNodes) {
Double dist = distances.get(k.getNodeData());
Double dist = distances.get(k);
if (minDistanceNode == null) {
minDistanceNode = k;
}
if (dist.compareTo(minDistance) < 0) {
minDistance = dist;
minDistanceNode = k;
}
}
unsettledNodes.remove(minDistanceNode);
settledNodes.add(minDistanceNode.getNodeData());
settledNodes.add(minDistanceNode);
for (Edge edge : graph.getEdges(minDistanceNode)) {
Node neighbor = graph.getOpposite(minDistanceNode, edge);
if (!settledNodes.contains(neighbor.getNodeData())) {
if (!settledNodes.contains(neighbor)) {
double dist = getShortestDistance(minDistanceNode) + edgeWeight(edge);
if (getShortestDistance(neighbor) > dist) {
distances.put(neighbor.getNodeData(), dist);
predecessors.put(neighbor.getNodeData(), edge);
distances.put(neighbor, dist);
predecessors.put(neighbor, edge);
unsettledNodes.add(neighbor);
maxDistance = Math.max(maxDistance, dist);
}
}
}
}
graph.readUnlock();
}
private double getShortestDistance(Node destination) {
Double d = distances.get(destination.getNodeData());
Double d = distances.get(destination);
if (d == null) {
return Double.POSITIVE_INFINITY;
} else {
return d;
}
}
@Override
protected double edgeWeight(Edge edge) {
if (timeInterval != null) {
return edge.getWeight(timeInterval.getLow(), timeInterval.getHigh());
}
return edge.getWeight();
}
@Override
public Node getPredecessor(Node node) {
Edge edge = predecessors.get(node.getNodeData());
Edge edge = predecessors.get(node);
if (edge != null) {
if (edge.getSource() != node) {
return edge.getSource();
......@@ -151,8 +140,9 @@ public class DijkstraShortestPathAlgorithm extends AbstractShortestPathAlgorithm
}
return null;
}
@Override
public Edge getPredecessorIncoming(Node node) {
return predecessors.get(node.getNodeData());
return predecessors.get(node);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册