given a DAG, I want to look for subgraph or pattern that occur several times. I don't need this algorithm to give me all such patterns, just do as best as it can and need to have linear complexity O(V+E) with V be number of vertices and E be number of edges or little worse
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here’s an outline of a simple, linear-time (in practice O(V+E)) approach that will spot “often-occurring” sub-DAGs without guaranteeing completeness, but which in most real-world DAGs will find the common patterns quickly.
Idea in a nutshell
Topo-sort the DAG.
Do a single bottom-up pass. At each node you compute a small “signature” (a hash or fingerprint) of its entire downstream sub-DAG up to a fixed depth (or, if you like, of the whole downstream DAG).
Store these signatures in a hash table (map from signature → count). Whenever two nodes receive the same signature, they are “likely” roots of isomorphic sub-DAGs. Common signatures (count ≥ 2) indicate repeated patterns.
Because each edge and vertex is visited just once (or a small constant number of times), this is essentially O(V+E).
1. Topo-sort
Compute any topological ordering of your DAG in O(V+E) time. Let that order be v₁, v₂, …, vₙ so that all edges go from higher indices to lower indices.
2. Bottom-up signature (hash) computation
We maintain:
sig[v]: a 64-bit (or larger) integer signature for the sub-DAG rooted at v.
A global hash-map CountMap: Map<signature, int>.
Algorithm sketch:
pseudo
// Assume each node v has a (small) label L[v] (optional; set it to 1 if unlabeled)
// choose any good 64-bit hash f(a,b,c,…) that mixes its arguments
function combine_hash(label, multiset_of_child_hashes):
// sort child hashes to get a canonical multiset order
H = SORTED_LIST(child_hashes)
return HASH( label, H[0], H[1], … )
// main
TopoSort DAG into order v1…vN so children appear before parents
for i in 1…N:
v = v_i
// collect child signatures
C = []
for each edge v -> w:
C.append( sig[w] )
// compute v’s signature
sig[v] = combine_hash( L[v], C )
// tally it
CountMap[ sig[v] ] += 1
// now CountMap[h] tells you how many roots share signature h
// any h with CountMap[h] > 1 corresponds to a repeated sub-DAG pattern
Complexity
Topo-sort: O(V+E)
One pass over vertices, for each v scanning its outgoing edges once → ∑out-deg(v)=E.
Sorting the small list of child hashes at each v: if you cap the max out-degree or child list size by a small constant D (or use an O(d·log d) routine), the total cost is ∑ O(out-deg(v)·log out-deg(v)) which in typical sparse graphs is ≲O(E).
Hash-map updates are amortized O(1).
→ Overall ≃ O(V + E·log d) which in practice is linear-time.
3. Retrieving the actual sub-DAGs (optional)
If you want to extract one representative subgraph for each “frequent” signature h:
As you compute sig[v], also record one pointer Rep[h] = v when you first see h.
Later sweep through CountMap, pick every h with CountMap[h] ≥ threshold (say ≥2), and follow the sub-DAG rooted at Rep[h] to inspect it.
4. Tuning & Extensions
• Bounded-depth: if your full downstream sub-DAGs are huge (and you only care about patterns up to depth D), stop the recursion after D levels and sprinkle in D as part of the hash.
• Rolling / streaming: you can process batches of nodes if your DAG is so huge it doesn’t fit memory; you only need to remember one level of signatures at a time.
• Better collision-resistance: use 128-bit or pair of hashes to reduce accidental collisions.
• Profile-driven: if most nodes have small fan-out but a few have huge fan-out, you can pre-bucket large vertices separately.
Why this “just works” in practice
You never enumerate all subgraphs; you only ever compute one signature per vertex.
Isomorphic sub-DAGs collapse to the same fingerprint in one pass.
You pay only O(1) work per edge (plus a small sort on each node’s children).
If you need more rigorous guarantees (or to catch every pattern), you’d have to pay a higher complexity price (often NP-hard). But for most “I just want to see what repeats” use cases, this linear-time bottom-up hashing is an excellent, easily implemented heuristic.