I am searching an algorithm or an idea for an algorithm for the following use case:
A directed graph consists of multiple vertices. Some of these vertices are annotated with a value (like a number) and have no parent vertices (predecessors). All other vertices are representing operations. An operation can be only performed if all annotated values of the parent vertices (predecessors) are known. The result or value of the operation is then stored in the vertex.
My solution idea:
- Store all vertices that have at least one parent vertex (predecessor) in a set
- While the set is not empty:
- Get the next vertex from the set
- Check if the value for all parent vertices (predecessors) are known
- If all values are known:
- Remove the vertex from the set
- Perform the operation
- Store the result of the operation in the vertex
- Loop (go to 2.)
- Else: Loop (go to 2.)
My problem: I think this solution idea could work but it is not efficient (especially for large graph structures).
Are there more efficient approaches to solve this problem?