general remarks
play

General remarks Algorithms Algorithms Week 6 Oliver Oliver - PowerPoint PPT Presentation

CS 270 CS 270 General remarks Algorithms Algorithms Week 6 Oliver Oliver Kullmann Kullmann Introduction Introduction Operations Operations Data Structures for Disjoint Sets Application: Application: connected connected We consider


  1. CS 270 CS 270 General remarks Algorithms Algorithms Week 6 Oliver Oliver Kullmann Kullmann Introduction Introduction Operations Operations Data Structures for Disjoint Sets Application: Application: connected connected We consider our last example for datastructures, supporting components components disjoint sets . Simple data Simple data Introduction 1 structure structure Again we learn how to use and how to build them. Advanced Advanced Operations data data 2 structure structure Reading from CLRS for week 6 Final Final Application: connected components remarks remarks 3 1 Chapter 21 (not Section 21.4). Simple data structure 4 Advanced data structure 5 Final remarks 6 CS 270 CS 270 Sets again The problem Algorithms Algorithms Oliver Oliver Last week we have implemented dynamic sets using binary Kullmann Kullmann search trees . Introduction Introduction Operations Operations The essence of dynamic sets is that we have just one set, Maintaining a collection Application: Application: which is growing and shrinking, and where we want to connected connected components components S = { S 1 , S 2 , . . . , S k } check elementship. Simple data Simple data structure structure Additionally we want also to determine extreme elements of disjoint sets. Advanced Advanced (minimum and maximum), and get from one element to data data structure Each set S i is represented by an element x ∈ S i . structure the next resp. previous one. Final Final remarks remarks The collection can change over time; thus these represent Now we have several sets, and the basic operations are dynamic sets. determining for an object in which of the sets it is They are implemented by disjoint-set data structures . computing the union (absorbing the old sets). However this is not done for general set union, but only for disjoint set union — this is an important special case, where we have very fast algorithms.

  2. CS 270 CS 270 Basic operations Which representatives? Algorithms Algorithms Oliver Oliver Kullmann Kullmann Is it possible that Find-Set ( x ) on one occasion returns z , and Introduction Introduction on another occasion a different z ′ ? Make-Set ( x ) creates a new set whose only element is x . Its Operations Operations representative is of course x . Application: Application: connected No — it is guaranteed that representatives stay the same if connected ( Assumption : x does not already appear in any of the components components the sets concerned are not touched. existing sets.) Simple data Simple data structure structure Thus as long as no Union -operations are performed, the Union ( x , y ) combines the set S x containing x and the set S y Advanced Advanced return-value of Find-Set are stable. data data containing y , forming a single new set S . structure structure And furthermore Union ( x ′ , y ′ )-operations only affect the The representative of this new set S is usually chosen to be Final Final remarks remarks return-values of calls for x in either the old S x ′ or S y ′ . either the representative of S x or the representative of S y . ( Side effect : S x , S y no longer exist by themselves.) Often actually the precise return-value of Find-Set ( x ) is not of Find-Set ( x ) returns (a pointer to) the representative of the relevance, but it is only used to determine whether two different set containing x . x , x ′ are in the same set — this is the case if and only if Find-Set ( x ) == Find-Set ( x ′ ) holds. CS 270 CS 270 Elements versus pointers Review of graphs Algorithms Algorithms One further important clarification is needed: Oliver Oliver Kullmann We have already seen “graphs”. Here now we consider a nice Kullmann application to graphs. Disjoint-sets data structures are not designed for searching! Introduction Introduction Operations Operations A graph consists of vertices (arbitrary objects), and edges . So the inputs for Union ( x , y ) and Find-Set ( x ) are in fact Application: Application: An edge in an undirected graph (the default, and just called connected connected components components graph ) connects two vertices v , w . not the elements themselves, but Simple data Simple data pointers (“iterators”) into the data structure . structure structure As a mathematical object an edge is just a 2-element set Advanced Advanced { v , w } (note that sets have no order, and thus the edge is data data Thus we don’t need to search for x and y in the data structure, structure structure undirected). Final Final but the input is already their place in it. However, how to obtain remarks remarks For example the following is a graph with 8 vertices and 6 edges: these “handles” for the elements? Make-Set ( x ) still has as input an element x itself — • • • • • there is no pointer to it yet. ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ So actually Make-Set ( x ) needs to return the pointer ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ • • • (“handle”) to the place (node) in the data structure. This pointer has to be stored, and used instead of x when This graph has three connected components . using Union ( x , y ) or Find-Set ( x ).

  3. CS 270 CS 270 Connected components Connected components illustrated Algorithms Algorithms A natural application of disjoint-set data structures is for Oliver Oliver Kullmann Kullmann computing the connected components of a graph. Introduction Introduction Input: An undirected graph G . Operations Operations Output: The connected components of G . Application: Application: connected connected components components Connected-Components ( G ) Simple data Simple data structure structure 1 for each vertex v of G Make-Set ( v ); Advanced Advanced 2 for each edge { u , v } of G data data structure structure 3 if ( Find-Set ( u ) � = Find-Set ( v )) Union ( u , v ); Final Final remarks remarks After computation of the connected components, we can determine whether two vertices u , v are in the same component (that is, are connected by some path) or not: Same-Component ( u , v ) 1 if ( Find-Set ( u ) == Find-Set ( v )) return true ; 2 else return false ; CS 270 CS 270 Connected components via DFS DFS on the example graph Algorithms Algorithms Oliver Oliver Kullmann Kullmann Let’s consider the example, using the following order on the Also via BFS and DFS we can determine the connected vertices (with induced order on the edges): components of a graph: Introduction Introduction Operations Operations For DFS we need in the outer loop (running through all 1 2 3 4 5 Application: Application: ✉✉✉✉✉✉✉✉✉✉✉ ✉ connected connected ✉ vertices u of G ) a counter, call it ccc (for “connected ✉ components ✉ components ✉ ✉ ✉ component counter”), initialised with 0, and incremented ✉ Simple data ✉ Simple data ✉ ✉ structure 6 7 8 structure with every call of the recursive procedure DFS-visit . Advanced Advanced data data In that way we can count the number of connected Running DFS, for each node we get the following values of structure structure components. discovery time, finishing time, and connected component Final Final remarks remarks number, together with the shown spanning forest: And if we want to know for a vertex in which component it is in, then we need another array cc of integers with length (1 , 8 , 1) (2 , 7 , 1) (9 , 14 , 2) (11 , 12 , 2) (15 , 16 , 3) | V | (the number of vertices), and before calling t ①①①①①①①① t DFS-visit(u) we set cc[u] to ccc . t t t t t t t This is a linear-time algorithm (linear in the number of vertices (3 , 6 , 1) (4 , 5 , 1) (10 , 13 , 2) and edges).

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend