Question: I have a `DiGrraph` and I want to prune any node that's not contained in one of the [simple paths](https://en.wikipedia.org/wiki/Simple_path) between two of the nodes that I specify. (Another way to think of it is any node that can't reach **both** the `start` and `end` points should be trimmed).

The best way I've found to do this is to get `all_simple_paths`, then to rebuild a new graph using those, but I'm hoping for a more elegant and less error prone solution. For example, is there a way to determine what's NOT on a simple path, and to then delete those nodes?

Answer: 
You can use the method `all_simple_paths` which returns a generator but you only need the first path. Then you can use the `G.subgraph(nbunch)` to return the induced graph from your path.

EDIT: to return the subgraphs induced by all simple paths just concatenate the uniques nodes returned by `all_simple_paths`.

```
import networkx as nx
import itertools

G = nx.complete_graph(10) # or DiGraph, MultiGraph, MultiDiGraph, etc
# Concatenate all the paths and keep unique nodes (in one line)
all_path_nodes = set(itertools.chain(*list(nx.all_simple_paths(G, source=0, target=3))))
# Extract the induced subgraph from a given list of nodes
H = G.subgraph(all_path_nodes)
print(nx.info(H))

```

Output:

```
Name: complete_graph(10)
Type: Graph
Number of nodes: 10
Number of edges: 45
Average degree:   9.0000

```
I did make some progress on this while @kikohs was working to understand my question and provide his answer, so I'm posting this as an alternative solution to the problem. I do think his answer is superior though! 

```
def _trim_branches(self, g, start, end):
    """Find all the paths from start to finish, and nuke any nodes that
    aren't in those paths.
    """
    good_nodes = set()
    for path in networkx.all_simple_paths(
            g,
            source=start,
            target=end):
        [good_nodes.add(n) for n in path]

    for node in g.nodes:
        if node not in good_nodes:
            g.remove_node(node)

    return g

```

Using `subgraph` to do the second loop is clearly better, as is his one-liner using `itertools.chain`. Great stuff around these parts today!