210 likes | 328 Views
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
E N D
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… A – B = B – A A B = A A S = A B = A (A B) = B
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 1 1 1 A B A B A B 3 3 2 2 6 6 C 4 D C D C 4 D 2 5 5 2 E E E Graph ST MST
Application: Minimal Spanning Trees • 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 1 1 1 A B A B A B 3 3 3 2 2 2 6 6 6 C 4 D C 4 D C 4 D 2 5 2 5 2 5 E E E 1 1 A B A B 3 3 2 2 6 6 C 4 D C 4 D 2 5 2 5 E E
Application: Minimal Spanning Trees • 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 1 A B 3 2 6 1 1 A B A B A B C 4 D 2 5 E C D C D C D 2 E E E 1 1 1 A B A B A B 3 2 2 2 C D C 4 D C D 2 2 2 E E E
Application: Minimal Spanning Trees A B 2 6 3 1 1 3 E F 1 2 5 3 C D 4
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, a1, a2, …, a3 • Represent A U as a bit string of length n where the ith bit in the string is 1 if ai A and 0 if ai A. • Intersection reduces to • Union reduces to • Compliment reduces to • Inefficient in terms of subset repetition