Ejemplo n.º 1
0
        public static (string[] path, int distance) Dijkstra(ISearchableGraph graph, string from, string to)
        {
            var startNode = new DijkstraNode(graph, from, 0, to);
            var result    = Search <HeapPriorityQueue <SearchNode> > .Execute(startNode);

            var path = result.GetPath().Select(sn => {
                var gsn = sn as DijkstraNode;
                return(gsn.graphNode);
            }).ToArray();

            return(path, result.cost);
        }
Ejemplo n.º 2
0
 private static bool IsCyclic(ISearchableGraph g)
 {
     try
     {
         var dfe = new CyclicSearch(g);
         dfe.Search();
         return(false);
     }
     catch (GraphCycleException)
     {
         return(true);
     }
 }
Ejemplo n.º 3
0
        public static IEnumerable <int> StronglyConnectedComponents(ISearchableGraph graph, ISearchableGraph reverse)
        {
            //run dfs of reverse graph
            var srg = new TopologicalSort(reverse);

            srg.Search();

            //look for v in graph in reverse post order
            var sg    = new DepthFirstSearch(graph);
            var order = srg.Order;

            foreach (var v in order)
            {
                //if not visited => explore and mark visted vertices as new component
                sg.Explore(v);
            }
            return(sg.Components);
        }
Ejemplo n.º 4
0
 public TopologicalSort(ISearchableGraph g) : base(g)
 {
 }
Ejemplo n.º 5
0
 public DepthFirstSearch(ISearchableGraph g)
 {
     Graph = g;
     ConnectedComponent  = new int[g.Size()];
     ConnectedComponents = 0;
 }
Ejemplo n.º 6
0
 public BreadthFirstSearchWithBipartiteDetection(ISearchableGraph g)
 {
     _graph      = g;
     _searchData = new SearchData(g.Size());
 }
Ejemplo n.º 7
0
 public BreadthFirstSearch(ISearchableGraph g)
 {
     _visitedFrom = new SearchData(g.Size());
     _graph       = g;
     _searchData  = new SearchData(g.Size());
 }
Ejemplo n.º 8
0
 public DijkstraNode(ISearchableGraph graph, string graphNode, int cost, string targetNode, DijkstraNode parent = null) : base(cost, parent)
 {
     this.graph      = graph;
     this.graphNode  = graphNode;
     this.targetNode = targetNode;
 }
Ejemplo n.º 9
0
 public CyclicSearch(ISearchableGraph g)
 {
     _g = g;
 }