Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:52:46+00:00 2026-06-03T06:52:46+00:00

Suppose we have a DAG with edges labeled with numbers. Define the value of

  • 0

Suppose we have a DAG with edges labeled with numbers. Define the value of a path as the product of the labels. For each (source,sink)-pair I want to find the sum of the values of all the paths from source to sink. You can do this in polynomial time with dynamic programming, but there are still some choices that can be made in how you decompose the problem. In my case I have one DAG that has to be evaluated repeatedly with different labelings. My question is: for a given DAG, how can we pre-compute a good strategy for computing these values for different labelings repeatedly? It would be nice if there was an algorithm that finds an optimal way, for example a way that minimizes the number of multiplications. But perhaps this is too much to ask, I would be very happy with an algorithm that just gives a good decomposition.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:52:47+00:00Added an answer on June 3, 2026 at 6:52 am

    Let S be the set of sources, V be the set of vertices of DAG, E be the set of edges, n = |V|, m = |S|, W be an n x n matrix that stores the edge weights, and C be an m x n matrix such that C[i,j] holds, at the end of the algorithm, the sum of values of all paths from i to j.
    To simplify the explanation and the correctness proof of the algorithm, I assume that the vertices of the graph from 1 to n are topologically ordered in which nodes 1 to m are the sources. This adds O(|E|+|V|) to the running time of our algorithm:

    Here is the pseudocode of the algorithm:

    1: set C[i,j] to 1 if i=j and i is a source node, and to 0 otherwise.
    2: sort the DAG topologically
    3: for k=1 to n (vertex traversal in the topological order)
    4:   foreach predecessor k' of vertex k
    5:     foreach i in S
    6:       C[i,k] += C[i,k']*W[k',k]
    

    There are a total of O(|E|+|V|) iterations for the two outer loops. Therefore, the running time of the algorithm is O((|V|+|E|).m) assuming that addition and multiplication take constant time. That includes the time for topological sorting.

    Proof of correctness: we prove by induction that after the completion of the k-th iteration of the outermost loop, C[i,k] is the sum of values of all paths from i to k for each i in S.
    Base Case: obvious for k = 1 (because first element doesn’t have any predecessors)
    Induction: Assume C[i,j] is correctly computed for all j < k. All paths from any source i to k has to pass through a predecessor k’ of k. Since we are iterating in the topological order k’ must be smaller than k, and according to our induction hypothesis C[i,k’] is the sum of values of path from i to k’. Moreover, the sum of values of paths from i to k that passes through a specific predecessor k’ is equal to the sum of values of paths from i to k’, i.e., C[i,k’], multiplied by W[k’,k]. Therefore, the sum of values of all paths from i to k is sum of C[i,k’]*W[k’,k] over all predecessors k’ of k.

    Same graph structure, different W matrix: If we need to compute matrix C for different graphs that have the same structure but different W, we can do the following: Let C’ be a matrix whose elements are list of 3-tuples. Replace line 6 above with

    C'[i,k].append((i,k',k))
    

    And then by iterating through the vertices in the topological order and iterating through the tuples in C'[i,k], you can compute C[i,k] without looking at the graph structure. That’s because the tuples implicitly represent the graph structure. In terms of complexity, that’s not any better or worse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose we have a DAG with one source. I would like to find nodes
Suppose I have a source file open and I launch a shell. I can
Suppose I have 20 string of different length, each of them is supposed to
Suppose I have a vector of ints, std::vector<int> numbers; that is populated with a
Suppose I have a macro defined as this: #define FOO(x,y) \ do { int
Suppose we have buyers and sellers that are trying to find each other in
suppose I have the following source table (called S): name gender code Bob 0
Suppose I have a table like: id............value ``````````````````` A............1 A............2 A............3 B............1 and so
Suppose we have a NAME table like this: PERSON_ID PART_TYPE VALUE and PERSON_ID is
Suppose I have: public class foobar { public int lorem; public int ipsum; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.