Docsity

Sets and Partitions, Slides of Number Theory

Partial preview of the text

Download Sets and Partitions and more Slides Number Theory in PDF only on Docsity!

Sets and Partitions

CSE 373

Data Structures

Sets

Reading

  • Reading Chapter 11

› Section 11.

Sets

Set ADT

  • Make a set• Union of a set with another• Intersection of a set with another• Subtraction of a set from another

Sets

Set: simple implementation

  • Store elements in a list, i.e., an ordered

sequence› There must be a consistent total order

among elements of the various sets thatwill be dealt with

  • All methods defined previously can be

done in O(n)› Not very interesting!

Sets

Equivalence Relations

  • A relation R is defined on set S if for

every pair of elements a, b

S, a R b is

either true or false.

  • An equivalence relation is a relation R

that satisfies the 3 properties:› Reflexive: a R a for all a

S

› Symmetric: a R b iff b R a; a, b

S

› Transitive: a R b and b R c implies a R c

Sets

Equivalence Classes

  • Given an equivalence relation R, decide

whether a pair of elements a, b

S is

such that a R b.

  • The equivalence class of an element a

is the subset of S of all elementsrelated to a.

  • Equivalence classes are disjoint sets

Sets

Methods for Partitions

  • makeSet(x) : creates a single set

containing the element x and its “name”

  • Union(A,B): returns the new set AUB

and destructs the old A and the old B

  • Find(p): returns the “name” of the set

that contains p

Sets

Disjoint Union - Find

  • Maintain a set of pairwise disjoint sets.

› {3,5,7} , {4,2,8}, {9}, {1,6}

  • Each set has a unique name, one of its

members› {3,5,7} , {4,2,8}, {9}, {1,6}

Sets

Find

  • Find(x) – return the name of the set

containing x.› {3,5,7,1,6}, {4,2,8}, {9},› Find(1) = 5› Find(4) = 8

Sets

An Application

-^

Build a random maze by erasing edges.

Sets

An Application (ct’d)

-^

Repeatedly pick random edges to delete.

Start

End

Sets

Desired Properties

  • None of the boundary is deleted• Every cell is reachable from every other

cell.

  • There are no cycles – no cell can reach

itself by a path unless it retraces somepart of the path.

Sets

A Good Solution

Start

End

Sets

Good Solution : A Hidden

Tree

Start

End