720 likes | 851 Views
Chapter 28 Supplement. Skip Lists (1). A skip list is a simple randomized data structure that can be used to implement a Map (key, value entries) Search and update times are O(log n) on average where n is the number of entries
E N D
Skip Lists (1) • A skip list is a simple randomized data structure that can be used to implement a Map (key, value entries) • Search and update times are O(log n) on average where n is the number of entries • Used extensively in computer games, cryptography, and computer simulations • Randomization in data structures and algorithm design usually produce simple and efficient results
Skip List (2) • A skip listis a based on multiple parallel, sorted linked lists • Underlying the skip list is an augmentation of an ordered linked lists with additional forward links, added in a randomized way so that a search in the list may quickly skip parts of the list (hence the name)
Skip List (3) • Built in layers • The bottom layer is an ordinary ordered linked list • Each higher layer acts as an “express lane” for the lists below, where an element in layer i appears in layer i+1 with some fixed probability p • Two commonly-used values for p are 1/2 or ¼ • On average, each element appears in 1/(1-p) lists • Pseudo random number generators are also used
Skip List (4) • A search for a target element begins at the head element in the top list, and proceeds horizontally until the current element is greater than or equal to the target • If the current element is equal to the target, it has been found • If the current element is greater than the target, the procedure is repeated after returning to the previous element and dropping down vertically to the next lower list
Skip List (5) • The expected number of steps in each linked list is 1/p, which can be seen by tracing the search path backwards from the target until reaching an element that appears in the next higher list
Skip List Structure • A skip list S consists of a series of lists S0, S1 , … , Sh such that • Each list stores a subset of the entries of D sorted by a non-decreasing key plus entries • Each list Si contains the special keys + and - • List S0 contains every entry of D plus + and - • Each list is a subsequence of the previous one, i.e.,S0 S1 … Sh • List Shcontains only the two special keys • h is referred to as the height of the skip list
- + - 12 23 26 31 34 44 56 64 78 + Example of a Skip List S3 S2 - 31 + S1 - 23 31 34 64 + S0
Example of a Skip List • The entries in Si+1 are chosen randomly from the entries in Si with probability ½ • One would expect the number of entries in S1 to have n/2 entries, S2 to have n/4 entries, and Si to have n/2i entries • Height should be around log n
Example: search for 78 - + Search using a Skip List S3 S2 - 31 + S1 - 23 31 34 64 + S0 - 12 23 26 31 34 44 56 64 78 +
A randomized algorithm performs coin tosses (i.e., uses random bits) to control its execution It contains statements of the type b random() if b= 0 do A … else { b= 1} do B … Its running time depends on the outcomes of the coin tosses One analyzes the expected running time of a randomized algorithm under the following assumptions the coins are unbiased, and the coin tosses are independent The worst-case running time of a randomized algorithm is often large but has very low probability (e.g., it occurs when all the coin tosses give “heads”) Randomized Algorithms
Insertion • To insert an entry (k, v) into a skip list, we use a randomized algorithm: • Determine the position on the bottom-level entry with the largest key less than or equal to k • We insert (k,v) immediately after p • Flip a coin • If tails, we algorithm stops • If heads, we insert (k,v) in the next level up in the appropriate position • Repeat this step until the Sh-1 level • Coin tosses can be simulated using java.util.Random by calling nextInt(2) • Expected running time isO(log n)
- + - 15 + - 15 23 + - 10 23 36 + 15 Insertion • Example: insert key 15, with i= 2 S3 p2 S2 S2 - + p1 S1 S1 - + 23 p0 S0 S0 - 10 23 36 +
Deletion • To remove an entry with key k from a skip list, we proceed as follows: • If the position p stores an entry with a key different than k, we return null • Otherwise, remove P and all the positions above • Expected running time is O(log n)
Deletion • Example: remove key 34 S3 - + p2 S2 S2 - + - 34 + p1 S1 S1 - + - 23 34 + 23 p0 S0 S0 - 12 23 45 + - 12 23 45 + 34
Search and Update Times (1) • The search time in a skip list is proportional to • the number of drop-down steps, plus • the number of scan-forward steps • The drop-down steps are bounded by the height of the skip list and thus are O(log n) with high probability • To analyze the scan-forward steps, we use yet another probabilistic fact: • The expected number of coin tosses required in order to get tails is 2
Search and Update Times (2) • When we scan forward in a list, the destination key does not belong to a higher list • A scan-forward step is associated with a former coin toss that gave tails • In each list the expected number of scan-forward steps is 2 • Thus, the expected number of scan-forward steps is O(log n) • We conclude that a search in a skip list takes O(log n) expected time • The analysis of insertion and deletion gives similar results
Kruskal’s Algorithm (1) • Use to build minimum spanning trees by using clusters • Initially each vertex is in own cluster all by itself • The algorithm then considers each edge in turn, ordered by increasing weight • If an edge e connects two different clusters, then e is added to the set of edges of the minimum spanning tree and the two clusters connected by e are merged into a single cluster
Kruskal’s Algorithm • If e connects to vertices that are already in the same cluster, the e is discarded • Once the algorithm has added enough edges to from a spanning tree, the process terminates
Kruskal Example 2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 946 DFW LAX 1235 1121 MIA 2342
Example Kruskal Example
2704 BOS 867 849 PVD ORD 187 740 144 JFK 1846 621 1258 184 802 SFO BWI 1391 1464 337 1090 DFW 946 1235 LAX 1121 MIA 2342 Kruskal Example
E D C B A Digraphs • A digraph is a graph whose edges are all directed • Short for “directed graph” • A graph G=(V,E) such that • Each edge goes in one direction: • Edge (a,b) goes from a to b, but not b to a • Applications • one-way streets • flights • task scheduling
Digraph Application • Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151
Directed DFS • One can specialize the traversal algorithms (DFS and BFS) to digraphs by traversing edges only along their direction E D C B A
Reachability • A fundamental issue with directed graphs is the notation of reachability • Determines where one can get to in a directed graph • Vertex u reaches vertex v if the graph has a directed path from u to v
Reachability Examples E D C A E D C F E D A B C F A B
a g c d e b f Strong Connectivity • Each vertex can reach all other vertices
Strong Connectivity Algorithm • Pick a vertex v in G • Perform a DFS from v in G • If there’s a w not visited, print “no” • Let G’ be G with edges reversed • Perform a DFS from v in G’ • If there’s a w not visited, print “no” • Else, print “yes”. a G: g c d e b f a g G’: c d e b f
a g c d e b f Strongly Connected Components • Maximal subgraphs such that each vertex can reach all other vertices in the subgraph { a , c , g } { f , d , e , b }
A* Algorithm (improved Dijkstra’s algorithm) http://www.policyalmanac.org/games/aStarTutorial.htm
A* Algorithm Introduction • The Search Algorithm • Let’s assume that we have someone who wants to get from point A to point B • Let’s assume that a wall separates the two points • This is illustrated with green being the starting point A, and red being the ending point B, and the blue filled squares being the wall in between on the next slide
Getting Started • The search area is divided into a square grid • Each item in the array represents one of the squares on the grid, and its status is recorded as walkable or unwalkable • The path is found by figuring out which squares we should take to get from A to B • Once the path is found, a person moves from the center of one square to the center of the next until the target is reached • These center points are called “nodes”
Starting the Search (1) • Begin at the starting point A and add it to an “open list” of squares to be considered • The open list is kind of like a shopping list • Right now there is just one item on the list, but more will be added later • It contains squares that might fall along the path that one may want to take, but maybe not • Basically, this is a list of squares that need to be checked out
Starting the Search (2) • Look at all the reachable or walkable squares adjacent to the starting point, ignoring squares with walls, water, or other illegal terrain • Add them to the open list, too • For each of these squares, save point A as its “parent square” • This parent square concept is important when one wants to trace the path • Drop the starting square A from the open list, and add it to a “closed list” of squares that you don’t need to look at again for now
The Search Area (2) The dark green square in the center is the starting square It is outlined in light blue to indicate that the square has been added to the closed list All of the adjacent squares are now on the open list of squares to be checked, and they are outlined in light green Each has a gray pointer that points back to its parent, which is the starting square
Path Scoring • The key to determining which squares to use when figuring out the path is the following equation: F = G + H • G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there • H = the estimated movement cost to move from that given square on the grid to the final destination, point B • This is often referred to as the heuristic (guess) • One really don’t know the actual distance until one finds the path, because all sorts of things can be in the way (walls, water, etc.)