Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #1030913 // BreadthFirstPathFinder - breadth-first search along virtual structure, collecting links [OK]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5329L/30K).

sclass BreadthFirstPathFinder<A> implements Steppable {
  // must set
  IF1<A, Cl<A>> getChildren;
  
  // graph representation - filled during exploration
  new MultiMap<A> links;
  
  // node queue (temporary)
  new LinkedList<A> queue;
  
  *() {}
  *(IF1<A, Cl<A>> *getChildren) {}
  *(IF1<A, Cl<A>> *getChildren, A startNode) { add(startNode); }
  
  void add(A a, A prev default null) {
    if (!links.containsKey(a)) {
      links.put(a, prev);
      queue.add(a);
    }
  }
  
  public bool step() {
    ping();
    if (empty(queue)) false;
    A a = popFirst(queue);
    fOr (A b : getChildren.get(a))
      add(b, a);
    true;
  }
  
  bool nodeReached(A a) {
    ret links.containsKey(a);
  }
  
  Cl<A> getBackLinks(A a) {
    ret links.get(a);
  }
  
  L<A> examplePath(A start, A dest) {
    new L<A> path;
    A node = dest;
    path.add(node);
    do {
      if (node == null) null; // no path found
      node = first(getBackLinks(node));
      path.add(node);
    } while (node != start);
    ret reversed(path);
  }
}

Author comment

Began life as a copy of #1021566

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030913
Snippet name: BreadthFirstPathFinder - breadth-first search along virtual structure, collecting links [OK]
Eternal ID of this version: #1030913/15
Text MD5: 31151c97a27f9ce254ca61184aac2a04
Transpilation MD5: 8eb4c2f55213f214e172820f0d3b7c71
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-21 23:29:32
Source code size: 1086 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 227 / 485
Version history: 14 change(s)
Referenced in: #1030919 - BreadthFirstPathFinder_withLinkType [OK]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)
#1034998 - G22SkeletonWalker [abandoned]