using System.Collections;
namespace GisSharpBlog.NetTopologySuite.Planargraph.Algorithm
{
///
/// Finds all connected s of a .
///
public class ConnectedSubgraphFinder
{
private readonly PlanarGraph graph;
///
/// Initializes a new instance of the class.
///
/// The .
public ConnectedSubgraphFinder(PlanarGraph graph)
{
this.graph = graph;
}
///
///
///
///
public IList GetConnectedSubgraphs()
{
IList subgraphs = new ArrayList();
GraphComponent.SetVisited(graph.GetNodeEnumerator(), false);
IEnumerator ienum = graph.GetEdgeEnumerator();
while (ienum.MoveNext())
{
Edge e = ienum.Current as Edge;
Node node = e.GetDirEdge(0).FromNode;
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.
///
///
///
private void AddReachable(Node startNode, Subgraph subgraph)
{
Stack nodeStack = new Stack();
nodeStack.Push(startNode);
while (!(nodeStack.Count == 0))
{
Node node = (Node) nodeStack.Pop();
AddEdges(node, nodeStack, subgraph);
}
}
///
/// Adds the argument node and all its out edges to the subgraph.
///
///
///
///
private void AddEdges(Node node, Stack nodeStack, Subgraph subgraph)
{
node.Visited = true;
IEnumerator i = ((DirectedEdgeStar) node.OutEdges).GetEnumerator();
while (i.MoveNext())
{
DirectedEdge de = (DirectedEdge) i.Current;
subgraph.Add(de.Edge);
Node toNode = de.ToNode;
if (!toNode.IsVisited)
{
nodeStack.Push(toNode);
}
}
}
}
}