Do a topological sort of the DAG, then scan the vertices from the target backwards to the source. For each vertex v
, keep a count of the number of paths from v
to the target. When you get to the source, the value of that count is the answer. That is O(V+E)
.
More Related Contents:
- Best algorithm for detecting cycles in a directed graph [closed]
- Finding all cycles in a directed graph
- Graph Algorithm To Find All Connections Between Two Arbitrary Vertices
- Find all paths between two graph nodes
- Cycles in an Undirected Graph
- Why doesn’t Dijkstra’s algorithm work for negative weight edges?
- Find the shortest path in a graph which visits certain nodes
- Compute the minimal number of swaps to order a sequence
- Find the paths between two given nodes?
- What are the practical factors to consider when choosing between Depth-First Search (DFS) and Breadth-First Search (BFS)? [closed]
- Why is the time complexity of both DFS and BFS O( V + E )
- Why DFS and not BFS for finding cycle in graphs
- Algorithm to simplify a weighted directed graph of debts
- Finding connected components of adjacency matrix graph
- O(n) or O(n)^2 time complexity of the algorithm? [closed]
- How can I find the time complexity of an algorithm?
- Peak signal detection in realtime timeseries data
- How to compare two colors for similarity/difference
- Least common multiple for 3 or more numbers
- Negative weights using Dijkstra’s Algorithm
- What’s the fastest algorithm for sorting a linked list?
- Need help in mod 1000000007 questions
- SICP example: Counting change, cannot understand
- Algorithm to tell if two arrays have identical members
- Quicksort vs heapsort
- Algorithm to find the most common substrings in a string
- Simplest way to get the top n elements of a Scala Iterable
- Find cycle of shortest length in a directed graph with positive weights
- How is 2D bin packing achieved programmatically?
- Optimizing Conway’s ‘Game of Life’