CS 410/584, Algorithm Design & Analysis: Lecture Notes 3

Similar documents
CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

CS4311 Design and Analysis of Algorithms. Analysis of Union-By-Rank with Path Compression

Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, Union-Find Structures

Divide-and-Conquer Algorithms Part Two

Equivalence Relations. Disjoint Union / Find. Dynamic Equivalence Problem. Equivalence Classes

Disjoint-Set Forests

5. Biconnected Components of A Graph

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Algorithm Design and Analysis

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Notes on Logarithmic Lower Bounds in the Cell Probe Model

Proof of Theorem 1. Tao Lei CSAIL,MIT. Here we give the proofs of Theorem 1 and other necessary lemmas or corollaries.

Equivalence Relations. Equivalence Classes. A new question. Building Equivalence Classes. CSE 326: Data Structures Disjoint Union/Find

CS 161: Design and Analysis of Algorithms

Disjoint Sets : ADT. Disjoint-Set : Find-Set

Directed Graphs (Digraphs) and Graphs

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Ring Sums, Bridges and Fundamental Sets

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Binary Decision Diagrams

Binary Decision Diagrams. Graphs. Boolean Functions

Branch-and-Bound for the Travelling Salesman Problem

Non-context-Free Languages. CS215, Lecture 5 c

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Graphs with few total dominating sets

Lecture 12: Algorithm Analysis

Lecture 4. 1 Estimating the number of connected components and minimum spanning tree

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

On shredders and vertex connectivity augmentation

Equivalence Relation. Equivalence Classes. Building Equivalence Classes. Disjoint Union Find 2016/2/9. Determining equivalence classes

Minimum Spanning Trees

INF2220: algorithms and data structures Series 1

Kartsuba s Algorithm and Linear Time Selection

Turing Machine Variants. Sipser pages

6.889 Lecture 4: Single-Source Shortest Paths

GRAPH ALGORITHMS Week 3 (16-21 October 2017)

Fundamental Algorithms 11

1 Terminology and setup

Using preprocessing to speed up Brandes betweenness centrality algorithm

/463 Algorithms - Fall 2013 Solution to Assignment 4

Computational Complexity

Data Structures for Disjoint Sets

Even More on Dynamic Programming

CS 70 Discrete Mathematics and Probability Theory Fall 2016 Seshia and Walrand Midterm 1 Solutions

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Tree-width and algorithms

Equivalence of DFAs and NFAs

Randomized Algorithms III Min Cut

U.C. Berkeley Better-than-Worst-Case Analysis Handout 3 Luca Trevisan May 24, 2018

Chapter 11. Min Cut Min Cut Problem Definition Some Definitions. By Sariel Har-Peled, December 10, Version: 1.

Algorithm Design and Analysis

9 Union Find. 9 Union Find. List Implementation. 9 Union Find. Union Find Data Structure P: Maintains a partition of disjoint sets over elements.

Computational Boolean Algebra. Pingqiang Zhou ShanghaiTech University

MATH 22 HAMILTONIAN GRAPHS. Lecture V: 11/18/2003

Queues. Principles of Computer Science II. Basic features of Queues

Lecture 1 : Data Compression and Entropy

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Computational Models Lecture 11, Spring 2009

Dynamic Programming on Trees. Example: Independent Set on T = (V, E) rooted at r V.

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

V. Adamchik 1. Recurrences. Victor Adamchik Fall of 2005

Dynamic Programming: Shortest Paths and DFA to Reg Exps

Lecture 18: P & NP. Revised, May 1, CLRS, pp

The null-pointers in a binary search tree are replaced by pointers to special null-vertices, that do not carry any object-data

Computer Science & Engineering 423/823 Design and Analysis of Algorithms

Cographs; chordal graphs and tree decompositions

Salembier s Min-tree algorithm turned into breadth first search

Lecture 11: Generalized Lovász Local Lemma. Lovász Local Lemma

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

EXHAUSTIVELY generating a class of combinatorial objects

Graph Search Howie Choset

Circuits. CSE 373 Data Structures

Topics in Theoretical Computer Science April 08, Lecture 8

arxiv: v1 [math.co] 10 Sep 2010

Optimal Tree-decomposition Balancing and Reachability on Low Treewidth Graphs

CS781 Lecture 3 January 27, 2011

k-distinct In- and Out-Branchings in Digraphs

Definition Existence CG vs Potential Games. Congestion Games. Algorithmic Game Theory

Current; Forest Tree Theorem; Potential Functions and their Bounds

Scheduling and Optimization Course (MPRI)

8. INTRACTABILITY I. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley. Last updated on 2/6/18 2:16 AM

What is SSA? each assignment to a variable is given a unique name all of the uses reached by that assignment are renamed

Question Paper Code :

Lecture 6: September 22

b + O(n d ) where a 1, b > 1, then O(n d log n) if a = b d d ) if a < b d O(n log b a ) if a > b d

Ngày 20 tháng 7 năm Discrete Optimization Graphs

Lecture 24: April 12

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

The Tutte polynomial of a graph, depth-first search, and simplicial complex partitions

Lecture 7: Dynamic Programming I: Optimal BSTs

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

CS Data Structures and Algorithm Analysis

arxiv: v1 [cs.ds] 1 Nov 2018

MA008/MIIZ01 Design and Analysis of Algorithms Lecture Notes 2

Binary Decision Diagrams

Algorithm Design and Analysis

CMPSCI 611 Advanced Algorithms Midterm Exam Fall 2015

Transcription:

CS 410/584, : Lecture Notes 3 Amortized Cost Not an algorithm design mechanism per se For analyzing cost of algorithms - - Example: Exercise 17.3-6 Implement a queue with two stacks Why? 1 enqueue(x, Q) Queue Operations dequeue(q) return( ) pop(so) Q SI G F E D SO A B C 2 1

CS 410/584, : Lecture Notes 3 A Complication dequeue(q) if empty(so) then while not empty(si) do return(top(so)) pop(so) SI R Q P SO 3 Complexity operation enqueue is always in a sequence of n queue ops, one dequeue could be as bad as but total cost of sequence isn t so bad as Consider: each element has $4 -pay $1 for -pay $2 for -pay $1 for 4 2

CS 410/584, : Lecture Notes 3 Assumptions 1. 2. 3. Union-Find 4. Naming by an element is not a limitation Usually want to know if Critical Assumption: the number of ops is proportional to 5 First Hack Array R[1..n] R[i] name of set Initially, R[i] = Union(i,j): Scan R. For every element k with R[k] = i, set i 1 2 3 4 5 R[i] 1 2 3 4 5 Each union costs 6 3

CS 410/584, : Lecture Notes 3 Improving Efficiency More efficiency - find just the elements of the set - always choose R[i] as before If j is a name, then LIST[j] points to and SIZE[j] = Union(i,j) {assume i=, j= } 1. Assume SIZE[i] SIZE[j] 2. Traverse LIST[i] and 3. Add LIST[i] to 4. Adjust SIZE[j] 7 Example i 1 2 3 4 5 6 7 R[i] 4 2 4 4 2 6 7 SIZE 0 2 0 3 0 1 1 LIST 5 1 6 7 2 3 Union(2,4) 4 8 4

CS 410/584, : Lecture Notes 3 Time Complexity Theorem: Can execute n-1 Union operations in O(n log n) time. -Time for Union is proportional to - Charge the cost to the elements moved when they go - How many times may a single element be moved? 9 Tree-Structured Union-Find Data structure: forest of trees, one per Almost linear time for n ops 7 8 6 3 2 4 1 9 5 Find(i) - Union(i,j) - 10 5

CS 410/584, : Lecture Notes 3 Can get bad trees Keep trees somewhat balanced: always merge Balancing 5 3 Lemma: If for every Union, the smaller tree is made a subtree of the larger, then any tree of height h has at least 2 h nodes. Proof: If h = 0, then the tree must be 4 1 2 11 Proof, Continued If h > 0, choose height h tree formed by Union s that has j i Look at last tree added, must have height Before the addition, T j has height 12 6

CS 410/584, : Lecture Notes 3 Path Compression Further improvement on Find time 1 1 a b 5 a b 5 3 2 4 3 h h c g d e f c 2 g 4 d e f 13 Complexity Need funny functions to prove complexity F(0) = F(i) = i 0 1 2 3 4 5 F(i) G is kind of an inverse G(n) = the least k such that In this universe, G(n) practical n for all 14 7

CS 410/584, : Lecture Notes 3 Complexity 2 Show that a sequence of c n Union/Find s can be done in Assume: Union takes Find takes units proportional to Definition: The u-height (union height) of an element relative to 1. Let be with 2. Construct a forest using 3. u-height(v) is height of v in 15 Complexity 3 Lemma: There are at most Proof: Node of u-height = r has at least descendents in forest F. If u-height(v) = u-height(w), sets of descendents are There are at most distinct sets of 2 r elements, so Corollary: 16 8

CS 410/584, : Lecture Notes 3 Complexity 4 Lemma: If during execution of (not ), w is a proper descendent of v, then Proof: w a proper descendent of v under implies Put nodes into groups by u-height u-height = r goes in u-height 0-1 2 3-4 5-16 17-65536 65537-2 65536 group 17 Complexity 5 How to count cost of Find s in c n operations. Split cost of Find between 1. 2. certain nodes In the end have sum over + sum over Suppose Find(i) traverses k nodes - How to split up k units? 18 9

CS 410/584, : Lecture Notes 3 Complexity 6 w Consider each node v on path 1. If v is root w is root v,w in different groups 2. If v,w in same group and w not the root, v i 19 Complexity 7 Along any upward path, u-heights increase At most different groups so case 1 applies to at most So a Find operation gets charged at most 20 10

CS 410/584, : Lecture Notes 3 Complexity 8 What happens when case 2 applies? v gets a new parent x and u-height(x) u-height(w) How many times can v get a new parent before its parent is in a higher group, and case 1 applies? At most 21 Complexity 9 Charges to nodes: (max nodes in )(max moves for ) F(g) N(g) r=f(g-1)+1 max moves for a node in a group 22 11

CS 410/584, : Lecture Notes 3 Graphs An undirected graph G is a pair (N, E) N E Example N = {a, b, c, d} E = {{a,b},{b,c},{a,c},{b,d},{c,d}} 23 Paths A path in graph G = (N,E) A sequence n 1, n 2,..., n k such that k 2 of nodes a b c d A path is simple if A path is a cycle if and k 3 24 12

CS 410/584, : Lecture Notes 3 Directed Graphs In a directed graph (digraph) E is Example E = {(a,b),(b,c),(a,c),(b,d), (c,d),(d,c)} a c b d 25 Representation Time and space complexity depends on how we capture the graph. Edge list {a,b}, {b,c}, {a,c}, {b,d}, {c,d} Adjacency List a b c d 26 13

CS 410/584, : Lecture Notes 3 Representation 2 Adjacency matrix a b c d a 1 b 1 c 1 d 0 27 Depth-First Search Undirected Graphs A means to visit all nodes of an undirected graph at node x pick edge {x,y} if y visited, pick another edge else if no more edges, we re done 28 14

CS 410/584, : Lecture Notes 3 1 2 3 7 Example DFS 8 4 5 6 29 DFS Algorithm DFS(v) mark v for each edge {v,w} do if w not marked then saved edges form the depth-first spanning tree T - B - DFS number DFNum[v] 30 15

CS 410/584, : Lecture Notes 3 Time Complexity Theorem: DFS requires O(max(n,e)) steps on a graph with n nodes and e edges (given as an adjacency list). Time spent looking for unmarked nodes DFS(v) is called once for each node. time spent, apart from recursive calls, is (#nodes ( adjacent to v) v N 31 Back Edges If {v,w} B, then v is an ancestor of w in the spanning forest (or vice versa). If v an ancestor of w in DFS tree, then DFNum[v] DFNum[w]. 32 16