Download presentation
Presentation is loading. Please wait.
1
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform operations on this set –Queries –Modifying operations We must choose a data structure to implement the dynamic set efficiently The “correct” data structure to choose is based on –Which operations need to be supported –How frequently each operation will be executed
2
Some Example Operations Search(S,k) Insert(S,x) Delete(S,x) Minimum or Maximum(S) Successor or Predecessor (S,x) Merge(S 1,S 2 )
3
Basic Data Structures/Containers Unsorted Arrays Sorted Array Unsorted linked list Sorted linked list Stack Queue Heap
4
Implementation Questions How can I implement a queue with two stacks? –Running time of enqueue? –Dequeue? How can I implement two stacks in one array A[1..n] so that neither stack overflows unless the total number of elements in both stacks exceeds n?
5
Unsorted Array Sorted Array Unsorted LL Sorted LL Heap Search Insert Delete Max/Min Pred/Succ Merge
6
Case Study: Dictionary Search(S,k) Insert(S,x) Delete(S,x) Is any one of the data structures listed so far always the best for implementing a dictionary? Under what conditions, if any, would each be best? What other standard data structure is often used for a dictionary?
7
Case Study: Priority Queue Insert(S,x) Max(S) Delete-max(S) Is any one of the data structures listed so far always the best for implementing a priority queue? Under what conditions, if any, would each be best?
8
Case Study: Minimum Spanning Trees Input –Weighted, connected undirected graph G=(V,E) Weight (length) function w on each edge e in E Task –Compute a spanning tree of G of minimum total weight Spanning tree –If there are n nodes in G, a spanning tree consists of n-1 edges such that no cycles are formed
9
Prim’s algorithm A greedy approach to edge selection –Initialize connected component N to be any node v –Select the minimum weight edge connecting N to V-N –Dynamic set implementation The nodes in V-N Value of a node is how close it is to any node in N To select minimum weight edge, choose node v with minimum value in dynamic set (Extract minimum) and add v to N When v is added to N, we need to update the value of the neighbors of v in V-N (Decrease key)
10
Maintain dynamic set of nodes in V-N If we started with node D, N is now {C,D} Dynamic set values of other nodes: –A, E, F: infinity –B: 4 –G: 6 Illustration ABC D EFG 1 2 2 3 4 5 5 6 10
11
Node B is added to N; edge (B,C) is added to T Need to update dynamic set values of A, E, F –Decrease-key operation Dynamic set values of other nodes: –A: 1 –E: 2 –F: 5 –G: 6 Updating Dynamic Set ABC D EFG 1 2 2 3 4 5 5 6 10
12
Node A is added to N; edge (A,B) is added to T Need to update dynamic set values of E –Decrease-key operation Dynamic set values of other nodes: –E: 2 (unchanged because 2 is smaller than 3) –F: 5 –G: 6 Updating Dynamic Set ABC D EFG 1 2 2 3 4 5 5 6 10
13
Dynamic Set Analysis How many objects in initial dynamic set representation of V-N? Time to build this dynamic set? How many extract-min operations need to happen? How many decrease-key operations may occur? Given all of the above, choose a data structure and tell me the implementation cost.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.