ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

Σχετικά έγγραφα
Fractional Colorings and Zykov Products of graphs

Αλγόριθµοι και Πολυπλοκότητα

Network Algorithms and Complexity Παραλληλοποίηση του αλγορίθμου του Prim. Αικατερίνη Κούκιου

Αλγόριθμοι και πολυπλοκότητα Minimum Spanning Trees

Αλγόριθµοι και Πολυπλοκότητα

Minimum Spanning Tree: Prim's Algorithm

Graph Algorithms. Παρουσίαση στα πλαίσια του μαθήματος «Παράλληλοι Αλγόριθμοι» Καούρη Γεωργία Μήτσου Βασιλική

Ενότητα 9 Ξένα Σύνολα που υποστηρίζουν τη λειτουργία της Ένωσης (Union-Find)

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

ΕΙΣΑΓΩΓΗ ΣΤΗΝ ΑΝΑΛΥΣΗ ΑΛΓΟΡΙΘΜΩΝ

4. GREEDY ALGORITHMS II

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Δομές Δεδομένων. Ιωάννης Γ. Τόλλης Τμήμα Επιστήμης Υπολογιστών Πανεπιστήμιο Κρήτης

Αλγόριθµοι και Πολυπλοκότητα

Ενότητα 9 Ξένα Σύνολα που υποστηρίζουν τη λειτουργία της Ένωσης (Union-Find)

Αλγόριθμοι και πολυπλοκότητα NP-Completeness (2)

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

EE512: Error Control Coding

Αλγόριθμοι Γραφημάτων

Αλγόριθμοι και Πολυπλοκότητα

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

Finite Field Problems: Solutions

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

HY380 Αλγόριθμοι και πολυπλοκότητα Hard Problems

Every set of first-order formulas is equivalent to an independent set

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

Δομές δεδομένων. Ενότητα 8: Ξένα Σύνολα που υποστηρίζουν τη λειτουργία της Ένωσης (Union-Find) Παναγιώτα Φατούρου Τμήμα Επιστήμης Υπολογιστών

Επίλυση Προβληµάτων µε Greedy Αλγόριθµους

Αλγόριθμοι Eλάχιστα μονοπάτια

AVL-trees C++ implementation

2 Composition. Invertible Mappings

Αλγόριθµοι και Πολυπλοκότητα

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Statistical Inference I Locally most powerful tests

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

The challenges of non-stable predicates

ORDINAL ARITHMETIC JULIAN J. SCHLÖDER

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Partition of weighted sets (problems with numbers)

Αλγόριθμοι και πολυπλοκότητα Depth-First Search

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

Σχεδιαση Αλγοριθμων -Τμημα Πληροφορικης ΑΠΘ - Κεφαλαιο 9ο

ST5224: Advanced Statistical Theory II

Reminders: linear functions

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

The Simply Typed Lambda Calculus

Αλγόριθμοι Επανάληψη για πρόοδο

Matrices and Determinants

Distances in Sierpiński Triangle Graphs

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Other Test Constructions: Likelihood Ratio & Bayes Tests

Ελάχιστο Συνδετικό Δέντρο

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

Σχέσεις, Ιδιότητες, Κλειστότητες

Bounding Nonsplitting Enumeration Degrees

Αλγόριθμοι Ταξινόμησης Μέρος 3

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

The Probabilistic Method - Probabilistic Techniques. Lecture 7: The Janson Inequality

Κατ οίκον Εργασία 5 Σκελετοί Λύσεων

Approximation of distance between locations on earth given by latitude and longitude

Ελάχιστο Συνδετικό Δέντρο

Ελάχιστο Συνδετικό Δέντρο

Αλγόριθµοι και Πολυπλοκότητα

Ελάχιστο Συνδετικό έντρο

C.S. 430 Assignment 6, Sample Solutions

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

Στοιχεία Θεωρίας Γράφων (Graph Theory)

Mean-Variance Analysis

ΕΡΓΑΣΙΑ ΣΤΟ ΜΑΘΗΜΑ Λ03Β ΑΛΓΟΡΙΘΜΟΙ ΔΙΚΤΥΩΝ & ΠΟΛΥΠΛΟΚΟΤΗΤΑ ΦΛΕΒΑΡΗΣ 2004

Ελάχιστο Συνδετικό έντρο

w o = R 1 p. (1) R = p =. = 1

Homework 8 Model Solution Section

ΤΜΗΜΑ ΗΛΕΚΤΡΟΛΟΓΩΝ ΜΗΧΑΝΙΚΩΝ ΚΑΙ ΜΗΧΑΝΙΚΩΝ ΥΠΟΛΟΓΙΣΤΩΝ

Συστήματα Διαχείρισης Βάσεων Δεδομένων

Section 8.3 Trigonometric Equations

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

Example Sheet 3 Solutions

derivation of the Laplacian from rectangular to spherical coordinates

Uniform Convergence of Fourier Series Michael Taylor

Δομές Δεδομένων & Αλγόριθμοι

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Διάλεξη 16: Σωροί. Στην ενότητα αυτή θα μελετηθούν τα εξής επιμέρους θέματα: - Ουρές Προτεραιότητας - Ο ΑΤΔ Σωρός, Υλοποίηση και πράξεις

Capacitors - Capacitance, Charge and Potential Difference

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

TMA4115 Matematikk 3

Multicut and Integer Multicomodity Flow in Trees (chap. 18) Αγγελής Γιώργος

Pg The perimeter is P = 3x The area of a triangle is. where b is the base, h is the height. In our case b = x, then the area is

Lecture 13 - Root Space Decomposition II

Θεωρία Γραφημάτων Θεμελιώσεις-Αλγόριθμοι-Εφαρμογές

ΑΛΓΟΡΙΘΜΟΙ. Ενότητα 9: Άπληστοι Αλγόριθμοι. Ιωάννης Μανωλόπουλος, Καθηγητής Αναστάσιος Γούναρης, Επίκουρος Καθηγητής Τμήμα Πληροφορικής ΑΠΘ

A Note on Intuitionistic Fuzzy. Equivalence Relation

On a four-dimensional hyperbolic manifold with finite volume

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

Models for Probabilistic Programs with an Adversary

Δομές Δεδομένων Ενότητα 5

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Αλγόριθμοι και πολυπλοκότητα Περιήγηση Πανεπιστημίων

5. Choice under Uncertainty

Transcript:

ΑΛΓΟΡΙΘΜΟΙ http://eclass.aueb.gr/courses/inf161/ Άνοιξη 016 - I. ΜΗΛΗΣ AΛΓΟΡΙΘΜΟΙ ΓΡΑΦΩΝ ΙΙΙ Minimum Spanning Trees ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 1

Trees Ένας γράφος T = (V, A) είναι δέντρο ΑΝ ΚΑΙ ΜΟΝΟ ΑΝ: is acyclic and connected is connected and has V - 1 edges is acyclic and has V - 1 edges is minimally connected: removal of any edge disconnects it is maimally acyclic: addition of any edge creates a cycle has a unique simple path between every pair of vertices 1 3 6 4 5 7 8 T = (V, A) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs

Spanning Trees ΕΙΣΟΔΟΣ: Γράφος G=(V, E) ΕΡΩΤΗΣΗ: Ένα δέντρο T = (V, Ε ), Ε Ε 1 3 1 3 6 4 5 6 4 5 8 7 8 7 G = (V, E) T = (V, E ) Συνδέει όλους τους κόμβους του γράφου με τις ελάχιστες ακμές Αλγόριθμος? ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 3

Minimum Spanning Tree (MST) ΕΙΣΟΔΟΣ: Γράφος με βάρη G=(V, E), w(e), e E ΕΡΩΤΗΣΗ: Ένα δέντρο T = (V, Ε ), Ε Ε με το ελάχιστο άθροισμα βαρών των ακμών του 1 4 16 6 7 6 8 10 5 4 3 5 1 14 18 11 4 7 9 8 3 1 4 6 7 6 8 5 5 11 4 7 9 8 3 G = (V, E) T = (V, Ε ) w(t) = 50 Αλγόριθμος? Brute force? There are n n- spanning trees of K n [Cayley, 1889] ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 4

Τομή Γράφου (Graph Cut) Γράφος G=(V,E) ΤΟΜΗ C=(S, V-S), S V : διαμέριση (partition) των κόμβων του G σε δύο υποσύνολα S και V-S C = {(u,v) u S, v V-S}, S V : το υποσύνολο των ακμών με ένα άκρο στο S και ένα στο V-S. 1 6 5 4 3 S S = {4, 5, 8} C = { (5, 6), (5, 7), (3, 4), (3, 5), (7, 8) } 7 8 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 5

Ιδιότητα τομής (Cut property) Έστω: - Χ ένα υποσύνολο των ακμών ενός MST - C μια τομή του G που δεν περιέχει ακμές από το Χ - e η μικρότερου βάρους ακμή της τομής C Τότε: οι ακμές Χ U {e} ανήκουν σε ένα MST (η ακμή e ανήκει στο ίδιο MST με τις Χ ) Χ Χ ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 6

Proof of Cut Property Oι ακμές Χ ανήκουν σε ένα MST, έστω Τ Έστω ότι e T Κάποια άλλη ακμή e C ανήκει στο Τ w(e ) w(e) Αντικατέστησε την e με την e στο Τ Το T = T U {e} {e } είναι δέντρο (γιατί?) W(T )=W(T) + w(e) w(e ) W(T) ATOΠΟ, γιατί το T είναι MST ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 7

A generic MST algorithm Η ιδιότητα τομής εγγυάται την ορθότητα κάθε αλγορίθμου σαν τον παρακάτω: X = { } \\ edges selected so far While X < V - 1: Choose S V: X has no edges between S and V-S Find the lightest edge e between S and V-S X = X U {e} ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 8

Prim's MST algorithm Εφάρμοσε την ιδιότητα τομής για την τομή S, V-S, όπου S είναι ΕΝΑ ΥΠΟ-ΔΕΝΤΡΟ ενός MST (X είναι το σύνολο των ακμών αυτού του υπό-δέντρου) Ξεκίνα από οποιοδήποτε κόμβο Σε κάθε βήμα προστίθεται στο MST ένας κόμβος και μία ακμή Επαναληπτικά: Διάλεξε τον κόμβο με την μικρότερη απόσταση από το τρέχον υπό-δέντρο (οποιοδήποτε κόμβο του) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 9

Prim's MST algorithm X = { } \\ edges selected so far S:={} \\ is any verte While X < V - 1: Find the lightest edge e=(u,v)between S and V-S \\ u S, v V-S S = S U {v} X = X U {e} Διάλεξε τον κόμβο με την μικρότερη απόσταση από το τρέχον υπό-δέντρο ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 10

Prim's MST algorithm - eample Διάλεξε τον κόμβο με την μικρότερη απόσταση από το τρέχον υπό-δέντρο Πώς υλοποιείται αποτελεσματικά? ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 11

Prim's MST algorithm π(u): συντομότερη του κόμβου u από το σύνολο S par(u): το MST PRIM(G,w) for all uv: π(u)=, par(u)=nil S={} π(s)=0 MAKE-HEAP (H,s) for all uv-{s}:insert(h,u) while NON EMPTY(H): DELETE-MIN (H,u) S = S U {u} for each vγ(u): if π(v) > w(u,v): π(v)= w(u,v) par(v)=u Greedy algorithm - Greedy choice: Διάλεξε τον κόμβο με τη μικρότερη απόσταση π(u) από το σύνολο S DECREASE-KEY(H,v,π(v) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 1

Dijkstra's algorithm π(u): συντομότερη απόσταση του κόμβου u από τον αρχικό κόμβο s par(u): τα συντομότερα μονοπάτια DIJKSTRA(G,s,w) for all uv: π(u)=, par(u)=nil S={} π(s)=0 MAKE-HEAP (H,s) for all uv-{s}:insert(h,u) while NON EMPTY(H): DELETE-MIN (H,u) S = S U {u} for each vγ(u): if π(v) > w(u,v)+ π(u): π(v)= w(u,v)+ π(u) par(v)=u Greedy algorithm - Greedy choice: Διάλεξε τον κόμβο με τη μικρότερη απόσταση π(u) από τον κόμβο s DECREASE-KEY(H,v,π(v)) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 13

Prim s and Dijkstra s algorithms: Compleity # of Operations Heap implementation Binary Binomial* Fibonacci* make-heap: 1 O(1) O(1) O(1) insert: n O(logn) O(1) O(1) delete-min: n O(logn) O(logn) O(logn) decrease-key: m O(logn) O(logn) O(1) Binary heap: 1 + n logn + n logn + m logn ~ O(m logn) Binomial heap: 1 + n + n logn + m logn ~ O(m logn) Fibonacci heap: 1 + n + n logn + m ~ O(m + n logn) * amortized ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 14

Kruskal's MST algorithm Έστω ένα υποσύνολο Χ των ακμών ενός MST του G e=(u,v) X T u and T v τα υπό-δέντρα του Τ στα οποία ανήκουν τα άκρα της e Υπάρχουν δύο περιπτώσεις: 1 η περίπτωση: Εάν T u T v, τότε αγνόησε την e (κύκλος) 1 3 6 4 7 5 Case 1: e=(5, 8) 8 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 15

Kruskal's MST algorithm Έστω ένα υποσύνολο Χ των ακμών ενός MST του G e=(u,v) X T u and T v τα υπό-δέντρα του Τ στα οποία ανήκουν τα άκρα της e Υπάρχουν δύο περιπτώσεις: η περίπτωση: Εάν T u T v ΚΑΙ η e είναι η μικρότερη στην τομή (T u,v- T u ), τότε X = X U {e} (η e ανήκει στο ΜST) 1 3 6 4 7 5 Case : e=(5, 6) 8 ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 16

Kruskal's MST algorithm η περίπτωση: Εάν T u T v ΚΑΙ η e=(u,v) είναι η μικρότερη στην τομή (T u,v- T u ), τότε X = X U {e} (η e ανήκει στο ΜST) 7 1 6 Αλγόριθμος: 5 4 Case : e=(5, 6) Επαναληπτικά (με αυτή τη σειρά ακμών): 8 3 Μπορούμε να εξετάσουμε τις ακμές με τέτοια σειρά ώστε η ακμή e να είναι η μικρότερη στην τομή (T u,v- T u )? ΝΑΙ: Αύξουσα σειρά βάρους w 1 w... w m Εάν η ακμή ΔΕΝ σχηματίζει κύκλο με τα τρέχοντα υπό-δεντρα του MST πρόσθεσέ την στο MST ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 17

Kruskal's MST algorithm Sort the edges s.t. w 1 w... w m \\ e i is the edge of weight w i X = { } \\ edges selected so far for i = 1 to m: if T u T v : X = X U {e i }; Πώς υλοποιείται αποτελεσματικά? ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 18

Union-Find A collection {S 1, S,..., S k } of DISJOINT dynamic sets : an element of some set S : the set which contains Operations MAKE-SET () Creates a new set S containing only element ; is not contained in any other eisting set UNION (,y) Unifies the disjoint sets containing elements and y (S and S y ) into a new set; Sets S and S y are destroyed FIND () Returns the set S (from a collection of disjoint sets) containing ; If, y S i, then FIND()=FIND(y) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 19

Kruskal's MST Algorithm Sort the edges s.t. w 1 w... w m \\ e i is the edge of weight w i X ={} \\ edges selected so far for each u V: make-set(u) for i = 1 to m: e i = (u,v) if find(u) find(v): union(u,v) X = X {e i } UNION-FIND make-set(u): creates a set containing element u union(u,v): unifies the disjoint sets containing elements u and v find(u): returns the set (from a collection of disjoint sets) containing element u SET = SUB-TREE ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 0

Kruskal's MST Algorithm Sort the edges s.t. w 1 w... w m \\ e i is the edge of weight w i X ={} \\ edges selected so far for each u V: make-set(u) for i = 1 to m: e i = (u,v) if find(u) find(v): union(u,v) X = X {e i } Compleity Sorting: O( E log E ) that is O( E log V ) - why? Union-Find: V make-set, E find, V -1 union each one of them costs O(log V ) (net slides) and altogether O( E log V ) In total: O( E log V ) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 1

Union-Find: Up-tree representation of sets Every set S i is represented by an up-tree The root of the tree is the representative of S i Every node has a pointer to its parent The root of the tree has a pointer to itself p[]: the parent of element e a d b c f g y h ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs

Union-Find: 1 st approach MAKE-SET() p[]= FIND() while <>p[]: =p[] return O(1) O(n) - why? UNION(,y) a=find() b=find(y) p[a]=b // or p[b]=a O(n) d e f O(n) time per operation a b c g h ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 3

Union-Find: 1 st approach UNION(,y) is not efficient A tree may become a chain O(n) time per operation Eample a + b b + c c... z a b Can we do better? a b ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 4 a

Union-Find: Union by Rank Make the root of the larger tree the parent of the root of the smaller tree... Union by rank R[]: the rank of element a measure of the size of the tree rooted at R[] is initiated to 0 by MAKE-SET R[] is updated only by UNION if R[a]=R[b] then R[new root]=r[b]+1 In this case R[] is the height of the tree rooted at ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 5

Union-Find: Union by Rank MAKE-SET () p[]= R[]=0 FIND () while <>p[]: =p[] return UNION (,y) d f a=find () b=find (y) a b c if R[a]>R[b]: p[b]=a else: p[a]=b if R[a]=R[b]: R[b]=R[b]+1 e g h ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 6

Union-Find: Union by Rank R[] = height of the tree rooted at MAKE-SET () O(1) FIND () O(R[]) UNION (,y) O(R[]) O(R[]) time per operation Now, the net lemma holds: Lemma: Let size() = # of nodes of S R[].Then, size() (see net slide for the proof) Obviously, n size() and by the lemma size() R[] Hence, n R[], that is R[] log n and therefore: O(log n) time per operation Can we do better? YES, much better!!! ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 7

Lemma: Let size() = # of nodes of S. Then, size() R[] Proof: By induction on # of UNIONs Basis (no UNION): R[]=0, size()=1 : 1 0 = 1 Hypothesis: The lemma holds before a UNION(,y) operation Inductive step: Three cases in UNION(,y) 1) R[]<R[y] : R[ ] = R[y] Union-Find: Union by Rank '] [ ] [ ] [ ] [ ) ( ) ( ') ( R y R y R R y size size size ) R[]>R[y]: R[ ] = R[] 3) R[]=R[y]: R[ ] = R[y]+1 '] [ ] [ ] [ ] [ ) ( ) ( ') ( R R y R R y size size size ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 8 '] [ 1 ] [ ] [ ] [ ] [ ) ( ) ( ) ' ( R y R y R y R R y size size size

Union-Find: Path compression FIND(): traces the path, a 1, a,..., a k, r from to the root r of S AND also makes all the parent pointers of, a 1, a,..., a k point to r directly r r FIND() Two-pass FIND() FIND () if <>p[]: p[]=find(p[]) return p[] ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 9

Union-Find: Path compression Theorem : If union by rank and path compression are used then the amortized compleity per operation is O(log*n) log*n = min{i0 : log (i) n 1} Iterated logarithm for n=1 (= 0 ) : log*n=1 for n= (= 1 ) : log*n=1 for n=3,4 (= ) : log*n= for n=5,6,,16 (= 4 ): log*n=3 for n=17,18,,65536 (= 16 ) : log*n=4 for n=65537,65538,, 65536 (= 64K ): log*n=5 for all practical purposes, log*n 5 ~ O(1) ΑΛΓΟΡΙΘΜΟΙ - ΑΝΟΙΞΗ 016 - Ι. ΜΗΛΗΣ 14 - GRAPHS III - MSTs 30