All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vividsolutions.jts.planargraph.algorithm.ConnectedSubgraphFinder Maven / Gradle / Ivy

Go to download

The JTS Topology Suite is an API for modelling and manipulating 2-dimensional linear geometry. It provides numerous geometric predicates and functions. JTS conforms to the Simple Features Specification for SQL published by the Open GIS Consortium.

There is a newer version: 1.13
Show newest version
package com.vividsolutions.jts.planargraph.algorithm;

import java.util.*;
import com.vividsolutions.jts.planargraph.*;

/**
 * Finds all connected {@link Subgraph}s of a {@link PlanarGraph}.
 * 

* Note: uses the isVisited flag on the nodes. */ public class ConnectedSubgraphFinder { private PlanarGraph graph; public ConnectedSubgraphFinder(PlanarGraph graph) { this.graph = graph; } public List getConnectedSubgraphs() { List subgraphs = new ArrayList(); GraphComponent.setVisited(graph.nodeIterator(), false); for (Iterator i = graph.edgeIterator(); i.hasNext(); ) { Edge e = (Edge) i.next(); Node node = e.getDirEdge(0).getFromNode(); if (! node.isVisited()) { subgraphs.add(findSubgraph(node)); } } return subgraphs; } private Subgraph findSubgraph(Node node) { Subgraph subgraph = new Subgraph(graph); addReachable(node, subgraph); return subgraph; } /** * Adds all nodes and edges reachable from this node to the subgraph. * Uses an explicit stack to avoid a large depth of recursion. * * @param node a node known to be in the subgraph */ private void addReachable(Node startNode, Subgraph subgraph) { Stack nodeStack = new Stack(); nodeStack.add(startNode); while (! nodeStack.empty()) { Node node = (Node) nodeStack.pop(); addEdges(node, nodeStack, subgraph); } } /** * Adds the argument node and all its out edges to the subgraph. * @param node the node to add * @param nodeStack the current set of nodes being traversed */ private void addEdges(Node node, Stack nodeStack, Subgraph subgraph) { node.setVisited(true); for (Iterator i = ((DirectedEdgeStar) node.getOutEdges()).iterator(); i.hasNext(); ) { DirectedEdge de = (DirectedEdge) i.next(); subgraph.add(de.getEdge()); Node toNode = de.getToNode(); if (! toNode.isVisited()) nodeStack.push(toNode); } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy