Question

Could the running time of algorithm max_ind_set_improved() be improved by using dynamic programming or memoization? And...

Could the running time of algorithm max_ind_set_improved() be improved by using dynamic programming or memoization? And if so, which of the two approaches would be preferable? Justify your answer.

def max_ind_set(nodes, edges):
    if len(nodes) <= 1: return len(nodes)
    node = nodes[0] # pick a node

    # case 1: node lies in independent set => remove neighbours as well
    nodes1 = [ n for n in nodes if n != node and (node, n) not in edges ]
    size1 = 1 + max_ind_set(nodes1, edges)

    # case 2: node does not lie in independent set
    nodes2 = [ n for n in nodes if n != node ]
    size2 = max_ind_set(nodes2, edges)

    return max(size1, size2)
def max_ind_set_improved(nodes, edges):
    if len(nodes) <= 1: return len(nodes)
    node = nodes[0] # pick a node

    # case 1: node lies in independent set => remove neighbours as well
    nodes1 = [ n for n in nodes if n != node and (node, n) not in edges ]
    size1 = 1 + max_ind_set_improved(nodes1, edges)

    if len([ n for n in nodes if (node, n) in edges ]) <= 1:
        return size1

    # case 2: node does not lie in independent set
    nodes2 = [ n for n in nodes if n != node ]
    size2 = max_ind_set_improved(nodes2, edges)

    return max(size1, size2)
if __name__ == '__main__':
    nodes = [1,2,3,4,5,6,7,8]
    edges = [(1,2),(1,3),(2,3),(2,4),(3,5),(4,5),(4,6),(5,7),(6,7),(6,8),(7,8)]
    # undirected graph => edges go in both directions (simplifies algorithms)
    edges += [ (b,a) for (a,b) in edges ]
    print("max_ind_set = {}".format(max_ind_set(nodes,edges)))
    print("max_ind_set_improved = {}".format(max_ind_set_improved(nodes,edges)))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please let me know if you require any further assistance.

Yes the running time of "max_ind_set_improved() " could be improved by using memoization. This is because if we use dynamic programming we will end up solving same sub-problems again and again. By using memoization we can effectively amortize that cost and produce an runtime complexity that is much more efficient in nature. for e.g. if we know node '3' lies in independent set, we can avoid computing the sub problem of finding if '3' lies in independent set again and again.

Add a comment
Know the answer?
Add Answer to:
Could the running time of algorithm max_ind_set_improved() be improved by using dynamic programming or memoization? And...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Could the running time of algorithm max_ind_set_improved() be improved by using dynamic programmi...

    Could the running time of algorithm max_ind_set_improved() be improved by using dynamic programming or memoization? And if so, which of the two approaches would be preferable? Justify your answer. def max_ind_set(nodes, edges): if len(nodes) <= 1: return len(nodes) node = nodes[0] # pick a node # case 1: node lies in independent set => remove neighbours as well nodes1 = [ n for n in nodes if n != node and (node, n) not in edges ] size1 = 1...

  • 1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing...

    1. Code a breadth First traversal. 2. Code a depth First traversal. Note, your traversals should return a string listing the nodes added to the min spanning tree and the order they are added. Using Python. Starter code: from collections import deque class Edge: def __init__(self, endIndex, next = None): self.endIndex = endIndex self.next = next class Node: def __init__(self, name): self.name = name self.visited = False self.connects = None class Graph: def __init__(self): self.nodeList = [] self.size = 20...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • Give pseudocode that performs the traceback to construct an LCS from a filled dynamic programming table...

    Give pseudocode that performs the traceback to construct an LCS from a filled dynamic programming table without using the “arrows”, in O(n + m) time. 2. Suppose we are given a “chain” of n nodes as shown below. Each node i is “neighbors” with the node to its left and the node to its right (if they exist). An independent set of these nodes is a subset of the nodes such that no two of the chosen nodes are neighbors....

  • (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL)...

    (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

  • PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a...

    PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...

  • I need to complete the code by implementing the min function and the alpha betta pruning...

    I need to complete the code by implementing the min function and the alpha betta pruning in order to complete the tic tac toe game using pything. code: # -*- coding: utf-8 -*- """ Created on: @author: """ import random from collections import namedtuple GameState = namedtuple('GameState', 'to_move, utility, board, moves') infinity = float('inf') game_result = { 1:"Player 1 Wins", -1:"Player 2 Wins", 0:"It is a Tie" } class Game: """To create a game, subclass this class and implement actions,...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_...

    Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT