public EdgeDepthFirstSearchAlgorithm( IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph, IDictionary <TEdge, GraphColor> colors ) : this(null, visitedGraph, colors) { }
/// <summary> /// Records all the edges that are part of the subtree of v /// </summary> /// <param name="g">visited graph</param> /// <param name="e">root edge</param> /// <param name="maxDepth">maximum expolration depth</param> /// <returns></returns> public static EdgeCollection OutEdgeTree( IEdgeListAndIncidenceGraph g, IEdge e, int maxDepth ) { if (g == null) { throw new ArgumentNullException("g"); } if (e == null) { throw new ArgumentNullException("e"); } EdgeDepthFirstSearchAlgorithm dfs = new EdgeDepthFirstSearchAlgorithm(g); dfs.BackEdge += new EdgeEventHandler(dfs_BackEdge); EdgeRecorderVisitor vis = new EdgeRecorderVisitor(); vis.Edges.Add(e); dfs.DiscoverTreeEdge += new EdgeEdgeEventHandler(vis.RecordTarget); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(e, 0); return(vis.Edges); }
/// <summary> /// Initializes a new instance of the <see cref="EdgeDepthFirstSearchAlgorithm{TVertex,TEdge}"/> class. /// </summary> /// <param name="host">Host to use if set, otherwise use this reference.</param> /// <param name="visitedGraph">Graph to visit.</param> /// <param name="edgesColors">Edges associated to their colors (treatment states).</param> public EdgeDepthFirstSearchAlgorithm( [CanBeNull] IAlgorithmComponent host, [NotNull] IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph, [NotNull] IDictionary <TEdge, GraphColor> edgesColors) : base(host, visitedGraph) { EdgesColors = edgesColors ?? throw new ArgumentNullException(nameof(edgesColors)); }
/// <summary> /// Construct a graph that filters edges and out-edges /// </summary> /// <param name="g">graph to filter</param> /// <param name="edgePredicate">edge predicate</param> /// <param name="vertexPredicate"></param> /// <exception cref="ArgumentNullException"> /// g or edgePredicate is null /// </exception> public FilteredEdgeListAndIncidenceGraph( IEdgeListAndIncidenceGraph g, IEdgePredicate edgePredicate, IVertexPredicate vertexPredicate ) : base(g,edgePredicate,vertexPredicate) { m_FilteredIncidenceGraph = new FilteredIncidenceGraph(g,edgePredicate,vertexPredicate); }
/// <summary> /// Construct a graph that filters edges and out-edges /// </summary> /// <param name="g">graph to filter</param> /// <param name="edgePredicate">edge predicate</param> /// <param name="vertexPredicate"></param> /// <exception cref="ArgumentNullException"> /// g or edgePredicate is null /// </exception> public FilteredEdgeListAndIncidenceGraph( IEdgeListAndIncidenceGraph g, IEdgePredicate edgePredicate, IVertexPredicate vertexPredicate ) : base(g, edgePredicate, vertexPredicate) { m_FilteredIncidenceGraph = new FilteredIncidenceGraph(g, edgePredicate, vertexPredicate); }
/// <summary> /// Creates a new edge dfs algorithm /// </summary> /// <param name="g"></param> /// <returns></returns> public EdgeDepthFirstSearchAlgorithm CreateEdgeDfs(IEdgeListAndIncidenceGraph g) { EdgeDepthFirstSearchAlgorithm edfs = new EdgeDepthFirstSearchAlgorithm( g ); edfs.TreeEdge += new EdgeEventHandler(this.TreeEdge); edfs.FinishEdge += new EdgeEventHandler(this.FinishEdge); return(edfs); }
public EdgeDepthFirstSearchAlgorithm( IAlgorithmComponent host, IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph, IDictionary <TEdge, GraphColor> colors ) : base(host, visitedGraph) { Contract.Requires(colors != null); this.colors = colors; }
public EdgeDepthFirstSearchAlgorithm( IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph, IDictionary <TEdge, GraphColor> colors ) : base(visitedGraph) { if (colors == null) { throw new ArgumentNullException("VertexColors"); } this.colors = colors; }
/// <summary> /// A depth first search algorithm on a directed graph /// </summary> /// <param name="g">The graph to traverse</param> /// <param name="colors">vertex color map</param> /// <exception cref="ArgumentNullException">g or colors are null</exception> public EdgeDepthFirstSearchAlgorithm( IEdgeListAndIncidenceGraph g, EdgeColorDictionary colors ) { if (g == null) throw new ArgumentNullException("g"); if (colors == null) throw new ArgumentNullException("Colors"); visitedGraph = g; edgeColors = colors; }
public EdgeDepthFirstSearchAlgorithm(IEdgeListAndIncidenceGraph g, EdgeColorDictionary colors) { this.maxDepth = 0x7fffffff; if (g == null) { throw new ArgumentNullException("g"); } if (colors == null) { throw new ArgumentNullException("Colors"); } this.visitedGraph = g; this.edgeColors = colors; }
/// <summary> /// A depth first search algorithm on a directed graph /// </summary> /// <param name="g">The graph to traverse</param> /// <param name="colors">vertex color map</param> /// <exception cref="ArgumentNullException">g or colors are null</exception> public EdgeDepthFirstSearchAlgorithm( IEdgeListAndIncidenceGraph g, EdgeColorDictionary colors ) { if (g == null) { throw new ArgumentNullException("g"); } if (colors == null) { throw new ArgumentNullException("Colors"); } visitedGraph = g; edgeColors = colors; }
/// <summary> /// A depth first search algorithm on a directed graph /// </summary> /// <param name="g">The graph to traverse</param> /// <exception cref="ArgumentNullException">g is null</exception> public EdgeDepthFirstSearchAlgorithm(IEdgeListAndIncidenceGraph g) :this(g,new EdgeColorDictionary()) {}
/// <summary> /// Records all the edges that are part of the subtree of v /// </summary> /// <param name="g">visited graph</param> /// <param name="e">root edge</param> /// <param name="maxDepth">maximum expolration depth</param> /// <returns></returns> public static EdgeCollection OutEdgeTree( IEdgeListAndIncidenceGraph g, IEdge e, int maxDepth ) { if (g==null) throw new ArgumentNullException("g"); if (e==null) throw new ArgumentNullException("e"); EdgeDepthFirstSearchAlgorithm dfs = new EdgeDepthFirstSearchAlgorithm(g); dfs.BackEdge +=new EdgeEventHandler(dfs_BackEdge); EdgeRecorderVisitor vis =new EdgeRecorderVisitor(); vis.Edges.Add(e); dfs.DiscoverTreeEdge +=new EdgeEdgeEventHandler(vis.RecordTarget); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(e,0); return vis.Edges; }
private static void RunImplicitEdgeDFSAndCheck <TVertex, TEdge>( IEdgeListAndIncidenceGraph <TVertex, TEdge> graph, TVertex sourceVertex, int maxDepth = int.MaxValue) where TEdge : IEdge <TVertex> { var parents = new Dictionary <TEdge, TEdge>(); var discoverTimes = new Dictionary <TEdge, int>(); var finishTimes = new Dictionary <TEdge, int>(); int time = 0; var dfs = new ImplicitEdgeDepthFirstSearchAlgorithm <TVertex, TEdge>(graph) { MaxDepth = maxDepth }; dfs.StartEdge += edge => { Assert.IsFalse(parents.ContainsKey(edge)); parents[edge] = edge; discoverTimes[edge] = time++; }; dfs.DiscoverTreeEdge += (edge, targetEdge) => { parents[targetEdge] = edge; Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[parents[targetEdge]]); discoverTimes[targetEdge] = time++; }; dfs.TreeEdge += edge => { Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]); }; dfs.BackEdge += edge => { Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]); }; dfs.ForwardOrCrossEdge += edge => { Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]); }; dfs.FinishEdge += edge => { Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]); finishTimes[edge] = time++; }; dfs.Compute(sourceVertex); // Check if (maxDepth == int.MaxValue) { Assert.AreEqual(discoverTimes.Count, finishTimes.Count); } else { Assert.GreaterOrEqual(discoverTimes.Count, finishTimes.Count); } TEdge[] exploredEdges = finishTimes.Keys.ToArray(); foreach (TEdge e1 in exploredEdges) { foreach (TEdge e2 in exploredEdges) { if (!e1.Equals(e2)) { Assert.IsTrue( finishTimes[e1] < discoverTimes[e2] || finishTimes[e2] < discoverTimes[e1] || (discoverTimes[e2] < discoverTimes[e1] && finishTimes[e1] < finishTimes[e2] && IsDescendant(parents, e1, e2)) || (discoverTimes[e1] < discoverTimes[e2] && finishTimes[e2] < finishTimes[e1] && IsDescendant(parents, e2, e1))); } } } }
/// <summary> /// Initializes a new instance of the <see cref="EdgeDepthFirstSearchAlgorithm{TVertex,TEdge}"/> class. /// </summary> /// <param name="visitedGraph">Graph to visit.</param> /// <param name="edgesColors">Edges associated to their colors (treatment states).</param> public EdgeDepthFirstSearchAlgorithm( [NotNull] IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph, [NotNull] IDictionary <TEdge, GraphColor> edgesColors) : this(null, visitedGraph, edgesColors) { }
/// <summary> /// Creates a new edge dfs algorithm /// </summary> /// <param name="g"></param> /// <returns></returns> public EdgeDepthFirstSearchAlgorithm CreateEdgeDfs(IEdgeListAndIncidenceGraph g) { EdgeDepthFirstSearchAlgorithm edfs = new EdgeDepthFirstSearchAlgorithm( g ); edfs.TreeEdge += new EdgeEventHandler(this.TreeEdge); edfs.FinishEdge += new EdgeEventHandler(this.FinishEdge); return edfs; }
public EdgeDepthFirstSearchAlgorithm(IEdgeListAndIncidenceGraph <TVertex, TEdge> g) : this(g, new Dictionary <TEdge, GraphColor>()) { }
private static void RunEdgeDFSAndCheck <TVertex, TEdge>( IEdgeListAndIncidenceGraph <TVertex, TEdge> graph, int maxDepth = int.MaxValue) where TEdge : IEdge <TVertex> { var parents = new Dictionary <TEdge, TEdge>(); var discoverTimes = new Dictionary <TEdge, int>(); var finishTimes = new Dictionary <TEdge, int>(); int time = 0; var dfs = new EdgeDepthFirstSearchAlgorithm <TVertex, TEdge>(graph) { MaxDepth = maxDepth }; dfs.InitializeEdge += edge => { Assert.AreEqual(GraphColor.White, dfs.EdgesColors[edge]); }; dfs.StartEdge += edge => { Assert.AreEqual(GraphColor.White, dfs.EdgesColors[edge]); Assert.IsFalse(parents.ContainsKey(edge)); parents[edge] = edge; discoverTimes[edge] = time++; }; dfs.DiscoverTreeEdge += (edge, targetEdge) => { parents[targetEdge] = edge; Assert.AreEqual(GraphColor.White, dfs.EdgesColors[targetEdge]); Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[parents[targetEdge]]); discoverTimes[targetEdge] = time++; }; dfs.TreeEdge += edge => { Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]); }; dfs.BackEdge += edge => { Assert.AreEqual(GraphColor.Gray, dfs.EdgesColors[edge]); }; dfs.ForwardOrCrossEdge += edge => { Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]); }; dfs.FinishEdge += edge => { Assert.AreEqual(GraphColor.Black, dfs.EdgesColors[edge]); finishTimes[edge] = time++; }; dfs.Compute(); // Check // All vertices should be black foreach (TEdge edge in graph.Edges) { Assert.IsTrue(dfs.EdgesColors.ContainsKey(edge)); Assert.AreEqual(dfs.EdgesColors[edge], GraphColor.Black); } foreach (TEdge e1 in graph.Edges) { foreach (TEdge e2 in graph.Edges) { if (!e1.Equals(e2)) { Assert.IsTrue( finishTimes[e1] < discoverTimes[e2] || finishTimes[e2] < discoverTimes[e1] || (discoverTimes[e2] < discoverTimes[e1] && finishTimes[e1] < finishTimes[e2] && IsDescendant(parents, e1, e2)) || (discoverTimes[e1] < discoverTimes[e2] && finishTimes[e2] < finishTimes[e1] && IsDescendant(parents, e2, e1))); } } } }
/// <summary> /// Initializes a new instance of the <see cref="EdgeDepthFirstSearchAlgorithm{TVertex,TEdge}"/> class. /// </summary> /// <param name="visitedGraph">Graph to visit.</param> public EdgeDepthFirstSearchAlgorithm( [NotNull] IEdgeListAndIncidenceGraph <TVertex, TEdge> visitedGraph) : this(visitedGraph, new Dictionary <TEdge, GraphColor>()) { }
/// <summary> /// A depth first search algorithm on a directed graph /// </summary> /// <param name="g">The graph to traverse</param> /// <exception cref="ArgumentNullException">g is null</exception> public EdgeDepthFirstSearchAlgorithm(IEdgeListAndIncidenceGraph g) : this(g, new EdgeColorDictionary()) { }