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 4047162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:39:22+00:00 2026-05-20T13:39:22+00:00

i have this DAG (it is similar to a binary tree, but it is

  • 0

i have this DAG (it is similar to a binary tree, but it is a graph.. has this a specified name?):
connected binary tree

(each number is a node and numbers in the node are for example, the program should run with random numbers)

it is represented as a list of list:

[[1],[2,3],[4,5,6]]

i have to find in the more functional way as possible the path that maximize the sum of nodes:

[1,3,6]

i’ve searched and this is really similar to projecteuler #18, but project euler asks the hole sum of the path, and in my homework i have to find not only the sum but all nodes.
i’ve tried to adapt some really good solutions to my problem but i didn’t managed to.
some advice?

  • 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-05-20T13:39:22+00:00Added an answer on May 20, 2026 at 1:39 pm

    As I understand your problem, node of depth n and rank r at that level will be connected to nodes of level n+1 with rank r and r+1.

    The direct path is certainly to look for some recurrence relation using some search function that will take one of your dags as input. You can certainly start by just looking for the max weight, when this works also building the list of nodes should not be a big problem.

    I had it working following this path, the code and the test sets I used are below… but I removed the most interesting part to avoid spoiling the subject. I can give you some more hint if necessary. That’s only to get you started.

    import unittest
    
    def weight(tdag, path):
        return sum([level[p] for p, level in zip(path,tdag)])
    
    def search_max(tdag):
        if len(tdag) == 1:
            return (0,)
        if len(tdag) > 1:
            # recursive call to search_max with some new tdag
            # when choosing first node at depth 2
            path1 = (0,) + search_max(...)
            # recursive call to search_max with some new tdag 
            # when choosing second node at depth 2
            # the result path should also be slightly changed
            # to get the expected result in path2
            path2 = (0,) + ...
            if weigth(tdag, path1) > weigth(tdag, path2):
                return path1
            else:
                return path2
    
    class Testweight(unittest.TestCase):
        def test1(self):
            self.assertEquals(1, weight([[1]],(0,)))
    
        def test2(self):
            self.assertEquals(3, weight([[1], [2, 3]],(0, 0)))
    
        def test3(self):
            self.assertEquals(4, weight([[1], [2, 3]],(0, 1)))
    
    class TestSearchMax(unittest.TestCase):
    
        def test_max_one_node(self):
            self.assertEquals((0,), search_max([[1]]))
    
        def test_max_two_nodes(self):
            self.assertEquals((0, 1), search_max([[1], [2, 3]]))
    
        def test_max_two_nodes_alternative(self):
            self.assertEquals((0, 0), search_max([[1], [3, 2]]))
    
        def test_max_3_nodes_1(self):
            self.assertEquals((0, 0, 0), search_max([[1], [3, 2], [6, 4, 5]]))
    
        def test_max_3_nodes_2(self):
            self.assertEquals((0, 0, 1), search_max([[1], [3, 2], [4, 6, 5]]))
    
        def test_max_3_nodes_3(self):
            self.assertEquals((0, 1, 1), search_max([[1], [2, 3], [4, 6, 5]]))
    
        def test_max_3_nodes_4(self):
            self.assertEquals((0, 1, 2), search_max([[1], [2, 3], [4, 5, 6]]))
    
    if __name__ == '__main__':
        unittest.main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a DAG, in which each node belongs to a category, how can this
I have a DAG. I have this operation to add a edge between two
I have an XML file that encodes a directed acyclic graph (DAG) that represents
I have been looking for C# examples to transform a DAG into a Tree.
I have a large (100,000+ nodes) Directed Acyclic Graph (DAG) and would like to
I have a large directed, acylic graph (DAG) from which I would like to
I have this $number = 0.5 if (is_float($number)) { echo 'float'; } else {
Have this scenario: public class Base { public string Name; } public Class ClassA
I have this batch file to export two files from svn. But it never
Have this result set below; am trying to insert the red numbers into post_position

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.