Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON CODING (DISCRETE STRUCTURES): (15 pts) Consider a set S ofn elements, ai, a, ..., an). As we discussed in class, the power set of

PYTHON CODING (DISCRETE STRUCTURES):

image text in transcribed

(15 pts) Consider a set S ofn elements, ai, a, ..., an). As we discussed in class, the power set of S, denoted P(S), is the set of all subsets of S. We know that there are 2" subsets of S, counting the empty set . Wrnte a function poer set(s) that returns the power set of s. You can represent the sets using Python lists. The empty set can be represented with the empty list, . The power set function should retun a list oflists. For example, calling power_set(l, 2]) should retum something like O.], 21. 1, 211. This problem will likely take a little thought, but it's doable! Note that you can find all the subsets of A recursively. To understand the algorithm, first observe that given a set A and any element x EA, the subsets of A can be divided evenly into those that contain x and those that do not. For example, let's consider the element 2 from the set , 2, 3) Subsets of (1, 2,3) that contain 2: Subsets of f1, 2, 33 that do not contain 2: (total: 4 subsets) (total: 4 subsets) We can approach the problem of finding ll subsets of l, 2, 3 like this: First, remove 2 and find all subsets of (1. 3) Now, make a copy of those subsets, and insert 2 into each of them Combine the two sets ofsubsets from above to get the answer. . Now we can generalize! To find all subsets of set A: Pick an arbitrary element x EA, and remove x firom. Find all subsets ofA (which is now smaller by one element). - .Now, make a copy of those subsets, and addx to each of them. Combine the two sets of subsets from above to get the answer. (this looks awfully recursive...) Recursion hint:What should your ba Python hint Suppose you have a list named listl and you want to make a copy of it Writing list2 = list! is legal, but it only creates a shallow copy. Any changes to listl will also be reflected in list2. (This is the same idea as having two references to the same aay in Java.) To make list2 independent firom listl, you can ask Python to make a deep copy. An easy way of doing this is to use the built-in -in deepcopy function, located in the import copy list2-copy.deepcopy (listi)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Databases Object Oriented Deductive Hypermedia Technologies

Authors: Kamran Parsaye, Mark Chignell, Setrag Khoshafian, Harry Wong

1st Edition

0471503452, 978-0471503453

More Books

Students also viewed these Databases questions