Graph: find subgraphs using a list of nodes, including wildcards

Does the following problem have a name and is there an algorithm for solving it ?: Given a graph directed or not, find all the paths that satisfy the specification given

  • list of exact nodes or
  • '*? which means only "any node or no node at all" or
  • '* {n}' which mean "any n sequentially connected nodes"

eg.

A -> B -> *? -> D which results in ABXD and ABYD and ABD etc.

or

A -> *{1} -> D -> *? -> E which results in ABXDZE and ABYDZE and ABDZE etc. etc.

thank

ps Does anyone know a graph library that does this in either R or perl or C?

+5
source share
2 answers

What I did at the end was:

  • The challenge is to find all paths of length N between two nodes. Cycles excluded.
  • - , . - > ( )
  • - ( unordered_map boost stl, ++) node - .
  • - , node .

A->B
A->C
B->D
C->E
E->D

, perl-, "edgelist":

my %hash = (
'A' => {'B' => 1, 'C' => 1},
'B' => {'D' => 1},
'C' => {'E' => 1},
'E' => {'D' => 1},
);

, DIRECTLY , (perl):

sub search {
    my ($from,$to) = @_;
    if( $to eq '*' ){ return defined($x=$hash{$from}) ? [keys $hash{$from}] : [] }
    return defined($x=$hash{$from}) && defined($x{$to}) ? [$to] : []
}

, a 'from' node, $to '*'. , $from.

.

.

sub path {
    my ($from,$to, $hops, $save_results) = @_;
    if( $hops < 0 ){ return 0 }
    $results = search($from, '*');
    if( "".@$results == 0 ){ return 0 }
    $found = 0;
    foreach $result (@$results){
        $a_node = new Tree::Nary($result);
        if( path($result, $to, $hops-1, $a_node) == 1 ){
            $save_results->insert($save_results, -1, $a_node);
            $found = 1;
        }
    }
    return $found;

}

, (.. $hops < 6?) - [sic].

- . Tree:: Nary (n-arry tree) . :

     |-> B -> D
A -> |-> C -> E -> D

, :

  • node, node node.

perl, ++, boost:: unordered_map hashtable. ++.

: 3281415 18601 perl 3 , A → '*' → '*' → B. , ++.

+1

, :

  • ,

, ( )

(, ) , node, x node, .

- node A node B, . , , DFS BFS.

+1

All Articles