Fall 2015, Kevin Quinn CSE373: Data Structures & Algorithms Lecture 25: Problem Solving CSE373: Data Structures and algorithms1.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science 2 Lecture 7: Extended binary trees
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Michael Alves, Patrick Dugan, Robert Daniels, Carlos Vicuna
The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Skip List & Hashing CSE, POSTECH.
Hashing as a Dictionary Implementation
CSE 326: Data Structures Lecture #4 Alon Halevy Spring Quarter 2001.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
CSE332: Data Abstractions Lecture 12: Introduction to Sorting Tyler Robison Summer
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hash Tables1 Part E Hash Tables  
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Tirgul 7. Find an efficient implementation of a dynamic collection of elements with unique keys Supported Operations: Insert, Search and Delete. The keys.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Hashing General idea: Get a large array
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Hashing The Magic Container. Interface Main methods: –Void Put(Object) –Object Get(Object) … returns null if not i –… Remove(Object) Goal: methods are.
COSC 2007 Data Structures II
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CS 221 Analysis of Algorithms Data Structures Dictionaries, Hash Tables, Ordered Dictionary and Binary Search Trees.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Hashing CS 105. Hashing Slide 2 Hashing - Introduction In a dictionary, if it can be arranged such that the key is also the index to the array that stores.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
Comp 335 File Structures Hashing.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
Sets, Maps and Hash Tables. RHS – SOC 2 Sets We have learned that different data struc- tures have different advantages – and drawbacks Choosing the proper.
Storage and Retrieval Structures by Ron Peterson.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
Hashing Hashing is another method for sorting and searching data.
Hashing – Part I CS 367 – Introduction to Data Structures.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
The ADT Table The ADT table, or dictionary Uses a search key to identify its items Its items are records that contain several pieces of data 2 Figure.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
A Introduction to Computing II Lecture 11: Hashtables Fall Session 2000.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CompSci 100e 8.1 Scoreboard l What else might we want to do with a data structure? AlgorithmInsertionDeletionSearch Unsorted Vector/array Sorted vector/array.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures Arrays and Lists Part 2 More List Operations.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CSC 143T 1 CSC 143 Highlights of Tables and Hashing [Chapter 11 p (Tables)] [Chapter 12 p (Hashing)]
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
C++ Memory Management – Homework Exercises
CSE 373: Data Structures & Algorithms Interviews and Problem Solving
Announcements HW5 due tonight Review session in class on Wednesday
Hashing CSE 2011 Winter July 2018.
Data Abstraction & Problem Solving with C++
Cse 373 April 24th – Hashing.
Recitation Outline C++ STL associative containers Examples
Data Structures and Algorithms
Presentation transcript:

Fall 2015, Kevin Quinn CSE373: Data Structures & Algorithms Lecture 25: Problem Solving CSE373: Data Structures and algorithms1

Tools at our Disposal Over the past 8 weeks we have developed a broad knowledge of data structures and algorithms (I hope): – Stacks and Queues Various implementation (Stack/List) – Trees Balanced (AVL), BST – Graphs Directed/Undirected, Acyclic/Cyclic, Weighted Dijkstra’s, BFS, DFS Topological Sort – PriorityQueues BinaryHeap – Dictionaries HashMap, TreeMap – Union Find Uptrees CSE373: Data Structures and algorithms2

Everything is a Trade-off Very rarely is there a “perfect” solution in the real world. – Often must prioritize things like: space vs. time simplicity vs. robustness Understanding the ins and outs of each structure allows you to make informed design decisions that balance these trade-offs. CSE373: Data Structures and algorithms3

Reinventing the Wheel More often than not, the problem you are trying to solve is not entirely unique – Usually it is possible to simplify a problem down to a few core principles Important operations Space/time constraints Once you have found an appropriate analog, allow the well-thought out design to assist you – Example: AVL trees handle balancing for you – Example: Hash tables will handle collisions for you CSE373: Data Structures and algorithms4

Don’t let the Abstract rule you! In this class, we have lived and died by the asymptotic runtime, however this is not always the case – Sometimes simple and readable code is more important – Sometimes you know very distinct things about your input Sorting input that is almost entirely sorted Dictionary of elements that have nearly identical keys CSE373: Data Structures and algorithms5

Question 1: Given a value ‘x’ and an array of integers, determine whether two of the numbers add up to ‘x’: CSE373: Data Structures and algorithms6 Questions you should have asked me: 1)Is the array in any particular order? 2)Should I consider the case where adding two large numbers could cause an overflow? 3)Is space a factor, in other words, can I use an additional structure(s)? 4)Is this method going to be called frequently with different/the same value of ‘x’? 5)About how many values should I expect to see in the array, or is that unspecified? 6)Will ‘x’ always be a positive value? 7)Can I assume the array won’t always be empty, what if its null?

Why these questions matter! 1) Is the array in any particular order? If the array is already sorted, then this question becomes a lot easier, and can be done in O(n) time. 2) Should I consider the case where adding two large numbers could cause an overflow? If the integers are very large, I should use something other than ‘ints’ to store my results, such as double or longs, or else I could get inconsistent results. 3) Is space a factor, in other words, can I use an additional structure(s)? If space is not a factor, then it might be better to leave the original array alone, and instead sort the array in a separate structure. Or even use a BST representation. CSE373: Data Structures and algorithms7

Why these questions matter! 1) Is this method going to be called frequently with different/the same value of ‘x’? This is a *great* question. If the client will be calling this frequently, it might make more sense to store a copy of the sorted array to prevent needing to re- sort it every time. This could drastically speed-up frequent calls. This process is called memoization. 2) About how many values should I expect to see in the array, or is that unspecified? Often times, it is safe to assume that there could be any range of values (or in our case, asymptotically very many). However, this is not always the case. Our solution to this problem may be different if we knew that there were always exactly 12 values in our array. CSE373: Data Structures and algorithms8

Question 1.5 CSE373: Data Structures and algorithms9 Given an array of integers, return a new array of the same values without any duplicates

Question 1.5 CSE373: Data Structures and algorithms10 Given an array of integers, return a new array of the same values without any duplicates create set, s for each value, x in input_array: add x to s create new array, result for each value, x in s: add x to result return result create set, s for each value, x in input_array: add x to s create new array, result for each value, x in s: add x to result return result

Question 2: Given an array that contains the values 1 through ‘n’ two times each, find the one number that is contained only 1 time. CSE373: Data Structures and algorithms11

Question 2: Given an array that contains the values 1 through ‘n’ two times each, find the one number that is contained only 1 time. CSE373: Data Structures and algorithms12 create map from strings->ints, map for each value, x in input_array: if !map.contains(x): map.put(x, 0) map.put(x, map.get(x) + 1) for each key in map, key: if map.get(key) == 1: return key create map from strings->ints, map for each value, x in input_array: if !map.contains(x): map.put(x, 0) map.put(x, map.get(x) + 1) for each key in map, key: if map.get(key) == 1: return key

Question 3: Given a list of integers, find the highest value obtainable by concatenating them together. For example: given [9, 918, 917], result = For example: given [1, 112, 113], result = CSE373: Data Structures and algorithms13

Question 3: Given a list of integers, find the highest value obtainable by concatenating them together. For example: given [9, 918, 917], result = For example: given [1, 112, 113], result = CSE373: Data Structures and algorithms14 -Convert all numbers to strings -Sort numbers based on largest first number, break ties by moving on to next digit if its greater than the previous -Convert all numbers to strings -Sort numbers based on largest first number, break ties by moving on to next digit if its greater than the previous

Question 4: CSE373: Data Structures and algorithms15 Given a very large file of integers (more than you can store in memory), return a list of the largest 100 numbers in the file

Question 4: CSE373: Data Structures and algorithms16 Given a very large file of integers (more than you can store in memory), return a list of the largest 100 numbers in the file Create min-heap, h Add first 100 values to h while there are remaining numbers: x = next number if x > h.getMin(): h.deleteMin() h.add(x) create new list, l while h.isEmpty(): l.add(h.deleteMin()) return l Create min-heap, h Add first 100 values to h while there are remaining numbers: x = next number if x > h.getMin(): h.deleteMin() h.add(x) create new list, l while h.isEmpty(): l.add(h.deleteMin()) return l

Question 5 CSE373: Data Structures and algorithms17 Given an unsorted array of values, find the 2 nd biggest value in the array. (Harder alternative) Find the k’th biggest value in the array

Question 5 CSE373: Data Structures and algorithms18 Given an unsorted array of values, find the 2 nd biggest value in the array. sort input_array return input_array[len – 2] sort input_array return input_array[len – 2] max = -infinity 2 nd _max = -infinity for each value, v in input_array: if v > max: 2 nd _max = max max = v return 2 nd _max max = -infinity 2 nd _max = -infinity for each value, v in input_array: if v > max: 2 nd _max = max max = v return 2 nd _max max-heap h = heapify(input_array) h.removeMax() Return h.removeMax() max-heap h = heapify(input_array) h.removeMax() Return h.removeMax()

Question 6 Given a list of strings, write a method that returns the frequency of the word with the highest frequency. (Harder version) Given a list of strings, write a method that returns a sorted list of words based on frequency CSE373: Data Structures and algorithms19

Question 6 Given a list of strings, write a method that returns the frequency of the word with the highest frequency. CSE373: Data Structures and algorithms20 max = 0 map from string->int, map for each string, s: if !map.contains(s): map.put(s,0) map.put(s, map.get(s) + 1) if map.get(s) > max: max = 0 map from string->int, map for each string, s: if !map.contains(s): map.put(s,0) map.put(s, map.get(s) + 1) if map.get(s) > max: max = 0

Question 7: CSE373: Data Structures and algorithms21 Given an array of strings that are each sorted lexicographically, determine the order of characters in the given alphabet. For example, given the english alphabet, the ordering is: “a,b,c,d,e,f... x,y,x”. Your output should be the lexicographic order of only the characters that were found in the input strings. For example: input = [xyz, yk, zk, xm, my], then the output would be [x,m,y,z,k]