week 6
play

Week 6 Oliver Kullmann Introduction Operations Data Structures - PowerPoint PPT Presentation

CS 270 Algorithms Week 6 Oliver Kullmann Introduction Operations Data Structures for Disjoint Sets Application: connected components Simple data Introduction 1 structure Advanced Operations data 2 structure Final Application:


  1. CS 270 Algorithms Week 6 Oliver Kullmann Introduction Operations Data Structures for Disjoint Sets Application: connected components Simple data Introduction 1 structure Advanced Operations data 2 structure Final Application: connected components remarks 3 Simple data structure 4 Advanced data structure 5 Final remarks 6

  2. CS 270 General remarks Algorithms Oliver Kullmann Introduction Operations Application: connected We consider our last example for datastructures, supporting components disjoint sets . Simple data structure Again we learn how to use and how to build them. Advanced data structure Reading from CLRS for week 6 Final remarks 1 Chapter 21 (not Section 21.4).

  3. CS 270 Sets again Algorithms Oliver Last week we have implemented dynamic sets using binary Kullmann search trees . Introduction Operations The essence of dynamic sets is that we have just one set, Application: which is growing and shrinking, and where we want to connected components check elementship. Simple data structure Additionally we want also to determine extreme elements Advanced (minimum and maximum), and get from one element to data structure the next resp. previous one. Final remarks Now we have several sets, and the basic operations are determining for an object in which of the sets it is 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.

  4. CS 270 The problem Algorithms Oliver Kullmann Introduction Operations Maintaining a collection Application: connected components S = { S 1 , S 2 , . . . , S k } Simple data structure of disjoint sets. Advanced data structure Each set S i is represented by an element x ∈ S i . Final remarks The collection can change over time; thus these represent dynamic sets. They are implemented by disjoint-set data structures .

  5. CS 270 Basic operations Algorithms Oliver Kullmann Introduction Make-Set ( x ) creates a new set whose only element is x . Its Operations representative is of course x . Application: connected ( Assumption : x does not already appear in any of the components existing sets.) Simple data structure Union ( x , y ) combines the set S x containing x and the set S y Advanced data containing y , forming a single new set S . structure The representative of this new set S is usually chosen to be Final remarks either the representative of S x or the representative of S y . ( Side effect : S x , S y no longer exist by themselves.) Find-Set ( x ) returns (a pointer to) the representative of the set containing x .

  6. CS 270 Which representatives? Algorithms Oliver Kullmann Is it possible that Find-Set ( x ) on one occasion returns z , and Introduction on another occasion a different z ′ ? Operations Application: No — it is guaranteed that representatives stay the same if connected components the sets concerned are not touched. Simple data structure Thus as long as no Union -operations are performed, the Advanced return-value of Find-Set are stable. data structure And furthermore Union ( x ′ , y ′ )-operations only affect the Final remarks return-values of calls for x in either the old S x ′ or S y ′ . Often actually the precise return-value of Find-Set ( x ) is not of relevance, but it is only used to determine whether two different x , x ′ are in the same set — this is the case if and only if Find-Set ( x ) == Find-Set ( x ′ ) holds.

  7. CS 270 Elements versus pointers Algorithms One further important clarification is needed: Oliver Kullmann Disjoint-sets data structures are not designed for searching! Introduction Operations So the inputs for Union ( x , y ) and Find-Set ( x ) are in fact Application: connected components not the elements themselves, but Simple data pointers (“iterators”) into the data structure . structure Advanced data Thus we don’t need to search for x and y in the data structure, structure Final but the input is already their place in it. However, how to obtain remarks 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 using Union ( x , y ) or Find-Set ( x ).

  8. CS 270 Review of graphs Algorithms Oliver We have already seen “graphs”. Here now we consider a nice Kullmann application to graphs. Introduction Operations A graph consists of vertices (arbitrary objects), and edges . Application: An edge in an undirected graph (the default, and just called connected components graph ) connects two vertices v , w . Simple data structure As a mathematical object an edge is just a 2-element set Advanced { v , w } (note that sets have no order, and thus the edge is data structure undirected). Final remarks For example the following is a graph with 8 vertices and 6 edges: • • • • • ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ ⑦ • • • This graph has three connected components .

  9. CS 270 Connected components Algorithms A natural application of disjoint-set data structures is for Oliver Kullmann computing the connected components of a graph. Introduction Input: An undirected graph G . Operations Output: The connected components of G . Application: connected components Connected-Components ( G ) Simple data structure 1 for each vertex v of G Make-Set ( v ); Advanced 2 for each edge { u , v } of G data structure 3 if ( Find-Set ( u ) � = Find-Set ( v )) Union ( u , v ); Final 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 ;

  10. CS 270 Connected components illustrated Algorithms Oliver Kullmann Introduction Operations Application: connected components Simple data structure Advanced data structure Final remarks

  11. CS 270 Connected components via DFS Algorithms Oliver Kullmann Also via BFS and DFS we can determine the connected components of a graph: Introduction Operations For DFS we need in the outer loop (running through all Application: connected vertices u of G ) a counter, call it ccc (for “connected components component counter”), initialised with 0, and incremented Simple data structure with every call of the recursive procedure DFS-visit . Advanced data In that way we can count the number of connected structure components. Final remarks 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 | V | (the number of vertices), and before calling DFS-visit(u) we set cc[u] to ccc . This is a linear-time algorithm (linear in the number of vertices and edges).

  12. CS 270 DFS on the example graph Algorithms Oliver Kullmann Let’s consider the example, using the following order on the vertices (with induced order on the edges): Introduction Operations 1 2 3 4 5 Application: ✉✉✉✉✉✉✉✉✉✉✉ ✉ connected ✉ ✉ ✉ components ✉ ✉ ✉ ✉ ✉ Simple data ✉ ✉ structure 6 7 8 Advanced data Running DFS, for each node we get the following values of structure discovery time, finishing time, and connected component Final remarks number, together with the shown spanning forest: (1 , 8 , 1) (2 , 7 , 1) (9 , 14 , 2) (11 , 12 , 2) (15 , 16 , 3) ①①①①①①①① t t t t t t t t t (3 , 6 , 1) (4 , 5 , 1) (10 , 13 , 2)

  13. CS 270 Connected components via BFS Algorithms Oliver The form of BFS presented in the lecture only explores the Kullmann connected component of the given start-vertex s . For example Introduction using start-vertex 2 on the previous graph, we the the (rooted) Operations BFS-tree 2 Application: ▲ 1 rrrr ▲ connected ▲ ▲ components 6 Simple data structure 7 Advanced (note that this is a spanning tree (only) for the connected data structure component of 2). Final remarks To get all connected components, first we need to add an outer loop which runs through all vertices, and enters BFS for the vertices which haven’t been discovered yet. Then using a connected-component-counter and an array for storing the index of the connected component of a vertex as before, we get the same functionality (regarding connected components) as with DFS.

  14. CS 270 Linked-list representation Algorithms Oliver Idea: Kullmann Introduction Each element is represented by a pointer to a cell. Operations We then use a linked list for each set. Application: connected Each cell has a next pointer to the next cell in the list, as components well as a rep pointer to the representative element at the Simple data structure head of the list. Advanced Each cell also has a last pointer to the last element in the data structure list; however, we shall only expect that this be correctly Final remarks defined for the representative cell. rep x next last

  15. CS 270 Example Algorithms Oliver A linked list representation of the sets Kullmann Introduction { a , f } , { b } , { g , c , e } , { d } . Operations Application: connected components Simple data structure Advanced a data f structure Final remarks b g c e d

Recommend


More recommend