1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
//
// DepthFirst.cs
//
// Authors:
// Alexander Chebaturkin (chebaturkin@gmail.com)
//
// Copyright (C) 2011 Alexander Chebaturkin
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
namespace Mono.CodeContracts.Static.DataStructures {
static class DepthFirst {
public static void Visit<Node, EdgeInfo> (IGraph<Node, EdgeInfo> graph,
Predicate<Node> nodeStartVisitor,
EdgeVisitor<Node, EdgeInfo> edgeVisitor)
{
new Visitor<Node, EdgeInfo> (graph, nodeStartVisitor, edgeVisitor).VisitAll ();
}
public static void Visit<Node, EdgeInfo> (IGraph<Node, EdgeInfo> graph,
Node startNode,
Predicate<Node> nodeStartVisitor,
EdgeVisitor<Node, EdgeInfo> edgeVisitor)
{
new Visitor<Node, EdgeInfo> (graph, nodeStartVisitor, edgeVisitor).VisitSubGraphNonRecursive (startNode);
}
#region Nested type: Info
public class Info<Node> {
public readonly Node Parent;
public readonly int StartTime;
public int FinishTime;
public bool SourceOfBackEdge;
public bool TargetOfBackEdge;
public Info (Node parent, int startTime)
{
this.Parent = parent;
this.StartTime = startTime;
}
}
#endregion
#region Nested type: Visitor
public class Visitor<Node, Edge> {
private readonly HashSet<Tuple<Node, Edge, Node>> back_edges = new HashSet<Tuple<Node, Edge, Node>> ();
private readonly EdgeVisitor<Node, Edge> edge_visitor;
private readonly IGraph<Node, Edge> graph;
private readonly Dictionary<Node, Info<Node>> history = new Dictionary<Node, Info<Node>> ();
private readonly Action<Node> node_finish_visitor;
private readonly Predicate<Node> node_start_visitor;
private readonly Stack<SearchFrame> todo = new Stack<SearchFrame> ();
private int time;
public Visitor (IGraph<Node, Edge> graph, Predicate<Node> nodeStartVisitor, EdgeVisitor<Node, Edge> edgeVisitor)
: this (graph, nodeStartVisitor, null, edgeVisitor)
{
}
public Visitor (IGraph<Node, Edge> graph, Predicate<Node> nodeStartVisitor)
: this (graph, nodeStartVisitor, null, null)
{
}
public Visitor (IGraph<Node, Edge> graph, Predicate<Node> nodeStartVisitor, Action<Node> nodeFinishVisitor, EdgeVisitor<Node, Edge> edgeVisitor)
{
this.graph = graph;
this.node_start_visitor = nodeStartVisitor;
this.node_finish_visitor = nodeFinishVisitor;
this.edge_visitor = edgeVisitor;
}
public virtual void VisitAll ()
{
foreach (Node node in this.graph.Nodes)
this.VisitSubGraphNonRecursive (node);
}
public void VisitSubGraphNonRecursive (Node node)
{
ScheduleNode (node, default(Node));
IterativeDFS ();
}
private void IterativeDFS ()
{
while (this.todo.Count > 0) {
SearchFrame frame = this.todo.Peek ();
if (frame.Edges.MoveNext ()) {
Pair<Edge, Node> current = frame.Edges.Current;
VisitEdgeNonRecursive (frame.Info, frame.Node, current.Key, current.Value);
} else {
if (this.node_finish_visitor != null)
this.node_finish_visitor (frame.Node);
frame.Info.FinishTime = ++this.time;
this.todo.Pop ();
}
}
}
private void VisitEdgeNonRecursive(Info<Node> sourceInfo, Node source, Edge info, Node target)
{
if (!VisitEdgeCommon (sourceInfo, source, info, target))
ScheduleNode (target, source);
}
private void VisitEdge (Info<Node> sourceInfo, Node source, Edge info, Node target)
{
if (!VisitEdgeCommon (sourceInfo, source, info, target))
VisitSubGraph (target, source);
}
private bool VisitEdgeCommon(Info<Node> sourceInfo, Node source, Edge info, Node target )
{
if (this.edge_visitor != null)
this.edge_visitor(source, info, target);
Info<Node> targetInfo;
if (this.history.TryGetValue(target, out targetInfo))
{
if (targetInfo.FinishTime != 0)
return true;
targetInfo.TargetOfBackEdge = true;
sourceInfo.SourceOfBackEdge = true;
this.back_edges.Add(new Tuple<Node, Edge, Node>(source, info, target));
return true;
}
return false;
}
public void VisitSubGraph (Node node, Node parent)
{
if (this.history.ContainsKey (node))
return;
var info = new Info<Node> (parent, ++this.time);
this.history [node] = info;
if (this.node_start_visitor != null && !this.node_start_visitor (node))
return;
VisitSuccessors (info, node);
if (this.node_finish_visitor != null)
this.node_finish_visitor (node);
info.FinishTime = ++this.time;
}
public void ScheduleNode (Node node, Node parent)
{
if (this.history.ContainsKey (node))
return;
var info = new Info<Node> (parent, ++this.time);
this.history [node] = info;
if (this.node_start_visitor != null && !this.node_start_visitor (node))
return;
this.todo.Push (new SearchFrame (node, this.graph.Successors (node).GetEnumerator (), info));
}
private void VisitSuccessors (Info<Node> info, Node node)
{
foreach (var successor in this.graph.Successors (node))
VisitEdge (info, node, successor.Key, successor.Value);
}
public bool IsVisited (Node node)
{
return this.history.ContainsKey (node);
}
public bool IsBackEdge (Node source, Edge info, Node target)
{
return this.back_edges.Contains (new Tuple<Node, Edge, Node> (source, info, target));
}
public Info<Node> DepthFirstInfo (Node node)
{
return this.history [node];
}
#region Nested type: SearchFrame
private struct SearchFrame {
public readonly IEnumerator<Pair<Edge, Node>> Edges;
public readonly Info<Node> Info;
public readonly Node Node;
public SearchFrame (Node node, IEnumerator<Pair<Edge, Node>> edges, Info<Node> info)
{
this.Node = node;
this.Edges = edges;
this.Info = info;
}
}
#endregion
}
#endregion
}
}
|