5.3: Disjoint Sets

Behaviour: merge two disjoint sets and return handle of new set
h2
h4 =Union(h0 ,h3 )
5.3: Disjoint Sets
h1
y
T.S.
h0
h3
x
3
5.3: Disjoint Sets
Frank Stajano
Lent 2015
Thomas Sauerwald
Outline
Disjoint Sets
Introduction to Graphs and Graph Searching
5.3: Disjoint Sets
T.S.
2
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
5.3: Disjoint Sets
T.S.
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
x
h0 =makeSet(x)
5.3: Disjoint Sets
T.S.
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
x
h0 =makeSet(x)
5.3: Disjoint Sets
T.S.
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
h0
x
h0 =makeSet(x)
5.3: Disjoint Sets
T.S.
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
h0
y
5.3: Disjoint Sets
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
h0
h1 =findSet(y)
5.3: Disjoint Sets
y
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
h1
h1 =findSet(y)
5.3: Disjoint Sets
y
T.S.
h0
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h1
y
5.3: Disjoint Sets
T.S.
h0
h3
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h4 =Union(h0 ,h3 )
5.3: Disjoint Sets
h1
y
T.S.
h0
h3
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h4 =Union(h0 ,h3 )
5.3: Disjoint Sets
h1
y
T.S.
h0
h3
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h4 =Union(h0 ,h3 )
5.3: Disjoint Sets
h1
y
T.S.
h0
h3
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h4 =Union(h0 ,h3 )
5.3: Disjoint Sets
h1
y
T.S.
h0
h3 = h4
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
y
5.3: Disjoint Sets
h3 = h4
h1
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h5 =Union(h1 ,h2 )
5.3: Disjoint Sets
h3 = h4
h1
y
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h5 =Union(h1 ,h2 )
5.3: Disjoint Sets
h3 = h4
h1
y
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h5 =Union(h1 ,h2 )
5.3: Disjoint Sets
h3 = h4
h1
y
T.S.
x
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h2
h5 =Union(h1 ,h2 )
h3 = h4
h1
y
x
h5
5.3: Disjoint Sets
T.S.
3
Disjoint Sets (aka Union Find)
Disjoint Sets Data Structure
Handle MakeSet(Item x)
Precondition: none of the existing sets contains x
Behaviour: create a new set {x} and return its handle
Handle FindSet(Item x)
Precondition: there exists a set that contains x (given pointer to x)
Behaviour: return the handle of the set that contains x
Handle Union(Handle h, Handle g)
Precondition: h 6= g
Behaviour: merge two disjoint sets and return handle of new set
h3 = h4
h5 =Union(h1 ,h2 )
y
x
h5
5.3: Disjoint Sets
T.S.
3
First Attempt: List Implementation
h1
x1
x2
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
h1
x1
x2
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
h1
x1
x2
Need to find
last element!
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
x2
Need to find
last element!
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
x2
Need to find
last element!
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
5.3: Disjoint Sets
T.S.
y2
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
5.3: Disjoint Sets
T.S.
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
FindSet(z3 )
h4
z1
5.3: Disjoint Sets
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
h4
z1
5.3: Disjoint Sets
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
h4
z1
5.3: Disjoint Sets
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
⇒ F IND takes constant time
h4
z1
5.3: Disjoint Sets
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
y2
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
⇒ F IND takes constant time
h4
z1
5.3: Disjoint Sets
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
Need to update all
backward pointers!
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
⇒ F IND takes constant time
h4
z1
5.3: Disjoint Sets
y2
T.S.
z2
z3
z4
4
First Attempt: List Implementation
U NION-Operation
Union(h1 , h2 )
Add extra pointer to the last
element in each list
h1
x1
⇒ U NION takes constant time
x2
x3
h2
y1
Need to update all
backward pointers!
F IND-Operation
Add backward pointer to the list
head from everywhere
FindSet(z3 )
⇒ F IND takes constant time
h4
z1
5.3: Disjoint Sets
y2
T.S.
z2
z3
z4
4
First Attempt: List Implementation (Analysis)
d = DisjointSet()
5.3: Disjoint Sets
T.S.
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h0
x0
5.3: Disjoint Sets
T.S.
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
5.3: Disjoint Sets
T.S.
h1
h0
x1
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
5.3: Disjoint Sets
T.S.
h1
h0
x1
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
5.3: Disjoint Sets
T.S.
h1
h0
x1
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
h0
x1
5.3: Disjoint Sets
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
h0
x1
5.3: Disjoint Sets
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
h0
x1
5.3: Disjoint Sets
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1 = d.MakeSet(x1 )
h0 = d.Union(h1 , h0 )
h2 = d.MakeSet(x2 )
5.3: Disjoint Sets
h2
h0
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
5.3: Disjoint Sets
h2
h0
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
5.3: Disjoint Sets
h2
h0
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
h0
x2
5.3: Disjoint Sets
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
h0
x2
5.3: Disjoint Sets
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
h0
x2
5.3: Disjoint Sets
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
5.3: Disjoint Sets
h3
h0
x3
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
5.3: Disjoint Sets
h3
h0
x3
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
5.3: Disjoint Sets
h3
h0
x3
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
h0
x3
5.3: Disjoint Sets
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
h0
x3
5.3: Disjoint Sets
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
h0
x3
5.3: Disjoint Sets
x2
x1
T.S.
x0
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
h0
x3
x2
Cost for n UNION operations:
5.3: Disjoint Sets
x0
x1
T.S.
Pn
i=1 i
= Θ(n2 )
5
First Attempt: List Implementation (Analysis)
d = DisjointSet()
h0 = d.MakeSet(x0 )
h1
h0
h2
h0
h3
h0
= d.MakeSet(x1 )
= d.Union(h1 , h0 )
= d.MakeSet(x2 )
= d.Union(h2 , h0 )
= d.MakeSet(x3 )
= d.Union(h3 , h0 )
h0
x3
better to append shorter list to longer
x2
Weighted-Union Heuristic
Cost for n UNION operations:
5.3: Disjoint Sets
x0
x1
T.S.
Pn
i=1 i
= Θ(n2 )
5
Weighted-Union Heuristic
Weighted-Union Heuristic
Keep track of the length of each list
5.3: Disjoint Sets
T.S.
6
Weighted-Union Heuristic
Weighted-Union Heuristic
Keep track of the length of each list
Append shorter list to the longer list (breaking ties arbitrarily)
5.3: Disjoint Sets
T.S.
6
Weighted-Union Heuristic
Weighted-Union Heuristic
Keep track of the length of each list
Append shorter list to the longer list (breaking ties arbitrarily)
can be done easily without significant overhead
5.3: Disjoint Sets
T.S.
6
Weighted-Union Heuristic
Weighted-Union Heuristic
Keep track of the length of each list
Append shorter list to the longer list (breaking ties arbitrarily)
can be done easily without significant overhead
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
5.3: Disjoint Sets
T.S.
6
Weighted-Union Heuristic
Weighted-Union Heuristic
Keep track of the length of each list
Append shorter list to the longer list (breaking ties arbitrarily)
can be done easily without significant overhead
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Amortized Analysis: Every operation has amortized cost
O(log n), but there may be operations with total cost Θ(n).
5.3: Disjoint Sets
T.S.
6
Analysis of Weighted-Union Heuristic
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
After each update of x, its set increases by a factor of at least 2
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
After each update of x, its set increases by a factor of at least 2
⇒ Backward pointer of x is updated at most log2 n times
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
After each update of x, its set increases by a factor of at least 2
⇒ Backward pointer of x is updated at most log2 n times
Other updates for UNION, MAKE-SET & FIND-SET take O(1) time per
operation
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
After each update of x, its set increases by a factor of at least 2
⇒ Backward pointer of x is updated at most log2 n times
Other updates for UNION, MAKE-SET & FIND-SET take O(1) time per
operation
5.3: Disjoint Sets
T.S.
7
Analysis of Weighted-Union Heuristic
h0
h1
x
h2
x
Theorem 21.1
Using the Weighted-Union heuristic, any sequence of m operations, n of
which are MAKE-SET operations, takes O(m + n · log n) time.
Proof:
Can we improve on this further?
n MAKE-SET operations ⇒ at most n − 1 UNION operations
Consider element x and the number of updates of the backward pointer
After each update of x, its set increases by a factor of at least 2
⇒ Backward pointer of x is updated at most log2 n times
Other updates for UNION, MAKE-SET & FIND-SET take O(1) time per
operation
5.3: Disjoint Sets
T.S.
7
How to Improve?
h0
Doubly-Linked List
MAKE-SET: O(1)
FIND-SET: O(n)
UNION: O(1)
5.3: Disjoint Sets
T.S.
8
How to Improve?
h1
h0
Doubly-Linked List
Weighted-Union Heuristic
MAKE-SET: O(1)
MAKE-SET: O(1)
UNION: O(1)
UNION: O(log n) (amortized)
FIND-SET: O(n)
5.3: Disjoint Sets
FIND-SET: O(1)
T.S.
8
How to Improve?
h1
h0
Basic Idea: Update Backward
Pointers only during FIND
Doubly-Linked List
Weighted-Union Heuristic
MAKE-SET: O(1)
MAKE-SET: O(1)
UNION: O(1)
UNION: O(log n) (amortized)
FIND-SET: O(n)
5.3: Disjoint Sets
FIND-SET: O(1)
T.S.
8
Disjoint Sets via Forests
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
5.3: Disjoint Sets
T.S.
9
Disjoint Sets via Forests
{b, c, e, h}
c
h
e
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
5.3: Disjoint Sets
T.S.
9
Disjoint Sets via Forests
{b, c, e, h}
c
h
e
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
5.3: Disjoint Sets
T.S.
9
Disjoint Sets via Forests
h
{b, c, e, h}
{d, f , g}
c
f
e
d
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
5.3: Disjoint Sets
T.S.
9
Disjoint Sets via Forests
h
{b, c, e, h}
{d, f , g}
{b, c, d, e, f , g, h}
c
f
f
e
c
d
g
b
h
d
e
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
5.3: Disjoint Sets
T.S.
9
Disjoint Sets via Forests
h
{b, c, e, h}
{d, f , g}
{b, c, d, e, f , g, h}
c
f
f
e
c
d
g
b
h
d
e
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
Append tree of smaller height
5.3: Disjoint Sets
T.S.
Union by Rank
9
Disjoint Sets via Forests
{b, c, e, h}
c
h
{d, f , g}
rank = 2
e
f
{b, c, d, e, f , g, h}
rank = 2
f
c
d
g
b
rank = 3
h
d
e
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
Append tree of smaller height
5.3: Disjoint Sets
T.S.
Union by Rank
9
Disjoint Sets via Forests
{b, c, e, h}
c
h
{d, f , g}
{b, c, d, e, f , g, h}
Rank may be just an upper bound on the height!
rank = 2
e
f
rank = 2
f
c
d
g
b
rank = 3
h
d
e
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
Append tree of smaller height
5.3: Disjoint Sets
T.S.
Union by Rank
9
Disjoint Sets via Forests
{b, c, e, h}
c
h
{d, f , g}
{b, c, d, e, f , g, h}
Rank may be just an upper bound on the height!
rank = 2
e
f
rank = 2
3
f
c
d
g
b
rank = 3
h
d
e
g
b
Forest Structure
Set is represented by a rooted tree with root being the representative
Every node has pointer .p to its parent (for root x, x.p = x)
UNION: Merge the two trees
Append tree of smaller height
5.3: Disjoint Sets
T.S.
Union by Rank
9
Path Compression during FIND-SET
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c f
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c f
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c f
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
c
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b h
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
c
h
d
e
g
b
0:
1:
2:
3:
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
b
f
b
h
c
d
e
0:
1:
2:
3:
g
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
f
b
h
c
d
e
0:
1:
2:
3:
g
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Path Compression during FIND-SET
f
b
h
Maintaining the exact height would be
costly, hence rank is only an upper bound!
0:
1:
2:
3:
c
d
e
g
FindSet(x )
if x 6= x.p
x.p = FindSet(x.p)
return x.p
5.3: Disjoint Sets
T.S.
10
Combining Union by Rank and Path Compression
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.
5.3: Disjoint Sets
T.S.
11
Combining Union by Rank and Path Compression
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.


0





1
α(n) = 2



3




4
5.3: Disjoint Sets
for 0 ≤ n ≤ 2,
for n = 3,
for 4 ≤ n ≤ 7,
for 8 ≤ n ≤ 2047,
for 2048 ≤ n ≤ 1080
T.S.
11
Combining Union by Rank and Path Compression
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.


0





1
α(n) = 2



3




4
for 0 ≤ n ≤ 2,
for n = 3,
for 4 ≤ n ≤ 7,
for 8 ≤ n ≤ 2047,
for 2048 ≤ n ≤ 1080
More than the number of atoms in the universe!
5.3: Disjoint Sets
T.S.
11
Combining Union by Rank and Path Compression
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.


0





1
α(n) = 2



3




4
for 0 ≤ n ≤ 2,
for n = 3,
for 4 ≤ n ≤ 7,
for 8 ≤ n ≤ 2047,
for 2048 ≤ n ≤ 1080
log∗ (n), the iterated logarithm, satifies
α(n) ≤ log∗ (n), but still log∗ (1080 ) = 5.
5.3: Disjoint Sets
T.S.
11
Combining Union by Rank and Path Compression
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.
In practice, α(n) is a small constant


0





1
α(n) = 2



3




4
5.3: Disjoint Sets
for 0 ≤ n ≤ 2,
for n = 3,
for 4 ≤ n ≤ 7,
for 8 ≤ n ≤ 2047,
for 2048 ≤ n ≤ 1080
T.S.
11
Combining Union by Rank and Path Compression
Data Structure is essentially optimal! (for more details see CLRS)
Theorem 21.14
Any sequence of m MAKE-SET, UNION, FIND-SET operations, n of
which are MAKE-SET operations, can be performed in O(m ·α(n)) time.
In practice, α(n) is a small constant


0





1
α(n) = 2



3




4
5.3: Disjoint Sets
for 0 ≤ n ≤ 2,
for n = 3,
for 4 ≤ n ≤ 7,
for 8 ≤ n ≤ 2047,
for 2048 ≤ n ≤ 1080
T.S.
11
Simulating the Effects of Union by Rank and Path Compression
5.3: Disjoint Sets
T.S.
12
Simulating the Effects of Union by Rank and Path Compression
Experimental Setup
1. Initialise singletons 1, 2, . . . , 300
2. For every 1 ≤ i ≤ 300, pick a random 1 ≤ r ≤ 300, r 6= i and
perform U NION(F IND(i), F IND(r ))
3. Perform j ∈ {0, 100, 200, 300, 600, 900} many F IND(r ), where
1 ≤ r ≤ 300 is random
5.3: Disjoint Sets
T.S.
12
Union by Rank without Path Compression
3
0
2
2
2
3
2
3
3
2
2
1
3
3
3
3
1
2
2
3
3
3
2
2
3
3
3
3
3
1
2
2
3
2
2
1
3
2
2
3
2
3
1
2
2
1
2
2
2
3
3
2
2
2
2
2
2
3
2
3
3
3
2
2
3
2
1
3
3
3
2
3
3
3
1
3
3
2
1
3
2
2
2
2
3
1
2
2
2
22
22
3
22
22
1
23
5.3: Disjoint Sets
3
22
33
23
22
33
13
12
23
211
22
213
232
32212
322
2232312222331
33332
22132332332
321
3222
313
221
32
3
22
3
12
T.S.
21
23
31
32
22
22
23
12
32
31
3
2
12
1
1
1
2
3
2
2
2
2
3
1
1
2
2
2
3
1
2
2
3
2
2
3
1
1
0
3
1
2
2
2
3
1
1
0
1
1
2
2
2
2
1
1
2
3
1
0
1
2
2
2
2
2
2
2
3
3
3
3
2
3
2
3
2
3
1
3
1
1
2
1
3
1
3
1
2
1
2
2
1
3
3
1
2
2
3
2
2
2
13
Union by Rank with Path Compression
1
0
1
1
2
2
2
3
3
2
2
1
1
2
3
1
1
2
2
2
3
2
2
2
2
2
3
2
2
1
2
1
3
2
2
1
1
2
1
3
2
2
1
2
2
1
2
2
2
1
1
1
2
1
2
1
2
2
2
2
2
3
2
2
2
2
1
1
1
1
1
3
2
3
1
2
3
2
1
2
2
2
2
2
3
1
1
2
2
22
22
3
12
22
1
13
5.3: Disjoint Sets
2
22
23
23
22
32
13
12
23
211
21
112
122
32212
1123
2222312222331
221
332221122221322122
312
222
22
12
22
3
12
T.S.
21
23
21
12
12
21
23
12
22
11
11
12
1
1
1
2
3
2
1
2
1
2
1
1
2
2
1
2
1
2
2
3
2
2
3
1
1
0
2
1
2
2
1
2
1
1
0
1
1
2
1
2
1
1
1
2
2
1
0
1
2
1
1
1
2
2
2
1
2
1
1
2
1
2
2
2
3
1
1
1
1
2
1
3
1
2
1
2
1
2
2
1
2
2
1
2
2
2
2
2
2
14
After 100 additional FINDs
1
0
1
1
2
1
2
3
3
1
2
1
1
1
1
1
1
2
2
1
2
2
2
2
2
2
2
1
2
1
1
1
3
2
2
1
1
2
1
3
2
1
1
2
2
1
2
2
2
1
1
1
2
1
1
1
2
2
2
2
2
3
2
1
2
2
1
1
1
1
1
3
2
3
1
2
3
2
1
2
2
2
1
2
2
1
1
2
2
21
21
2
12
22
1
12
5.3: Disjoint Sets
2
21
13
23
12
31
13
11
23
111
21
112
112
32212
111
1212312222231
133211
1122121322
211
112
212
122
22
12
12
2
12
T.S.
11
11
21
11
12
11
12
12
21
11
11
12
1
1
1
1
3
2
1
1
1
2
1
1
1
1
1
2
1
2
2
1
2
2
1
1
1
0
2
1
2
1
1
2
1
1
0
1
1
2
1
1
1
1
1
2
2
1
0
1
1
1
1
1
1
2
1
1
1
1
1
2
1
2
2
2
3
1
1
1
1
2
1
2
1
2
1
2
1
2
1
1
2
2
1
2
2
2
2
1
2
15
After 200 additional FINDs
1
0
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
2
1
1
1
1
2
2
2
1
2
1
2
1
1
1
3
2
1
1
1
2
1
2
2
1
1
2
2
1
2
2
2
1
1
1
2
1
1
1
2
2
2
2
2
1
2
1
2
1
1
1
1
1
1
3
2
2
1
2
3
2
1
1
1
2
1
2
2
1
1
2
2
11
21
1
12
22
1
12
5.3: Disjoint Sets
2
21
13
21
12
31
13
11
22
111
21
112
111
31212
111
1211211121221
131211
1122111311
211
112
212
122
21
12
11
1
12
T.S.
11
11
21
11
12
11
12
12
11
11
11
12
1
1
1
1
3
2
1
1
1
2
1
1
1
1
1
2
1
2
1
1
2
1
1
1
1
0
1
1
1
1
1
2
1
1
0
1
1
2
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
2
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
2
1
2
1
2
1
1
1
2
1
1
2
2
1
1
2
1
2
1
2
16
After 300 additional FINDs
1
0
1
1
1
1
2
1
2
1
1
1
1
1
1
1
1
2
1
1
1
1
1
2
2
1
2
1
2
1
1
1
3
2
1
1
1
2
1
2
2
1
1
1
1
1
2
2
1
1
1
1
1
1
1
1
2
2
2
2
2
1
2
1
1
1
1
1
1
1
1
1
2
2
1
2
2
2
1
1
1
2
1
2
2
1
1
2
1
11
21
1
12
22
1
11
5.3: Disjoint Sets
2
21
13
11
12
21
12
11
22
111
11
111
111
21111
111
1111111121221
111211
1122111111
211
112
112
112
21
12
11
1
12
T.S.
11
11
21
11
12
11
11
11
11
11
11
11
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
2
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
2
1
2
1
1
1
1
1
1
1
2
1
1
1
2
1
1
1
2
1
1
2
2
1
1
1
1
2
1
2
17
After 600 additional FINDs
1
0
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
2
1
1
1
1
1
2
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
1
1
2
1
1
1
1
1
1
1
1
1
2
2
1
1
2
2
1
1
1
1
1
2
1
1
1
2
1
11
11
1
12
12
1
11
5.3: Disjoint Sets
2
11
11
11
12
11
12
11
11
111
11
111
111
11111
111
1111111111111
111211
1112111111
111
112
112
112
11
11
11
1
12
T.S.
11
11
11
11
11
11
11
11
11
11
11
11
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
18
After 900 additional FINDs
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
1
1
1
1
1
2
1
1
1
1
1
11
11
1
12
12
1
11
5.3: Disjoint Sets
2
11
11
11
12
11
11
11
11
111
11
111
111
11111
111
1111111111111
111211
1111111111
111
111
111
111
11
11
11
1
12
T.S.
11
11
11
11
11
11
11
11
11
11
11
11
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
19
Outline
Disjoint Sets
Introduction to Graphs and Graph Searching
5.3: Disjoint Sets
T.S.
20
Origin of Graph Theory
Source: Wikipedia
Seven Bridges at Königsberg 1737
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
A
B
D
C
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
A
B
D
C
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
A
B
D
C
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
A
B
D
C
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
A
B
D
C
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
A
B
D
C
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
A
B
D
Is there a tour which visits every
island exactly once?
C
5.3: Disjoint Sets
T.S.
21
Origin of Graph Theory
A
B
D
C
Source: Wikipedia
Source: Wikipedia
Seven Bridges at Königsberg 1737
Leonhard Euler (1707-1783)
Is there a tour which crosses
each bridge exactly once?
A
B
D
Is there a tour which visits every
island exactly once?
1B course: Complexity Theory
C
5.3: Disjoint Sets
T.S.
21
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
V : the set of vertices
E: the set of edges (arcs)
5.3: Disjoint Sets
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
5.3: Disjoint Sets
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
5.3: Disjoint Sets
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
5.3: Disjoint Sets
T.S.
1
2
3
4
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
5.3: Disjoint Sets
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
Path p = (1, 2, 3, 4)
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
Path p = (1, 2, 3, 1), which is a cycle
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
G is not a DAG
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
A graph G = (V , E) consists of:
G is not a DAG
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
Undirected Graph
A graph G = (V , E) consists of:
V : the set of vertices
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
If each pair of vertices has a
path linking them, then G is
connected
5.3: Disjoint Sets
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
G is not a DAG
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
G is not (strongly)
connected
Undirected Graph
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
A graph G = (V , E) consists of:
V : the set of vertices
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
If each pair of vertices has a
path linking them, then G is
connected
5.3: Disjoint Sets
G is connected
1
2
3
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
What is a Graph?
Directed Graph
G is not a DAG
A graph G = (V , E) consists of:
1
2
3
4
V : the set of vertices
E: the set of edges (arcs)
G is not (strongly)
connected
Undirected Graph
V = {1, 2, 3, 4}
E = {(1, 2), (1, 3), (2, 3), (3, 1), (3, 4)}
A graph G = (V , E) consists of:
V : the set of vertices
Later: edge-weighted graphs G = (V , E,
1 w)
E: the set of (undirected) edges
Paths and Connectivity
A sequence of edges between
two vertices forms a path
If each pair of vertices has a
path linking them, then G is
connected
5.3: Disjoint Sets
G is connected
3
2
4
V = {1, 2, 3, 4}
E = {{1, 2}, {1, 3}, {2, 3}, {3, 4}}
T.S.
22
590
Chapter 22 Elementary
Graph Algorithms
Representations
of Directed
and Undirected Graphs
1
2
5
4
1
2
4
5
2
4
2
3
3
1
2
(a)
5
5
4
5
1
3
4
1
1 2 3 4 5
0 1 0 0 1
4
5
0 1 1 0 1
1 1 0 1 0
2
3
3
2
1 0 1 1 1
0 1 0 1 0
(b)
(c)
Figure 22.1 Two representations of an undirected graph. (a) An undirected graph G with 5 vertices
and 7 edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation
of G.
1
2
3
4
5
6
(a)
1
2
3
4
5
6
2
5
6
2
4
6
(b)
4
5
1
2
3
4
5
6
1
0
0
0
0
0
0
2
1
0
0
1
0
0
3
0
0
0
0
0
0
4
1
0
0
0
1
0
5
0
1
1
0
0
0
6
0
0
1
0
0
1
(c)
Figure 22.2 Two representations of a directed graph. (a) A directed graph G with 6 vertices and 8
edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation of G.
shortest-paths
presented in Chapter 25T.S.
assume that their input graphs
5.3: Disjointalgorithms
Sets
23
590
Chapter 22 Elementary
Graph Algorithms
Representations
of Directed
and Undirected Graphs
1
2
5
4
1
2
4
5
2
4
2
3
3
1
2
(a)
5
5
4
5
1
3
4
1
1 2 3 4 5
0 1 0 0 1
4
5
0 1 1 0 1
1 1 0 1 0
2
3
3
2
1 0 1 1 1
0 1 0 1 0
(b)
(c)
Figure 22.1 Two representations of an undirected graph. (a) An undirected graph G with 5 vertices
and 7 edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation
of G.
1
2
3
4
5
6
(a)
1
2
3
4
5
6
2
5
6
2
4
6
(b)
4
5
1
2
3
4
5
6
1
0
0
0
0
0
0
2
1
0
0
1
0
0
3
0
0
0
0
0
0
4
1
0
0
0
1
0
5
0
1
1
0
0
0
6
0
0
1
0
0
1
(c)
Figure 22.2 Two representations of a directed graph. (a) A directed graph G with 6 vertices and 8
edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation of G.
shortest-paths
presented in Chapter 25T.S.
assume that their input graphs
5.3: Disjointalgorithms
Sets
23
590
Chapter 22 Elementary
Graph Algorithms
Representations
of Directed
and Undirected Graphs
1
2
5
4
1
2
4
5
2
4
2
3
3
1
2
(a)
5
5
4
5
1
3
4
1
1 2 3 4 5
0 1 0 0 1
4
5
0 1 1 0 1
1 1 0 1 0
2
3
3
2
1 0 1 1 1
0 1 0 1 0
(b)
(c)
Figure 22.1 Two representations of an undirected graph. (a) An undirected graph G with 5 vertices
and 7 edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation
of G.
1
2
3
4
5
6
(a)
1
2
3
4
5
6
2
5
6
2
4
6
(b)
4
5
1
2
3
4
5
6
1
0
0
0
0
0
0
2
1
0
0
1
0
0
3
0
0
0
0
0
0
4
1
0
0
0
1
0
5
0
1
1
0
0
0
6
0
0
1
0
0
1
(c)
Figure 22.2 Two representations of a directed graph. (a) A directed graph G with 6 vertices and 8
edges. (b) An adjacency-list representation of G. (c) The adjacency-matrix representation of G.
shortest-paths
presented in Chapter 25T.S.
assume that their input graphs
5.3: Disjointalgorithms
Sets
23