├── .gitattributes ├── .gitignore ├── README.md ├── build └── QuickGraphCore.nuspec └── src ├── NuGetLogo32x32.png ├── NuGetLogo50x50.png ├── QuickGraph.sln ├── QuickGraph ├── AdjacencyGraph.cs ├── Algorithms │ ├── AlgorithmBase.cs │ ├── AlgorithmEventHandler.cs │ ├── AlgorithmExtensions.cs │ ├── CentralityApproximationAlgorithm.cs │ ├── Cliques │ │ ├── BronKerboshMaximumCliqueAlgorithm.cs │ │ ├── MaximumCliqueAlgorithmBase.cs │ │ └── ncliques.pdf │ ├── ComputationState.cs │ ├── Condensation │ │ ├── CondensationGraphAlgorithm.cs │ │ ├── CondensedEdge.cs │ │ ├── EdgeMergeCondensationGraphAlgorithm.cs │ │ └── MergedEdge.cs │ ├── ConnectedComponents │ │ ├── ConnectedComponentsAlgorithm.cs │ │ ├── IncrementalConnectedComponentsAlgorithm.cs │ │ ├── StronglyConnectedComponentAlgorithm.cs │ │ └── WeaklyConnectedComponentsAlgorithm.cs │ ├── Contracts │ │ ├── IAlgorithmContract.cs │ │ └── IComputationContract.cs │ ├── DistanceRelaxers.cs │ ├── EulerianTrailAlgorithm.cs │ ├── Exploration │ │ ├── CloneableVertexGraphExplorerAlgorithm.cs │ │ ├── ITransitionFactory.cs │ │ └── TransitionFactoryImplicitGraph.cs │ ├── IAlgorithm.cs │ ├── IComputation.cs │ ├── IConnectedComponentAlgorithm.cs │ ├── IDistanceRecorderAlgorithm.cs │ ├── IDistanceRelaxer.cs │ ├── IEdgeColorizerAlgorithm.cs │ ├── IEdgePredecessorRecorderAlgorithm.cs │ ├── IEndPathEdgeRecorderAlgorithm.cs │ ├── ITreeBuilderAlgorithm.cs │ ├── IUndirectedTreeBuilderAlgorithm.cs │ ├── IUndirectedVertexPredecessorRecorderAlgorithm.cs │ ├── IVertexColorizerAlgorithm.cs │ ├── IVertexPredecessorRecorderAlgorithm.cs │ ├── IVertexTimeStamperAlgorithm.cs │ ├── LengauerTarjanDominatorAlgorithm.cs │ ├── MaximumBipartiteMatchingAlgorithm.cs │ ├── MaximumFlow │ │ ├── AllVerticesGraphAugmentorAlgorithm.cs │ │ ├── BipartiteToMaximumFlowGraphAugmentorAlgorithm.cs │ │ ├── EdmondsKarpMaximumFlowAlgorithm.cs │ │ ├── GraphAugmentorAlgorithmBase.cs │ │ ├── GraphBalancingAlgorithm.cs │ │ ├── MaximumFlowAlgorithmBase.cs │ │ ├── MultiSourceSinkGraphAugmentorAlgorithm.cs │ │ └── ReverseEdgeAugmentorAlgorithm.cs │ ├── MinimumSpanningTree │ │ ├── IMinimumSpanningTreeAlgorithm.cs │ │ └── KruskalMinimumSpanningTreeAlgorithm.cs │ ├── Observers │ │ ├── Contracts │ │ │ └── IObserverContract.cs │ │ ├── DisposableAction.cs │ │ ├── EdgePredecessorRecorderObserver.cs │ │ ├── EdgeRecorderObserver.cs │ │ ├── IObserver.cs │ │ ├── UndirectedVertexDistanceRecorderObserver.cs │ │ ├── UndirectedVertexPredecessorRecorderObserver.cs │ │ ├── VertexDistanceRecorderObserver.cs │ │ ├── VertexPredecessorPathRecorderObserver.cs │ │ ├── VertexPredecessorRecorderObserver.cs │ │ ├── VertexRecorderObserver.cs │ │ └── VertexTimeStamperObserver.cs │ ├── PageRankAlgorithm.cs │ ├── RandomGraphFactory.cs │ ├── RandomWalks │ │ ├── CyclePoppingRandomTreeAlgorithm.cs │ │ ├── IEdgeChain.cs │ │ ├── IMarkovEdgeChain.cs │ │ ├── MarkovEdgeChainBase.cs │ │ ├── NormalizedMarkovEdgeChain.cs │ │ ├── RandomWalkAlgorithm.cs │ │ ├── RoundRobinEdgeChain.cs │ │ ├── VanishingWeightedMarkovEdgeChain.cs │ │ ├── WeightedMarkedEdgeChain.cs │ │ └── WeightedMarkovEdgeChainBase.cs │ ├── RankedShortestPath │ │ ├── HoffmanPavleyRankedShortestPathAlgorithm.cs │ │ └── RankedShortestPathAlgorithmBase.cs │ ├── RootedAlgorithmBase.cs │ ├── RootedSearchAlgorithmBase.cs │ ├── Search │ │ ├── BestFirstFrontierSearchAlgorithm.cs │ │ ├── BidirectionalDepthFirstSearchAlgorithm.cs │ │ ├── BreadthFirstSearchAlgorithm.cs │ │ ├── DepthFirstSearchAlgorithm.cs │ │ ├── EdgeDepthFirstSearchAlgorithm.cs │ │ ├── HeightFirstSearchAlgorithm.cs │ │ ├── ImplicitDepthFirstSearchAlgorithm.cs │ │ ├── ImplicitEdgeDepthFirstSearchAlgorithm.cs │ │ ├── UndirectedBreathFirstSearchAlgorithm.cs │ │ ├── UndirectedDepthFirstSearchAlgorithm.cs │ │ └── UndirectedEdgeDepthFirstSearchAlgorithm.cs │ ├── Services │ │ ├── IAlgorithmComponent.cs │ │ ├── IAlgorithmServices.cs │ │ ├── ICancelManager.cs │ │ └── IService.cs │ ├── ShortestPath │ │ ├── AStartShortestPathAlgorithm.cs │ │ ├── BellmanFordShortestPathAlgorithm.cs │ │ ├── DagShortestPathAlgorithm.cs │ │ ├── DijkstraShortestPathAlgorithm.cs │ │ ├── FloydWarshallAllShortestPathAlgorithm.cs │ │ ├── ShortestPathAlgorithmBase.cs │ │ ├── UndirectedDijkstraShortestPathAlgorithm.cs │ │ └── UndirectedShortestPathAlgorithmBase.cs │ ├── TarjanOfflineLeastCommonAncestorAlgorithm.cs │ └── TopologicalSort │ │ ├── SourceFirstTopologicalSortAlgorithm.cs │ │ ├── TopologicalSortAlgorithm.cs │ │ ├── UndirectedFirstTopologicalSortAlgorithm.cs │ │ └── UndirectedTopologicalSortAlgorithm.cs ├── ArrayAdjacencyGraph.cs ├── ArrayBidirectionalGraph.cs ├── ArrayUndirectedGraph.cs ├── BidirectionAdapterGraph.cs ├── BidirectionalGraph.cs ├── BidirectionalMatrixGraph.cs ├── Collections │ ├── BinaryHeap.cs │ ├── BinaryQueue.cs │ ├── EdgeEdgeDictionary.cs │ ├── EdgeList.cs │ ├── FibonacciHeap.cs │ ├── FibonacciQueue.cs │ ├── ForestDisjointSet.cs │ ├── IDisjointSet.cs │ ├── IEdgeList.cs │ ├── IPriorityQueue.cs │ ├── IQueue.cs │ ├── IVertexEdgeDictionary.cs │ ├── Queue.cs │ ├── SoftHeap.cs │ ├── VertexEdgeDictionary.cs │ └── VertexList.cs ├── CompressedSparseRowGraph.cs ├── Contracts │ ├── Collections │ │ ├── IDisjointSetContract.cs │ │ ├── IEdgeListContract.cs │ │ └── IVertexEdgeDictionaryContract.cs │ ├── DummyContract.cs │ ├── EnumerableContract.cs │ ├── GraphContract.cs │ ├── IBidirectionalGraphContract.cs │ ├── IBidirectionalIncidenceGraphContract.cs │ ├── ICloneableEdgeContract.cs │ ├── IEdgeContract.cs │ ├── IEdgeListGraphContract.cs │ ├── IEdgeSetContract.cs │ ├── IGraphContract.cs │ ├── IImplicitGraphContract.cs │ ├── IImplicitUndirectedGraphContract.cs │ ├── IImplicitVertexSetContract.cs │ ├── IIncidenceGraphContract.cs │ ├── IMutableBidirectionalGraphContract.cs │ ├── IMutableEdgeListGraphContract.cs │ ├── IMutableGraphContract.cs │ ├── IMutableIncidenceGraphContract.cs │ ├── IMutableUndirectedGraphContract.cs │ ├── IMutableVertexAndEdgeSetContract.cs │ ├── IMutableVertexListGraphContract.cs │ ├── IMutableVertexSetContract.cs │ ├── IUndirectedEdgeContract.cs │ ├── IUndirectedGraphContract.cs │ └── IVertexSetContract.cs ├── CreateEdgeDelegate.cs ├── CreateVertexDelegate.cs ├── DelegateBidirectionalIncidenceGraph.cs ├── DelegateImplicitGraph.cs ├── DelegateImplicitUndirectedGraph.cs ├── DelegateIncidenceGraph.cs ├── DelegateUndirectedGraph.cs ├── DelegateVertexAndEdgeListGraph.cs ├── Diagrams │ └── TraversalInterfaces.cd ├── Edge.cs ├── EdgeEdgeEventArgs.cs ├── EdgeEventArgs.cs ├── EdgeExtensions.cs ├── EdgeFactory.cs ├── EdgeIdentity.cs ├── EdgeListGraph.cs ├── EdgePredicate.cs ├── EquatableEdge.cs ├── FuncDelegates.cs ├── GraphColor.cs ├── GraphExtensions.cs ├── HashCodeHelper.cs ├── IBidirectionalGraph.cs ├── IBidirectionalIncidenceGraph.cs ├── ICloneableEdge.cs ├── IEdge.cs ├── IEdgeListAndIncidenceGraph.cs ├── IEdgeListGraph.cs ├── IEdgeSet.cs ├── IGraph.cs ├── IHierarchy.cs ├── IHyperEdge.cs ├── IImplicitGraph.cs ├── IImplicitUndirectedGraph.cs ├── IImplicitVertexSet.cs ├── IIncidenceGraph.cs ├── IMutableBidirectionalGraph.cs ├── IMutableEdgeListGraph.cs ├── IMutableGraph.cs ├── IMutableIncidenceGraph.cs ├── IMutableUndirectedGraph.cs ├── IMutableVertexAndEdgeListGraph.cs ├── IMutableVertexAndEdgeSet.cs ├── IMutableVertexListGraph.cs ├── IMutableVertexSet.cs ├── ITagged.cs ├── IUndirectedEdge.cs ├── IUndirectedGraph.cs ├── IVertexAndEdgeListGraph.cs ├── IVertexListGraph.cs ├── IVertexSet.cs ├── IdentifiableEdgeFactory.cs ├── IdentifiableVertexFactory.cs ├── NegativeCycleGraphException.cs ├── NegativeWeightException.cs ├── NonAcyclicGraphException.cs ├── NonStronglyConnectedGraphException.cs ├── ParallelEdgeNotAllowedException.cs ├── Predicates │ ├── FilteredBidirectionalGraph.cs │ ├── FilteredEdgeListGraph.cs │ ├── FilteredGraph.cs │ ├── FilteredImplicitGraph.cs │ ├── FilteredImplicitVertexSetGraph.cs │ ├── FilteredIncidenceGraph.cs │ ├── FilteredUndirectedGraph.cs │ ├── FilteredVertexAndEdgeListGraph.cs │ ├── FilteredVertexListGraph.cs │ ├── InDictionaryVertexPredicate.cs │ ├── IsolatedVertexPredicate.cs │ ├── ResidualEdgePrediate.cs │ ├── ReversedResidualEdgePredicate.cs │ └── SinkVertexPredicate.cs ├── QuickGraph.csproj ├── QuickGraphException.cs ├── ReversedBidirectionalListGraph.cs ├── SEdge.cs ├── SEquatableEdge.cs ├── SEquatableUndirectedEdge.cs ├── SReversedEdge.cs ├── STaggedEdge.cs ├── STaggedEquatableEdge.cs ├── STaggedUndirectedEdge.cs ├── SUndirectedEdge.cs ├── TaggedEdge.cs ├── TaggedEquatableEdge.cs ├── TaggedUndirectedEdge.cs ├── TryFuncDelegates.cs ├── UndirectedBidirectionalGraph.cs ├── UndirectedEdge.cs ├── UndirectedEdgeEventArgs.cs ├── UndirectedGraph.cs ├── VertexEventArgs.cs ├── VertexFactory.cs ├── VertexIdentity.cs ├── VertexIndexer.cs ├── VertexPredicate.cs ├── quickgraph.banner.png └── quickgraph.png └── Tests └── QuickGraph_Tests_01 ├── London_Underground_Test.cs └── QuickGraph_Tests_01.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QuickGraph 2 | 3 | fork from http://quickgraph.codeplex.com/ 4 | just adding support .net core3.0 .NET Standard 2.1 5 | 6 | -------------------------------------------------------------------------------- /build/QuickGraphCore.nuspec: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | QuickGraphCore 5 | 1.0.0 6 | mokeyish, pelikhan 7 | mokeyish, pelikhan 8 | QuickGraphCore 9 | http://quickgraph.codeplex.com/license 10 | https://nuget.org/Content/gallery/img/default-package-icon.svg 11 | https://github.com/mokeyish/QuickGraph 12 | false 13 | QuickGraph provides generic directed/undirected graph datastructures and algorithms for .Net 14 | QuickGraphCore add netstandard2.0 and net40 support. 15 | Copyright 2017 16 | algorithms graph directed undirected dfs bfs graphviz graphml data structure 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/NuGetLogo32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mokeyish/QuickGraph/3ef664d330c41e44d272b7664ffa4a3e44b05361/src/NuGetLogo32x32.png -------------------------------------------------------------------------------- /src/NuGetLogo50x50.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mokeyish/QuickGraph/3ef664d330c41e44d272b7664ffa4a3e44b05361/src/NuGetLogo50x50.png -------------------------------------------------------------------------------- /src/QuickGraph.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29123.89 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuickGraph", "QuickGraph\QuickGraph.csproj", "{14156EF3-3119-436C-A590-5499C8EA7CE2}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{1A3D8E58-90CB-4767-ADB4-BDDFE86C287B}" 9 | ProjectSection(SolutionItems) = preProject 10 | NuGetLogo32x32.png = NuGetLogo32x32.png 11 | NuGetLogo50x50.png = NuGetLogo50x50.png 12 | EndProjectSection 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{617A3844-7708-452D-AE31-D93C6186C27B}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuickGraph_Tests_01", "Tests\QuickGraph_Tests_01\QuickGraph_Tests_01.csproj", "{DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Release|Any CPU = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {14156EF3-3119-436C-A590-5499C8EA7CE2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {14156EF3-3119-436C-A590-5499C8EA7CE2}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {14156EF3-3119-436C-A590-5499C8EA7CE2}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {14156EF3-3119-436C-A590-5499C8EA7CE2}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834}.Release|Any CPU.Build.0 = Release|Any CPU 32 | EndGlobalSection 33 | GlobalSection(SolutionProperties) = preSolution 34 | HideSolutionNode = FALSE 35 | EndGlobalSection 36 | GlobalSection(NestedProjects) = preSolution 37 | {14156EF3-3119-436C-A590-5499C8EA7CE2} = {1A3D8E58-90CB-4767-ADB4-BDDFE86C287B} 38 | {DE8C1154-D1B6-44D9-9F6D-2DA75E8EA834} = {617A3844-7708-452D-AE31-D93C6186C27B} 39 | EndGlobalSection 40 | GlobalSection(ExtensibilityGlobals) = postSolution 41 | SolutionGuid = {D3DF0295-12D1-4CA8-9E52-AF9646E56126} 42 | EndGlobalSection 43 | EndGlobal 44 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/AlgorithmEventHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Algorithms 6 | { 7 | public delegate void AlgorithmEventHandler( 8 | IAlgorithm sender, 9 | EventArgs e); 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Cliques/BronKerboshMaximumCliqueAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using QuickGraph.Algorithms.Services; 6 | 7 | namespace QuickGraph.Algorithms.Cliques 8 | { 9 | // under construction 10 | class BronKerboshMaximumCliqueAlgorithm 11 | : MaximumCliqueAlgorithmBase 12 | where TEdge : IEdge 13 | { 14 | protected BronKerboshMaximumCliqueAlgorithm( 15 | IAlgorithmComponent host, 16 | IUndirectedGraph visitedGraph) 17 | : base(host, visitedGraph) 18 | {} 19 | 20 | protected BronKerboshMaximumCliqueAlgorithm( 21 | IUndirectedGraph visitedGraph) 22 | : base(visitedGraph) 23 | {} 24 | 25 | protected override void InternalCompute() 26 | { 27 | // the currently growing clique; 28 | var R = new List(); 29 | // prospective nodes which are connected to all nodes in R 30 | // and using which R can be expanded 31 | var P = new List(); 32 | // nodes already processed i.e. nodes which were previously in P 33 | // and hence all maximal cliques containing them have already been reported 34 | var X = new List(); 35 | 36 | // An important invariant is that all nodes which are connected to every node 37 | // of R are either in P or X. 38 | 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Cliques/MaximumCliqueAlgorithmBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using QuickGraph.Algorithms.Services; 6 | 7 | namespace QuickGraph.Algorithms.Cliques 8 | { 9 | public abstract class MaximumCliqueAlgorithmBase 10 | : AlgorithmBase> 11 | where TEdge : IEdge 12 | { 13 | protected MaximumCliqueAlgorithmBase(IAlgorithmComponent host, IUndirectedGraph visitedGraph) 14 | : base(host, visitedGraph) 15 | {} 16 | 17 | protected MaximumCliqueAlgorithmBase(IUndirectedGraph visitedGraph) 18 | : base(visitedGraph) 19 | {} 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Cliques/ncliques.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mokeyish/QuickGraph/3ef664d330c41e44d272b7664ffa4a3e44b05361/src/QuickGraph/Algorithms/Cliques/ncliques.pdf -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/ComputationState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | /// 6 | /// The computation state of a graph algorithm 7 | /// 8 | [Serializable] 9 | public enum ComputationState 10 | { 11 | /// 12 | /// The algorithm is not running 13 | /// 14 | NotRunning, 15 | /// 16 | /// The algorithm is running 17 | /// 18 | Running, 19 | /// 20 | /// An abort has been requested. The algorithm is still running and will cancel as soon as it checks 21 | /// the cancelation state 22 | /// 23 | PendingAbortion, 24 | /// 25 | /// The computation is finished succesfully. 26 | /// 27 | Finished, 28 | /// 29 | /// The computation was aborted 30 | /// 31 | Aborted 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Condensation/CondensedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Collections; 4 | 5 | namespace QuickGraph.Algorithms.Condensation 6 | { 7 | [Serializable] 8 | public sealed class CondensedEdge : Edge 9 | where TEdge : IEdge 10 | where TGraph : IMutableVertexAndEdgeSet, new() 11 | { 12 | private List edges = new List(); 13 | public CondensedEdge(TGraph source, TGraph target) 14 | :base(source,target) 15 | { } 16 | 17 | public IList Edges 18 | { 19 | get { return this.edges; } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Condensation/MergedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Collections; 4 | 5 | namespace QuickGraph.Algorithms.Condensation 6 | { 7 | [Serializable] 8 | public sealed class MergedEdge : Edge 9 | where TEdge : IEdge 10 | { 11 | private List edges = new List(); 12 | 13 | public MergedEdge(TVertex source, TVertex target) 14 | :base(source,target) 15 | { } 16 | 17 | public IList Edges 18 | { 19 | get { return this.edges; } 20 | } 21 | 22 | public static MergedEdge Merge( 23 | MergedEdge inEdge, 24 | MergedEdge outEdge 25 | ) 26 | { 27 | MergedEdge newEdge = new MergedEdge( 28 | inEdge.Source, outEdge.Target); 29 | newEdge.edges = new List(inEdge.Edges.Count + outEdge.Edges.Count); 30 | newEdge.edges.AddRange(inEdge.Edges); 31 | newEdge.edges.AddRange(outEdge.edges); 32 | 33 | return newEdge; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Contracts/IAlgorithmContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Algorithms.Contracts 8 | { 9 | [ContractClassFor(typeof(IAlgorithm<>))] 10 | abstract class IAlgorithmContract 11 | : IAlgorithm 12 | { 13 | #region IAlgorithm Members 14 | 15 | TGraph IAlgorithm.VisitedGraph 16 | { 17 | get 18 | { 19 | Contract.Ensures(Contract.Result() != null); 20 | 21 | return default(TGraph); 22 | } 23 | } 24 | 25 | #endregion 26 | 27 | #region IComputation Members 28 | 29 | object IComputation.SyncRoot 30 | { 31 | get { throw new NotImplementedException(); } 32 | } 33 | 34 | ComputationState IComputation.State 35 | { 36 | get { throw new NotImplementedException(); } 37 | } 38 | 39 | void IComputation.Compute() 40 | { 41 | throw new NotImplementedException(); 42 | } 43 | 44 | void IComputation.Abort() 45 | { 46 | throw new NotImplementedException(); 47 | } 48 | 49 | event EventHandler IComputation.StateChanged 50 | { 51 | add { throw new NotImplementedException(); } 52 | remove { throw new NotImplementedException(); } 53 | } 54 | 55 | event EventHandler IComputation.Started 56 | { 57 | add { throw new NotImplementedException(); } 58 | remove { throw new NotImplementedException(); } 59 | } 60 | 61 | event EventHandler IComputation.Finished 62 | { 63 | add { throw new NotImplementedException(); } 64 | remove { throw new NotImplementedException(); } 65 | } 66 | 67 | event EventHandler IComputation.Aborted 68 | { 69 | add { throw new NotImplementedException(); } 70 | remove { throw new NotImplementedException(); } 71 | } 72 | 73 | #endregion 74 | } 75 | } -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Contracts/IComputationContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Algorithms.Contracts 5 | { 6 | [ContractClassFor(typeof(IComputation))] 7 | abstract class IComputationContract 8 | : IComputation 9 | { 10 | #region IComputation Members 11 | object IComputation.SyncRoot 12 | { 13 | get 14 | { 15 | Contract.Ensures(Contract.Result() != null); 16 | return null; 17 | } 18 | } 19 | 20 | ComputationState IComputation.State 21 | { 22 | get 23 | { 24 | Contract.Ensures(Enum.IsDefined(typeof(ComputationState), Contract.Result())); 25 | 26 | return default(ComputationState); 27 | } 28 | } 29 | 30 | void IComputation.Compute() 31 | { 32 | // todo contracts on events 33 | } 34 | 35 | void IComputation.Abort() 36 | { 37 | } 38 | 39 | event EventHandler IComputation.StateChanged 40 | { 41 | add { throw new NotImplementedException(); } 42 | remove { throw new NotImplementedException(); } 43 | } 44 | 45 | event EventHandler IComputation.Started 46 | { 47 | add { throw new NotImplementedException(); } 48 | remove { throw new NotImplementedException(); } 49 | } 50 | 51 | event EventHandler IComputation.Finished 52 | { 53 | add { throw new NotImplementedException(); } 54 | remove { throw new NotImplementedException(); } 55 | } 56 | 57 | event EventHandler IComputation.Aborted 58 | { 59 | add { throw new NotImplementedException(); } 60 | remove { throw new NotImplementedException(); } 61 | } 62 | 63 | #endregion 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/DistanceRelaxers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace QuickGraph.Algorithms 7 | { 8 | public static class DistanceRelaxers 9 | { 10 | public static readonly IDistanceRelaxer ShortestDistance = new ShortestDistanceRelaxer(); 11 | 12 | sealed class ShortestDistanceRelaxer 13 | : IDistanceRelaxer 14 | { 15 | internal ShortestDistanceRelaxer() { } 16 | 17 | public double InitialDistance 18 | { 19 | get { return double.MaxValue; } 20 | } 21 | 22 | public int Compare(double a, double b) 23 | { 24 | return a.CompareTo(b); 25 | } 26 | 27 | public double Combine(double distance, double weight) 28 | { 29 | return distance + weight; 30 | } 31 | } 32 | 33 | public readonly static IDistanceRelaxer CriticalDistance = new CriticalDistanceRelaxer(); 34 | 35 | sealed class CriticalDistanceRelaxer : 36 | IDistanceRelaxer 37 | { 38 | internal CriticalDistanceRelaxer() { } 39 | 40 | public double InitialDistance 41 | { 42 | get { return double.MinValue; } 43 | } 44 | 45 | public int Compare(double a, double b) 46 | { 47 | return -a.CompareTo(b); 48 | } 49 | 50 | public double Combine(double distance, double weight) 51 | { 52 | return distance + weight; 53 | } 54 | } 55 | 56 | public readonly static IDistanceRelaxer EdgeShortestDistance = new EdgeDistanceRelaxer(); 57 | 58 | sealed class EdgeDistanceRelaxer 59 | : IDistanceRelaxer 60 | { 61 | 62 | public double InitialDistance 63 | { 64 | get { return 0; } 65 | } 66 | 67 | public int Compare(double a, double b) 68 | { 69 | return a.CompareTo(b); 70 | } 71 | 72 | public double Combine(double distance, double weight) 73 | { 74 | return distance + weight; 75 | } 76 | } 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Exploration/ITransitionFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Algorithms.Exploration 5 | { 6 | public interface ITransitionFactory 7 | where TVertex : ICloneable 8 | where TEdge : IEdge 9 | { 10 | bool IsValid(TVertex v); 11 | IEnumerable Apply(TVertex source); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickGraph.Algorithms.Services; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms 6 | { 7 | [ContractClass(typeof(Contracts.IAlgorithmContract<>))] 8 | public interface IAlgorithm : 9 | IComputation 10 | { 11 | TGraph VisitedGraph { get;} 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IComputation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Algorithms 5 | { 6 | [ContractClass(typeof(Contracts.IComputationContract))] 7 | public interface IComputation 8 | { 9 | object SyncRoot { get; } 10 | ComputationState State { get; } 11 | 12 | void Compute(); 13 | void Abort(); 14 | 15 | event EventHandler StateChanged; 16 | event EventHandler Started; 17 | event EventHandler Finished; 18 | event EventHandler Aborted; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IConnectedComponentAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Algorithms 6 | { 7 | public interface IConnectedComponentAlgorithm : IAlgorithm 8 | where TGraph : IGraph 9 | where TEdge : IEdge 10 | { 11 | int ComponentCount { get;} 12 | IDictionary Components { get;} 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IDistanceRecorderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | /// 6 | /// An algorithm that exposes events to compute a distance map between vertices 7 | /// 8 | /// type of the vertices 9 | /// type of the edges 10 | public interface IDistanceRecorderAlgorithm 11 | where TEdge : IEdge 12 | { 13 | event VertexAction InitializeVertex; 14 | event VertexAction DiscoverVertex; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IDistanceRelaxer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Algorithms 6 | { 7 | public interface IDistanceRelaxer 8 | : IComparer 9 | { 10 | double InitialDistance { get;} 11 | double Combine(double distance, double weight); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IEdgeColorizerAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Algorithms 5 | { 6 | public interface IEdgeColorizerAlgorithm 7 | where TEdge : IEdge 8 | { 9 | IDictionary EdgeColors { get;} 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IEdgePredecessorRecorderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface IEdgePredecessorRecorderAlgorithm 6 | where TEdge : IEdge 7 | { 8 | event EdgeEdgeAction DiscoverTreeEdge; 9 | event EdgeAction FinishEdge; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IEndPathEdgeRecorderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace QuickGraph.Algorithms 3 | { 4 | public interface IEndPathEdgeRecorderAlgorithm 5 | where TEdge : IEdge 6 | { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/ITreeBuilderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface ITreeBuilderAlgorithm 6 | where TEdge : IEdge 7 | { 8 | event EdgeAction TreeEdge; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IUndirectedTreeBuilderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface IUndirectedTreeBuilderAlgorithm 6 | where TEdge : IEdge 7 | { 8 | event UndirectedEdgeAction TreeEdge; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IUndirectedVertexPredecessorRecorderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface IUndirectedVertexPredecessorRecorderAlgorithm 6 | : IUndirectedTreeBuilderAlgorithm 7 | where TEdge : IEdge 8 | { 9 | event VertexAction StartVertex; 10 | event VertexAction FinishVertex; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IVertexColorizerAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Algorithms 5 | { 6 | public interface IVertexColorizerAlgorithm 7 | where TEdge : IEdge 8 | { 9 | GraphColor GetVertexColor(TVertex v); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IVertexPredecessorRecorderAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface IVertexPredecessorRecorderAlgorithm 6 | : ITreeBuilderAlgorithm 7 | where TEdge : IEdge 8 | { 9 | event VertexAction StartVertex; 10 | event VertexAction FinishVertex; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/IVertexTimeStamperAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph.Algorithms 4 | { 5 | public interface IVertexTimeStamperAlgorithm 6 | where TEdge : IEdge 7 | { 8 | event VertexAction DiscoverVertex; 9 | event VertexAction FinishVertex; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MaximumFlow/AllVerticesGraphAugmentorAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickGraph.Algorithms.Services; 3 | 4 | namespace QuickGraph.Algorithms.MaximumFlow 5 | { 6 | public sealed class AllVerticesGraphAugmentorAlgorithm 7 | : GraphAugmentorAlgorithmBase> 8 | where TEdge : IEdge 9 | { 10 | public AllVerticesGraphAugmentorAlgorithm( 11 | IMutableVertexAndEdgeSet visitedGraph, 12 | VertexFactory vertexFactory, 13 | EdgeFactory edgeFactory 14 | ) 15 | : this(null, visitedGraph, vertexFactory, edgeFactory) 16 | { } 17 | 18 | public AllVerticesGraphAugmentorAlgorithm( 19 | IAlgorithmComponent host, 20 | IMutableVertexAndEdgeSet visitedGraph, 21 | VertexFactory vertexFactory, 22 | EdgeFactory edgeFactory 23 | ) 24 | :base(host, visitedGraph,vertexFactory,edgeFactory) 25 | {} 26 | 27 | protected override void AugmentGraph() 28 | { 29 | var cancelManager = this.Services.CancelManager; 30 | foreach (var v in this.VisitedGraph.Vertices) 31 | { 32 | if (cancelManager.IsCancelling) break; 33 | 34 | this.AddAugmentedEdge(this.SuperSource, v); 35 | this.AddAugmentedEdge(v, this.SuperSink); 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MaximumFlow/BipartiteToMaximumFlowGraphAugmentorAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using QuickGraph.Algorithms.Services; 6 | using System.Diagnostics.Contracts; 7 | 8 | namespace QuickGraph.Algorithms.MaximumFlow 9 | { 10 | /// 11 | /// This algorithm modifies a bipartite graph into a related graph, where each Vertex in one partition is 12 | /// connected to a newly added "SuperSource" and each Vertex in the other partition is connected to a newly added "SuperSink" 13 | /// When the maximum flow of this related graph is computed, the edges used for the flow are also those which make up 14 | /// the maximum match for the bipartite graph. 15 | /// 16 | /// 17 | /// 18 | class BipartiteToMaximumFlowGraphAugmentorAlgorithm 19 | : GraphAugmentorAlgorithmBase> 20 | where TEdge : IEdge 21 | { 22 | public BipartiteToMaximumFlowGraphAugmentorAlgorithm( 23 | IMutableVertexAndEdgeSet visitedGraph, 24 | IEnumerable vertexSetA, 25 | IEnumerable vertexSetB, 26 | VertexFactory vertexFactory, 27 | EdgeFactory edgeFactory 28 | ) 29 | : this(null, visitedGraph, vertexSetA, vertexSetB, vertexFactory, edgeFactory) 30 | { } 31 | 32 | public BipartiteToMaximumFlowGraphAugmentorAlgorithm( 33 | IAlgorithmComponent host, 34 | IMutableVertexAndEdgeSet visitedGraph, 35 | IEnumerable vertexSetA, 36 | IEnumerable vertexSetB, 37 | VertexFactory vertexFactory, 38 | EdgeFactory edgeFactory 39 | ) 40 | : base(host, visitedGraph,vertexFactory,edgeFactory) 41 | { 42 | this.VertexSetA = vertexSetA; 43 | this.VertexSetB = vertexSetB; 44 | } 45 | 46 | public IEnumerable VertexSetA { get; private set; } 47 | public IEnumerable VertexSetB { get; private set; } 48 | 49 | 50 | protected override void AugmentGraph() 51 | { 52 | var cancelManager = this.Services.CancelManager; 53 | foreach (var v in this.VertexSetA) 54 | { 55 | if (cancelManager.IsCancelling) break; 56 | 57 | this.AddAugmentedEdge(this.SuperSource, v); 58 | } 59 | 60 | foreach (var v in this.VertexSetB) 61 | { 62 | if (cancelManager.IsCancelling) break; 63 | 64 | this.AddAugmentedEdge(v, this.SuperSink); 65 | } 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MaximumFlow/MaximumFlowAlgorithmBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Algorithms.Services; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph.Algorithms.MaximumFlow 7 | { 8 | /// 9 | /// Abstract base class for maximum flow algorithms. 10 | /// 11 | [Serializable] 12 | public abstract class MaximumFlowAlgorithm : 13 | AlgorithmBase>, 14 | IVertexColorizerAlgorithm 15 | where TEdge : IEdge 16 | { 17 | private TVertex source; 18 | private TVertex sink; 19 | 20 | protected MaximumFlowAlgorithm( 21 | IAlgorithmComponent host, 22 | IMutableVertexAndEdgeListGraph visitedGraph, 23 | Func capacities, 24 | EdgeFactory edgeFactory 25 | ) 26 | : base(host, visitedGraph) 27 | { 28 | Contract.Requires(capacities != null); 29 | 30 | this.Capacities = capacities; 31 | this.Predecessors = new Dictionary(); 32 | this.EdgeFactory = edgeFactory; 33 | this.ResidualCapacities = new Dictionary(); 34 | this.VertexColors = new Dictionary(); 35 | } 36 | 37 | #region Properties 38 | 39 | public Dictionary Predecessors { get; private set; } 40 | 41 | public Func Capacities { get; private set; } 42 | 43 | public Dictionary ResidualCapacities { get; private set; } 44 | 45 | public EdgeFactory EdgeFactory { get; private set; } 46 | 47 | public Dictionary ReversedEdges { get; protected set; } 48 | 49 | public Dictionary VertexColors { get; private set; } 50 | 51 | public GraphColor GetVertexColor(TVertex vertex) 52 | { 53 | return this.VertexColors[vertex]; 54 | } 55 | 56 | public TVertex Source 57 | { 58 | get { return this.source; } 59 | set 60 | { 61 | Contract.Requires(value != null); 62 | this.source = value; 63 | } 64 | } 65 | 66 | public TVertex Sink 67 | { 68 | get { return this.sink; } 69 | set 70 | { 71 | Contract.Requires(value != null); 72 | this.sink = value; 73 | } 74 | } 75 | 76 | public double MaxFlow { get; set; } 77 | 78 | #endregion 79 | 80 | public double Compute(TVertex source, TVertex sink) 81 | { 82 | this.Source = source; 83 | this.Sink = sink; 84 | this.Compute(); 85 | return this.MaxFlow; 86 | } 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MaximumFlow/MultiSourceSinkGraphAugmentorAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickGraph.Algorithms.Services; 3 | 4 | namespace QuickGraph.Algorithms.MaximumFlow 5 | { 6 | public sealed class MultiSourceSinkGraphAugmentorAlgorithm 7 | : GraphAugmentorAlgorithmBase> 8 | where TEdge : IEdge 9 | { 10 | public MultiSourceSinkGraphAugmentorAlgorithm( 11 | IMutableBidirectionalGraph visitedGraph, 12 | VertexFactory vertexFactory, 13 | EdgeFactory edgeFactory) 14 | :this(null, visitedGraph, vertexFactory, edgeFactory) 15 | {} 16 | 17 | public MultiSourceSinkGraphAugmentorAlgorithm( 18 | IAlgorithmComponent host, 19 | IMutableBidirectionalGraph visitedGraph, 20 | VertexFactory vertexFactory, 21 | EdgeFactory edgeFactory) 22 | :base(host, visitedGraph,vertexFactory,edgeFactory) 23 | {} 24 | 25 | protected override void AugmentGraph() 26 | { 27 | var cancelManager = this.Services.CancelManager; 28 | foreach (var v in this.VisitedGraph.Vertices) 29 | { 30 | if (cancelManager.IsCancelling) break; 31 | 32 | // is source 33 | if (this.VisitedGraph.IsInEdgesEmpty(v)) 34 | this.AddAugmentedEdge(this.SuperSource, v); 35 | 36 | // is sink 37 | if (this.VisitedGraph.IsOutEdgesEmpty(v)) 38 | this.AddAugmentedEdge(v,this.SuperSink); 39 | } 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MinimumSpanningTree/IMinimumSpanningTreeAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Algorithms.MinimumSpanningTree 6 | { 7 | public interface IMinimumSpanningTreeAlgorithm 8 | : IAlgorithm> 9 | , ITreeBuilderAlgorithm 10 | where TEdge : IEdge 11 | { 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/MinimumSpanningTree/KruskalMinimumSpanningTreeAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using QuickGraph.Collections; 5 | using System.Diagnostics.Contracts; 6 | using QuickGraph.Algorithms.Services; 7 | 8 | namespace QuickGraph.Algorithms.MinimumSpanningTree 9 | { 10 | [Serializable] 11 | public sealed class KruskalMinimumSpanningTreeAlgorithm 12 | : AlgorithmBase> 13 | , IMinimumSpanningTreeAlgorithm 14 | where TEdge : IEdge 15 | { 16 | readonly Func edgeWeights; 17 | 18 | public KruskalMinimumSpanningTreeAlgorithm( 19 | IUndirectedGraph visitedGraph, 20 | Func edgeWeights 21 | ) 22 | : this(null, visitedGraph, edgeWeights) 23 | {} 24 | 25 | public KruskalMinimumSpanningTreeAlgorithm( 26 | IAlgorithmComponent host, 27 | IUndirectedGraph visitedGraph, 28 | Func edgeWeights 29 | ) 30 | :base(host, visitedGraph) 31 | { 32 | Contract.Requires(edgeWeights != null); 33 | 34 | this.edgeWeights = edgeWeights; 35 | } 36 | 37 | public event EdgeAction ExamineEdge; 38 | private void OnExamineEdge(TEdge edge) 39 | { 40 | var eh = this.ExamineEdge; 41 | if (eh != null) 42 | eh(edge); 43 | } 44 | 45 | public event EdgeAction TreeEdge; 46 | private void OnTreeEdge(TEdge edge) 47 | { 48 | var eh = this.TreeEdge; 49 | if (eh != null) 50 | eh(edge); 51 | } 52 | 53 | protected override void InternalCompute() 54 | { 55 | var cancelManager = this.Services.CancelManager; 56 | var ds = new ForestDisjointSet(this.VisitedGraph.VertexCount); 57 | foreach (var v in this.VisitedGraph.Vertices) 58 | ds.MakeSet(v); 59 | 60 | if (cancelManager.IsCancelling) 61 | return; 62 | 63 | var queue = new BinaryQueue(this.edgeWeights); 64 | foreach (var e in this.VisitedGraph.Edges) 65 | queue.Enqueue(e); 66 | 67 | if (cancelManager.IsCancelling) 68 | return; 69 | 70 | while (queue.Count > 0) 71 | { 72 | var e = queue.Dequeue(); 73 | this.OnExamineEdge(e); 74 | if (!ds.AreInSameSet(e.Source, e.Target)) 75 | { 76 | this.OnTreeEdge(e); 77 | ds.Union(e.Source, e.Target); 78 | } 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/Contracts/IObserverContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Algorithms.Observers.Contracts 5 | { 6 | [ContractClassFor(typeof(IObserver<>))] 7 | abstract class IObserverContract 8 | : IObserver 9 | { 10 | IDisposable IObserver.Attach(TAlgorithm algorithm) 11 | { 12 | Contract.Requires(algorithm != null); 13 | Contract.Ensures(Contract.Result() != null); 14 | 15 | return default(IDisposable); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/DisposableAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Algorithms.Observers 8 | { 9 | struct DisposableAction 10 | : IDisposable 11 | { 12 | public delegate void Action(); 13 | 14 | Action action; 15 | 16 | public DisposableAction(Action action) 17 | { 18 | Contract.Requires(action != null); 19 | this.action = action; 20 | } 21 | 22 | public void Dispose() 23 | { 24 | var a = this.action; 25 | this.action = null; 26 | if (a != null) 27 | a(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/EdgeRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.Observers 6 | { 7 | /// 8 | /// 9 | /// 10 | /// type of a vertex 11 | /// type of an edge 12 | /// 15 | [Serializable] 16 | public sealed class EdgeRecorderObserver : 17 | IObserver> 18 | where TEdge : IEdge 19 | { 20 | private readonly IList edges; 21 | 22 | public EdgeRecorderObserver() 23 | :this(new List()) 24 | {} 25 | 26 | public EdgeRecorderObserver(IList edges) 27 | { 28 | Contract.Requires(edges != null); 29 | 30 | this.edges = edges; 31 | } 32 | 33 | public IList Edges 34 | { 35 | get 36 | { 37 | return this.edges; 38 | } 39 | } 40 | 41 | public IDisposable Attach(ITreeBuilderAlgorithm algorithm) 42 | { 43 | algorithm.TreeEdge +=new EdgeAction(RecordEdge); 44 | return new DisposableAction( 45 | () => algorithm.TreeEdge -= new EdgeAction(RecordEdge) 46 | ); 47 | } 48 | 49 | private void RecordEdge(TEdge args) 50 | { 51 | Contract.Requires(args != null); 52 | 53 | this.Edges.Add(args); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/IObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Algorithms.Observers 5 | { 6 | /// 7 | /// An algorithm observer 8 | /// 9 | /// type of the algorithm 10 | /// 13 | [ContractClass(typeof(Contracts.IObserverContract<>))] 14 | public interface IObserver 15 | { 16 | /// 17 | /// Attaches to the algorithm events 18 | /// and returns a disposable object that can be used 19 | /// to detach from the events 20 | /// 21 | /// 22 | /// 23 | IDisposable Attach(TAlgorithm algorithm); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/UndirectedVertexDistanceRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Algorithms.ShortestPath; 5 | 6 | namespace QuickGraph.Algorithms.Observers 7 | { 8 | /// 9 | /// A distance recorder for undirected tree builder algorithms 10 | /// 11 | /// type of a vertex 12 | /// type of an edge 13 | [Serializable] 14 | public sealed class UndirectedVertexDistanceRecorderObserver 15 | : IObserver> 16 | where TEdge : IEdge 17 | { 18 | private readonly IDistanceRelaxer distanceRelaxer; 19 | private readonly Func edgeWeights; 20 | private readonly IDictionary distances; 21 | 22 | public UndirectedVertexDistanceRecorderObserver(Func edgeWeights) 23 | : this(edgeWeights, DistanceRelaxers.EdgeShortestDistance, new Dictionary()) 24 | {} 25 | 26 | public UndirectedVertexDistanceRecorderObserver( 27 | Func edgeWeights, 28 | IDistanceRelaxer distanceRelaxer, 29 | IDictionary distances) 30 | { 31 | Contract.Requires(edgeWeights != null); 32 | Contract.Requires(distanceRelaxer != null); 33 | Contract.Requires(distances != null); 34 | 35 | this.edgeWeights = edgeWeights; 36 | this.distanceRelaxer = distanceRelaxer; 37 | this.distances = distances; 38 | } 39 | 40 | public IDistanceRelaxer DistanceRelaxer 41 | { 42 | get { return this.distanceRelaxer; } 43 | } 44 | 45 | public Func EdgeWeights 46 | { 47 | get { return this.edgeWeights; } 48 | } 49 | 50 | public IDictionary Distances 51 | { 52 | get { return this.distances; } 53 | } 54 | 55 | public IDisposable Attach(IUndirectedTreeBuilderAlgorithm algorithm) 56 | { 57 | algorithm.TreeEdge += new UndirectedEdgeAction(this.TreeEdge); 58 | return new DisposableAction( 59 | () => algorithm.TreeEdge -= new UndirectedEdgeAction(this.TreeEdge) 60 | ); 61 | } 62 | 63 | private void TreeEdge(Object sender, UndirectedEdgeEventArgs args) 64 | { 65 | double sourceDistance; 66 | if (!this.distances.TryGetValue(args.Source, out sourceDistance)) 67 | this.distances[args.Source] = sourceDistance = this.distanceRelaxer.InitialDistance; 68 | this.distances[args.Target] = this.DistanceRelaxer.Combine(sourceDistance, this.edgeWeights(args.Edge)); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/UndirectedVertexPredecessorRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.Observers 6 | { 7 | /// 8 | /// 9 | /// 10 | /// type of a vertex 11 | /// type of an edge 12 | /// 15 | [Serializable] 16 | public sealed class UndirectedVertexPredecessorRecorderObserver : 17 | IObserver> 18 | where TEdge : IEdge 19 | { 20 | private readonly IDictionary vertexPredecessors; 21 | 22 | public UndirectedVertexPredecessorRecorderObserver() 23 | :this(new Dictionary()) 24 | {} 25 | 26 | public UndirectedVertexPredecessorRecorderObserver( 27 | IDictionary vertexPredecessors) 28 | { 29 | Contract.Requires(vertexPredecessors != null); 30 | 31 | this.vertexPredecessors = vertexPredecessors; 32 | } 33 | 34 | public IDictionary VertexPredecessors 35 | { 36 | get { return this.vertexPredecessors; } 37 | } 38 | 39 | public IDisposable Attach(IUndirectedTreeBuilderAlgorithm algorithm) 40 | { 41 | algorithm.TreeEdge += new UndirectedEdgeAction(TreeEdge); 42 | return new DisposableAction( 43 | () => algorithm.TreeEdge -= new UndirectedEdgeAction(TreeEdge) 44 | ); 45 | } 46 | 47 | void TreeEdge(Object sender, UndirectedEdgeEventArgs e) 48 | { 49 | this.vertexPredecessors[e.Target] = e.Edge; 50 | } 51 | 52 | public bool TryGetPath(TVertex vertex, out IEnumerable path) 53 | { 54 | return EdgeExtensions.TryGetPath(this.VertexPredecessors, vertex, out path); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/VertexDistanceRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Algorithms.ShortestPath; 5 | 6 | namespace QuickGraph.Algorithms.Observers 7 | { 8 | /// 9 | /// A distance recorder for directed tree builder algorithms 10 | /// 11 | /// type of a vertex 12 | /// type of an edge 13 | [Serializable] 14 | public sealed class VertexDistanceRecorderObserver 15 | : IObserver> 16 | where TEdge : IEdge 17 | { 18 | private readonly IDistanceRelaxer distanceRelaxer; 19 | private readonly Func edgeWeights; 20 | private readonly IDictionary distances; 21 | 22 | public VertexDistanceRecorderObserver(Func edgeWeights) 23 | : this(edgeWeights, DistanceRelaxers.EdgeShortestDistance, new Dictionary()) 24 | { } 25 | 26 | public VertexDistanceRecorderObserver( 27 | Func edgeWeights, 28 | IDistanceRelaxer distanceRelaxer, 29 | IDictionary distances) 30 | { 31 | Contract.Requires(edgeWeights != null); 32 | Contract.Requires(distanceRelaxer != null); 33 | Contract.Requires(distances != null); 34 | 35 | this.edgeWeights = edgeWeights; 36 | this.distanceRelaxer = distanceRelaxer; 37 | this.distances = distances; 38 | } 39 | 40 | public IDistanceRelaxer DistanceRelaxer 41 | { 42 | get { return this.distanceRelaxer; } 43 | } 44 | 45 | public Func EdgeWeights 46 | { 47 | get { return this.edgeWeights; } 48 | } 49 | 50 | public IDictionary Distances 51 | { 52 | get { return this.distances; } 53 | } 54 | 55 | public IDisposable Attach(ITreeBuilderAlgorithm algorithm) 56 | { 57 | algorithm.TreeEdge += new EdgeAction(this.TreeEdge); 58 | return new DisposableAction( 59 | () => algorithm.TreeEdge -= new EdgeAction(this.TreeEdge) 60 | ); 61 | } 62 | 63 | private void TreeEdge(TEdge edge) 64 | { 65 | var source = edge.Source; 66 | var target = edge.Target; 67 | 68 | double sourceDistance; 69 | if (!this.distances.TryGetValue(source, out sourceDistance)) 70 | this.distances[source] = sourceDistance = this.distanceRelaxer.InitialDistance; 71 | this.distances[target] = this.DistanceRelaxer.Combine(sourceDistance, this.edgeWeights(edge)); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/VertexPredecessorRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.Observers 6 | { 7 | /// 8 | /// 9 | /// 10 | /// type of a vertex 11 | /// type of an edge 12 | /// 15 | [Serializable] 16 | public sealed class VertexPredecessorRecorderObserver : 17 | IObserver> 18 | where TEdge : IEdge 19 | { 20 | private readonly Dictionary vertexPredecessors; 21 | 22 | public VertexPredecessorRecorderObserver() 23 | :this(new Dictionary()) 24 | {} 25 | 26 | public VertexPredecessorRecorderObserver( 27 | Dictionary vertexPredecessors) 28 | { 29 | Contract.Requires(vertexPredecessors != null); 30 | 31 | this.vertexPredecessors = vertexPredecessors; 32 | } 33 | 34 | public IDictionary VertexPredecessors 35 | { 36 | get { return this.vertexPredecessors; } 37 | } 38 | 39 | public IDisposable Attach(ITreeBuilderAlgorithm algorithm) 40 | { 41 | algorithm.TreeEdge += new EdgeAction(TreeEdge); 42 | return new DisposableAction( 43 | () => algorithm.TreeEdge -= new EdgeAction(TreeEdge) 44 | ); 45 | } 46 | 47 | void TreeEdge(TEdge e) 48 | { 49 | this.vertexPredecessors[e.Target] = e; 50 | } 51 | 52 | public bool TryGetPath(TVertex vertex, out IEnumerable path) 53 | { 54 | return EdgeExtensions.TryGetPath(this.VertexPredecessors, vertex, out path); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/VertexRecorderObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.Observers 6 | { 7 | /// 8 | /// 9 | /// 10 | /// type of a vertex 11 | /// type of an edge 12 | /// 15 | [Serializable] 16 | public sealed class VertexRecorderObserver : 17 | IObserver> 18 | where TEdge : IEdge 19 | { 20 | private readonly IList vertices; 21 | public VertexRecorderObserver() 22 | : this(new List()) 23 | { } 24 | 25 | public VertexRecorderObserver(IList vertices) 26 | { 27 | Contract.Requires(vertices != null); 28 | 29 | this.vertices = vertices; 30 | } 31 | 32 | public IEnumerable Vertices 33 | { 34 | get 35 | { 36 | return this.vertices; 37 | } 38 | } 39 | 40 | public IDisposable Attach(IVertexTimeStamperAlgorithm algorithm) 41 | { 42 | algorithm.DiscoverVertex += new VertexAction(algorithm_DiscoverVertex); 43 | return new DisposableAction( 44 | () => algorithm.DiscoverVertex -= new VertexAction(algorithm_DiscoverVertex) 45 | ); 46 | } 47 | 48 | void algorithm_DiscoverVertex(TVertex v) 49 | { 50 | this.vertices.Add(v); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Observers/VertexTimeStamperObserver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.Observers 6 | { 7 | /// 8 | /// 9 | /// 10 | /// type of a vertex 11 | /// type of an edge 12 | /// 15 | [Serializable] 16 | public sealed class VertexTimeStamperObserver : 17 | IObserver> 18 | where TEdge : IEdge 19 | { 20 | private readonly Dictionary discoverTimes; 21 | private readonly Dictionary _finishTimes; 22 | private int currentTime = 0; 23 | 24 | 25 | public VertexTimeStamperObserver() 26 | :this(new Dictionary(), new Dictionary()) 27 | {} 28 | 29 | public VertexTimeStamperObserver(Dictionary discoverTimes) 30 | { 31 | Contract.Requires(discoverTimes != null); 32 | 33 | this.discoverTimes = discoverTimes; 34 | this._finishTimes = null; 35 | } 36 | 37 | public VertexTimeStamperObserver( 38 | Dictionary discoverTimes, 39 | Dictionary finishTimes) 40 | { 41 | Contract.Requires(discoverTimes != null); 42 | Contract.Requires(finishTimes != null); 43 | 44 | this.discoverTimes = discoverTimes; 45 | this._finishTimes = finishTimes; 46 | } 47 | 48 | public IDictionary DiscoverTimes 49 | { 50 | get { return this.discoverTimes; } 51 | } 52 | 53 | public IDictionary FinishTimes 54 | { 55 | get { return this._finishTimes; } 56 | } 57 | 58 | public IDisposable Attach(IVertexTimeStamperAlgorithm algorithm) 59 | { 60 | algorithm.DiscoverVertex+=new VertexAction(DiscoverVertex); 61 | if (this._finishTimes != null) 62 | algorithm.FinishVertex+=new VertexAction(FinishVertex); 63 | 64 | return new DisposableAction( 65 | () => 66 | { 67 | algorithm.DiscoverVertex -= new VertexAction(DiscoverVertex); 68 | if (this._finishTimes != null) 69 | algorithm.FinishVertex -= new VertexAction(FinishVertex); 70 | }); 71 | } 72 | 73 | void DiscoverVertex(TVertex v) 74 | { 75 | this.discoverTimes[v] = this.currentTime++; 76 | } 77 | 78 | void FinishVertex(TVertex v) 79 | { 80 | this._finishTimes[v] = this.currentTime++; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/IEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | namespace QuickGraph.Algorithms.RandomWalks 3 | { 4 | public interface IEdgeChain 5 | where TEdge : IEdge 6 | { 7 | bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor); 8 | bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge successor); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/IMarkovEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace QuickGraph.Algorithms.RandomWalks 3 | { 4 | public interface IMarkovEdgeChain 5 | : IEdgeChain 6 | where TEdge : IEdge 7 | { 8 | Random Rand { get;set;} 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/MarkovEdgeChainBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Algorithms.RandomWalks 5 | { 6 | [Serializable] 7 | public abstract class MarkovEdgeChainBase : 8 | IMarkovEdgeChain 9 | where TEdge : IEdge 10 | { 11 | private Random rand = new Random(); 12 | 13 | public Random Rand 14 | { 15 | get 16 | { 17 | return this.rand; 18 | } 19 | set 20 | { 21 | this.rand = value; 22 | } 23 | } 24 | 25 | public abstract bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor); 26 | public abstract bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge successor); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/NormalizedMarkovEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace QuickGraph.Algorithms.RandomWalks 6 | { 7 | [Serializable] 8 | public sealed class NormalizedMarkovEdgeChain : 9 | MarkovEdgeChainBase 10 | where TEdge : IEdge 11 | { 12 | public override bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor) 13 | { 14 | int outDegree = g.OutDegree(u); 15 | if (outDegree > 0) 16 | { 17 | int index = this.Rand.Next(0, outDegree); 18 | successor = g.OutEdge(u, index); 19 | return true; 20 | } 21 | 22 | successor = default(TEdge); 23 | return false; 24 | } 25 | 26 | public override bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge successor) 27 | { 28 | var edgeCount = Enumerable.Count(edges); 29 | 30 | if (edgeCount > 0) 31 | { 32 | int index = this.Rand.Next(0, edgeCount); 33 | successor = Enumerable.ElementAt(edges, index); 34 | return true; 35 | } 36 | 37 | successor = default(TEdge); 38 | return false; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/RoundRobinEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace QuickGraph.Algorithms.RandomWalks 6 | { 7 | [Serializable] 8 | public sealed class RoundRobinEdgeChain 9 | : IEdgeChain 10 | where TEdge : IEdge 11 | { 12 | private Dictionary outEdgeIndices = new Dictionary(); 13 | 14 | public bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor) 15 | { 16 | int outDegree = g.OutDegree(u); 17 | if (outDegree > 0) 18 | { 19 | int index; 20 | if (!outEdgeIndices.TryGetValue(u, out index)) 21 | { 22 | index = 0; 23 | outEdgeIndices.Add(u, index); 24 | } 25 | TEdge e = g.OutEdge(u, index); 26 | this.outEdgeIndices[u] = (++index) % outDegree; 27 | 28 | successor = e; 29 | return true; 30 | } 31 | 32 | successor = default(TEdge); 33 | return false; 34 | } 35 | 36 | public bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge successor) 37 | { 38 | var edgeCount = Enumerable.Count(edges); 39 | 40 | if (edgeCount > 0) 41 | { 42 | int index; 43 | if (!outEdgeIndices.TryGetValue(u, out index)) 44 | { 45 | index = 0; 46 | outEdgeIndices.Add(u, index); 47 | } 48 | var e = Enumerable.ElementAt(edges, index); 49 | this.outEdgeIndices[u] = (++index) % edgeCount; 50 | successor = e; 51 | } 52 | successor = default(TEdge); 53 | return false; 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/VanishingWeightedMarkovEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | namespace QuickGraph.Algorithms.RandomWalks 4 | { 5 | [Serializable] 6 | public sealed class VanishingWeightedMarkovEdgeChain : 7 | WeightedMarkovEdgeChainBase 8 | where TEdge : IEdge 9 | { 10 | private double factor; 11 | 12 | public VanishingWeightedMarkovEdgeChain(IDictionary weights) 13 | :this(weights,0.2) 14 | {} 15 | 16 | public VanishingWeightedMarkovEdgeChain(IDictionary weights, double factor) 17 | :base(weights) 18 | { 19 | this.factor = factor; 20 | } 21 | 22 | public double Factor 23 | { 24 | get 25 | { 26 | return this.factor; 27 | } 28 | set 29 | { 30 | this.factor = value; 31 | } 32 | } 33 | 34 | public override bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor) 35 | { 36 | if (!g.IsOutEdgesEmpty(u)) 37 | { 38 | // get outweight 39 | double outWeight = GetOutWeight(g, u); 40 | // get succesor 41 | TEdge s; 42 | if (this.TryGetSuccessor(g, u, this.Rand.NextDouble() * outWeight, out s)) 43 | { 44 | // update probabilities 45 | this.Weights[s] *= this.Factor; 46 | 47 | // normalize 48 | foreach (TEdge e in g.OutEdges(u)) 49 | { 50 | checked 51 | { 52 | this.Weights[e] /= outWeight; 53 | } 54 | } 55 | 56 | successor = s; 57 | return true; 58 | } 59 | } 60 | 61 | successor = default(TEdge); 62 | return false; 63 | } 64 | 65 | public override bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge successor) 66 | { 67 | // get outweight 68 | double outWeight = this.GetWeights(edges); 69 | // get succesor 70 | TEdge s; 71 | if (this.TryGetSuccessor(edges, this.Rand.NextDouble() * outWeight, out s)) 72 | { 73 | // update probabilities 74 | this.Weights[s] *= this.Factor; 75 | 76 | // normalize 77 | foreach (var e in edges) 78 | { 79 | checked 80 | { 81 | this.Weights[e] /= outWeight; 82 | } 83 | } 84 | 85 | 86 | successor = s; 87 | return true; 88 | } 89 | 90 | successor = default(TEdge); 91 | return false; 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/WeightedMarkedEdgeChain.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Algorithms.RandomWalks 5 | { 6 | [Serializable] 7 | public sealed class WeightedMarkovEdgeChain : 8 | WeightedMarkovEdgeChainBase 9 | where TEdge : IEdge 10 | { 11 | public WeightedMarkovEdgeChain(IDictionary weights) 12 | :base(weights) 13 | {} 14 | 15 | public override bool TryGetSuccessor(IImplicitGraph g, TVertex u, out TEdge successor) 16 | { 17 | // get number of out-edges 18 | int n = g.OutDegree(u); 19 | if (n > 0) 20 | { 21 | // compute out-edge su 22 | double outWeight = GetOutWeight(g, u); 23 | // scale and get next edge 24 | double r = this.Rand.NextDouble() * outWeight; 25 | return TryGetSuccessor(g, u, r, out successor); 26 | } 27 | 28 | successor = default(TEdge); 29 | return false; 30 | } 31 | 32 | public override bool TryGetSuccessor(IEnumerable edges, TVertex u, out TEdge sucessor) 33 | { 34 | // compute out-edge su 35 | double outWeight = GetWeights(edges); 36 | // scale and get next edge 37 | double r = this.Rand.NextDouble() * outWeight; 38 | return TryGetSuccessor(edges, r, out sucessor); 39 | } 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RandomWalks/WeightedMarkovEdgeChainBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Algorithms.RandomWalks 6 | { 7 | [Serializable] 8 | public abstract class WeightedMarkovEdgeChainBase : 9 | MarkovEdgeChainBase 10 | where TEdge : IEdge 11 | { 12 | private IDictionary weights; 13 | public WeightedMarkovEdgeChainBase(IDictionary weights) 14 | { 15 | Contract.Requires(weights != null); 16 | 17 | this.weights = weights; 18 | } 19 | 20 | public IDictionary Weights 21 | { 22 | get { return this.weights; } 23 | set { this.weights = value; } 24 | } 25 | 26 | protected double GetOutWeight(IImplicitGraph g, TVertex u) 27 | { 28 | var edges = g.OutEdges(u); 29 | return GetWeights(edges); 30 | } 31 | 32 | protected double GetWeights(IEnumerable edges) 33 | { 34 | double outWeight = 0; 35 | foreach (var e in edges) 36 | { 37 | outWeight += this.weights[e]; 38 | } 39 | return outWeight; 40 | } 41 | 42 | protected bool TryGetSuccessor(IImplicitGraph g, TVertex u, double position, out TEdge successor) 43 | { 44 | Contract.Requires(g != null); 45 | Contract.Requires(u != null); 46 | 47 | var edges = g.OutEdges(u); 48 | return TryGetSuccessor(edges, position, out successor); 49 | } 50 | 51 | protected bool TryGetSuccessor(IEnumerable edges, double position, out TEdge successor) 52 | { 53 | Contract.Requires(edges != null); 54 | 55 | double pos = 0; 56 | double nextPos = 0; 57 | foreach (var e in edges) 58 | { 59 | nextPos = pos + this.weights[e]; 60 | if (position >= pos && position <= nextPos) 61 | { 62 | successor = e; 63 | return true; 64 | } 65 | pos = nextPos; 66 | } 67 | 68 | successor = default(TEdge); 69 | return false; 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RankedShortestPath/RankedShortestPathAlgorithmBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using QuickGraph.Algorithms.Services; 5 | using QuickGraph.Algorithms.ShortestPath; 6 | using System.Diagnostics.Contracts; 7 | using System.Linq; 8 | 9 | namespace QuickGraph.Algorithms.RankedShortestPath 10 | { 11 | public abstract class RankedShortestPathAlgorithmBase 12 | : RootedAlgorithmBase 13 | where TEdge : IEdge 14 | where TGraph : IGraph 15 | { 16 | private readonly IDistanceRelaxer distanceRelaxer; 17 | private int shortestPathCount = 3; 18 | private List> computedShortestPaths; 19 | 20 | public int ShortestPathCount 21 | { 22 | get { return this.shortestPathCount; } 23 | set 24 | { 25 | Contract.Requires(value > 1); 26 | Contract.Ensures(this.ShortestPathCount == value); 27 | 28 | this.shortestPathCount = value; 29 | } 30 | } 31 | 32 | public int ComputedShortestPathCount 33 | { 34 | get 35 | { 36 | Contract.Ensures(Contract.Result() == Enumerable.Count(this.ComputedShortestPaths)); 37 | 38 | return this.computedShortestPaths == null ? 0 : this.computedShortestPaths.Count; 39 | } 40 | } 41 | 42 | public IEnumerable> ComputedShortestPaths 43 | { 44 | get 45 | { 46 | if (this.computedShortestPaths == null) 47 | yield break; 48 | else 49 | foreach (var path in this.computedShortestPaths) 50 | yield return path; 51 | } 52 | } 53 | 54 | protected void AddComputedShortestPath(List path) 55 | { 56 | Contract.Requires(path != null); 57 | Contract.Requires(Enumerable.All(path, e => e != null)); 58 | 59 | var pathArray = path.ToArray(); 60 | this.computedShortestPaths.Add(pathArray); 61 | } 62 | 63 | public IDistanceRelaxer DistanceRelaxer 64 | { 65 | get { return this.distanceRelaxer; } 66 | } 67 | 68 | protected RankedShortestPathAlgorithmBase( 69 | IAlgorithmComponent host, 70 | TGraph visitedGraph, 71 | IDistanceRelaxer distanceRelaxer) 72 | : base(host, visitedGraph) 73 | { 74 | Contract.Requires(distanceRelaxer != null); 75 | 76 | this.distanceRelaxer = distanceRelaxer; 77 | } 78 | 79 | protected override void Initialize() 80 | { 81 | base.Initialize(); 82 | this.computedShortestPaths = new List>(this.ShortestPathCount); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RootedAlgorithmBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using QuickGraph.Algorithms.Services; 3 | using System.Diagnostics.Contracts; 4 | using System.Collections.Generic; 5 | 6 | namespace QuickGraph.Algorithms 7 | { 8 | [Serializable] 9 | public abstract class RootedAlgorithmBase 10 | : AlgorithmBase 11 | { 12 | private TVertex rootVertex; 13 | private bool hasRootVertex; 14 | 15 | protected RootedAlgorithmBase( 16 | IAlgorithmComponent host, 17 | TGraph visitedGraph) 18 | :base(host, visitedGraph) 19 | {} 20 | 21 | public bool TryGetRootVertex(out TVertex rootVertex) 22 | { 23 | if (this.hasRootVertex) 24 | { 25 | rootVertex = this.rootVertex; 26 | return true; 27 | } 28 | else 29 | { 30 | rootVertex = default(TVertex); 31 | return false; 32 | } 33 | } 34 | 35 | public void SetRootVertex(TVertex rootVertex) 36 | { 37 | Contract.Requires(rootVertex != null); 38 | 39 | bool changed = Comparer.Default.Compare(this.rootVertex, rootVertex) != 0; 40 | this.rootVertex = rootVertex; 41 | if (changed) 42 | this.OnRootVertexChanged(EventArgs.Empty); 43 | this.hasRootVertex = true; 44 | } 45 | 46 | public void ClearRootVertex() 47 | { 48 | this.rootVertex = default(TVertex); 49 | this.hasRootVertex = false; 50 | } 51 | 52 | public event EventHandler RootVertexChanged; 53 | protected virtual void OnRootVertexChanged(EventArgs e) 54 | { 55 | Contract.Requires(e != null); 56 | 57 | var eh = this.RootVertexChanged; 58 | if (eh != null) 59 | eh(this, e); 60 | } 61 | 62 | public void Compute(TVertex rootVertex) 63 | { 64 | Contract.Requires(rootVertex != null); 65 | 66 | this.SetRootVertex(rootVertex); 67 | this.Compute(); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/RootedSearchAlgorithmBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | using QuickGraph.Algorithms.Services; 7 | 8 | namespace QuickGraph.Algorithms 9 | { 10 | public abstract class RootedSearchAlgorithmBase 11 | : RootedAlgorithmBase 12 | { 13 | private TVertex _goalVertex; 14 | private bool hasGoalVertex; 15 | 16 | protected RootedSearchAlgorithmBase( 17 | IAlgorithmComponent host, 18 | TGraph visitedGraph) 19 | :base(host, visitedGraph) 20 | {} 21 | 22 | public bool TryGetGoalVertex(out TVertex goalVertex) 23 | { 24 | if (this.hasGoalVertex) 25 | { 26 | goalVertex = this._goalVertex; 27 | return true; 28 | } 29 | else 30 | { 31 | goalVertex = default(TVertex); 32 | return false; 33 | } 34 | } 35 | 36 | public void SetGoalVertex(TVertex goalVertex) 37 | { 38 | Contract.Requires(goalVertex != null); 39 | 40 | bool changed = Comparer.Default.Compare(this._goalVertex, goalVertex) != 0; 41 | this._goalVertex = goalVertex; 42 | if (changed) 43 | this.OnGoalVertexChanged(EventArgs.Empty); 44 | this.hasGoalVertex = true; 45 | } 46 | 47 | public void ClearGoalVertex() 48 | { 49 | this._goalVertex = default(TVertex); 50 | this.hasGoalVertex = false; 51 | } 52 | 53 | public event EventHandler GoalReached; 54 | protected virtual void OnGoalReached() 55 | { 56 | var eh = this.GoalReached; 57 | if (eh != null) 58 | eh(this, EventArgs.Empty); 59 | } 60 | 61 | public event EventHandler GoalVertexChanged; 62 | protected virtual void OnGoalVertexChanged(EventArgs e) 63 | { 64 | Contract.Requires(e != null); 65 | 66 | var eh = this.GoalVertexChanged; 67 | if (eh != null) 68 | eh(this, e); 69 | } 70 | 71 | public void Compute(TVertex root, TVertex goal) 72 | { 73 | Contract.Requires(root != null); 74 | Contract.Requires(goal != null); 75 | 76 | this.SetGoalVertex(goal); 77 | this.Compute(root); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Services/IAlgorithmComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Algorithms.Services 6 | { 7 | public interface IAlgorithmComponent 8 | { 9 | IAlgorithmServices Services { get; } 10 | T GetService() where T : IService; 11 | bool TryGetService(out T service) where T : IService; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Services/IAlgorithmServices.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph.Algorithms.Services 7 | { 8 | /// 9 | /// Common services available to algorithm instances 10 | /// 11 | public interface IAlgorithmServices 12 | { 13 | ICancelManager CancelManager { get; } 14 | } 15 | 16 | class AlgorithmServices : 17 | IAlgorithmServices 18 | { 19 | readonly IAlgorithmComponent host; 20 | 21 | public AlgorithmServices(IAlgorithmComponent host) 22 | { 23 | Contract.Requires(host != null); 24 | 25 | this.host = host; 26 | } 27 | 28 | ICancelManager _cancelManager; 29 | public ICancelManager CancelManager 30 | { 31 | get 32 | { 33 | if (this._cancelManager == null) 34 | this._cancelManager = this.host.GetService(); 35 | return this._cancelManager; 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Services/ICancelManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading; 5 | 6 | namespace QuickGraph.Algorithms.Services 7 | { 8 | public interface ICancelManager : 9 | IService 10 | { 11 | /// 12 | /// Raised when the cancel method is called 13 | /// 14 | event EventHandler CancelRequested; 15 | 16 | /// 17 | /// Requests the component to cancel its computation 18 | /// 19 | void Cancel(); 20 | 21 | /// 22 | /// Gets a value indicating if a cancellation request is pending. 23 | /// 24 | /// 25 | bool IsCancelling { get; } 26 | 27 | /// 28 | /// Raised when the cancel state has been reseted 29 | /// 30 | event EventHandler CancelReseted; 31 | 32 | /// 33 | /// Resets the cancel state 34 | /// 35 | void ResetCancel(); 36 | } 37 | 38 | class CancelManager : 39 | ICancelManager 40 | { 41 | private int cancelling; 42 | 43 | public event EventHandler CancelRequested; 44 | 45 | public void Cancel() 46 | { 47 | var value = Interlocked.Increment(ref this.cancelling); 48 | if (value == 0) 49 | { 50 | var eh = this.CancelRequested; 51 | if (eh != null) 52 | eh(this, EventArgs.Empty); 53 | } 54 | } 55 | 56 | public bool IsCancelling 57 | { 58 | get { return this.cancelling > 0; } 59 | } 60 | 61 | /// 62 | /// Raised when the cancel state has been reseted 63 | /// 64 | public event EventHandler CancelReseted; 65 | 66 | /// 67 | /// Resets the cancel state 68 | /// 69 | public void ResetCancel() 70 | { 71 | var value = Interlocked.Exchange(ref this.cancelling, 0); 72 | if (value != 0) 73 | { 74 | var eh = this.CancelReseted; 75 | if (eh != null) 76 | eh(this, EventArgs.Empty); 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/Services/IService.cs: -------------------------------------------------------------------------------- 1 | namespace QuickGraph.Algorithms.Services 2 | { 3 | /// 4 | /// Interface implemented by graph services 5 | /// 6 | public interface IService 7 | {} 8 | } 9 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/TopologicalSort/TopologicalSortAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | using QuickGraph.Algorithms.Search; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Algorithms.TopologicalSort 8 | { 9 | [Serializable] 10 | public sealed class TopologicalSortAlgorithm : 11 | AlgorithmBase> 12 | where TEdge : IEdge 13 | { 14 | private IList vertices = new List(); 15 | private bool allowCyclicGraph = false; 16 | 17 | public TopologicalSortAlgorithm(IVertexListGraph g) 18 | :this(g, new List()) 19 | {} 20 | 21 | public TopologicalSortAlgorithm( 22 | IVertexListGraph g, 23 | IList vertices) 24 | :base(g) 25 | { 26 | Contract.Requires(vertices != null); 27 | 28 | this.vertices = vertices; 29 | } 30 | 31 | public IList SortedVertices 32 | { 33 | get 34 | { 35 | return vertices; 36 | } 37 | } 38 | 39 | public bool AllowCyclicGraph 40 | { 41 | get { return this.allowCyclicGraph; } 42 | } 43 | 44 | private void BackEdge(TEdge args) 45 | { 46 | if (!this.AllowCyclicGraph) 47 | throw new NonAcyclicGraphException(); 48 | } 49 | 50 | private void FinishVertex(TVertex v) 51 | { 52 | vertices.Insert(0, v); 53 | } 54 | 55 | protected override void InternalCompute() 56 | { 57 | DepthFirstSearchAlgorithm dfs = null; 58 | try 59 | { 60 | dfs = new DepthFirstSearchAlgorithm( 61 | this, 62 | this.VisitedGraph, 63 | new Dictionary(this.VisitedGraph.VertexCount) 64 | ); 65 | dfs.BackEdge += new EdgeAction(this.BackEdge); 66 | dfs.FinishVertex += new VertexAction(this.FinishVertex); 67 | 68 | dfs.Compute(); 69 | } 70 | finally 71 | { 72 | if (dfs != null) 73 | { 74 | dfs.BackEdge -= new EdgeAction(this.BackEdge); 75 | dfs.FinishVertex -= new VertexAction(this.FinishVertex); 76 | } 77 | } 78 | } 79 | 80 | public void Compute(IList vertices) 81 | { 82 | this.vertices = vertices; 83 | this.vertices.Clear(); 84 | this.Compute(); 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/QuickGraph/Algorithms/TopologicalSort/UndirectedTopologicalSortAlgorithm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | using QuickGraph.Algorithms.Search; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Algorithms.TopologicalSort 8 | { 9 | [Serializable] 10 | public sealed class UndirectedTopologicalSortAlgorithm 11 | : AlgorithmBase> 12 | where TEdge : IEdge 13 | { 14 | private IList vertices; 15 | private bool allowCyclicGraph = false; 16 | 17 | public UndirectedTopologicalSortAlgorithm(IUndirectedGraph g) 18 | : this(g, new List()) 19 | { } 20 | 21 | public UndirectedTopologicalSortAlgorithm( 22 | IUndirectedGraph g, 23 | IList vertices) 24 | : base(g) 25 | { 26 | Contract.Requires(vertices != null); 27 | 28 | this.vertices = vertices; 29 | } 30 | 31 | public IList SortedVertices 32 | { 33 | get 34 | { 35 | return vertices; 36 | } 37 | } 38 | 39 | public bool AllowCyclicGraph 40 | { 41 | get { return this.allowCyclicGraph; } 42 | set { this.allowCyclicGraph = value; } 43 | } 44 | 45 | private void BackEdge(object sender, UndirectedEdgeEventArgs a) 46 | { 47 | if (!this.AllowCyclicGraph) 48 | throw new NonAcyclicGraphException(); 49 | } 50 | 51 | private void FinishVertex(TVertex v) 52 | { 53 | vertices.Insert(0, v); 54 | } 55 | 56 | protected override void InternalCompute() 57 | { 58 | UndirectedDepthFirstSearchAlgorithm dfs = null; 59 | try 60 | { 61 | dfs = new UndirectedDepthFirstSearchAlgorithm( 62 | this, 63 | VisitedGraph, 64 | new Dictionary(this.VisitedGraph.VertexCount) 65 | ); 66 | dfs.BackEdge += new UndirectedEdgeAction(this.BackEdge); 67 | dfs.FinishVertex += new VertexAction(this.FinishVertex); 68 | 69 | dfs.Compute(); 70 | } 71 | finally 72 | { 73 | if (dfs != null) 74 | { 75 | dfs.BackEdge -= new UndirectedEdgeAction(this.BackEdge); 76 | dfs.FinishVertex -= new VertexAction(this.FinishVertex); 77 | } 78 | } 79 | } 80 | 81 | public void Compute(IList vertices) 82 | { 83 | this.vertices = vertices; 84 | this.vertices.Clear(); 85 | this.Compute(); 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/BinaryQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph.Collections 7 | { 8 | [Serializable] 9 | public sealed class BinaryQueue : 10 | IPriorityQueue 11 | { 12 | private readonly Func distances; 13 | private readonly BinaryHeap heap; 14 | 15 | public BinaryQueue( 16 | Func distances 17 | ) 18 | : this(distances, Comparer.Default.Compare) 19 | { } 20 | 21 | public BinaryQueue( 22 | Func distances, 23 | Func distanceComparison 24 | ) 25 | { 26 | Contract.Requires(distances != null); 27 | Contract.Requires(distanceComparison != null); 28 | 29 | this.distances = distances; 30 | this.heap = new BinaryHeap(distanceComparison); 31 | } 32 | 33 | public void Update(TVertex v) 34 | { 35 | this.heap.Update(this.distances(v), v); 36 | } 37 | 38 | public int Count 39 | { 40 | get { return this.heap.Count; } 41 | } 42 | 43 | public bool Contains(TVertex value) 44 | { 45 | return this.heap.IndexOf(value) > -1; 46 | } 47 | 48 | public void Enqueue(TVertex value) 49 | { 50 | this.heap.Add(this.distances(value), value); 51 | } 52 | 53 | public TVertex Dequeue() 54 | { 55 | return this.heap.RemoveMinimum().Value; 56 | } 57 | 58 | public TVertex Peek() 59 | { 60 | return this.heap.Minimum().Value; 61 | } 62 | 63 | public TVertex[] ToArray() 64 | { 65 | return this.heap.ToValueArray(); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/EdgeEdgeDictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Runtime.Serialization; 5 | 6 | namespace QuickGraph.Collections 7 | { 8 | [Serializable] 9 | public class EdgeEdgeDictionary 10 | : Dictionary 11 | , ICloneable 12 | , ISerializable 13 | where TEdge : IEdge 14 | { 15 | public EdgeEdgeDictionary() 16 | { } 17 | 18 | public EdgeEdgeDictionary(int capacity) 19 | : base(capacity) 20 | { } 21 | 22 | protected EdgeEdgeDictionary( 23 | SerializationInfo info, StreamingContext context) 24 | : base(info, context) { } 25 | 26 | public EdgeEdgeDictionary Clone() 27 | { 28 | var clone = new EdgeEdgeDictionary(this.Count); 29 | foreach (var kv in this) 30 | clone.Add(kv.Key, kv.Value); 31 | return clone; 32 | } 33 | 34 | object ICloneable.Clone() 35 | { 36 | return this.Clone(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/EdgeList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Collections 6 | { 7 | [Serializable] 8 | public sealed class EdgeList 9 | : List 10 | , IEdgeList 11 | , ICloneable 12 | where TEdge : IEdge 13 | { 14 | public EdgeList() 15 | { } 16 | 17 | public EdgeList(int capacity) 18 | : base(capacity) 19 | { } 20 | 21 | public EdgeList(EdgeList list) 22 | : base(list) 23 | {} 24 | 25 | public EdgeList Clone() 26 | { 27 | return new EdgeList(this); 28 | } 29 | 30 | IEdgeList IEdgeList.Clone() 31 | { 32 | return this.Clone(); 33 | } 34 | 35 | object ICloneable.Clone() 36 | { 37 | return this.Clone(); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/IDisjointSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Collections 6 | { 7 | /// 8 | /// A disjoint-set data structure 9 | /// 10 | /// 11 | [ContractClass(typeof(QuickGraph.Collections.Contracts.IDisjointSetContract<>))] 12 | public interface IDisjointSet 13 | { 14 | /// 15 | /// Gets the current number of sets 16 | /// 17 | int SetCount { get; } 18 | 19 | /// 20 | /// Gets the current number of elements. 21 | /// 22 | int ElementCount { get; } 23 | 24 | /// 25 | /// Creates a new set for the value 26 | /// 27 | /// 28 | void MakeSet(T value); 29 | 30 | /// 31 | /// Finds the set containing the value 32 | /// 33 | /// 34 | /// 35 | T FindSet(T value); 36 | 37 | /// 38 | /// Gets a value indicating if left and right are contained in the same set 39 | /// 40 | /// 41 | /// 42 | /// 43 | bool AreInSameSet(T left, T right); 44 | 45 | /// 46 | /// Merges the sets from the two values 47 | /// 48 | /// 49 | /// 50 | /// true if left and right were unioned, false if they already belong to the same set 51 | bool Union(T left, T right); 52 | 53 | /// 54 | /// Gets a value indicating whether the value is in the data structure 55 | /// 56 | /// 57 | /// 58 | [Pure] 59 | bool Contains(T value); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/IEdgeList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Collections 6 | { 7 | /// 8 | /// A cloneable list of edges 9 | /// 10 | /// 11 | /// 12 | [ContractClass(typeof(IEdgeListContract<,>))] 13 | public interface IEdgeList 14 | : IList 15 | , ICloneable 16 | where TEdge : IEdge 17 | { 18 | /// 19 | /// Trims excess allocated space 20 | /// 21 | void TrimExcess(); 22 | /// 23 | /// Gets a clone of this list 24 | /// 25 | /// 26 | new IEdgeList Clone(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/IPriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace QuickGraph.Collections 7 | { 8 | public interface IPriorityQueue 9 | : IQueue 10 | { 11 | void Update(T value); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/IQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph.Collections 6 | { 7 | public interface IQueue 8 | { 9 | int Count { get; } 10 | 11 | bool Contains(T value); 12 | void Enqueue(T value); 13 | T Dequeue(); 14 | T Peek(); 15 | 16 | T[] ToArray(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/IVertexEdgeDictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Serialization; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph.Collections 7 | { 8 | /// 9 | /// A dictionary of vertices to a list of edges 10 | /// 11 | /// 12 | /// 13 | [ContractClass(typeof(IVertexEdgeDictionaryContract<,>))] 14 | public interface IVertexEdgeDictionary 15 | : IDictionary> 16 | , ICloneable 17 | , ISerializable 18 | where TEdge : IEdge 19 | { 20 | /// 21 | /// Gets a clone of the dictionary. The vertices and edges are not cloned. 22 | /// 23 | /// 24 | new IVertexEdgeDictionary Clone(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/Queue.cs: -------------------------------------------------------------------------------- 1 | namespace QuickGraph.Collections 2 | { 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | [Serializable] 7 | public sealed class Queue : System.Collections.Generic.Queue, IQueue{ } 8 | } 9 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/VertexEdgeDictionary.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Runtime.Serialization; 5 | 6 | 7 | namespace QuickGraph.Collections 8 | { 9 | [Serializable] 10 | public sealed class VertexEdgeDictionary 11 | : Dictionary> 12 | , IVertexEdgeDictionary 13 | , ICloneable 14 | , ISerializable 15 | where TEdge : IEdge 16 | { 17 | public VertexEdgeDictionary() { } 18 | public VertexEdgeDictionary(int capacity) 19 | : base(capacity) 20 | { } 21 | public VertexEdgeDictionary(IEqualityComparer vertexComparer) 22 | : base(vertexComparer) 23 | { } 24 | public VertexEdgeDictionary(int capacity, IEqualityComparer vertexComparer) 25 | : base(capacity, vertexComparer) 26 | { } 27 | 28 | public VertexEdgeDictionary( 29 | SerializationInfo info, StreamingContext context) 30 | : base(info, context) { } 31 | 32 | public VertexEdgeDictionary Clone() 33 | { 34 | var clone = new VertexEdgeDictionary(this.Count); 35 | foreach (var kv in this) 36 | clone.Add(kv.Key, kv.Value.Clone()); 37 | return clone; 38 | } 39 | 40 | IVertexEdgeDictionary IVertexEdgeDictionary.Clone() 41 | { 42 | return this.Clone(); 43 | } 44 | 45 | object ICloneable.Clone() 46 | { 47 | return this.Clone(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/QuickGraph/Collections/VertexList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Runtime.Serialization; 5 | 6 | namespace QuickGraph.Collections 7 | { 8 | [Serializable] 9 | public sealed class VertexList 10 | : List 11 | , ICloneable 12 | { 13 | public VertexList() 14 | { } 15 | 16 | public VertexList(int capacity) 17 | : base(capacity) 18 | { } 19 | 20 | public VertexList(VertexList other) 21 | : base(other) 22 | { } 23 | 24 | public VertexList Clone() 25 | { 26 | return new VertexList(this); 27 | } 28 | 29 | object ICloneable.Clone() 30 | { 31 | return this.Clone(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/Collections/IDisjointSetContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Collections.Contracts 8 | { 9 | [ContractClassFor(typeof(IDisjointSet<>))] 10 | abstract class IDisjointSetContract 11 | : IDisjointSet 12 | { 13 | int IDisjointSet.SetCount 14 | { 15 | get 16 | { 17 | return default(int); 18 | } 19 | } 20 | 21 | int IDisjointSet.ElementCount 22 | { 23 | get 24 | { 25 | return default(int); 26 | } 27 | } 28 | 29 | [ContractInvariantMethod] 30 | void ObjectInvariant() 31 | { 32 | IDisjointSet ithis = this; 33 | Contract.Invariant(0 <= ithis.SetCount); 34 | Contract.Invariant(ithis.SetCount <= ithis.ElementCount); 35 | } 36 | 37 | 38 | void IDisjointSet.MakeSet(T value) 39 | { 40 | IDisjointSet ithis = this; 41 | Contract.Requires(value != null); 42 | Contract.Requires(!ithis.Contains(value)); 43 | Contract.Ensures(ithis.Contains(value)); 44 | Contract.Ensures(ithis.SetCount == Contract.OldValue(ithis.SetCount) + 1); 45 | Contract.Ensures(ithis.ElementCount == Contract.OldValue(ithis.ElementCount) + 1); 46 | } 47 | 48 | T IDisjointSet.FindSet(T value) 49 | { 50 | IDisjointSet ithis = this; 51 | Contract.Requires(value != null); 52 | Contract.Requires(ithis.Contains(value)); 53 | 54 | return default(T); 55 | } 56 | 57 | bool IDisjointSet.Union(T left, T right) 58 | { 59 | IDisjointSet ithis = this; 60 | Contract.Requires(left != null); 61 | Contract.Requires(ithis.Contains(left)); 62 | Contract.Requires(right != null); 63 | Contract.Requires(ithis.Contains(right)); 64 | 65 | return default(bool); 66 | } 67 | 68 | [Pure] 69 | bool IDisjointSet.Contains(T value) 70 | { 71 | return default(bool); 72 | } 73 | 74 | bool IDisjointSet.AreInSameSet(T left, T right) 75 | { 76 | IDisjointSet ithis = this; 77 | Contract.Requires(left != null); 78 | Contract.Requires(right != null); 79 | Contract.Requires(ithis.Contains(left)); 80 | Contract.Requires(ithis.Contains(right)); 81 | 82 | return default(bool); 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/Collections/IEdgeListContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Collections 6 | { 7 | [ContractClassFor(typeof(IEdgeList<,>))] 8 | abstract class IEdgeListContract 9 | : IEdgeList 10 | where TEdge : IEdge 11 | { 12 | IEdgeList IEdgeList.Clone() 13 | { 14 | Contract.Ensures(Contract.Result>() != null); 15 | throw new NotImplementedException(); 16 | } 17 | 18 | void IEdgeList.TrimExcess() 19 | { } 20 | 21 | #region others 22 | int IList.IndexOf(TEdge item) 23 | { 24 | throw new NotImplementedException(); 25 | } 26 | 27 | void IList.Insert(int index, TEdge item) 28 | { 29 | throw new NotImplementedException(); 30 | } 31 | 32 | void IList.RemoveAt(int index) 33 | { 34 | throw new NotImplementedException(); 35 | } 36 | 37 | TEdge IList.this[int index] 38 | { 39 | get 40 | { 41 | throw new NotImplementedException(); 42 | } 43 | set 44 | { 45 | throw new NotImplementedException(); 46 | } 47 | } 48 | 49 | void ICollection.Add(TEdge item) 50 | { 51 | throw new NotImplementedException(); 52 | } 53 | 54 | void ICollection.Clear() 55 | { 56 | throw new NotImplementedException(); 57 | } 58 | 59 | bool ICollection.Contains(TEdge item) 60 | { 61 | throw new NotImplementedException(); 62 | } 63 | 64 | void ICollection.CopyTo(TEdge[] array, int arrayIndex) 65 | { 66 | throw new NotImplementedException(); 67 | } 68 | 69 | int ICollection.Count 70 | { 71 | get { throw new NotImplementedException(); } 72 | } 73 | 74 | bool ICollection.IsReadOnly 75 | { 76 | get { throw new NotImplementedException(); } 77 | } 78 | 79 | bool ICollection.Remove(TEdge item) 80 | { 81 | throw new NotImplementedException(); 82 | } 83 | 84 | IEnumerator IEnumerable.GetEnumerator() 85 | { 86 | throw new NotImplementedException(); 87 | } 88 | 89 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 90 | { 91 | throw new NotImplementedException(); 92 | } 93 | 94 | object ICloneable.Clone() 95 | { 96 | throw new NotImplementedException(); 97 | } 98 | #endregion 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/DummyContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace System.Diagnostics.Contracts 4 | { 5 | /// 6 | /// Ensures that System.Diagnostics.Contracts namespace exists 7 | /// 8 | class DummyContract 9 | {} 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/EnumerableContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | using System.Diagnostics; 6 | using System.Linq; 7 | 8 | namespace QuickGraph 9 | { 10 | public static class EnumerableContract 11 | { 12 | [Pure] 13 | public static bool ElementsNotNull(IEnumerable elements) 14 | { 15 | Contract.Requires(elements != null); 16 | #if DEBUG 17 | 18 | return Enumerable.All(elements, e => e != null); 19 | #else 20 | return true; 21 | #endif 22 | } 23 | 24 | [Pure] 25 | public static bool All(int lowerBound, int exclusiveUpperBound, Func predicate) 26 | { 27 | for (int i = lowerBound; i < exclusiveUpperBound; i++) 28 | { 29 | if (!predicate(i)) return false; 30 | } 31 | return true; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/GraphContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Contracts 8 | { 9 | /// 10 | /// Debug only assertions and assumptions 11 | /// 12 | public static class GraphContract 13 | { 14 | [Pure] 15 | public static bool VertexCountEqual(this IVertexSet left, IVertexSet right) 16 | { 17 | Contract.Requires(left != null); 18 | Contract.Requires(right != null); 19 | 20 | return left.VertexCount == right.VertexCount; 21 | } 22 | 23 | [Pure] 24 | public static bool EdgeCountEqual 25 | (this IEdgeListGraph left, IEdgeListGraph right) 26 | where TEdge : IEdge 27 | { 28 | Contract.Requires(left != null); 29 | Contract.Requires(right != null); 30 | 31 | return left.EdgeCount == right.EdgeCount; 32 | } 33 | 34 | [Pure] 35 | public static bool InVertexSet( 36 | IVertexSet g, 37 | TVertex v) 38 | { 39 | Contract.Requires(g != null); 40 | Contract.Requires(v != null); 41 | // todo make requires 42 | return g.ContainsVertex(v); 43 | } 44 | 45 | [Pure] 46 | public static bool InVertexSet( 47 | IEdgeListGraph g, 48 | TEdge e) 49 | where TEdge : IEdge 50 | { 51 | Contract.Requires(g != null); 52 | Contract.Requires(e != null); 53 | 54 | return InVertexSet(g, e.Source) 55 | && InVertexSet(g, e.Target); 56 | } 57 | 58 | [Pure] 59 | public static bool InEdgeSet( 60 | IEdgeListGraph g, 61 | TEdge e) 62 | where TEdge : IEdge 63 | { 64 | Contract.Requires(g != null); 65 | Contract.Requires(e != null); 66 | 67 | return InVertexSet(g, e) 68 | && g.ContainsEdge(e); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/ICloneableEdgeContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph.Contracts 8 | { 9 | [ContractClassFor(typeof(ICloneableEdge<,>))] 10 | abstract class ICloneableEdgeContract 11 | : ICloneableEdge 12 | where TEdge : IEdge 13 | { 14 | TEdge ICloneableEdge.Clone(TVertex source, TVertex target) 15 | { 16 | Contract.Requires(source != null); 17 | Contract.Requires(target != null); 18 | Contract.Ensures(Contract.Result() != null); 19 | Contract.Ensures(Contract.Result().Source.Equals(source)); 20 | Contract.Ensures(Contract.Result().Target.Equals(target)); 21 | 22 | return default(TEdge); 23 | } 24 | 25 | #region IEdge Members 26 | 27 | TVertex IEdge.Source 28 | { 29 | get { throw new NotImplementedException(); } 30 | } 31 | 32 | TVertex IEdge.Target 33 | { 34 | get { throw new NotImplementedException(); } 35 | } 36 | 37 | #endregion 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IEdgeContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Contracts 6 | { 7 | [ContractClassFor(typeof(IEdge<>))] 8 | abstract class IEdgeContract 9 | : IEdge 10 | { 11 | [ContractInvariantMethod] 12 | void IEdgeInvariant() 13 | { 14 | IEdge ithis = this; 15 | Contract.Invariant(ithis.Source != null); 16 | Contract.Invariant(ithis.Target != null); 17 | } 18 | 19 | TVertex IEdge.Source 20 | { 21 | get 22 | { 23 | Contract.Ensures(Contract.Result() != null); 24 | return default(TVertex); 25 | } 26 | } 27 | 28 | TVertex IEdge.Target 29 | { 30 | get 31 | { 32 | Contract.Ensures(Contract.Result() != null); 33 | return default(TVertex); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IEdgeListGraphContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Contracts 5 | { 6 | [ContractClassFor(typeof(IEdgeListGraph<,>))] 7 | abstract class IEdgeListGraphContract 8 | : IEdgeListGraph 9 | where TEdge : IEdge 10 | { 11 | #region IGraph Members 12 | bool IGraph.IsDirected 13 | { 14 | get { throw new NotImplementedException(); } 15 | } 16 | 17 | bool IGraph.AllowParallelEdges 18 | { 19 | get { throw new NotImplementedException(); } 20 | } 21 | 22 | #endregion 23 | 24 | #region IEdgeSet Members 25 | 26 | bool IEdgeSet.IsEdgesEmpty 27 | { 28 | get { throw new NotImplementedException(); } 29 | } 30 | 31 | int IEdgeSet.EdgeCount 32 | { 33 | get { throw new NotImplementedException(); } 34 | } 35 | 36 | System.Collections.Generic.IEnumerable IEdgeSet.Edges 37 | { 38 | get { throw new NotImplementedException(); } 39 | } 40 | 41 | bool IEdgeSet.ContainsEdge(TEdge edge) 42 | { 43 | throw new NotImplementedException(); 44 | } 45 | #endregion 46 | 47 | #region IVertexSet Members 48 | 49 | public bool IsVerticesEmpty { 50 | get { throw new NotImplementedException(); } 51 | } 52 | 53 | public int VertexCount { 54 | get { throw new NotImplementedException(); } 55 | } 56 | 57 | public System.Collections.Generic.IEnumerable Vertices { 58 | get { throw new NotImplementedException(); } 59 | } 60 | 61 | #endregion 62 | 63 | #region IImplicitVertexSet Members 64 | 65 | public bool ContainsVertex(TVertex vertex) { 66 | throw new NotImplementedException(); 67 | } 68 | 69 | #endregion 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IEdgeSetContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | using System.Linq; 6 | 7 | namespace QuickGraph.Contracts 8 | { 9 | [ContractClassFor(typeof(IEdgeSet<,>))] 10 | abstract class IEdgeSetContract 11 | : IEdgeSet 12 | where TEdge : IEdge 13 | { 14 | bool IEdgeSet.IsEdgesEmpty 15 | { 16 | get 17 | { 18 | IEdgeSet ithis = this; 19 | Contract.Ensures(Contract.Result() == (ithis.EdgeCount == 0)); 20 | 21 | return default(bool); 22 | } 23 | } 24 | 25 | int IEdgeSet.EdgeCount 26 | { 27 | get 28 | { 29 | IEdgeSet ithis = this; 30 | Contract.Ensures(Contract.Result() == Enumerable.Count(ithis.Edges)); 31 | 32 | return default(int); 33 | } 34 | } 35 | 36 | IEnumerable IEdgeSet.Edges 37 | { 38 | get 39 | { 40 | Contract.Ensures(Contract.Result>() != null); 41 | Contract.Ensures(Enumerable.All(Contract.Result>(), e => e != null)); 42 | 43 | return default(IEnumerable); 44 | } 45 | } 46 | 47 | [Pure] 48 | bool IEdgeSet.ContainsEdge(TEdge edge) 49 | { 50 | IEdgeSet ithis = this; 51 | Contract.Requires(edge != null); 52 | Contract.Ensures(Contract.Result() == Contract.Exists(ithis.Edges, e => e.Equals(edge))); 53 | 54 | return default(bool); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IGraphContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Contracts 6 | { 7 | [ContractClassFor(typeof(IGraph<,>))] 8 | abstract class IGraphContract 9 | : IGraph 10 | where TEdge : IEdge 11 | { 12 | bool IGraph.IsDirected 13 | { 14 | get { return default(bool); } 15 | } 16 | 17 | bool IGraph.AllowParallelEdges 18 | { 19 | get { return default(bool); } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IImplicitVertexSetContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph.Contracts 7 | { 8 | [ContractClassFor(typeof(IImplicitVertexSet<>))] 9 | abstract class IImplicitVertexSetContract 10 | : IImplicitVertexSet 11 | { 12 | [Pure] 13 | bool IImplicitVertexSet.ContainsVertex(TVertex vertex) 14 | { 15 | IImplicitVertexSet ithis = this; 16 | Contract.Requires(vertex != null); 17 | 18 | return default(bool); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IMutableGraphContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Contracts 6 | { 7 | [ContractClassFor(typeof(IMutableGraph<,>))] 8 | abstract class IMutableGraphContract 9 | : IMutableGraph 10 | where TEdge : IEdge 11 | { 12 | #region IMutableGraph Members 13 | void IMutableGraph.Clear() 14 | { 15 | IMutableGraph ithis = this; 16 | } 17 | #endregion 18 | 19 | #region IGraph Members 20 | 21 | bool IGraph.IsDirected 22 | { 23 | get { throw new NotImplementedException(); } 24 | } 25 | 26 | bool IGraph.AllowParallelEdges 27 | { 28 | get { throw new NotImplementedException(); } 29 | } 30 | 31 | #endregion 32 | 33 | 34 | event EventHandler IMutableGraph.Cleared 35 | { 36 | add { throw new NotImplementedException(); } 37 | remove { throw new NotImplementedException(); } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IUndirectedEdgeContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using System.Collections.Generic; 4 | 5 | namespace QuickGraph.Contracts 6 | { 7 | [ContractClassFor(typeof(IUndirectedEdge<>))] 8 | abstract class IUndirectedEdgeContract 9 | : IUndirectedEdge 10 | { 11 | [ContractInvariantMethod] 12 | void IUndirectedEdgeInvariant() 13 | { 14 | IUndirectedEdge ithis = this; 15 | Contract.Invariant(Comparer.Default.Compare(ithis.Source, ithis.Target) <= 0); 16 | } 17 | 18 | #region IEdge Members 19 | 20 | public TVertex Source { 21 | get { throw new NotImplementedException(); } 22 | } 23 | 24 | public TVertex Target { 25 | get { throw new NotImplementedException(); } 26 | } 27 | 28 | #endregion 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/QuickGraph/Contracts/IVertexSetContract.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | using System.Linq; 6 | 7 | namespace QuickGraph.Contracts 8 | { 9 | [ContractClassFor(typeof(IVertexSet<>))] 10 | abstract class IVertexSetContract 11 | : IVertexSet 12 | { 13 | bool IVertexSet.IsVerticesEmpty 14 | { 15 | get 16 | { 17 | IVertexSet ithis = this; 18 | Contract.Ensures(Contract.Result() == (ithis.VertexCount == 0)); 19 | 20 | return default(bool); 21 | } 22 | } 23 | 24 | int IVertexSet.VertexCount 25 | { 26 | get 27 | { 28 | IVertexSet ithis = this; 29 | Contract.Ensures(Contract.Result() == Enumerable.Count(ithis.Vertices)); 30 | 31 | return default(int); 32 | } 33 | } 34 | 35 | IEnumerable IVertexSet.Vertices 36 | { 37 | get 38 | { 39 | Contract.Ensures(Contract.Result>() != null); 40 | 41 | return default(IEnumerable); 42 | } 43 | } 44 | 45 | #region IImplicitVertexSet Members 46 | 47 | public bool ContainsVertex(TVertex vertex) { 48 | throw new NotImplementedException(); 49 | } 50 | 51 | #endregion 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/QuickGraph/CreateEdgeDelegate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace QuickGraph 3 | { 4 | /// 5 | /// An edge factory delegate 6 | /// 7 | [Serializable] 8 | public delegate TEdge CreateEdgeDelegate( 9 | IVertexListGraph g, 10 | TVertex source, 11 | TVertex target) 12 | where TEdge : IEdge; 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/CreateVertexDelegate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace QuickGraph 3 | { 4 | /// 5 | /// A vertex factory delegate. 6 | /// 7 | [Serializable] 8 | public delegate TVertex CreateVertexDelegate( 9 | IVertexListGraph g) 10 | where TEdge : IEdge; 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/DelegateBidirectionalIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph 8 | { 9 | /// 10 | /// A delegate based bidirectional implicit graph 11 | /// 12 | /// 13 | /// 14 | [Serializable] 15 | public class DelegateBidirectionalIncidenceGraph 16 | : DelegateIncidenceGraph 17 | , IBidirectionalIncidenceGraph 18 | where TEdge : IEdge, IEquatable 19 | { 20 | readonly TryFunc> tryGetInEdges; 21 | 22 | public DelegateBidirectionalIncidenceGraph( 23 | TryFunc> tryGetOutEdges, 24 | TryFunc> tryGetInEdges) 25 | :base(tryGetOutEdges) 26 | { 27 | Contract.Requires(tryGetInEdges != null); 28 | 29 | this.tryGetInEdges = tryGetInEdges; 30 | } 31 | 32 | public TryFunc> TryGetInEdgesFunc 33 | { 34 | get { return this.tryGetInEdges; } 35 | } 36 | 37 | #region IBidirectionalImplicitGraph Members 38 | 39 | public bool IsInEdgesEmpty(TVertex v) 40 | { 41 | foreach (var edge in this.InEdges(v)) 42 | return false; 43 | return true; 44 | } 45 | 46 | public int InDegree(TVertex v) 47 | { 48 | return Enumerable.Count(this.InEdges(v)); 49 | } 50 | 51 | public IEnumerable InEdges(TVertex v) 52 | { 53 | IEnumerable result; 54 | if (!this.tryGetInEdges(v, out result)) 55 | return Enumerable.Empty(); 56 | return result; 57 | } 58 | 59 | public bool TryGetInEdges(TVertex v, out IEnumerable edges) 60 | { 61 | return this.tryGetInEdges(v, out edges); 62 | } 63 | 64 | public TEdge InEdge(TVertex v, int index) 65 | { 66 | return Enumerable.ElementAt(this.InEdges(v), index); 67 | } 68 | 69 | public int Degree(TVertex v) 70 | { 71 | return this.InDegree(v) + this.OutDegree(v); 72 | } 73 | #endregion 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/QuickGraph/DelegateImplicitGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | using System.Diagnostics; 6 | using System.Linq; 7 | 8 | namespace QuickGraph 9 | { 10 | /// 11 | /// A delegate-based implicit graph 12 | /// 13 | /// type of the vertices 14 | /// type of the edges 15 | [Serializable] 16 | public class DelegateImplicitGraph 17 | : IImplicitGraph 18 | where TEdge : IEdge, IEquatable 19 | { 20 | readonly TryFunc> tryGetOutEdges; 21 | 22 | public DelegateImplicitGraph( 23 | TryFunc> tryGetOutEdges) 24 | { 25 | Contract.Requires(tryGetOutEdges != null); 26 | 27 | this.tryGetOutEdges = tryGetOutEdges; 28 | } 29 | 30 | public TryFunc> TryGetOutEdgesFunc 31 | { 32 | get { return this.tryGetOutEdges; } 33 | } 34 | 35 | public bool IsOutEdgesEmpty(TVertex v) 36 | { 37 | foreach (var edge in this.OutEdges(v)) 38 | return false; 39 | return true; 40 | } 41 | 42 | public int OutDegree(TVertex v) 43 | { 44 | return Enumerable.Count(this.OutEdges(v)); 45 | } 46 | 47 | public IEnumerable OutEdges(TVertex v) 48 | { 49 | IEnumerable result; 50 | if (!this.tryGetOutEdges(v, out result)) 51 | return Enumerable.Empty(); 52 | return result; 53 | } 54 | 55 | public bool TryGetOutEdges(TVertex v, out IEnumerable edges) 56 | { 57 | return this.tryGetOutEdges(v, out edges); 58 | } 59 | 60 | public TEdge OutEdge(TVertex v, int index) 61 | { 62 | return Enumerable.ElementAt(this.OutEdges(v), index); 63 | } 64 | 65 | public bool IsDirected 66 | { 67 | get { return true; } 68 | } 69 | 70 | public bool AllowParallelEdges 71 | { 72 | get { return true; } 73 | } 74 | 75 | public bool ContainsVertex(TVertex vertex) 76 | { 77 | IEnumerable edges; 78 | return 79 | this.tryGetOutEdges(vertex, out edges); 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/QuickGraph/DelegateIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A delegate-based incidence graph 10 | /// 11 | /// type of the vertices 12 | /// type of the edges 13 | [Serializable] 14 | public class DelegateIncidenceGraph 15 | : DelegateImplicitGraph 16 | , IIncidenceGraph 17 | where TEdge : IEdge, IEquatable 18 | { 19 | public DelegateIncidenceGraph( 20 | TryFunc> tryGetOutEdges) 21 | :base(tryGetOutEdges) {} 22 | 23 | public bool ContainsEdge(TVertex source, TVertex target) 24 | { 25 | TEdge edge; 26 | return this.TryGetEdge(source, target, out edge); 27 | } 28 | 29 | public bool TryGetEdges(TVertex source, TVertex target, out IEnumerable edges) 30 | { 31 | IEnumerable outEdges; 32 | List result = null; 33 | if (this.TryGetOutEdges(source, out outEdges)) 34 | foreach (var e in outEdges) 35 | if (e.Target.Equals(target)) 36 | { 37 | if (result == null) 38 | result = new List(); 39 | result.Add(e); 40 | } 41 | 42 | edges = result == null ? null : result.ToArray(); 43 | return edges != null; 44 | } 45 | 46 | public bool TryGetEdge(TVertex source, TVertex target, out TEdge edge) 47 | { 48 | IEnumerable edges; 49 | if (this.TryGetOutEdges(source, out edges)) 50 | foreach (var e in edges) 51 | if (e.Target.Equals(target)) 52 | { 53 | edge = e; 54 | return true; 55 | } 56 | 57 | edge = default(TEdge); 58 | return false; 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/QuickGraph/Edge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// The default implementation. 9 | /// 10 | /// The type of the vertex. 11 | [Serializable] 12 | [DebuggerDisplay("{Source}->{Target}")] 13 | public class Edge 14 | : IEdge 15 | { 16 | private readonly TVertex source; 17 | private readonly TVertex target; 18 | 19 | /// 20 | /// Initializes a new instance of the class. 21 | /// 22 | /// The source. 23 | /// The target. 24 | public Edge(TVertex source, TVertex target) 25 | { 26 | Contract.Requires(source != null); 27 | Contract.Requires(target != null); 28 | Contract.Ensures(this.Source.Equals(source)); 29 | Contract.Ensures(this.Target.Equals(target)); 30 | 31 | this.source = source; 32 | this.target = target; 33 | } 34 | 35 | /// 36 | /// Gets the source vertex 37 | /// 38 | /// 39 | public TVertex Source 40 | { 41 | get { return this.source; } 42 | } 43 | 44 | /// 45 | /// Gets the target vertex 46 | /// 47 | /// 48 | public TVertex Target 49 | { 50 | get { return this.target; } 51 | } 52 | 53 | /// 54 | /// Returns a that represents the current . 55 | /// 56 | /// 57 | /// A that represents the current . 58 | /// 59 | public override string ToString() 60 | { 61 | return this.Source + "->" + this.Target; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/QuickGraph/EdgeEdgeEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | /// 7 | /// The handler for events involving two edges 8 | /// 9 | public delegate void EdgeEdgeAction(TEdge edge, TEdge targetEdge) 10 | where TEdge : IEdge; 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/EdgeEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | /// 7 | /// An event involving an edge. 8 | /// 9 | /// The type of the vertex. 10 | /// The type of the edge. 11 | [Serializable] 12 | public class EdgeEventArgs 13 | : EventArgs 14 | where TEdge : IEdge 15 | { 16 | private readonly TEdge edge; 17 | /// 18 | /// Initializes a new instance of the class. 19 | /// 20 | /// The edge. 21 | public EdgeEventArgs(TEdge edge) 22 | { 23 | Contract.Requires(edge != null); 24 | 25 | this.edge = edge; 26 | } 27 | 28 | /// 29 | /// Gets the edge. 30 | /// 31 | /// The edge. 32 | public TEdge Edge 33 | { 34 | get { return this.edge; } 35 | } 36 | } 37 | 38 | /// 39 | /// The handler for events involving edges 40 | /// 41 | /// type of the vertices 42 | /// type of the edges 43 | /// 44 | public delegate void EdgeAction(TEdge e) 45 | where TEdge : IEdge; 46 | } 47 | -------------------------------------------------------------------------------- /src/QuickGraph/EdgeFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | /// 6 | /// An edge factory 7 | /// 8 | public delegate TEdge EdgeFactory(TVertex source, TVertex target) 9 | where TEdge : IEdge; 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/EdgeIdentity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace QuickGraph 7 | { 8 | public delegate string EdgeIdentity(TEdge edge) 9 | where TEdge : IEdge; 10 | } 11 | -------------------------------------------------------------------------------- /src/QuickGraph/EdgePredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | [Pure] 7 | public delegate bool EdgePredicate(TEdge e) 8 | where TEdge : IEdge; 9 | } 10 | -------------------------------------------------------------------------------- /src/QuickGraph/EquatableEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics; 6 | 7 | namespace QuickGraph 8 | { 9 | /// 10 | /// An equatable edge implementation 11 | /// 12 | /// type of the vertices 13 | [Serializable] 14 | [DebuggerDisplay("{Source}->{Target}")] 15 | public class EquatableEdge 16 | : Edge 17 | , IEquatable> 18 | { 19 | public EquatableEdge(TVertex source, TVertex target) 20 | : base(source, target) 21 | { } 22 | 23 | public bool Equals(EquatableEdge other) 24 | { 25 | return 26 | (object)other != null && 27 | this.Source.Equals(other.Source) && 28 | this.Target.Equals(other.Target); 29 | } 30 | 31 | public override bool Equals(object obj) 32 | { 33 | return this.Equals(obj as EquatableEdge); 34 | } 35 | 36 | public override int GetHashCode() 37 | { 38 | return 39 | HashCodeHelper.Combine(this.Source.GetHashCode(), this.Target.GetHashCode()); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/QuickGraph/FuncDelegates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | //public delegate void Action(T value); 8 | //public delegate void Action(T1 value1, T2 value2); 9 | //public delegate TResult Func(); 10 | //public delegate TResult Func(T value); 11 | //public delegate TResult Func(T1 value1, T2 value2); 12 | //public delegate TResult Func(T1 value1, T2 value2, T3 value3); 13 | } 14 | -------------------------------------------------------------------------------- /src/QuickGraph/GraphColor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | /// 6 | /// Colors used in vertex coloring algorithms 7 | /// 8 | [Serializable] 9 | public enum GraphColor : byte 10 | { 11 | /// 12 | /// Usually initial color, 13 | /// 14 | White = 0, 15 | /// 16 | /// Usually intermidiate color, 17 | /// 18 | Gray, 19 | /// 20 | /// Usually finished color 21 | /// 22 | Black 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/QuickGraph/HashCodeHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | static class HashCodeHelper 8 | { 9 | const Int32 FNV1_prime_32 = 16777619; 10 | const Int32 FNV1_basis_32 = unchecked((int)2166136261); 11 | const Int64 FNV1_prime_64 = 1099511628211; 12 | const Int64 FNV1_basis_64 = unchecked((int)14695981039346656037); 13 | 14 | public static Int32 GetHashCode(Int64 x) 15 | { 16 | return Combine((Int32)x, (Int32)(((UInt64)x) >> 32)); 17 | } 18 | 19 | private static Int32 Fold(Int32 hash, byte value) 20 | { 21 | return (hash * FNV1_prime_32) ^ (Int32)value; 22 | } 23 | 24 | private static Int32 Fold(Int32 hash, Int32 value) 25 | { 26 | return Fold(Fold(Fold(Fold(hash, 27 | (byte)value), 28 | (byte)(((UInt32)value) >> 8)), 29 | (byte)(((UInt32)value) >> 16)), 30 | (byte)(((UInt32)value) >> 24)); 31 | } 32 | 33 | /// 34 | /// Combines two hashcodes in a strong way. 35 | /// 36 | /// 37 | /// 38 | /// 39 | public static Int32 Combine(Int32 x, Int32 y) 40 | { 41 | return Fold(Fold(FNV1_basis_32, x), y); 42 | } 43 | 44 | /// 45 | /// Combines three hashcodes in a strong way. 46 | /// 47 | /// 48 | /// 49 | /// 50 | /// 51 | public static Int32 Combine(Int32 x, Int32 y, Int32 z) 52 | { 53 | return Fold(Fold(Fold(FNV1_basis_32, x), y), z); 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/QuickGraph/IBidirectionalGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A directed graph datastructure that is efficient 10 | /// to traverse both in and out edges. 11 | /// 12 | /// The type of the vertex. 13 | /// The type of the edge. 14 | [ContractClass(typeof(IBidirectionalGraphContract<,>))] 15 | public interface IBidirectionalGraph 16 | : IVertexAndEdgeListGraph 17 | , IBidirectionalIncidenceGraph 18 | where TEdge : IEdge 19 | { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/QuickGraph/IBidirectionalIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A directed graph datastructure that is efficient 10 | /// to traverse both in and out edges. 11 | /// 12 | /// The type of the vertex. 13 | /// The type of the edge. 14 | [ContractClass(typeof(IBidirectionalIncidenceGraphContract<,>))] 15 | public interface IBidirectionalIncidenceGraph 16 | : IIncidenceGraph 17 | where TEdge : IEdge 18 | { 19 | /// 20 | /// Determines whether has no in-edges. 21 | /// 22 | /// The vertex 23 | /// 24 | /// true if has no in-edges; otherwise, false. 25 | /// 26 | [Pure] 27 | bool IsInEdgesEmpty(TVertex v); 28 | 29 | /// 30 | /// Gets the number of in-edges of 31 | /// 32 | /// The vertex. 33 | /// The number of in-edges pointing towards 34 | [Pure] 35 | int InDegree(TVertex v); 36 | 37 | /// 38 | /// Gets the collection of in-edges of . 39 | /// 40 | /// The vertex 41 | /// The collection of in-edges of 42 | [Pure] 43 | IEnumerable InEdges(TVertex v); 44 | 45 | /// 46 | /// Tries to get the in-edges of 47 | /// 48 | /// 49 | /// 50 | /// 51 | [Pure] 52 | bool TryGetInEdges(TVertex v, out IEnumerable edges); 53 | 54 | /// 55 | /// Gets the in-edge at location . 56 | /// 57 | /// The vertex. 58 | /// The index. 59 | /// 60 | [Pure] 61 | TEdge InEdge(TVertex v, int index); 62 | 63 | /// 64 | /// Gets the degree of , i.e. 65 | /// the sum of the out-degree and in-degree of . 66 | /// 67 | /// The vertex 68 | /// The sum of OutDegree and InDegree of 69 | [Pure] 70 | int Degree(TVertex v); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/QuickGraph/ICloneableEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using QuickGraph.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// A cloneable edge 9 | /// 10 | /// type of the vertex 11 | /// type of the edge 12 | [ContractClass(typeof(ICloneableEdgeContract<,>))] 13 | public interface ICloneableEdge 14 | : IEdge 15 | where TEdge : IEdge 16 | { 17 | /// 18 | /// Clones the edge content to a different pair of 19 | /// and vertices 20 | /// 21 | /// The source vertex of the new edge 22 | /// The target vertex of the new edge 23 | /// A clone of the edge with new source and target vertices 24 | TEdge Clone(TVertex source, TVertex target); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/QuickGraph/IEdge.cs: -------------------------------------------------------------------------------- 1 | using QuickGraph.Contracts; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | /// 7 | /// A directed edge 8 | /// 9 | /// type of the vertices 10 | [ContractClass(typeof(IEdgeContract<>))] 11 | public interface IEdge 12 | { 13 | /// 14 | /// Gets the source vertex 15 | /// 16 | TVertex Source { get;} 17 | /// 18 | /// Gets the target vertex 19 | /// 20 | TVertex Target { get;} 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/QuickGraph/IEdgeListAndIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | namespace QuickGraph 2 | { 3 | /// 4 | /// An incidence graph whose edges can be enumerated 5 | /// 6 | /// type of the vertices 7 | /// type of the edges 8 | public interface IEdgeListAndIncidenceGraph 9 | : IEdgeListGraph 10 | , IIncidenceGraph 11 | where TEdge : IEdge 12 | { 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/QuickGraph/IEdgeListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A graph whose edges can be enumerated 10 | /// 11 | /// type of the vertices 12 | /// type of the edges 13 | [ContractClass(typeof(IEdgeListGraphContract<,>))] 14 | public interface IEdgeListGraph 15 | : IGraph 16 | , IEdgeSet 17 | , IVertexSet 18 | where TEdge : IEdge 19 | {} 20 | } 21 | -------------------------------------------------------------------------------- /src/QuickGraph/IEdgeSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using QuickGraph.Contracts; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph 8 | { 9 | /// 10 | /// A set of edges 11 | /// 12 | /// The type of the vertex. 13 | /// The type of the edge. 14 | [ContractClass(typeof(IEdgeSetContract<,>))] 15 | public interface IEdgeSet 16 | where TEdge : IEdge 17 | { 18 | /// 19 | /// Gets a value indicating whether there are no edges in this set. 20 | /// 21 | /// 22 | /// true if this set is empty; otherwise, false. 23 | /// 24 | bool IsEdgesEmpty { get; } 25 | /// 26 | /// Gets the edge count. 27 | /// 28 | /// The edge count. 29 | int EdgeCount { get; } 30 | /// 31 | /// Gets the edges. 32 | /// 33 | /// The edges. 34 | IEnumerable Edges { get; } 35 | /// 36 | /// Determines whether the specified edge contains edge. 37 | /// 38 | /// The edge. 39 | /// 40 | /// true if the specified edge contains edge; otherwise, false. 41 | /// 42 | [Pure] 43 | bool ContainsEdge(TEdge edge); 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/QuickGraph/IGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using QuickGraph.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// A graph with vertices of type 9 | /// and edges of type 10 | /// 11 | /// type of the vertices 12 | /// type of the edges 13 | [ContractClass(typeof(IGraphContract<,>))] 14 | public interface IGraph 15 | where TEdge : IEdge 16 | { 17 | /// 18 | /// Gets a value indicating if the graph is directed 19 | /// 20 | bool IsDirected { get;} 21 | /// 22 | /// Gets a value indicating if the graph allows parallel edges 23 | /// 24 | bool AllowParallelEdges { get;} 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/QuickGraph/IHyperEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph 5 | { 6 | public interface IHyperEdge 7 | { 8 | int EndPointCount { get;} 9 | IEnumerable EndPoints { get;} 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/IImplicitGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Contracts; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A implicit directed graph datastructure 10 | /// 11 | /// The type of the vertex. 12 | /// The type of the edge. 13 | [ContractClass(typeof(IImplicitGraphContract<,>))] 14 | public interface IImplicitGraph 15 | : IGraph 16 | , IImplicitVertexSet 17 | where TEdge : IEdge 18 | { 19 | /// 20 | /// Determines whether there are out-edges associated to . 21 | /// 22 | /// The vertex. 23 | /// 24 | /// true if has no out-edges; otherwise, false. 25 | /// 26 | [Pure] 27 | bool IsOutEdgesEmpty(TVertex v); 28 | 29 | /// 30 | /// Gets the count of out-edges of 31 | /// 32 | /// The vertex. 33 | /// The count of out-edges of 34 | [Pure] 35 | int OutDegree(TVertex v); 36 | 37 | /// 38 | /// Gets the out-edges of . 39 | /// 40 | /// The vertex. 41 | /// An enumeration of the out-edges of . 42 | [Pure] 43 | IEnumerable OutEdges(TVertex v); 44 | 45 | /// 46 | /// Tries to get the out-edges of 47 | /// 48 | /// 49 | /// 50 | /// 51 | [Pure] 52 | bool TryGetOutEdges(TVertex v, out IEnumerable edges); 53 | 54 | /// 55 | /// Gets the out-edge of at position . 56 | /// 57 | /// The vertex. 58 | /// The index. 59 | /// The out-edge at position 60 | [Pure] 61 | TEdge OutEdge(TVertex v, int index); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/QuickGraph/IImplicitUndirectedGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | [ContractClass(typeof(IImplicitUndirectedGraphContract<,>))] 9 | public interface IImplicitUndirectedGraph 10 | : IImplicitVertexSet 11 | , IGraph 12 | where TEdge : IEdge 13 | { 14 | [Pure] 15 | EdgeEqualityComparer EdgeEqualityComparer { get; } 16 | 17 | [Pure] 18 | IEnumerable AdjacentEdges(TVertex v); 19 | 20 | [Pure] 21 | int AdjacentDegree(TVertex v); 22 | 23 | [Pure] 24 | bool IsAdjacentEdgesEmpty(TVertex v); 25 | 26 | [Pure] 27 | TEdge AdjacentEdge(TVertex v, int index); 28 | 29 | [Pure] 30 | bool TryGetEdge(TVertex source, TVertex target, out TEdge edge); 31 | 32 | [Pure] 33 | bool ContainsEdge(TVertex source, TVertex target); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/QuickGraph/IImplicitVertexSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using QuickGraph.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// An implicit set of vertices 9 | /// 10 | /// type of the vertices 11 | [ContractClass(typeof(IImplicitVertexSetContract<>))] 12 | public interface IImplicitVertexSet 13 | { 14 | /// 15 | /// Determines whether the specified vertex contains vertex. 16 | /// 17 | /// The vertex. 18 | /// 19 | /// true if the specified vertex contains vertex; otherwise, false. 20 | /// 21 | [Pure] 22 | bool ContainsVertex(TVertex vertex); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/QuickGraph/IIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Contracts; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | [ContractClass(typeof(IIncidenceGraphContract<,>))] 9 | public interface IIncidenceGraph 10 | : IImplicitGraph 11 | where TEdge : IEdge 12 | { 13 | bool ContainsEdge(TVertex source, TVertex target); 14 | bool TryGetEdges( 15 | TVertex source, 16 | TVertex target, 17 | out IEnumerable edges); 18 | bool TryGetEdge( 19 | TVertex source, 20 | TVertex target, 21 | out TEdge edge); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableBidirectionalGraph.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.Contracts; 2 | using QuickGraph.Contracts; 3 | namespace QuickGraph 4 | { 5 | /// 6 | /// A mutable bidirectional directed graph 7 | /// 8 | /// type of the vertices 9 | /// type of the edges 10 | [ContractClass(typeof(IMutableBidirectionalGraphContract<,>))] 11 | public interface IMutableBidirectionalGraph 12 | : IMutableVertexAndEdgeListGraph 13 | , IBidirectionalGraph 14 | where TEdge : IEdge 15 | { 16 | /// 17 | /// Removes in-edges of that match 18 | /// predicate . 19 | /// 20 | /// 21 | /// 22 | /// Number of edges removed 23 | int RemoveInEdgeIf(TVertex v, EdgePredicate edgePredicate); 24 | 25 | /// 26 | /// Clears in-edges of 27 | /// 28 | /// 29 | void ClearInEdges(TVertex v); 30 | 31 | /// 32 | /// Clears in-edges and out-edges of 33 | /// 34 | /// 35 | void ClearEdges(TVertex v); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableEdgeListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable edge list graph. 10 | /// 11 | /// the vertex type 12 | /// the edge type 13 | [ContractClass(typeof(IMutableEdgeListGraphContract<,>))] 14 | public interface IMutableEdgeListGraph 15 | : IMutableGraph 16 | , IEdgeListGraph 17 | where TEdge : IEdge 18 | { 19 | /// 20 | /// Adds the edge to the graph 21 | /// 22 | /// 23 | /// true if the edge was added, otherwise false. 24 | bool AddEdge(TEdge edge); 25 | 26 | /// 27 | /// Raised when an edge is added to the graph. 28 | /// 29 | event EdgeAction EdgeAdded; 30 | 31 | /// 32 | /// Adds a set of edges to the graph. 33 | /// 34 | /// 35 | /// the number of edges successfully added to the graph. 36 | int AddEdgeRange(IEnumerable edges); 37 | 38 | /// 39 | /// Removes from the graph 40 | /// 41 | /// 42 | /// true if was successfully removed; otherwise false. 43 | bool RemoveEdge(TEdge edge); 44 | 45 | /// 46 | /// Raised when an edge has been removed from the graph. 47 | /// 48 | event EdgeAction EdgeRemoved; 49 | 50 | /// 51 | /// Removes all edges that match . 52 | /// 53 | /// A pure delegate that takes an and returns true if the edge should be removed. 54 | /// the number of edges removed. 55 | int RemoveEdgeIf(EdgePredicate predicate); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using QuickGraph.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// A mutable graph instance 9 | /// 10 | /// type of the vertices 11 | /// type of the edges 12 | [ContractClass(typeof(IMutableGraphContract<,>))] 13 | public interface IMutableGraph 14 | : IGraph 15 | where TEdge : IEdge 16 | { 17 | /// 18 | /// Clears the vertex and edges 19 | /// 20 | void Clear(); 21 | 22 | /// 23 | /// Called when the graph vertices and edges have been cleared. 24 | /// 25 | event EventHandler Cleared; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable incidence graph 10 | /// 11 | /// 12 | /// 13 | [ContractClass(typeof(IMutableIncidenceGraphContract<,>))] 14 | public interface IMutableIncidenceGraph 15 | : IMutableGraph 16 | , IIncidenceGraph 17 | where TEdge : IEdge 18 | { 19 | /// 20 | /// Removes all out edges of 21 | /// where evalutes to true. 22 | /// 23 | /// 24 | /// 25 | /// 26 | int RemoveOutEdgeIf( 27 | TVertex v, 28 | EdgePredicate predicate); 29 | 30 | /// 31 | /// Trims the out edges of vertex 32 | /// 33 | /// 34 | void ClearOutEdges(TVertex v); 35 | 36 | /// 37 | /// Trims excess storage allocated for edges 38 | /// 39 | void TrimEdgeExcess(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableUndirectedGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable indirect graph 10 | /// 11 | /// 12 | /// 13 | [ContractClass(typeof(IMutableUndirectedGraphContract<,>))] 14 | public interface IMutableUndirectedGraph 15 | : IMutableEdgeListGraph 16 | , IMutableVertexSet 17 | , IUndirectedGraph 18 | , IMutableVertexAndEdgeSet 19 | where TEdge : IEdge 20 | { 21 | int RemoveAdjacentEdgeIf(TVertex vertex, EdgePredicate predicate); 22 | void ClearAdjacentEdges(TVertex vertex); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableVertexAndEdgeListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using QuickGraph.Contracts; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable vertex and edge list graph 10 | /// 11 | /// 12 | /// 13 | public interface IMutableVertexAndEdgeListGraph 14 | : IMutableVertexListGraph 15 | , IMutableEdgeListGraph 16 | , IMutableVertexAndEdgeSet 17 | , IVertexAndEdgeListGraph 18 | where TEdge : IEdge 19 | { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableVertexAndEdgeSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using QuickGraph.Contracts; 6 | using System.Diagnostics.Contracts; 7 | 8 | namespace QuickGraph 9 | { 10 | /// 11 | /// A mutable vertex and edge set 12 | /// 13 | /// 14 | /// 15 | [ContractClass(typeof(IMutableVertexAndEdgeSetContract<,>))] 16 | public interface IMutableVertexAndEdgeSet 17 | : IEdgeListGraph 18 | , IMutableVertexSet 19 | , IMutableEdgeListGraph 20 | where TEdge : IEdge 21 | { 22 | /// 23 | /// Adds the vertices and edge to the graph. 24 | /// 25 | /// 26 | /// true if the edge was added, otherwise false. 27 | bool AddVerticesAndEdge(TEdge edge); 28 | 29 | /// 30 | /// Adds a set of edges (and it's vertices if necessary) 31 | /// 32 | /// 33 | /// the number of edges added. 34 | int AddVerticesAndEdgeRange(IEnumerable edges); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableVertexListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable vertex list graph 10 | /// 11 | /// 12 | /// 13 | [ContractClass(typeof(IMutableVertexListGraphContract<,>))] 14 | public interface IMutableVertexListGraph : 15 | IMutableIncidenceGraph, 16 | IMutableVertexSet 17 | where TEdge : IEdge 18 | { 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/QuickGraph/IMutableVertexSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A mutable vertex set 10 | /// 11 | /// 12 | [ContractClass(typeof(IMutableVertexSetContract<>))] 13 | public interface IMutableVertexSet 14 | : IVertexSet 15 | { 16 | event VertexAction VertexAdded; 17 | bool AddVertex(TVertex v); 18 | int AddVertexRange(IEnumerable vertices); 19 | 20 | event VertexAction VertexRemoved; 21 | bool RemoveVertex(TVertex v); 22 | int RemoveVertexIf(VertexPredicate pred); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/QuickGraph/ITagged.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// An instance holding a tag 9 | /// 10 | /// 11 | public interface ITagged 12 | { 13 | /// 14 | /// Gets or sets the tag 15 | /// 16 | TTag Tag { get; set; } 17 | 18 | /// 19 | /// Raised when the tag is changed 20 | /// 21 | event EventHandler TagChanged; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/QuickGraph/IUndirectedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using QuickGraph.Contracts; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// An undirected edge. 9 | /// 10 | /// 11 | /// Invariant: source must be less or equal to target (using the default comparer) 12 | /// 13 | /// type of the vertices 14 | [ContractClass(typeof(IUndirectedEdgeContract<>))] 15 | public interface IUndirectedEdge 16 | : IEdge 17 | { 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/QuickGraph/IUndirectedGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | using QuickGraph.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// An undirected graph 10 | /// 11 | /// 12 | /// 13 | [ContractClass(typeof(IUndirectedGraphContract<,>))] 14 | public interface IUndirectedGraph 15 | : IImplicitUndirectedGraph 16 | , IEdgeListGraph 17 | , IGraph 18 | where TEdge : IEdge 19 | { 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/QuickGraph/IVertexAndEdgeListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | /// 6 | /// A directed graph where vertices and edges can be enumerated efficiently. 7 | /// 8 | /// type of the vertices 9 | /// type of the edges 10 | public interface IVertexAndEdgeListGraph 11 | : IVertexListGraph 12 | , IEdgeListGraph 13 | where TEdge : IEdge 14 | {} 15 | } 16 | -------------------------------------------------------------------------------- /src/QuickGraph/IVertexListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph 5 | { 6 | /// 7 | /// A directed graph datastructure where out-edges can be traversed, 8 | /// i.e. a vertex set + implicit graph. 9 | /// 10 | /// The type of the vertex. 11 | /// The type of the edge. 12 | public interface IVertexListGraph 13 | : IIncidenceGraph 14 | , IVertexSet 15 | where TEdge : IEdge 16 | { 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/QuickGraph/IVertexSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | using QuickGraph.Contracts; 6 | 7 | namespace QuickGraph 8 | { 9 | /// 10 | /// A set of vertices 11 | /// 12 | /// type of the vertices 13 | [ContractClass(typeof(IVertexSetContract<>))] 14 | public interface IVertexSet 15 | : IImplicitVertexSet 16 | { 17 | /// 18 | /// Gets a value indicating whether there are no vertices in this set. 19 | /// 20 | /// 21 | /// true if the vertex set is empty; otherwise, false. 22 | /// 23 | bool IsVerticesEmpty { get;} 24 | 25 | /// 26 | /// Gets the vertex count. 27 | /// 28 | /// The vertex count. 29 | int VertexCount { get;} 30 | 31 | /// 32 | /// Gets the vertices. 33 | /// 34 | /// The vertices. 35 | IEnumerable Vertices { get;} 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/QuickGraph/IdentifiableEdgeFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// A factory of identifiable edges. 9 | /// 10 | public delegate TEdge IdentifiableEdgeFactory(TVertex source, TVertex target, string id) 11 | where TEdge: IEdge; 12 | } 13 | -------------------------------------------------------------------------------- /src/QuickGraph/IdentifiableVertexFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// A factory of identifiable vertices. 9 | /// 10 | public delegate TVertex IdentifiableVertexFactory(string id); 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/NegativeCycleGraphException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | [Serializable] 8 | public class NegativeCycleGraphException 9 | : QuickGraphException 10 | { 11 | public NegativeCycleGraphException() { } 12 | public NegativeCycleGraphException(string message) : base(message) { } 13 | public NegativeCycleGraphException(string message, Exception inner) : base(message, inner) { } 14 | protected NegativeCycleGraphException( 15 | System.Runtime.Serialization.SerializationInfo info, 16 | System.Runtime.Serialization.StreamingContext context) 17 | : base(info, context) { } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/QuickGraph/NegativeWeightException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | [System.Serializable] 6 | public class NegativeWeightException 7 | : QuickGraphException 8 | { 9 | public NegativeWeightException() { } 10 | public NegativeWeightException(string message) : base(message) { } 11 | public NegativeWeightException(string message, Exception inner) : base(message, inner) { } 12 | protected NegativeWeightException( 13 | System.Runtime.Serialization.SerializationInfo info, 14 | System.Runtime.Serialization.StreamingContext context) 15 | : base(info, context) { } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/QuickGraph/NonAcyclicGraphException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | [Serializable] 6 | public class NonAcyclicGraphException 7 | : QuickGraphException 8 | { 9 | public NonAcyclicGraphException() { } 10 | public NonAcyclicGraphException(string message) : base( message ) { } 11 | public NonAcyclicGraphException(string message, System.Exception inner) : base( message, inner ) { } 12 | protected NonAcyclicGraphException( 13 | System.Runtime.Serialization.SerializationInfo info, 14 | System.Runtime.Serialization.StreamingContext context) : base( info, context ) { } 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/QuickGraph/NonStronglyConnectedGraphException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace QuickGraph 3 | { 4 | /// 5 | /// Exception raised when an algorithm detects a non-strongly connected graph. 6 | /// 7 | [Serializable] 8 | public class NonStronglyConnectedGraphException 9 | : QuickGraphException 10 | { 11 | public NonStronglyConnectedGraphException() { } 12 | public NonStronglyConnectedGraphException(string message) : base( message ) { } 13 | public NonStronglyConnectedGraphException(string message, System.Exception inner) : base( message, inner ) { } 14 | protected NonStronglyConnectedGraphException( 15 | System.Runtime.Serialization.SerializationInfo info, 16 | System.Runtime.Serialization.StreamingContext context) : base( info, context ) { } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/QuickGraph/ParallelEdgeNotAllowedException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | [Serializable] 6 | public class ParallelEdgeNotAllowedException 7 | : QuickGraphException 8 | { 9 | public ParallelEdgeNotAllowedException() { } 10 | public ParallelEdgeNotAllowedException(string message) : base( message ) { } 11 | public ParallelEdgeNotAllowedException(string message, System.Exception inner) : base( message, inner ) { } 12 | protected ParallelEdgeNotAllowedException( 13 | System.Runtime.Serialization.SerializationInfo info, 14 | System.Runtime.Serialization.StreamingContext context) : base( info, context ) { } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public class FilteredGraph : IGraph 8 | where TEdge : IEdge 9 | where TGraph : IGraph 10 | { 11 | private readonly TGraph baseGraph; 12 | private readonly EdgePredicate edgePredicate; 13 | private readonly VertexPredicate vertexPredicate; 14 | 15 | public FilteredGraph( 16 | TGraph baseGraph, 17 | VertexPredicate vertexPredicate, 18 | EdgePredicate edgePredicate 19 | ) 20 | { 21 | Contract.Requires(baseGraph != null); 22 | Contract.Requires(vertexPredicate != null); 23 | Contract.Requires(edgePredicate != null); 24 | 25 | this.baseGraph = baseGraph; 26 | this.vertexPredicate = vertexPredicate; 27 | this.edgePredicate = edgePredicate; 28 | } 29 | 30 | /// 31 | /// Underlying filtered graph 32 | /// 33 | public TGraph BaseGraph 34 | { 35 | get 36 | { 37 | return baseGraph; 38 | } 39 | } 40 | 41 | /// 42 | /// Edge predicate used to filter the edges 43 | /// 44 | public EdgePredicate EdgePredicate 45 | { 46 | get 47 | { 48 | return edgePredicate; 49 | } 50 | } 51 | 52 | public VertexPredicate VertexPredicate 53 | { 54 | get 55 | { 56 | return vertexPredicate; 57 | } 58 | } 59 | 60 | protected bool TestEdge(TEdge edge) 61 | { 62 | return this.VertexPredicate(edge.Source) 63 | && this.VertexPredicate(edge.Target) 64 | && this.EdgePredicate(edge); 65 | } 66 | 67 | public bool IsDirected 68 | { 69 | get { return this.BaseGraph.IsDirected; } 70 | } 71 | 72 | public bool AllowParallelEdges 73 | { 74 | get 75 | { 76 | return baseGraph.AllowParallelEdges; 77 | } 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredImplicitGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Predicates 6 | { 7 | [Serializable] 8 | public class FilteredImplicitGraph 9 | : FilteredImplicitVertexSet 10 | , IImplicitGraph 11 | where TEdge : IEdge 12 | where TGraph : IImplicitGraph 13 | { 14 | public FilteredImplicitGraph( 15 | TGraph baseGraph, 16 | VertexPredicate vertexPredicate, 17 | EdgePredicate edgePredicate 18 | ) 19 | :base(baseGraph,vertexPredicate,edgePredicate) 20 | { } 21 | 22 | [Pure] 23 | public bool IsOutEdgesEmpty(TVertex v) 24 | { 25 | return this.OutDegree(v) == 0; 26 | } 27 | 28 | [Pure] 29 | public int OutDegree(TVertex v) 30 | { 31 | int count =0; 32 | foreach (var edge in this.BaseGraph.OutEdges(v)) 33 | if (this.TestEdge(edge)) 34 | count++; 35 | return count; 36 | } 37 | 38 | [Pure] 39 | public IEnumerable OutEdges(TVertex v) 40 | { 41 | foreach (var edge in this.BaseGraph.OutEdges(v)) 42 | if (this.TestEdge(edge)) 43 | yield return edge; 44 | } 45 | 46 | [Pure] 47 | public bool TryGetOutEdges(TVertex v, out IEnumerable edges) 48 | { 49 | IEnumerable baseEdges; 50 | if (!this.BaseGraph.TryGetOutEdges(v, out baseEdges)) 51 | { 52 | edges = null; 53 | return false; 54 | } 55 | 56 | edges = this.OutEdges(v); 57 | return true; 58 | } 59 | 60 | [Pure] 61 | public TEdge OutEdge(TVertex v, int index) 62 | { 63 | throw new NotSupportedException(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredImplicitVertexSetGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public class FilteredImplicitVertexSet 8 | : FilteredGraph 9 | , IImplicitVertexSet 10 | where TEdge : IEdge 11 | where TGraph : IGraph, IImplicitVertexSet 12 | { 13 | public FilteredImplicitVertexSet( 14 | TGraph baseGraph, 15 | VertexPredicate vertexPredicate, 16 | EdgePredicate edgePredicate 17 | ) 18 | :base(baseGraph,vertexPredicate,edgePredicate) 19 | { } 20 | 21 | [Pure] 22 | public bool ContainsVertex(TVertex vertex) 23 | { 24 | return 25 | this.VertexPredicate(vertex) && 26 | this.BaseGraph.ContainsVertex(vertex); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredIncidenceGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public class FilteredIncidenceGraph 8 | : FilteredImplicitGraph 9 | , IIncidenceGraph 10 | where TEdge : IEdge 11 | where TGraph : IIncidenceGraph 12 | { 13 | public FilteredIncidenceGraph( 14 | TGraph baseGraph, 15 | VertexPredicate vertexPredicate, 16 | EdgePredicate edgePredicate 17 | ) 18 | :base(baseGraph,vertexPredicate,edgePredicate) 19 | {} 20 | 21 | public bool ContainsEdge(TVertex source, TVertex target) 22 | { 23 | if (!this.VertexPredicate(source)) 24 | return false; 25 | if (!this.VertexPredicate(target)) 26 | return false; 27 | 28 | foreach (var edge in this.BaseGraph.OutEdges(source)) 29 | if (edge.Target.Equals(target) && this.EdgePredicate(edge)) 30 | return true; 31 | return false; 32 | } 33 | 34 | public bool TryGetEdge( 35 | TVertex source, 36 | TVertex target, 37 | out TEdge edge) 38 | { 39 | IEnumerable unfilteredEdges; 40 | if (this.VertexPredicate(source) && 41 | this.VertexPredicate(target) && 42 | this.BaseGraph.TryGetEdges(source, target, out unfilteredEdges)) 43 | { 44 | foreach (var ufe in unfilteredEdges) 45 | if (this.EdgePredicate(ufe)) 46 | { 47 | edge = ufe; 48 | return true; 49 | } 50 | } 51 | edge = default(TEdge); 52 | return false; 53 | } 54 | 55 | public bool TryGetEdges( 56 | TVertex source, 57 | TVertex target, 58 | out IEnumerable edges) 59 | { 60 | edges = null; 61 | if (!this.VertexPredicate(source)) 62 | return false; 63 | if (!this.VertexPredicate(target)) 64 | return false; 65 | 66 | IEnumerable unfilteredEdges; 67 | if (this.BaseGraph.TryGetEdges(source, target, out unfilteredEdges)) 68 | { 69 | List filtered = new List(); 70 | foreach (var edge in unfilteredEdges) 71 | if (this.EdgePredicate(edge)) 72 | filtered.Add(edge); 73 | edges = filtered; 74 | return true; 75 | } 76 | 77 | return false; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredVertexAndEdgeListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Predicates 6 | { 7 | [Serializable] 8 | public class FilteredVertexAndEdgeListGraph : 9 | FilteredVertexListGraph, 10 | IVertexAndEdgeListGraph 11 | where TEdge : IEdge 12 | where TGraph : IVertexAndEdgeListGraph 13 | { 14 | public FilteredVertexAndEdgeListGraph( 15 | TGraph baseGraph, 16 | VertexPredicate vertexPredicate, 17 | EdgePredicate edgePredicate 18 | ) 19 | :base(baseGraph,vertexPredicate,edgePredicate) 20 | { } 21 | 22 | public bool IsEdgesEmpty 23 | { 24 | get 25 | { 26 | return this.EdgeCount == 0; 27 | } 28 | } 29 | 30 | public int EdgeCount 31 | { 32 | get 33 | { 34 | int count = 0; 35 | foreach (var edge in this.BaseGraph.Edges) 36 | { 37 | if ( 38 | this.VertexPredicate(edge.Source) 39 | && this.VertexPredicate(edge.Target) 40 | && this.EdgePredicate(edge)) 41 | count++; 42 | } 43 | return count; 44 | } 45 | } 46 | 47 | public IEnumerable Edges 48 | { 49 | get 50 | { 51 | foreach(TEdge edge in this.BaseGraph.Edges) 52 | { 53 | if ( 54 | this.VertexPredicate(edge.Source) 55 | && this.VertexPredicate(edge.Target) 56 | && this.EdgePredicate(edge)) 57 | yield return edge; 58 | } 59 | } 60 | } 61 | 62 | [Pure] 63 | public bool ContainsEdge(TEdge edge) 64 | { 65 | foreach (var e in this.Edges) 66 | if (Comparer.Default.Compare(edge, e) == 0) 67 | return true; 68 | return false; 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/FilteredVertexListGraph.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public class FilteredVertexListGraph 8 | : FilteredIncidenceGraph 9 | , IVertexListGraph 10 | where TEdge : IEdge 11 | where Graph : IVertexListGraph 12 | { 13 | public FilteredVertexListGraph( 14 | Graph baseGraph, 15 | VertexPredicate vertexPredicate, 16 | EdgePredicate edgePredicate 17 | ) 18 | :base(baseGraph,vertexPredicate,edgePredicate) 19 | { } 20 | 21 | public bool IsVerticesEmpty 22 | { 23 | get 24 | { 25 | foreach (var v in this.BaseGraph.Vertices) 26 | if (this.VertexPredicate(v)) 27 | return false; 28 | return true; 29 | } 30 | } 31 | 32 | public int VertexCount 33 | { 34 | get 35 | { 36 | int count = 0; 37 | foreach (var v in this.BaseGraph.Vertices) 38 | if (this.VertexPredicate(v)) 39 | count++; 40 | return count; 41 | } 42 | } 43 | 44 | public IEnumerable Vertices 45 | { 46 | get 47 | { 48 | foreach (var v in this.BaseGraph.Vertices) 49 | if (this.VertexPredicate(v)) 50 | yield return v; 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/InDictionaryVertexPredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Predicates 6 | { 7 | [Serializable] 8 | public sealed class InDictionaryVertexPredicate 9 | { 10 | private readonly IDictionary dictionary; 11 | 12 | public InDictionaryVertexPredicate( 13 | IDictionary dictionary) 14 | { 15 | Contract.Requires(dictionary != null); 16 | this.dictionary = dictionary; 17 | } 18 | 19 | [Pure] 20 | public bool Test(TVertex v) 21 | { 22 | Contract.Requires(v != null); 23 | 24 | return this.dictionary.ContainsKey(v); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/IsolatedVertexPredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Predicates 5 | { 6 | /// 7 | /// A vertex predicate that detects vertex with no in or out edges. 8 | /// 9 | /// type of the vertices 10 | /// type of the edges 11 | public sealed class IsolatedVertexPredicate 12 | where TEdge : IEdge 13 | { 14 | private readonly IBidirectionalGraph visitedGraph; 15 | 16 | public IsolatedVertexPredicate(IBidirectionalGraph visitedGraph) 17 | { 18 | Contract.Requires(visitedGraph!=null); 19 | 20 | this.visitedGraph = visitedGraph; 21 | } 22 | 23 | [Pure] 24 | public bool Test(TVertex v) 25 | { 26 | Contract.Requires(v != null); 27 | 28 | return this.visitedGraph.IsInEdgesEmpty(v) 29 | && this.visitedGraph.IsOutEdgesEmpty(v); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/ResidualEdgePrediate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | 5 | namespace QuickGraph.Predicates 6 | { 7 | public sealed class ResidualEdgePredicate 8 | where TEdge : IEdge 9 | { 10 | private readonly IDictionary residualCapacities; 11 | 12 | public ResidualEdgePredicate( 13 | IDictionary residualCapacities) 14 | { 15 | Contract.Requires(residualCapacities != null); 16 | 17 | this.residualCapacities = residualCapacities; 18 | } 19 | 20 | public IDictionary ResidualCapacities 21 | { 22 | get 23 | { 24 | return this.residualCapacities; 25 | } 26 | } 27 | 28 | public bool Test(TEdge e) 29 | { 30 | Contract.Requires(e != null); 31 | return 0 < this.residualCapacities[e]; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/ReversedResidualEdgePredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics.Contracts; 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public sealed class ReversedResidualEdgePredicate 8 | where TEdge : IEdge 9 | { 10 | private readonly IDictionary residualCapacities; 11 | private readonly IDictionary reversedEdges; 12 | 13 | public ReversedResidualEdgePredicate( 14 | IDictionary residualCapacities, 15 | IDictionary reversedEdges) 16 | { 17 | Contract.Requires(residualCapacities != null); 18 | Contract.Requires(reversedEdges != null); 19 | 20 | this.residualCapacities = residualCapacities; 21 | this.reversedEdges = reversedEdges; 22 | } 23 | 24 | /// 25 | /// Residual capacities map 26 | /// 27 | public IDictionary ResidualCapacities 28 | { 29 | get 30 | { 31 | return this.residualCapacities; 32 | } 33 | } 34 | 35 | /// 36 | /// Reversed edges map 37 | /// 38 | public IDictionary ReversedEdges 39 | { 40 | get 41 | { 42 | return this.reversedEdges; 43 | } 44 | } 45 | 46 | public bool Test(TEdge e) 47 | { 48 | Contract.Requires(e != null); 49 | return 0 < this.residualCapacities[reversedEdges[e]]; 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/QuickGraph/Predicates/SinkVertexPredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph.Predicates 5 | { 6 | [Serializable] 7 | public sealed class SinkVertexPredicate 8 | where TEdge : IEdge 9 | { 10 | private readonly IIncidenceGraph visitedGraph; 11 | 12 | public SinkVertexPredicate(IIncidenceGraph visitedGraph) 13 | { 14 | Contract.Requires(visitedGraph != null); 15 | 16 | this.visitedGraph = visitedGraph; 17 | } 18 | 19 | public bool Test(TVertex v) 20 | { 21 | return this.visitedGraph.IsOutEdgesEmpty(v); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/QuickGraph/QuickGraph.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | netstandard2.0;net462;net48;netcoreapp2.2;netcoreapp3.0 4 | mokeyish, pelikhan, Tom-Wells-NW 5 | QuickGraph Core 3.0 6 | QuickGraph.Core3.Tom-Wells-NW 7 | fork from https://github.com/mokeyish/QuickGraph just adding support .net core3.0 .NET Standard 2.1 8 | https://github.com/Tom-Wells-NW/QuickGraph 9 | QuickGraph;netstandard2.0; 10 | true 11 | 0.7.0.3 12 | 0.7.0.3 13 | 0.7.0.3-beta 14 | en-US 15 | git 16 | .NET Standard Version >= 2.0 17 | .NET Core Version >= 2.2 18 | .NET Framework Version = 4.6.2 19 | .NET Framework Version = 4.8 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /src/QuickGraph/QuickGraphException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | [Serializable] 6 | public abstract class QuickGraphException 7 | : Exception 8 | { 9 | protected QuickGraphException() { } 10 | protected QuickGraphException(string message) : base(message) { } 11 | protected QuickGraphException(string message, Exception inner) : base(message, inner) { } 12 | protected QuickGraphException( 13 | System.Runtime.Serialization.SerializationInfo info, 14 | System.Runtime.Serialization.StreamingContext context) 15 | : base(info, context) { } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/QuickGraph/SEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// An struct based implementation. 10 | /// 11 | /// The type of the vertex. 12 | [Serializable] 13 | [DebuggerDisplay(EdgeExtensions.DebuggerDisplayEdgeFormatString)] 14 | [StructLayout(LayoutKind.Auto)] 15 | public struct SEdge 16 | : IEdge 17 | { 18 | private readonly TVertex source; 19 | private readonly TVertex target; 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | /// The source. 25 | /// The target. 26 | public SEdge(TVertex source, TVertex target) 27 | { 28 | Contract.Requires(source != null); 29 | Contract.Requires(target != null); 30 | Contract.Ensures(Contract.ValueAtReturn(out this).Source.Equals(source)); 31 | Contract.Ensures(Contract.ValueAtReturn(out this).Target.Equals(target)); 32 | 33 | this.source = source; 34 | this.target = target; 35 | } 36 | 37 | /// 38 | /// Gets the source vertex 39 | /// 40 | /// 41 | public TVertex Source 42 | { 43 | get { return this.source; } 44 | } 45 | 46 | /// 47 | /// Gets the target vertex 48 | /// 49 | /// 50 | public TVertex Target 51 | { 52 | get { return this.target; } 53 | } 54 | 55 | /// 56 | /// Returns a that represents the current . 57 | /// 58 | /// 59 | /// A that represents the current . 60 | /// 61 | public override string ToString() 62 | { 63 | return String.Format( 64 | EdgeExtensions.EdgeFormatString, 65 | this.Source, 66 | this.Target); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/QuickGraph/SReversedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | using System.Runtime.InteropServices; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A reversed edge 10 | /// 11 | /// type of the vertices 12 | /// type of the edges 13 | [Serializable] 14 | [StructLayout(LayoutKind.Auto)] 15 | [DebuggerDisplay("{Source}<-{Target}")] 16 | public struct SReversedEdge 17 | : IEdge 18 | , IEquatable> 19 | where TEdge : IEdge 20 | { 21 | private readonly TEdge originalEdge; 22 | public SReversedEdge(TEdge originalEdge) 23 | { 24 | Contract.Requires(originalEdge != null); 25 | 26 | this.originalEdge = originalEdge; 27 | } 28 | 29 | public TEdge OriginalEdge 30 | { 31 | get { return this.originalEdge; } 32 | } 33 | 34 | public TVertex Source 35 | { 36 | get { return this.OriginalEdge.Target; } 37 | } 38 | 39 | public TVertex Target 40 | { 41 | get { return this.OriginalEdge.Source; } 42 | } 43 | 44 | [Pure] 45 | public override bool Equals(object obj) 46 | { 47 | if (!(obj is SReversedEdge)) 48 | return false; 49 | 50 | return Equals((SReversedEdge)obj); 51 | } 52 | 53 | [Pure] 54 | public override int GetHashCode() 55 | { 56 | return this.OriginalEdge.GetHashCode() ^ 16777619; 57 | } 58 | 59 | [Pure] 60 | public override string ToString() 61 | { 62 | return String.Format("R({0})", this.OriginalEdge); 63 | } 64 | 65 | [Pure] 66 | public bool Equals(SReversedEdge other) 67 | { 68 | return this.OriginalEdge.Equals(other.OriginalEdge); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/STaggedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using System.Runtime.InteropServices; 4 | using System.Diagnostics; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A tagged edge as value type. 10 | /// 11 | /// type of the vertices 12 | /// 13 | [Serializable] 14 | [StructLayout(LayoutKind.Auto)] 15 | [DebuggerDisplay("{Source}->{Target}:{Tag}")] 16 | public struct STaggedEdge 17 | : IEdge 18 | , ITagged 19 | { 20 | readonly TVertex source; 21 | readonly TVertex target; 22 | TTag tag; 23 | 24 | public STaggedEdge(TVertex source, TVertex target, TTag tag) 25 | { 26 | Contract.Requires(source != null); 27 | Contract.Requires(target != null); 28 | 29 | this.source = source; 30 | this.target = target; 31 | this.tag = tag; 32 | this.TagChanged = null; 33 | } 34 | 35 | public TVertex Source 36 | { 37 | get { return this.source; } 38 | } 39 | 40 | public TVertex Target 41 | { 42 | get { return this.target; } 43 | } 44 | 45 | public event EventHandler TagChanged; 46 | 47 | void OnTagChanged(EventArgs e) 48 | { 49 | var eh = this.TagChanged; 50 | if (eh != null) 51 | eh(this, e); 52 | } 53 | 54 | public TTag Tag 55 | { 56 | get { return this.tag; } 57 | set 58 | { 59 | if (!object.Equals(this.tag, value)) 60 | { 61 | this.tag = value; 62 | this.OnTagChanged(EventArgs.Empty); 63 | } 64 | } 65 | } 66 | 67 | /// 68 | /// Returns a that represents the current . 69 | /// 70 | /// 71 | /// A that represents the current . 72 | /// 73 | public override string ToString() 74 | { 75 | return String.Format("{0}->{1}:{2}", this.Source, this.Target, this.Tag); 76 | } 77 | 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/QuickGraph/SUndirectedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | using System.Runtime.InteropServices; 5 | using System.Collections.Generic; 6 | 7 | namespace QuickGraph 8 | { 9 | /// 10 | /// An struct based implementation. 11 | /// 12 | /// The type of the vertex. 13 | [Serializable] 14 | [DebuggerDisplay(EdgeExtensions.DebuggerDisplayUndirectedEdgeFormatString)] 15 | [StructLayout(LayoutKind.Auto)] 16 | public struct SUndirectedEdge 17 | : IUndirectedEdge 18 | { 19 | private readonly TVertex source; 20 | private readonly TVertex target; 21 | 22 | /// 23 | /// Initializes a new instance of the class. 24 | /// 25 | /// The source. 26 | /// The target. 27 | public SUndirectedEdge(TVertex source, TVertex target) 28 | { 29 | Contract.Requires(source != null); 30 | Contract.Requires(target != null); 31 | Contract.Requires(Comparer.Default.Compare(source, target) <= 0); 32 | Contract.Ensures(Contract.ValueAtReturn(out this).Source.Equals(source)); 33 | Contract.Ensures(Contract.ValueAtReturn(out this).Target.Equals(target)); 34 | 35 | this.source = source; 36 | this.target = target; 37 | } 38 | 39 | /// 40 | /// Gets the source vertex 41 | /// 42 | /// 43 | public TVertex Source 44 | { 45 | get { return this.source; } 46 | } 47 | 48 | /// 49 | /// Gets the target vertex 50 | /// 51 | /// 52 | public TVertex Target 53 | { 54 | get { return this.target; } 55 | } 56 | 57 | /// 58 | /// Returns a that represents the current . 59 | /// 60 | /// 61 | /// A that represents the current . 62 | /// 63 | public override string ToString() 64 | { 65 | return String.Format( 66 | EdgeExtensions.UndirectedEdgeFormatString, 67 | this.Source, 68 | this.Target); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/QuickGraph/TaggedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | [Serializable] 7 | public class TaggedEdge 8 | : Edge 9 | , ITagged 10 | { 11 | private TTag tag; 12 | 13 | public TaggedEdge(TVertex source, TVertex target, TTag tag) 14 | :base(source,target) 15 | { 16 | Contract.Ensures(Object.Equals(this.Tag,tag)); 17 | 18 | this.tag = tag; 19 | } 20 | 21 | public event EventHandler TagChanged; 22 | 23 | protected virtual void OnTagChanged(EventArgs e) 24 | { 25 | var eh = this.TagChanged; 26 | if (eh != null) 27 | eh(this, e); 28 | } 29 | 30 | public TTag Tag 31 | { 32 | get { return this.tag; } 33 | set 34 | { 35 | if (!object.Equals(this.tag, value)) 36 | { 37 | this.tag = value; 38 | this.OnTagChanged(EventArgs.Empty); 39 | } 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/QuickGraph/TaggedEquatableEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | using System.Diagnostics; 4 | 5 | namespace QuickGraph 6 | { 7 | /// 8 | /// An equatable, tagged, edge 9 | /// 10 | /// type of the vertices 11 | /// 12 | [Serializable] 13 | [DebuggerDisplay("{Source}->{Target}:{Tag}")] 14 | public class TaggedEquatableEdge 15 | : EquatableEdge 16 | , ITagged 17 | { 18 | private TTag tag; 19 | 20 | public TaggedEquatableEdge(TVertex source, TVertex target, TTag tag) 21 | : base(source, target) 22 | { 23 | Contract.Ensures(Object.Equals(this.Tag, tag)); 24 | 25 | this.tag = tag; 26 | } 27 | 28 | public event EventHandler TagChanged; 29 | 30 | protected virtual void OnTagChanged(EventArgs e) 31 | { 32 | var eh = this.TagChanged; 33 | if (eh != null) 34 | eh(this, e); 35 | } 36 | 37 | public TTag Tag 38 | { 39 | get { return this.tag; } 40 | set 41 | { 42 | if (!object.Equals(this.tag, value)) 43 | { 44 | this.tag = value; 45 | this.OnTagChanged(EventArgs.Empty); 46 | } 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/QuickGraph/TaggedUndirectedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | using System.Collections.Generic; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// A tagged undirected edge. 10 | /// 11 | /// The type of the vertex. 12 | /// Type type of the tag 13 | [Serializable] 14 | [DebuggerDisplay(EdgeExtensions.DebuggerDisplayTaggedUndirectedEdgeFormatString)] 15 | public class TaggedUndirectedEdge 16 | : UndirectedEdge 17 | , ITagged 18 | { 19 | private TTag tag; 20 | 21 | /// 22 | /// Initializes a new instance of the class. 23 | /// 24 | /// The source. 25 | /// The target. 26 | /// the tag 27 | public TaggedUndirectedEdge(TVertex source, TVertex target, TTag tag) 28 | :base(source, target) 29 | { 30 | this.tag = tag; 31 | } 32 | 33 | /// 34 | /// Raised when the tag is changed 35 | /// 36 | public event EventHandler TagChanged; 37 | 38 | void OnTagChanged(EventArgs e) 39 | { 40 | var eh = this.TagChanged; 41 | if (eh != null) 42 | eh(this, e); 43 | } 44 | 45 | /// 46 | /// Gets or sets the tag 47 | /// 48 | public TTag Tag 49 | { 50 | get { return this.tag; } 51 | set 52 | { 53 | if (!object.Equals(this.tag, value)) 54 | { 55 | this.tag = value; 56 | this.OnTagChanged(EventArgs.Empty); 57 | } 58 | } 59 | } 60 | 61 | /// 62 | /// Returns a that represents the current . 63 | /// 64 | /// 65 | /// A that represents the current . 66 | /// 67 | public override string ToString() 68 | { 69 | return String.Format( 70 | EdgeExtensions.TaggedUndirectedEdgeFormatString, 71 | this.Source, 72 | this.Target, 73 | this.Tag); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/QuickGraph/TryFuncDelegates.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Diagnostics.Contracts; 5 | 6 | namespace QuickGraph 7 | { 8 | [Pure] 9 | public delegate bool TryFunc(T arg, out TResult result); 10 | [Pure] 11 | public delegate bool TryFunc(T1 arg, T2 arg2, out TResult result); 12 | [Pure] 13 | public delegate bool TryFunc(T1 arg, T2 arg2, T3 arg3, out TResult result); 14 | [Pure] 15 | public delegate bool TryFunc(T1 arg, T2 arg2, T3 arg3, T4 arg4, out TResult result); 16 | } 17 | -------------------------------------------------------------------------------- /src/QuickGraph/UndirectedEdge.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Diagnostics.Contracts; 4 | using System.Collections.Generic; 5 | 6 | namespace QuickGraph 7 | { 8 | /// 9 | /// The default implementation. 10 | /// 11 | /// The type of the vertex. 12 | [Serializable] 13 | [DebuggerDisplay(EdgeExtensions.DebuggerDisplayUndirectedEdgeFormatString)] 14 | public class UndirectedEdge 15 | : IUndirectedEdge 16 | { 17 | private readonly TVertex source; 18 | private readonly TVertex target; 19 | 20 | /// 21 | /// Initializes a new instance of the class. 22 | /// 23 | /// The source. 24 | /// The target. 25 | public UndirectedEdge(TVertex source, TVertex target) 26 | { 27 | Contract.Requires(source != null); 28 | Contract.Requires(target != null); 29 | Contract.Requires(Comparer.Default.Compare(source, target) <= 0); 30 | Contract.Ensures(this.source.Equals(source)); 31 | Contract.Ensures(this.target.Equals(target)); 32 | 33 | this.source = source; 34 | this.target = target; 35 | } 36 | 37 | /// 38 | /// Gets the source vertex 39 | /// 40 | /// 41 | public TVertex Source 42 | { 43 | get { return this.source; } 44 | } 45 | 46 | /// 47 | /// Gets the target vertex 48 | /// 49 | /// 50 | public TVertex Target 51 | { 52 | get { return this.target; } 53 | } 54 | 55 | /// 56 | /// Returns a that represents the current . 57 | /// 58 | /// 59 | /// A that represents the current . 60 | /// 61 | public override string ToString() 62 | { 63 | return String.Format( 64 | EdgeExtensions.UndirectedEdgeFormatString, 65 | this.Source, 66 | this.Target); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/QuickGraph/UndirectedEdgeEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace QuickGraph 6 | { 7 | [Serializable] 8 | public class UndirectedEdgeEventArgs 9 | : EdgeEventArgs 10 | where TEdge : IEdge 11 | { 12 | private readonly bool reversed; 13 | 14 | public UndirectedEdgeEventArgs(TEdge edge, bool reversed) 15 | :base(edge) 16 | { 17 | this.reversed = reversed; 18 | } 19 | 20 | public bool Reversed 21 | { 22 | get { return this.reversed; } 23 | } 24 | 25 | public TVertex Source 26 | { 27 | get { return this.reversed ? this.Edge.Target : this.Edge.Source; } 28 | } 29 | 30 | public TVertex Target 31 | { 32 | get { return this.reversed ? this.Edge.Source : this.Edge.Target; } 33 | } 34 | } 35 | 36 | public delegate void UndirectedEdgeAction( 37 | Object sender, 38 | UndirectedEdgeEventArgs e) 39 | where TEdge : IEdge; 40 | } 41 | -------------------------------------------------------------------------------- /src/QuickGraph/VertexEventArgs.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | [Serializable] 7 | public abstract class VertexEventArgs : EventArgs 8 | { 9 | private readonly TVertex vertex; 10 | protected VertexEventArgs(TVertex vertex) 11 | { 12 | Contract.Requires(vertex != null); 13 | this.vertex = vertex; 14 | } 15 | 16 | public TVertex Vertex 17 | { 18 | get { return this.vertex; } 19 | } 20 | } 21 | 22 | public delegate void VertexAction(TVertex vertex); 23 | } 24 | -------------------------------------------------------------------------------- /src/QuickGraph/VertexFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace QuickGraph 4 | { 5 | public delegate TVertex VertexFactory(); 6 | } 7 | -------------------------------------------------------------------------------- /src/QuickGraph/VertexIdentity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph 8 | { 9 | [Pure] 10 | public delegate string VertexIdentity(TVertex v); 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/VertexIndexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Diagnostics.Contracts; 6 | 7 | namespace QuickGraph 8 | { 9 | [Pure] 10 | public delegate int VertexIndexer(TVertex v); 11 | } 12 | -------------------------------------------------------------------------------- /src/QuickGraph/VertexPredicate.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics.Contracts; 3 | 4 | namespace QuickGraph 5 | { 6 | [Pure] 7 | public delegate bool VertexPredicate(TVertex v); 8 | } 9 | -------------------------------------------------------------------------------- /src/QuickGraph/quickgraph.banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mokeyish/QuickGraph/3ef664d330c41e44d272b7664ffa4a3e44b05361/src/QuickGraph/quickgraph.banner.png -------------------------------------------------------------------------------- /src/QuickGraph/quickgraph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mokeyish/QuickGraph/3ef664d330c41e44d272b7664ffa4a3e44b05361/src/QuickGraph/quickgraph.png -------------------------------------------------------------------------------- /src/Tests/QuickGraph_Tests_01/QuickGraph_Tests_01.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp3.0 5 | 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | --------------------------------------------------------------------------------