CSSE 230 Day 25 Skip Lists.

Slides:



Advertisements
Similar presentations
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Advertisements

Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
HISTORY The problem was originally proposed in 1848 by the chess player Max Bezzel, and over the years, many mathematicians, including Gauss have worked.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
§5 Backtracking Algorithms A sure-fire way to find the answer to a problem is to make a list of all candidate answers, examine each, and following the.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Backtracking & Brute Force Optimization Intro2CS – weeks
AVL Balanced Trees Introduction Data structure AVL tree balance condition AVL node level AVL rotations Choosing the rotation Performing a balanced insertion.
CSG3F3/ Desain dan Analisis Algoritma
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Game playing Types of games Deterministic vs. chance
Instructor: Lilian de Greef Quarter: Summer 2017
Problem Solving: Brute Force Approaches
MA/CSSE 473 Day 20 Finish Josephus
Backtracking, Search, Heuristics
Recursion Topic 5.
AVL Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSE373: Data Structure & Algorithms Lecture 23: More Sorting and Other Classes of Algorithms Catie Baker Spring 2015.
BackTracking CS255.
Intro to Computer Science II
Divide-and-Conquer 6/30/2018 9:16 AM
AVL Trees 6/25/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M.
CSCI 104 Backtracking Search
Lecture No.43 Data Structures Dr. Sohail Aslam.
Lecture 15 AVL Trees Slides modified from © 2010 Goodrich, Tamassia & by Prof. Naveen Garg’s Lectures.
Binary Search Trees (Continued)
Datastructure.
Lecture 22 Binary Search Trees Chapter 10 of textbook
C ODEBREAKER Class discussion.
Problem Solving by Searching
CSE 143 Lecture 19 More Recursive Backtracking
Chapter 16 Tree Implementations
i206: Lecture 13: Recursion, continued Trees
Using a Stack Chapter 6 introduces the stack data type.
Games with Chance Other Search Algorithms
structures and their relationships." - Linus Torvalds
Data Structures (CS212D) Overview & Review.
AVL Trees 4/29/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Recursion Copyright (c) Pearson All rights reserved.
Problem Solving: Brute Force Approaches
Back Tracking.
Multi-Way Search Trees
Analysis and design of algorithm
Using a Stack Chapter 6 introduces the stack data type.
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.
Data Structures: Introductory lecture
Chapter 6: Transform and Conquer
adapted from Recursive Backtracking by Mike Scott, UT Austin
Data Structures (CS212D) Overview & Review.
The N-queens problem Team: 404 brain not found
Using a Stack Chapter 6 introduces the stack data type.
Haskell Tips You can turn any function that takes two inputs into an infix operator: mod 7 3 is the same as 7 `mod` 3 takeWhile returns all initial.
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2001 Bin Sort, Radix.
Using a Stack Chapter 6 introduces the stack data type.
Copyright © Aiman Hanna All rights reserved
Backtracking and Branch-and-Bound
The N-Queens Problem Search The N-Queens Problem Most slides from Milos Hauskrecht.
1 Lecture 13 CS2013.
Backtracking, Search, Heuristics
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
structures and their relationships." - Linus Torvalds
Backtracking, Search, Heuristics
Solving Problems by Searching
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CSSE 230 Day 25 Skip Lists

Reminders/Announcements

Skip Lists An alternative to balanced trees Sorted data. Random. Expected times are O(log n).

An alternative to balanced trees Indexed lists One-level index. 2nd-level index. 3rd-level index log-n-level index. Problem: insertion and deletion. Solution: Randomized node height: Skip lists. Pugh, 1990 CACM. https://people.ok.ubc.ca/ylucet/DS/SkipList.html Remember the problem with keeping trees completely balanced”? Bottom bullet used to say: Notice that skip lists do not share with binary trees the problem that threads are designed to solve. This is a real tongue-twister. Also, the old applet disappeared from the Univ of Bern: http://iamwww.unibe.ch/~wenger/DA/SkipList/. Replced with one above. Note that we can iterate through the list easily and in increasing order, like a threaded BST”

A slightly different skip list representation Uses a bit more space, makes the code simpler. Michael Goodrich and Roberto Tamassia.

Methods in SkipListNode class

Search algorithm

Insertion diagram

Insertion algorithm

Remove algorithm

(sort of) Analysis of Skip Lists No guarantees that we won't get O(N) behavior. The interaction of the random number generator and the order in which things are inserted/deleted could lead to a long chain of nodes with the same height. But this is very unlikely. Expected time for search, insert, and remove are O(log n).

Questions

Exhaustive Search and Backtracking A taste of artificial intelligence

Exhaustive search Given: a large set of possible solutions to a problem Goal: Find all solutions (or an optimal solution) from that set Questions we ask: How do we represent the possible solutions? How do we organize the search? Can we avoid checking some obvious non- solutions? Examples: Mazes The “15” puzzle The “search space” http://www.numeracysoftware.com/maze4.bmp

In backtracking, we always try to extend a partial solution dead end ? dead end dead end ? start ? ? dead end dead end How do we represent the possible solutions? series of moves on the call stack How do we organize the search? depth-first, recursive calls Can we eliminate subsets of the possible solution set without checking each one? not in this case http://www.cis.upenn.edu/~matuszek/cit594-2004/Lectures/38-backtracking.ppt ? success! Note: the search is a tree and we explore it using a pre-order traversal

In backtracking, we always try to extend a partial solution How do we represent the possible solutions? series of moves on the call stack How do we organize the search? depth-first, recursive calls Can we eliminate subsets of the possible solution set without checking each one? not in this case

The non-attacking chess queens problem is a famous example In how many ways can N chess queens be placed on an NxN grid, so that none of the queens can attack any other queen? I.e. there are not two queens on the same row, same column, or same diagonal. There is no "formula" for generating a solution. The famous computer scientist Niklaus Wirth described his approach to the problem in 1971: Program Development by Stepwise Refinement http://sunnyday.mit.edu/16.355/wirth- refinement.html#3 Ask who needs a refresher on the moves the queens can make. Draw 4x4 solution on board. http://en.wikipedia.org/wiki/Queen_(chess)

With a partner, discuss "possible solution" search strategies In how many ways can N chess queens be placed on an NxN grid, so that none of the queens can attack any other queen? I.e. no two queens on the same row, same column, or same diagonal. In the next few slides we explore various possibilities for the search space and count the number of potential solutions that must be tried in each case. Two minutes No Peeking!

Search Space Possibilities 1/5 Very naive approach. Perhaps stupid is a better word! There are N queens, N2 squares. For each queen, try every possible square, allowing the possibility of multiple queens in the same square. Represent each potential solution as an N-item array of pairs of integers (a row and a column for each queen). Generate all such arrays (you should be able to write code that would do this) and check to see which ones are solutions. Number of possibilities to try in the NxN case: Specific number for N=8: (n^2)^n = n^(2n) Number on slide = 64^8 281,474,976,710,656

Search Space Possibilities 2/5 Slight improvement. There are N queens, N2 squares. For each queen, try every possible square, notice that we can't have multiple queens on the same square. Represent each potential solution as an N-item array of pairs of integers (a row and a column for each queen). Generate all such arrays and check to see which ones are solutions. Number of possibilities to try in NxN case: Specific number for N=8: Number on slide = 64! / 56! = 64x63x62x61x60x59x58x57 178,462,987,637,760 (vs. 281,474,976,710,656)

Search Space Possibilities 3/5 Slightly better approach. There are N queens, N columns. If two queens are in the same column, they will attack each other. Thus there must be exactly one queen per column. Represent a potential solution as an N-item array of integers. Each array position represents the queen in one column. The number stored in an array position represents the row of that column's queen. Show array for 4x4 solution. Generate all such arrays and check to see which ones are solutions. Number of possibilities to try in NxN case: Specific number for N=8: This is 8^8 16,777,216

Search Space Possibilities 4/5 Still better approach There must also be exactly one queen per row. Represent the data just as before, but notice that the data in the array is a set! Generate each of these and check to see which ones are solutions. How to generate? A good thing to think about. Number of possibilities to try in NxN case: Specific number for N=8: This is 8! 40,320

Search Space Possibilities 5/5 Backtracking solution Instead of generating all permutations of N queens and checking to see if each is a solution, we generate "partial placements" by placing one queen at a time on the board Once we have successfully placed k<N queens, we try to extend the partial solution by placing a queen in the next column. When we extend to N queens, we have a solution. Demonstrate the process for the 8x8 case using the applet whose link is on the next slide.

Experimenting with 8 x 8 Case Play the game: http://homepage.tinet.ie/~pdpals/8queens.htm See the solutions: http://www.dcs.ed.ac.uk/home/mlj/demos/queens (if you can figure out how to enable Java in your browser)

Program output: >java RealQueen 5 SOLUTION: 1 3 5 2 4 Tommorrow: We'll look at details of the algorithm. Bring your computer, capabl;e of compiling and running Java programs.