Skip to content

Commit

Permalink
Merge pull request #19 from jlaw9/master
Browse files Browse the repository at this point in the history
Create Bi-Directional copy of original network
  • Loading branch information
jlaw9 committed May 25, 2017
2 parents 1fc37db + 8ddb7b7 commit 4ce831f
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 283 deletions.
122 changes: 18 additions & 104 deletions src/main/java/com/dpgil/pathlinker/path_linker/internal/Algorithms.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public class Algorithms
private static HashSet<CyEdge> initialHiddenEdges;
private static HashSet<CyEdge> hiddenEdges;
private static HashMap<CyEdge, Double> _edgeWeights;
// Boolean indicating whether or not to include the undirected outgoing/incoming edges
// For now, always use both undirected and directed edges
private static boolean includeUndirected = true;


/**
Expand Down Expand Up @@ -215,7 +212,7 @@ public static ArrayList<Path> ksp(
// incoming edges to this node and all previous nodes have
// been hidden
List<CyEdge> inEdges =
getAdjacentEdgeList(network, nodeSpur, CyEdge.Type.INCOMING);
network.getAdjacentEdgeList(nodeSpur, CyEdge.Type.INCOMING);
for (CyEdge inEdge : inEdges)
{
hiddenEdges.add(inEdge);
Expand Down Expand Up @@ -419,11 +416,10 @@ public int compare(AStarData data1, AStarData data2)

// examine all neighbors to this node and consider adding them to
// the fringe
List<CyNode> neighbors =
getNeighborList(network, currNode, CyEdge.Type.OUTGOING);
for (CyNode nextNode : neighbors)
List<CyEdge> neighbors =
network.getAdjacentEdgeList(currNode, CyEdge.Type.OUTGOING);
for (CyEdge nextEdge : neighbors)
{
CyEdge nextEdge = getEdge(network, currNode, nextNode);
// doesn't consider edges that are hidden. uses this structure
// of hiding edges because manipulating the graph completely
// dominates runtime in cytoscape
Expand All @@ -432,6 +428,8 @@ public int compare(AStarData data1, AStarData data2)
continue;
}

CyNode nextNode = nextEdge.getTarget();

// the actual distance to the node from the source
double nextActDist =
currData.actDist + getWeight(network, nextEdge);
Expand Down Expand Up @@ -529,11 +527,10 @@ public int compare(CyNode o1, CyNode o2)
// that lead to the target. however, we don't want to reverse the
// network and call a normal SSD because manipulating the network
// dominates runtime in Cytoscape
for (CyNode neighbor :
getNeighborList(network, current, CyEdge.Type.INCOMING))
for (CyEdge neighborEdge : network
.getAdjacentEdgeList(current, CyEdge.Type.INCOMING))
{

CyEdge neighborEdge = getEdge(network, neighbor, current);
CyNode neighbor = neighborEdge.getSource();

double newCost =
distances.get(current) + getWeight(network, neighborEdge);
Expand Down Expand Up @@ -585,10 +582,10 @@ public static HashMap<CyNode, Double> singleSourceDijkstra(
CyNode current = pq.remove(0);

// goes through the neighbors
for (CyNode neighbor :
getNeighborList(network, current, CyEdge.Type.OUTGOING))
for (CyEdge neighborEdge : network
.getAdjacentEdgeList(current, CyEdge.Type.OUTGOING))
{
CyEdge neighborEdge = getEdge(network, current, neighbor);
CyNode neighbor = neighborEdge.getTarget();

double newCost =
distances.get(current) + getWeight(network, neighborEdge);
Expand Down Expand Up @@ -662,12 +659,13 @@ public int compare(CyNode o1, CyNode o2)
// return path reconstructed
break;
}

// goes through the neighbors
for (CyNode neighbor :
getNeighborList(network, current, CyEdge.Type.OUTGOING))
for (CyEdge neighborEdge : network
.getAdjacentEdgeList(current, CyEdge.Type.OUTGOING))
{
CyNode neighbor = neighborEdge.getTarget();

CyEdge neighborEdge = getEdge(network, current, neighbor);
double newCost =
distances.get(current) + getWeight(network, neighborEdge);

Expand Down Expand Up @@ -763,14 +761,14 @@ public static CyEdge getEdge(
CyNode source,
CyNode target)
{
List<CyEdge> dirConnections =
List<CyEdge> connections =
network.getConnectingEdgeList(source, target, CyEdge.Type.DIRECTED);

// getConnectingEdgeList() returns both the incoming and outgoing edges connected to the two nodes
// Using CyEdge.Type.OUTGOING gave strange results in testing, so I stuck with this loop
// Use this for loop to get the edge directed from source->target
// Currently PathLinker does not support multi-graphs, so just return the first edge found
for (CyEdge edge : dirConnections)
for (CyEdge edge : connections)
{
if (edge.getSource().equals(source)
&& edge.getTarget().equals(target))
Expand All @@ -780,92 +778,8 @@ public static CyEdge getEdge(
}
}

// If undirected edges are allowed, then also check for an undirected edge between them
// Currently directed edges are favored over undirected edges
if (includeUndirected)
{
List<CyEdge> undirConnections =
network.getConnectingEdgeList(source, target, CyEdge.Type.UNDIRECTED);
for (CyEdge edge : undirConnections)
{
return edge;
}

}

return null;
}


/**
* Returns the list of edges connected to this node based on the specified CyEdge.Type
* This is a wrapper around the built-in CyNetwork function getAdjacentEdgeList. In the built-in function:
* If CyEdge.Type.OUTGOING is passed, the directed outgoing adjacent edges will be returned
* If CyEdge.Type.INCOMING is passed, the directed incoming adjacent edges will be returned
* If CyEdge.Type.DIRECTED is passed, the directed (outgoing and incoming) adjacent edges will be returned
* If CyEdge.Type.UNDIRECTED is passed, the undirected adjacent edges will be returned
*
* In networks containing both directed and undirected networks, we want to get both the directed and undirected adjacent edges
* hence why this wrapper was made
*
* Undirected edges will be included based on the value of includeUndirected which is currently always true
*
* @param network
* the network
* @param node
* the node to get the connecting edges
* @param edgeType
* Should be either CyEdge.Type.OUTGOING or CyEdge.Type.INCOMING
* @return List<CyEdge> the list of edges (either outgoing or incoming) connected to the given node
*/
public static List<CyEdge> getAdjacentEdgeList(CyNetwork network, CyNode node, CyEdge.Type edgeType)
{

// retrieves all of the directed edges incoming to or outgoing from the given node
List<CyEdge> adjacentEdges = network.getAdjacentEdgeList(node, edgeType);

// If specified, add all of the undirected edges connected to the given node as well
if (includeUndirected){
adjacentEdges.addAll(network.getAdjacentEdgeList(node, CyEdge.Type.UNDIRECTED));
}

return adjacentEdges;
}

/**
* Returns the list of nodes neighboring the input node based on the specified CyEdge.Type
* This is a wrapper around the built-in CyNetwork function getNeighborList. In the built-in function:
* If CyEdge.Type.OUTGOING is passed, the nodes connected by a directed outgoing edge will be returned
* If CyEdge.Type.INCOMING is passed, the nodes connected by a directed incoming edge will be returned
* If CyEdge.Type.DIRECTED is passed, the nodes connected by a directed (outgoing and incoming) edge will be returned
* If CyEdge.Type.UNDIRECTED is passed, the nodes connected by an undirected edge will be returned
*
* In networks containing both directed and undirected networks, we want to get both the directed and undirected neighbors
* hence why this wrapper was made
*
* Undirected edges will be included based on the value of includeUndirected which is currently always true
*
* @param network
* the network
* @param node
* the node for which to get the neighboring nodes
* @param edgeType
* Should be either CyEdge.Type.OUTGOING or CyEdge.Type.INCOMING
* @return List<CyNode> the list of incoming or outgoing neighboring nodes
*/
public static List<CyNode> getNeighborList(CyNetwork network, CyNode node, CyEdge.Type edgeType)
{

// retrieves all of the directed edges incoming to or outgoing from the given node
List<CyNode> neighbors = network.getNeighborList(node, edgeType);

// If specified, add all of the undirected edges connected to the given node as well
if (includeUndirected){
neighbors.addAll(network.getNeighborList(node, CyEdge.Type.UNDIRECTED));
}

return neighbors;
}


/**
Expand Down

0 comments on commit 4ce831f

Please sign in to comment.