Docsity

Union-Find Algorithm and Graph Theory, Lecture notes of Computer Science

Partial preview of the text

Download Union-Find Algorithm and Graph Theory and more Lecture notes Computer Science in PDF only on Docsity!

Union-Find, Graphs, etc

What next?

A. Union-Find

B. MST algorithm animated demos

C. Graph algorithm data structures

D. Different Kruskal/Prim MST example

E. Proofs of Properties of Trees

F. Something else (and I’ll raise my hand and

say what, especially if you give me candy

afterwards)

Union-find, aka Disjoint sets

 function MakeSet(x) # create 1-element subset
 x.parent := x
 function Find(x) # returns representative element
# of subset containing x
 if x.parent == x
 return x
 else
 x.parent := Find(x.parent)
 return x.parent
 function Union(x, y) # merge two subsets into 1
 xRoot := Find(x)
 yRoot := Find(y)
 xRoot.parent := yRoot

Some tree properties

1. Removing a cycle edge cannot disconnect a

graph

2. A tree with n nodes has n-1 edges

3. Any connected undirected graph with n

nodes and n-1 edges is a tree

4. An undirected graph is a tree if and only if

there is a unique path between any pair of

nodes

5. The lightest edge across a graph cut is part

of some MST

Some tree properties

1. Removing a cycle edge cannot disconnect a

graph

2. A tree with n nodes has n-1 edges

3. Any connected undirected graph with n

nodes and n-1 edges is a tree

4. An undirected graph is a tree if and only if

there is a unique path between any pair of

nodes

5. The lightest edge across a graph cut is part

of some MST

3/20/2006 Graphs 8

Main Methods of the Graph ADT

Vertices and edges

 are positions
 store elements

Accessor methods

 aVertex()
 incidentEdges(v)
 endVertices(e)
 opposite(v, e)
 areAdjacent(v, w)

Update methods

 insertVertex(o)
 insertEdge(v, w, o)
 removeVertex(v)
 removeEdge(e)
 replace(v,x)
 replace(e,x)

Generic methods

 numVertices()
 numEdges()
 vertices()
 edges()

3/20/2006 Graphs 10

Adjacency List Structure

Edge list structure
Incidence sequence
for each vertex

 sequence of references to edge objects of incident edges

Augmented edge
objects

 references to associated positions in incidence sequences of end vertices

u

v

w

a b

a

u v w

b

3/20/2006 Graphs 11

Adjacency Matrix Structure

Edge list structure
Augmented vertex
objects

 Integer key (index) associated with vertex

2D-array adjacency
array

 Reference to edge object for adjacent vertices  Null for non nonadjacent vertices

u

v

w

a b

0 ∅^ ∅

a 2 ∅ ∅

0 u 1 v 2 w

b