/// <summary> /// Records all the vertices that are part of the out-subtree of v /// </summary> /// <param name="g">visited graph</param> /// <param name="v">root vertex</param> /// <param name="maxDepth">Maximum exploration depth</param> /// <returns></returns> public static VertexCollection OutVertexTree( IVertexListGraph g, IVertex v, int maxDepth ) { if (g == null) { throw new ArgumentNullException("g"); } if (v == null) { throw new ArgumentNullException("v"); } DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(g); dfs.BackEdge += new EdgeEventHandler(dfs_BackEdge); VertexRecorderVisitor vis = new VertexRecorderVisitor(); vis.Vertices.Add(v); dfs.TreeEdge += new EdgeEventHandler(vis.RecordTarget); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(v, 0); return(vis.Vertices); }
public static void CloneOutVertexTree( IVertexListGraph g, ISerializableVertexAndEdgeListGraph sub, IVertex v, int maxDepth ) { if (g == null) { throw new ArgumentNullException("g"); } if (sub == null) { throw new ArgumentNullException("sub"); } if (v == null) { throw new ArgumentNullException("v"); } DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(g); PopulatorVisitor pop = new PopulatorVisitor(sub); dfs.StartVertex += new VertexEventHandler(pop.StartVertex); dfs.TreeEdge += new EdgeEventHandler(pop.TreeEdge); dfs.MaxDepth = maxDepth; dfs.Initialize(); dfs.Visit(v, 0); }
/// <summary> /// Search strict dangling vertices, for example /// |\ /// | \ /// |__\Y_________Z /// In the above draft, Z is a dangling vertex, but Y is not. /// </summary> /// <returns></returns> public IEnumerable <CurveVertex> SearchDanglingVertices() { // Search none loop vertices. var noneLoopVertices = SearchNoneLoopVertices(); // Depth first search the graph, and find all the path end vertices. var result = new HashSet <CurveVertex>(); var observer = new VertexPredecessorPathRecorderObserver <CurveVertex, SEdge <CurveVertex> >(); var dfs = new DepthFirstSearchAlgorithm <CurveVertex, SEdge <CurveVertex> >((IVertexListGraph <CurveVertex, SEdge <CurveVertex> >)_graph); using (observer.Attach(dfs)) { dfs.Compute(); } // Dangling vertex must be an end path vertex. foreach (var curveVertex in observer.EndPathVertices) { if (!noneLoopVertices.Contains(curveVertex)) { continue; } result.Add(curveVertex); // Check whether this path's root point is a dangling vertex. var root = GetRootVertex(observer.VertexPredecessors, curveVertex); if (root != null && noneLoopVertices.Contains(root.Value)) { result.Add(root.Value); } } return(result); }
public IEnumerable <IEnumerable <SEdge <CurveVertex> > > SearchDanglingPaths() { // Search all none loop vertices var noneLoopVertices = SearchNoneLoopVertices(); var danglingEdges = new List <SEdge <CurveVertex> >(); foreach (var vertex in noneLoopVertices) { IEnumerable <SEdge <CurveVertex> > edges = null; if (_graph.TryGetOutEdges(vertex, out edges)) { danglingEdges.AddRange(edges); } } // Create a small graph. var tempGraph = danglingEdges.ToAdjacencyGraph <CurveVertex, SEdge <CurveVertex> >(); // Deep first search the graph and get all its paths. var observer = new VertexPredecessorPathRecorderObserver <CurveVertex, SEdge <CurveVertex> >(); var dfs = new DepthFirstSearchAlgorithm <CurveVertex, SEdge <CurveVertex> >((IVertexListGraph <CurveVertex, SEdge <CurveVertex> >)tempGraph); using (observer.Attach(dfs)) { dfs.Compute(); } return(observer.AllPaths()); }
public void GraphWithSelfEdgesPUT1(int v, int e, bool self) { AdjacencyGraph g = null; RandomGraph.Graph(g, v, e, new Random(), self); DepthFirstSearchAlgorithm bfs = new DepthFirstSearchAlgorithm(g); }
public void ClearRootVertex() { var graph = new AdjacencyGraph <int, Edge <int> >(); var algorithm = new DepthFirstSearchAlgorithm <int, Edge <int> >(graph); ClearRootVertex_Test(algorithm); }
private void GenerateSpanningTree() { _spanningTree = new BidirectionalGraph <TVertex, Edge <TVertex> >(false); _spanningTree.AddVertexRange(VisitedGraph.Vertices); IQueue <TVertex> vb = new QuikGraph.Collections.Queue <TVertex>(); vb.Enqueue(VisitedGraph.Vertices.OrderBy(v => VisitedGraph.InDegree(v)).First()); switch (Parameters.SpanningTreeGeneration) { case SpanningTreeGeneration.BFS: var bfs = new BreadthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph, vb, new Dictionary <TVertex, GraphColor>()); bfs.TreeEdge += e => { ThrowIfCancellationRequested(); _spanningTree.AddEdge(new Edge <TVertex>(e.Source, e.Target)); }; bfs.Compute(); break; case SpanningTreeGeneration.DFS: var dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph); dfs.TreeEdge += e => { ThrowIfCancellationRequested(); _spanningTree.AddEdge(new Edge <TVertex>(e.Source, e.Target)); }; dfs.Compute(); break; } }
protected override void InternalCompute() { DepthFirstSearchAlgorithm <TVertex, TEdge> dfs = null; try { dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, this.VisitedGraph, new Dictionary <TVertex, GraphColor>(this.VisitedGraph.VertexCount) ); dfs.BackEdge += new EdgeAction <TVertex, TEdge>(this.BackEdge); dfs.FinishVertex += new VertexAction <TVertex>(this.FinishVertex); dfs.Compute(); } finally { if (dfs != null) { dfs.BackEdge -= new EdgeAction <TVertex, TEdge>(this.BackEdge); dfs.FinishVertex -= new VertexAction <TVertex>(this.FinishVertex); } } }
private void TarjanOfflineLeastCommonAncestorAlgorithm <TVertex, TEdge>( IVertexListGraph <TVertex, TEdge> g, TVertex root, SEquatableEdge <TVertex>[] pairs ) where TEdge : IEdge <TVertex> { var lca = g.OfflineLeastCommonAncestorTarjan(root, pairs); var predecessors = new VertexPredecessorRecorderObserver <TVertex, TEdge>(); var dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>(g); using (predecessors.Attach(dfs)) dfs.Compute(root); TVertex ancestor; foreach (var pair in pairs) { if (lca(pair, out ancestor)) { Assert.True(EdgeExtensions.IsPredecessor(predecessors.VertexPredecessors, root, pair.Source)); Assert.True(EdgeExtensions.IsPredecessor(predecessors.VertexPredecessors, root, pair.Target)); } } }
public RunPipeCollection AllTestPipes() { if (this.graph.VerticesCount == 1) { // only the root vertex return(new RunPipeCollection()); } DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm( this.graph ); // attach leaf recorder PredecessorRecorderVisitor pred = new PredecessorRecorderVisitor(); dfs.RegisterPredecessorRecorderHandlers(pred); dfs.Compute(this.Root); // create pipies RunPipeCollection pipes = new RunPipeCollection(); foreach (EdgeCollection edges in pred.AllPaths()) { RunPipe pipe = new RunPipe(this.Fixture); foreach (IEdge e in edges) { pipe.Invokers.Add((RunInvokerVertex)e.Target); } pipes.Add(pipe); } return(pipes); }
protected virtual void GenerateSpanningTree(CancellationToken cancellationToken) { SpanningTree = new BidirectionalGraph <TVertex, Edge <TVertex> >(false); SpanningTree.AddVertexRange(VisitedGraph.Vertices.OrderBy(v => VisitedGraph.InDegree(v))); EdgeAction <TVertex, TEdge> action = e => { cancellationToken.ThrowIfCancellationRequested(); SpanningTree.AddEdge(new Edge <TVertex>(e.Source, e.Target)); }; switch (Parameters.SpanningTreeGeneration) { case SpanningTreeGeneration.BFS: var bfsAlgo = new BreadthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph); bfsAlgo.TreeEdge += action; bfsAlgo.Compute(); break; case SpanningTreeGeneration.DFS: var dfsAlgo = new DepthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph); dfsAlgo.TreeEdge += action; dfsAlgo.ForwardOrCrossEdge += action; dfsAlgo.Compute(); break; } }
protected override void InternalCompute() { this.Components.Clear(); this.Roots.Clear(); this.DiscoverTimes.Clear(); componentCount = 0; dfsTime = 0; DepthFirstSearchAlgorithm <TVertex, TEdge> dfs = null; try { dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, VisitedGraph, new Dictionary <TVertex, GraphColor>(this.VisitedGraph.VertexCount) ); dfs.DiscoverVertex += new VertexEventHandler <TVertex>(this.DiscoverVertex); dfs.FinishVertex += new VertexEventHandler <TVertex>(this.FinishVertex); dfs.Compute(); } finally { if (dfs != null) { dfs.DiscoverVertex -= new VertexEventHandler <TVertex>(this.DiscoverVertex); dfs.FinishVertex -= new VertexEventHandler <TVertex>(this.FinishVertex); } } }
protected override void InternalCompute() { DepthFirstSearchAlgorithm <TVertex, TEdge> dfs = null; try { dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, this.VisitedGraph, new Dictionary <TVertex, GraphColor>(this.VisitedGraph.VertexCount) ); dfs.BackEdge += BackEdge; dfs.FinishVertex += VertexFinished; dfs.DiscoverVertex += DiscoverVertex; dfs.FinishVertex += FinishVertex; dfs.Compute(); } finally { if (dfs != null) { dfs.BackEdge -= BackEdge; dfs.FinishVertex -= VertexFinished; dfs.DiscoverVertex -= DiscoverVertex; dfs.FinishVertex -= FinishVertex; } } }
/// <inheritdoc /> protected override void InternalCompute() { // Shortcut for empty graph if (VisitedGraph.IsVerticesEmpty) { return; } var dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>(VisitedGraph); try { dfs.StartVertex += OnStartVertex; dfs.TreeEdge += OnEdgeDiscovered; dfs.ForwardOrCrossEdge += OnForwardOrCrossEdge; dfs.Compute(); } finally { dfs.StartVertex -= OnStartVertex; dfs.TreeEdge -= OnEdgeDiscovered; dfs.ForwardOrCrossEdge -= OnForwardOrCrossEdge; } }
public void FowardOrCrossEdge(Object sender, EdgeEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Edge.Target], GraphColor.Black); }
/// <summary> /// Computes a depth first tree. /// </summary> /// <typeparam name="TVertex">The type of the vertex.</typeparam> /// <typeparam name="TEdge">The type of the edge.</typeparam> /// <param name="visitedGraph">The visited graph.</param> /// <param name="root">The root.</param> /// <returns></returns> public static TryFunc <TVertex, IEnumerable <TEdge> > TreeDepthFirstSearch <TVertex, TEdge>( #if !NET20 this #endif IVertexListGraph <TVertex, TEdge> visitedGraph, TVertex root) where TEdge : IEdge <TVertex> { Contract.Requires(visitedGraph != null); Contract.Requires(root != null); Contract.Requires(visitedGraph.ContainsVertex(root)); Contract.Ensures(Contract.Result <TryFunc <TVertex, IEnumerable <TEdge> > >() != null); var algo = new DepthFirstSearchAlgorithm <TVertex, TEdge>(visitedGraph); var predecessorRecorder = new VertexPredecessorRecorderObserver <TVertex, TEdge>(); using (predecessorRecorder.Attach(algo)) algo.Compute(root); var predecessors = predecessorRecorder.VertexPredecessors; return(delegate(TVertex v, out IEnumerable <TEdge> edges) { return EdgeExtensions.TryGetPath(predecessors, v, out edges); }); }
public void GraphWithoutSelfEdges() { AdjacencyGraph g = new AdjacencyGraph( new QuickGraph.Providers.VertexAndEdgeProvider(), true); RandomGraph.Graph(g, 20, 100, new Random(), false); DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(g); dfs.StartVertex += new VertexHandler(this.StartVertex); dfs.DiscoverVertex += new VertexHandler(this.DiscoverVertex); dfs.ExamineEdge += new EdgeHandler(this.ExamineEdge); dfs.TreeEdge += new EdgeHandler(this.TreeEdge); dfs.BackEdge += new EdgeHandler(this.BackEdge); dfs.ForwardOrCrossEdge += new EdgeHandler(this.FowardOrCrossEdge); dfs.FinishVertex += new VertexHandler(this.FinishVertex); Parents.Clear(); DiscoverTimes.Clear(); FinishTimes.Clear(); m_Time = 0; foreach (IVertex v in g.Vertices) { Parents[v] = v; } // compute dfs.Compute(); CheckDfs(g, dfs); }
public void SetRootVertex_Throws() { var graph = new AdjacencyGraph <TestVertex, Edge <TestVertex> >(); var algorithm = new DepthFirstSearchAlgorithm <TestVertex, Edge <TestVertex> >(graph); SetRootVertex_Throws_Test(algorithm); }
public void SolveReturnsAllSolutions() { var problem = new Problem(); var algorithm = new DepthFirstSearchAlgorithm(); var solutions = algorithm.Solve(problem); }
public void ExamineEdge(Object sender, EdgeEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Edge.Source], GraphColor.Gray); }
/// <inheritdoc /> protected override void InternalCompute() { DepthFirstSearchAlgorithm <TVertex, TEdge> dfs = null; try { dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, VisitedGraph, new Dictionary <TVertex, GraphColor>(VisitedGraph.VertexCount)); dfs.BackEdge += OnBackEdge; dfs.FinishVertex += OnVertexFinished; dfs.DiscoverVertex += DiscoverVertex; dfs.FinishVertex += FinishVertex; dfs.Compute(); } finally { if (dfs != null) { dfs.BackEdge -= OnBackEdge; dfs.FinishVertex -= OnVertexFinished; dfs.DiscoverVertex -= DiscoverVertex; dfs.FinishVertex -= FinishVertex; SortedVertices = _sortedVertices.Reverse().ToArray(); } } }
public void StartVertex(Object sender, VertexEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Vertex], GraphColor.White); }
/// <inheritdoc /> protected override void InternalCompute() { DepthFirstSearchAlgorithm <TVertex, TEdge> dfs = null; try { dfs = new DepthFirstSearchAlgorithm <TVertex, TEdge>( this, VisitedGraph, new Dictionary <TVertex, GraphColor>(VisitedGraph.VertexCount)); dfs.DiscoverVertex += OnVertexDiscovered; dfs.FinishVertex += OnVertexFinished; dfs.Compute(); } finally { if (dfs != null) { dfs.DiscoverVertex -= OnVertexDiscovered; dfs.FinishVertex -= OnVertexFinished; } } Debug.Assert(ComponentCount >= 0); Debug.Assert(VisitedGraph.VertexCount >= 0 || ComponentCount == 0); Debug.Assert(VisitedGraph.Vertices.All(v => Components.ContainsKey(v))); Debug.Assert(VisitedGraph.VertexCount == Components.Count); Debug.Assert(Components.Values.All(c => c <= ComponentCount)); }
private IReadOnlyCollection <IReadOnlyCollection <Identifier> > GetCyclePaths(IVertexListGraph <Identifier, SEquatableEdge <Identifier> > graph) { if (graph == null) { throw new ArgumentNullException(nameof(graph)); } var examinedEdges = new List <IEdge <Identifier> >(); var cycles = new List <IReadOnlyCollection <Identifier> >(); var dfs = new DepthFirstSearchAlgorithm <Identifier, SEquatableEdge <Identifier> >(graph); void onExamineEdge(SEquatableEdge <Identifier> e) => examinedEdges.Add(e); void onCyclingEdgeFound(SEquatableEdge <Identifier> e) => OnCyclingEdgeFound(examinedEdges, cycles, e); try { dfs.ExamineEdge += onExamineEdge; dfs.BackEdge += onCyclingEdgeFound; dfs.Compute(); return(cycles); } finally { dfs.ExamineEdge -= onExamineEdge; dfs.BackEdge -= onCyclingEdgeFound; } }
//private VertexIntDictionary components; ///// <summary> /// Initializes a new instance of the CompressNamespacesAlgorithm class. /// </summary> /// <param name="inputGraph"></param> public CompressNamespacesAlgorithm(IVertexListGraph inputGraph) { this.visitedGraph = inputGraph; dfs = new DepthFirstSearchAlgorithm(inputGraph); dfs.FinishVertex += new VertexEventHandler(dfs_FinishVertex); dfs.DiscoverVertex += new VertexEventHandler(dfs_DiscoverVertex); dfs.TreeEdge += new EdgeEventHandler(dfs_TreeEdge); }
public void FinishVertex(Object sender, VertexEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Vertex], GraphColor.Black); FinishTimes[args.Vertex] = m_Time++; }
public void TreeEdge(Object sender, EdgeEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Edge.Target], GraphColor.White); Parents[args.Edge.Target] = args.Edge.Source; }
public void ComputeWithRoot() { var graph = new AdjacencyGraph <int, Edge <int> >(); graph.AddVertex(0); var algorithm = new DepthFirstSearchAlgorithm <int, Edge <int> >(graph); ComputeWithRoot_Test(algorithm); }
public void DiscoverVertex(Object sender, VertexEventArgs args) { Assert.IsTrue(sender is DepthFirstSearchAlgorithm); DepthFirstSearchAlgorithm algo = (DepthFirstSearchAlgorithm)sender; Assert.AreEqual(algo.Colors[args.Vertex], GraphColor.Gray); Assert.AreEqual(algo.Colors[Parents[args.Vertex]], GraphColor.Gray); DiscoverTimes[args.Vertex] = m_Time++; }
public static IDictionary <int, double> Get(AdjacencyGraph <int, Edge <int> > g) { var dfs = new DepthFirstSearchAlgorithm <int, Edge <int> >(g); var recorder = new VertexDistanceRecorderObserver <int, Edge <int> >(edgeWeights => 1.0); using (recorder.Attach(dfs)) { dfs.Compute(); return(recorder.Distances); } }
public static IDictionary <string, double> Get(AdjacencyGraph <string, TaggedEdge <string, string> > g) { var dfs = new DepthFirstSearchAlgorithm <string, TaggedEdge <string, string> >(g); var recorder = new VertexDistanceRecorderObserver <string, TaggedEdge <string, string> >(edgeWeights => double.Parse(edgeWeights.Tag)); using (recorder.Attach(dfs)) { dfs.Compute(); return(recorder.Distances); } }