Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This needs an amount of memory that is proportional to what is required to store the graph. It will return every subgraph that is a tree of the desired size exactly once.</p> <p>Keep in mind that I just typed it into here. There could be bugs. But the idea is that you walk the nodes one at a time, for each node searching for all trees that include that node, but none of the nodes that were searched previously. (Because those have already been exhausted.) That inner search is done recursively by listing edges to nodes in the tree, and for each edge deciding whether or not to include it in your tree. (If it would make a cycle, or add an exhausted node, then you can't include that edge.) If you include it your tree then the used nodes grow, and you have new possible edges to add to your search.</p> <p>To reduce memory use, the edges that are left to look at is manipulated in place by all of the levels of the recursive call rather than the more obvious approach of duplicating that data at each level. If that list was copied, your total memory usage would get up to the size of the tree times the number of edges in the graph.</p> <pre><code>def find_all_trees(graph, tree_length): exhausted_node = set([]) used_node = set([]) used_edge = set([]) current_edge_groups = [] def finish_all_trees(remaining_length, edge_group, edge_position): while edge_group &lt; len(current_edge_groups): edges = current_edge_groups[edge_group] while edge_position &lt; len(edges): edge = edges[edge_position] edge_position += 1 (node1, node2) = nodes(edge) if node1 in exhausted_node or node2 in exhausted_node: continue node = node1 if node1 in used_node: if node2 in used_node: continue else: node = node2 used_node.add(node) used_edge.add(edge) edge_groups.append(neighbors(graph, node)) if 1 == remaining_length: yield build_tree(graph, used_node, used_edge) else: for tree in finish_all_trees(remaining_length -1 , edge_group, edge_position): yield tree edge_groups.pop() used_edge.delete(edge) used_node.delete(node) edge_position = 0 edge_group += 1 for node in all_nodes(graph): used_node.add(node) edge_groups.append(neighbors(graph, node)) for tree in finish_all_trees(tree_length, 0, 0): yield tree edge_groups.pop() used_node.delete(node) exhausted_node.add(node) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload