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).

1  
sclass BreadthFirstPathFinder<A> implements Steppable {
2  
  // must set
3  
  IF1<A, Cl<A>> getChildren;
4  
  
5  
  // graph representation - filled during exploration
6  
  new MultiMap<A> links;
7  
  
8  
  // node queue (temporary)
9  
  new LinkedList<A> queue;
10  
  
11  
  *() {}
12  
  *(IF1<A, Cl<A>> *getChildren) {}
13  
  *(IF1<A, Cl<A>> *getChildren, A startNode) { add(startNode); }
14  
  
15  
  void add(A a, A prev default null) {
16  
    if (!links.containsKey(a)) {
17  
      links.put(a, prev);
18  
      queue.add(a);
19  
    }
20  
  }
21  
  
22  
  public bool step() {
23  
    ping();
24  
    if (empty(queue)) false;
25  
    A a = popFirst(queue);
26  
    fOr (A b : getChildren.get(a))
27  
      add(b, a);
28  
    true;
29  
  }
30  
  
31  
  bool nodeReached(A a) {
32  
    ret links.containsKey(a);
33  
  }
34  
  
35  
  Cl<A> getBackLinks(A a) {
36  
    ret links.get(a);
37  
  }
38  
  
39  
  L<A> examplePath(A start, A dest) {
40  
    new L<A> path;
41  
    A node = dest;
42  
    path.add(node);
43  
    do {
44  
      if (node == null) null; // no path found
45  
      node = first(getBackLinks(node));
46  
      path.add(node);
47  
    } while (node != start);
48  
    ret reversed(path);
49  
  }
50  
}

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: 146 / 383
Version history: 14 change(s)
Referenced in: [show references]