Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4546928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:01:27+00:00 2026-05-21T16:01:27+00:00

Given an undirected graph, I want to generate all subgraphs which are trees of

  • 0

Given an undirected graph, I want to generate all subgraphs which are trees of size N, where size refers to the number of edges in the tree.

I am aware that there are a lot of them (exponentially many at least for graphs with constant connectivity) – but that’s fine, as I believe the number of nodes and edges makes this tractable for at least smallish values of N (say 10 or less).

The algorithm should be memory-efficient – that is, it shouldn’t need to have all graphs or some large subset of them in memory at once, since this is likely to exceed available memory even for relatively small graphs. So something like DFS is desirable.

Here’s what I’m thinking, in pseudo-code, given the starting graph graph and desired length N:

Pick any arbitrary node, root as a starting point and call alltrees(graph, N, root)

alltrees(graph, N, root)
 given that node root has degree M, find all M-tuples with integer, non-negative values whose values sum to N (for example, for 3 children and N=2, you have (0,0,2), (0,2,0), (2,0,0), (0,1,1), (1,0,1), (1,1,0), I think)
 for each tuple (X1, X2, ... XM) above
   create a subgraph "current" initially empty
   for each integer Xi in X1...XM (the current tuple)
    if Xi is nonzero
     add edge i incident on root to the current tree
     add alltrees(graph with root removed, N-1, node adjacent to root along edge i)
   add the current tree to the set of all trees
 return the set of all trees

This finds only trees containing the chosen initial root, so now remove this node and call alltrees(graph with root removed, N, new arbitrarily chosen root), and repeat until the size of the remaining graph < N (since no trees of the required size will exist).

I forgot also that each visited node (each root for some call of alltrees) needs to be marked, and the set of children considered above should only be the adjacent unmarked children. I guess we need to account for the case where no unmarked children exist, yet depth > 0, this means that this “branch” failed to reach the required depth, and cannot form part of the solution set (so the whole inner loop associated with that tuple can be aborted).

So will this work? Any major flaws? Any simpler/known/canonical way to do this?

One issue with the algorithm outlined above is that it doesn’t satisfy the memory-efficient requirement, as the recursion will hold large sets of trees in memory.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T16:01:28+00:00Added an answer on May 21, 2026 at 4:01 pm

    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.

    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.

    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.

    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 < len(current_edge_groups):
                edges = current_edge_groups[edge_group]
                while edge_position < 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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an undirected graph in which each node has a Cartesian coordinate in space
Given an undirected graph what is the best algorithm to detect if it contains
Given a node x in an undirected graph that is known to be part
In the Lemon C++ Graph Library, given a node in an undirected graph say,
Does anyone know an algorithm for the following problem: Given a undirected connected graph
I saw this question on a forum: http://www.geeksforgeeks.org/archives/19042 Given an undirected graph and a
Given an undirected graph with n nodes, where each of the nodes has a
We are given an undirected graph G = (V, E) and two vertices s,
Given a table holding edges in a directed graph like this: CREATE TABLE edges
Given an undirected cyclic planar graph G(V,E) with vertex weights W(V), a fixed plane

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.