Question

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 + 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 programmi...
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 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...

  • 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...

  • (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...

  • Throughout this script, can you provide helpful comments about how each function works in the script...

    Throughout this script, can you provide helpful comments about how each function works in the script plus it's significance to the alignment process. Also are there any errors in this script or a way to improve this script? Any help would be appreciated. Thank you. THIS IS A PYTHON CODE AND IS IN IT'S FORMAT _ ITS CLEAR! #!/usr/bin/env python # file=input("Please provide fasta file with 2 sequences:") match=float(input('What is the match score?:')) missmatch=float(input('What is the missmatch score?:')) gap=float(input('What is...

  • PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please...

    PYTHON. Continues off another code. I don't understand this. Someone please help! Comment the lines please so I can understand LinkedList ADT: class myLinkedList:     def __init__(self):         self.__head = None         self.__tail = None         self.__size = 0     def insert(self, i, data):         if self.isEmpty():             self.__head = listNode(data)             self.__tail = self.__head         elif i <= 0:             self.__head = listNode(data, self.__head)         elif i >= self.__size:             self.__tail.setNext(listNode(data))             self.__tail = self.__tail.getNext()         else:             current = self.__getIthNode(i - 1)             current.setNext(listNode(data,...

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