CS50 WEEK 6 Kenny Yu.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
CS252: Systems Programming Ninghui Li Program Interview Questions.
COL 106 Shweta Agrawal and Amit Kumar
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:
CHAPTER 5 PRIORITY QUEUES (HEAPS) §1 ADT Model Objects: A finite ordered list with zero or more elements. Operations:  PriorityQueue Initialize( int.
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
BST Data Structure A BST node contains: A BST contains
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Hashing General idea: Get a large array
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Trees, Binary Search Trees, Recursion, Project 2 Bryce Boe 2013/08/01 CS24, Summer 2013 C.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Dictionaries CS 105. L11: Dictionaries Slide 2 Definition The Dictionary Data Structure structure that facilitates searching objects are stored with search.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction.
Information and Computer Sciences University of Hawaii, Manoa
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Topic 24 Heaps "You think you know when you can learn, are more sure when you can write even more when you can teach, but certain when you can program."
Data structures Abstract data types Java classes for Data structures and ADTs.
CSC 211 Data Structures Lecture 13
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.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
CS 206 Introduction to Computer Science II 11 / 16 / 2009 Instructor: Michael Eckmann.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Dictionaries CS /02/05 L7: Dictionaries Slide 2 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved.
Sets of Digital Data CSCI 2720 Fall 2005 Kraemer.
+ David Kauchak cs312 Review. + Midterm Will be posted online this afternoon You will have 2 hours to take it watch your time! if you get stuck on a problem,
Data Structures Systems Programming. Systems Programming: Data Structures 2 2 Systems Programming: 2 Data Structures  Queues –Queuing System Models –Queue.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 15 Other Data Structures Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
CS 261 – Data Structures Hash Tables Part II: Using Buckets.
Final Exam Review CS Total Points – 20 Points Writing Programs – 65 Points Tracing Algorithms, determining results, and drawing pictures – 50.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
Chapter 12 – Data Structures
Week 4 - Friday CS221.
12 C Data Structures.
Heaps And Priority Queues
CS 1114: Implementing Search
Mark Redekopp David Kempe
Problems with Linked List (as we’ve seen so far…)
Cse 373 April 24th – Hashing.
Week 11 - Friday CS221.
Hashing Exercises.
Cse 373 April 26th – Exam Review.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
- Alan Perlis Topic 24 Heaps "You think you know when you can learn,
CS6045: Advanced Algorithms
DATA STRUCTURE.
Tree A tree is a data structure in which each node is comprised of some data as well as node pointers to child nodes
Presentation transcript:

CS50 WEEK 6 Kenny Yu

Announcements Problem Set 6 Walkthrough up Problem Set 4 [Sudoku] Returned Problem Set 5 [Bitmaps + Jpegs] to be returned soon My section resources are posted here: https://cloud.cs50.net/~kennyyu/section/

Agenda Data Structures Bitwise operators Strategies for Big Board Stacks Queues Linked Lists Trees Binary Search Trees Tries Hash Tables Bitwise operators ~,|, &, ^, <<, >> Strategies for Big Board Space vs. Time (vs. Correctness ?) Valgrind Compiler optimization flags

Stack A stack is a first-in-last-out (FILO) data structure Operations: Think of cafeteria trays, the call stack Operations: Push: we add an item onto the top of the stack Pop: we remove the top item of the stack Peek: we retrieve the top item of the stack without removing it

Stack – Sample Header File /* stack.h * contains the type definitions and function headers * for stacks */ /* alias ‘struct stack’ to be ‘stack’; ‘struct stack’ * still needs to be defined elsewhere */ typedef struct stack stack; /* stack operations. We can only store ints. */ void push(stack *, int); int pop(stack *); int peek(stack *);

Queues A queue is a first-in-first-out (FIFO) data structure Think of waiting in a line Operations Enqueue: Add an item to the end of the queue Dequeue: Remove the first item of the queue Peek: Retrieve the first item of the queue without removing it

Queue – Sample Header File /* queue.h * contains the type definitions and function headers * for stacks */ /* alias ‘struct queue’ to be ‘queue’; ‘struct queue’ * still needs to be defined elsewhere */ typedef struct queue queue; /* queue operations. We can only store ints. */ void enqueue(queue *, int); int dequeue(queue *); int peek(queue *);

Interview Question 1 QUESTION: How would you implement a queue using stacks?

Interview Question 1 QUESTION: How would you implement a queue using stacks? HINT: Use two stacks.

Interview Question 1 QUESTION: How would you implement a queue using stacks? SOLUTION: A queue will really be two two stacks, stack_in and stack_out. Enqueue: when we enqueue an item onto our queue, we really push the item into stack_in Dequeue: when we dequeue an item from our queue, we first check if stack_out has any items (1) If stack_out is not empty, then pop the first item and return it (2) If stack_out is empty, then we pop items from stack_in and push each item, in the same order we pop them, into stack_out. Then do (1).

Interview Question 1 QUESTION: How would you implement a queue using stacks? SOLUTION: A queue will really be two two stacks, stack_in and stack_out. Enqueue: when we enqueue an item onto our queue, we really push the item into stack_in Dequeue: when we dequeue an item from our queue, we first check if stack_out has any items (1) If stack_out is not empty, then pop the first item and return it (2) If stack_out is empty, then we pop items from stack_in and push each item, in the same order we pop them, into stack_out. Then do (1). What is the big O of enqueue? dequeue?

Interview Question 1 QUESTION: How would you implement a stack using queues? SOLUTION: A stack will really be two two queues, queue_in and queue_out. Push: when we push an item onto our stack, we really enqueue the item into queue_in Pop: when we pop an item from our stack, we first check if queue_out has any items (1) If queue_out is not empty, then dequeue the first item and return it (2) If queue_out is empty, then we dequeue items from queue_in and enqueue each item, in the same order we dequeued them, into queue_out. Then do (1). What is the big O of enqueue? dequeue? Enqueue: O(1). Dequeue: The amortized (average) runtime is O(1).

Linked Lists 1 5 4 2 3 NULL

Linked Lists A linked list consists of nodes, where each node has a value and a pointer to the next object (node) in the list. struct lnode { int value; struct lnode *next; };

Linked Lists struct lnode { int value; struct lnode *next; }; value NULL 4 6 struct lnode struct lnode

Adding/removing from a linked list Can’t lose any pointers (or else we lose the rest of the list!) value next 4 NULL value next value next 4 6 NULL struct lnode struct lnode

Adding/removing from a linked list Can’t lose any pointers (or else we lose the rest of the list!) value next 4 value next value next 4 6 NULL struct lnode struct lnode

Adding/removing from a linked list Can’t lose any pointers (or else we lose the rest of the list) value next 4 value next value next 4 6 NULL struct lnode struct lnode

Iterating over a linked list typedef struct lnode lnode; /* assume the list has size greater than n */ int get_nth_value(lnode *root, int n) { /* TODO */ }

Iterating over a linked list typedef struct lnode lnode; /* assume the list has size greater than n */ int get_nth_value(lnode *root, int n) { lnode *current = root; for (int i = 0; i < n; i++) current = current->next; return current->value; }

Linked Lists If we only have a pointer to the start of the list, what are the Big O for these operations? Insert_first Insert_last Remove_first Remove_last find

Linked Lists If we only have a pointer to the start of the list, what are the Big O for these operations? Insert_first – O(1) Insert_last – O(n) Remove_first – O(1) Remove_last – O(n) Find – O(n)

Interview Question 2 How would you detect a cycle in a linked list with minimum space? 1 5 4 2 3

Interview Question 2 How would you detect a cycle in a linked list with minimum space? Hint: Use two pointers. 1 5 4 2 3

Interview Question 2 How would you detect a cycle in a linked list with minimum space? Have two pointers called hare and tortoise. Start them off pointing to the same node. On every iteration, move hare 2 nodes ahead (if it can), and move tortoise one node ahead. If they ever at some time point to the same address in memory, then there is a cycle in the list. 1 5 4 2 3

Doubly Linked Lists struct lnode { struct lnode *prev; int value; struct lnode *next; }; prev value next prev value next prev value next NULL 4 5 6 NULL struct lnode struct lnode struct lnode

Binary Search Trees 5 3 9 1 7 6 8 NULL

Binary Search Trees A binary search tree (BST) consists of nodes that has a value and two pointers, one pointer to its left child node and one pointer to its right child node Invariants: Every element in the left subtree is less than the current element Every element in the right subtree is greater than the current element Left and right child nodes are also BSTs.

Binary Search Trees struct bstnode { int value; struct bstnode *left; struct bstnode *right; }; 5 3 X 9 X 1 X X 7 6 X X 8 X X

Binary Search Trees A BST is balanced if every node has two children. What are the big O for these operations in a balanced BST? What about an unbalanced BST? Remove Add Min Find

Binary Search Trees A BST is balanced if every node has two children. What are the big O for these operations? RemoveMin – balanced: O(log n), unbalanced: O(n) Add – balanced: O (log n), unbalanced: O(n) Traverse down the tree to find the appropriate spot Min – balanced: O (log n), unbalanced: O(n) Traverse all the way left Find – balanced: O (log n), unbalanced: O(n) Analagous to a binary search

Trie X 1 X 1 X X X 1 X X 1 X X 1

Trie A trie is a tree with N pointers and a boolean variable, is_terminating Each pointer represents a letter in the alphabet of N letters. The existence of a pointer, combined with is_terminating, represents the existence of that word is_terminating indicates whether what we’ve looked at so far is in the data structure

Trie – What words are in our dict? ptrs is_terminated struct trie_node { struct trie_node *ptrs[N]; bool is_terminated; }; Here N = 2; Alphabet: {a,b} X 1 X 1 X X X 1 X X 1 X X 1

Trie – What words are in our dict? ptrs is_terminated struct trie_node { struct trie_node *ptrs[N]; bool is_terminated; }; Here N = 2; Alphabet: {a,b} X 1 X 1 a b X 1 ba X X 1 X X 1 X X 1 bab aba abb

Why use a trie? Very efficient lookup Heavy memory usage Especially if many words in your language share common prefixes Lookup for a word is O(n), where n is the length of the string—basically constant time! Heavy memory usage

Hash Tables A hash table consists of an array and a hash function Allows us to check whether something is contained in a data structure without checking the entire thing A hash function maps input (in our case, a string) to a number (called the input’s hash value) We use the hash value as an index in the associated array When we check to see if a string is in our dictionary, we compute the string’s hash value, and check if array[hash_value] is set

Hash Tables 3 4 5 X 6 7 8 ... 1 2 10 11

Hash Tables Good hash functions are Deterministic (calling the hash function on the same string always returns the same result) Uniformly distributed What happens if two strings get mapped to the same hash value? We have a collision.

Hash Tables How do we solve collisions? Several methods, here are two ways Separate chaining – each bucket in our hash table is actually a pointer to a linked list if a word hashes to a bucket that already has words, we append it to the linked list at that bucket Linear probing – if a word hashes to a bucket that already has words, then we keep scanning down the buckets to find the first one that is empty.

Hash Tables – Separate Chaining 3 4 5 X 6 7 8 ... 1 3 … 10 12 … 11 X

Hash Tables Assuming a good hash function with few collisions, what is the run time for these operations? Add Remove find

Hash Tables Assuming a good hash function with few collisions, what is the run time for these operations? Add – O(1) Remove – O(1) Find – O(1) All constant time! Tradeoff between Time and Space—must use a lot of space for a very large array

Agenda Data Structures Bitwise operators Strategies for Big Board Stacks Queues Linked Lists Trees Binary Search Trees Tries Hash Tables Bitwise operators ~,|, &, ^, <<, >> Strategies for Big Board Space vs. Time (vs. Correctness ?) Valgrind Compiler optimization flags

Bitwise Operators Remember: all data is represented as bits Bitwise operators allow you to manipulate data at the bit level.

Bitwise Operators: ~, |, &, ^ Bitwise negation (x = ~42) Bitwise AND (x = 4 & 5) Bitwise OR (x = 0x4 | 0x8) ~ 1 & 1 | 1

Bitwise operators: XOR (^) Bitwise XOR (exclusive or) Useful properties: x ^ x == 0 (for any value x) x ^ 0 == x (for any value x) Associative and commutative y ^ x ^ y = x ^ (y ^ y) = x ^ 1

Interview Question 3 How do you swap two variables without a temporary variable?

Interview Question 3 How do you swap two variables without a temporary variable? HINT: use XOR

Interview Question 3 How do you swap two variables without a temporary variable? HINT: use XOR int x = 3; int y = 4; x = x ^ y; // (x == 3^4) y = x ^ y; // (y == (3 ^ 4) ^ 4 = 3) x = x ^ y; // (x == (3 ^ 4) ^ 3 = 4)

Interview Question 4 You have an array of length 2N – 1, which contains the numbers 0-(N-1), all repeated once except for one of the numbers. Using minimum space, find that number.

Interview Question 4 You have an array of length 2N – 1, which contains the numbers 0-(N-1), all repeated once except for one of the numbers. Using minimum space, find that number. Hint: Use XOR

Interview Question 4 You have an array of length 2N – 1, which contains the numbers 0-(N-1), all repeated once except for one of the numbers. Using minimum space, find that number. Hint: Use XOR Solution: XOR all the numbers together! [2,3,4,3,4,1,1,0,0] -> 2 ^ (3 ^ 3) ^ (4 ^ 4) … (0 ^ 0) -> 2

Interview Question 5 Using the minimum number of instructions, write an algorithm to quickly determine whether a number is a power of 2.

Interview Question 5 Using the minimum number of instructions, write an algorithm to quickly determine whether a number is a power of 2. Hint: Use AND

Interview Question 5 Using the minimum number of instructions, write an algorithm to quickly determine whether a number is a power of 2. Hint: Use AND int is_power_of_2(int n) { return (n & (n – 1)) == 0; } 00010000 & 00001111 00000000

Bit shifting Shift left << We shift all the bits over to the left, and fill in the remaining positions with 0s x = x << 3; // same as saying multiplying x by 8 Shift right >> Logical right shift – for unsigned ints, >> will shift all the bits to the right, and fill in the remaining positions with 0s Arithmetic right shift – for signed ints, >> will shift all the bits to the right, and fill in the remaining positions with 0s or 1s, depending on the sign of the int x = x >> 3;

Interview Question 6 You have a string that possibly contains repeats of some letter. Find, with minimum space, all the letters that are not contained in the string.

Interview Question 6 You have a string that possibly contains repeats of some letter. Find, with minimum space, all the letters that are not contained in the string. HINT: Use bit vectors

Interview Question 6 You have a string that possibly contains repeats of some letter. Find, with minimum space, all the letters that are not contained in the string. HINT: Use bit vectors Instead of using an array of 26 ints (or chars), we can be even more efficient. All we really need is 26 boolean values (T/F), which we can represent with 26 bits. Since an int is 32 bits (on a 32-bit machine), all we need is just one int!

Interview Question 6 unsigned int bitvector = 0; /* the right-most position is position 0 */ void set_nth_position(int n) { bitvector |= (1 << n); /* ‘turns on’ nth bit */ } /* returns 0 or 1 */ int get_nth_position(int n) { return (bitvector & (1 << n)) >> n;

Agenda Data Structures Bitwise operators Strategies for Big Board Stacks Queues Linked Lists Trees Binary Search Trees Tries Hash Tables Bitwise operators ~,|, &, ^, <<, >> Strategies for Big Board Space vs. Time (vs. Correctness ?) Valgrind Compiler optimization flags

Valgrind Remember to free all the memory you malloc! Use valgrind to find any memory leaks valgrind –v –leak-check=full <program_name>

Space vs. Time (vs. Correctness?) So far we’ve seen tradeoffs between space and time BST’s have logarithmic time operations, but don’t use up too much space Hash tables with good hash functions have constant time operations, but use up a lot of space. We can often increase our memory usage to make our programs faster, or we can make our programs run a bit slower but use less memory. But we can trade correctness to make our programs run faster/use less memory.

Bloom Filters (How to make your spellchecker fast) Have one array (actually all you need is a bit vector) Insertion Use multiple hash functions on one string Turn on the corresponding position (set the position to one), using the hash values as indices into the array. Lookup A string is in the dictionary if and only if all the entries at the hash indices are 1. Problems with this?

Bloom Filters May get false positives! (in our case, some words that are actually misspelled will be considered to be inside our dictionary) Example: Hash values of “cat” are 2, 10, 100 Hash values of “dog” are 3, 200, 304 Hash values of “qqmore” are 10, 100, 304 Play around with your hash functions or use more hash functions to reduce the likelihood of false positives

Compiler flags gcc –o speller speller.c dictionary.c –O3 –fast –m64 -O3 : level 3 compiler optimizations -fast (does not work on appliance): turns on all compiler speed optimizations to make your programs as fast as possible -m64 (does not work on appliance): Compile for a 64-bit machine. 64-bit machines have more registers (and thus faster computation); make sure to check the sizes of your types!

Agenda Data Structures Bitwise operators Strategies for Big Board Stacks Queues Linked Lists Trees Binary Search Trees Tries Hash Tables Bitwise operators ~,|, &, ^, <<, >> Strategies for Big Board Space vs. Time (vs. Correctness ?) Valgrind Compiler optimization flags

Fun Fun Fun https://cloud.cs50.net/~kennyyu/section/week7/ Open instructions.txt