 # Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets.

## Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets."— Presentation transcript:

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sets

Introduction –Sets are an important mathematical data structure Naturally occurring Theoretically described Powerful notation –Definition An unordered collection of distinct objects that share one or more common property –Unordered means no inherent order –Distinct means there are no duplicate member  x[(x  S  P(x))  (P(x)  x  S)] Element of

Notation and Terms –Sets are denoted with capital letters –Curley brackets are also used {a,b,c} –Empty Set denoted by  or {} not the same as {  } –Finite sets can be defined by enumerating the elements of the set –Infinite sets require definition of defining property { x | P(x) } Example: { x | x  N  x  10 } Such that set of all nonnegative integers

Notation and Terms –Other Commonly Used Sets Z : set of all integers Q : set of all rational numbers R : set of all real numbers C : set of all complex numbers Z + : set of all positive integers –Subset (A  B)  x (x  A  x  B) A  B proper subset –  x (x  A  x  B)   x(x  B  x  A) –Equal Sets (A = B)  x [(x  A  x  B)  (x  B  x  A)] (A  B)  (B  A)

Notation and Terms –Power Set (  (S) ) |  (S)| = _________ where n = |S| Note: the empty set is a subset of every set –Venn Diagrams

Set Operations –Union (A  B) {x | x  A  x  B} –Intersection (A  B) {x | x  A  x  B} –Complement (A) For a set A   (S), A is {x | x  S  x  A} –Set Difference (A – B) { x | x  A  x  B } –Cartesian Product (A  B) {(x,y) | x  A  y  B} Ordered pair

Set Operations What can you conclude about the sets A & B if… 1.A – B = B – A 2.A  B = A 3.A  S =  4.A  B = A 5.(A  B) = B

Set Identities IdentityName A   = A A  U = A Identity Laws A  U = U A   =  Domination Laws A  A = A A  A = A Idempotent Laws (A) = A Complementation Laws A  B = B  A A  B = B  A Commutative Laws A  (B  C) = (A  B)  C A  (B  C) = (A  B)  C Associative Laws A  (B  C) = (A  B)  (A  C) A  (B  C) = (A  B)  (A  C) Distributive Laws (A  B) = A  B (A  B) = A  B De Morgan’s Laws A  (A  B) = A A  (A  B) = A Absorption Laws A  A = U A  A =  Complement Laws

Practice Problems Mathematical Structures for Computer Science Pg. 202 # 10, 12, 18, 19, 20, 23, 30, 38, 39, 40, 50, 51, 55, 58, 62, 66, 69

Application: Minimal Spanning Trees A tree is nothing more than an undirected graph without cycles A spanning tree is a tree that contains all the vertices in the graph There can be many such spanning trees and we usually want to find the optimal (minimal) weighted one –Thus, this is an optimization problem

Application: Minimal Spanning Trees AB CD E AB CD E AB CD E

PRIM’s Algorithm Given a set of vertices, V, and a set of edges, E Start with an empty set of edges, F, and a set of vertices, Y, initialized to contain an arbitrary vertex, say ‘A’ At each step find the minimum weight edge, where one end is from (V-Y) and the other from Y –Add this edge to F –Add the vertex from the edge not in Y to the set Y The problem is solved when Y = V

Application: Minimal Spanning Trees AB CD E AB CD E AB CD E AB CD E AB CD E

KRUSKAL’s Algorithm Given a set of vertices, V, and a set of edges, E Start by creating disjoint subsets of V, one for each vertex, containing only that vertex Look at each edge in turn, from minimal weight to maximum weight If the edge connects two vertices in disjoint subsets then the edge is added to the MST and the two disjoint subsets are merged Otherwise we throw away the edge and leave the subsets alone Stop when we only have 1 disjoint subset or we run out of edges to consider

Application: Minimal Spanning Trees AB CD E AB CD E AB CD E AB CD E AB CD E AB CD E AB CD E

AB CD EF

Greedy Algorithms Arrives at a solution by making a sequence of choices, each of which looks the best at the moment Hope that globally optimal solution will be obtained from locally optimal choices Prim’s & Kruskal’s Algorithms –Always pick the minimal weight edge to add to the set of edges Ncoins problem –Always pick the largest coin available –Does this always work?

Greedy Algorithms Not every greedy algorithm produces optimal solution Must prove that a greedy algorithm produces optimal solution to a problem in order to use it Proof for Prim’s algorithm see pg 149.

Greedy Algorithms Basic approach to greedy algorithms: –Start with { } –Add items to the set in sequence as follows: Selection: choose the next item according to a greedy criterion Feasibility: determine if the new set is feasible – can it lead to a solution? Solution: is the new set a solution? –Stop when the set represents a solution

Greedy Algorithms – more practice Foundations of Algorithms Chapter 4 Practice Problems: Pg. 181 # 1, 2, 5, 6, 9, 43, 44

Application: Set Representation –How can we effectively represent sets in a computer? –Solution I : Store as a collection of distinct items Tends to be inefficient since set operations will then require massive amounts of searching –Solution II: Store in some arbitrary but well defined order Let U be the universal set (assume finite & reasonable size) Order U, for instance, a 1, a 2, …, a 3 Represent A  U as a bit string of length n where the ith bit in the string is 1 if a i  A and 0 if a i  A. Intersection reduces to  Union reduces to  Compliment reduces to  Inefficient in terms of subset repetition