Data structures and libraries

Similar documents
Graphs. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

Problem solving paradigms

Data structures. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

Dynamic Programming. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. School of Computer Science Reykjavík University

Lecture Notes. char myarray [ ] = {0, 0, 0, 0, 0 } ; The memory diagram associated with the array can be drawn like this

The ADT priority queue Orders its items by a priority value The first item removed is the one having the highest priority value

CSE 100 Minimum Spanning Trees Prim s and Kruskal

Introduction. Bjarki Ágúst Guðmundsson Tómas Ken Magnússon. Árangursrík forritun og lausn verkefna. School of Computer Science Reykjavík University

CSE 100: GRAPH ALGORITHMS

This course is intended for 3rd and/or 4th year undergraduate majors in Computer Science.

About this exam review

Lecture 13. Reading: Weiss, Ch. 9, Ch 8 CSE 100, UCSD: LEC 13. Page 1 of 29

Chapter 9. Greedy Technique. Copyright 2007 Pearson Addison-Wesley. All rights reserved.

Minimum-Spanning-Tree problem. Minimum Spanning Trees (Forests) Minimum-Spanning-Tree problem

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

Graphiti A Simple Graph Language. Language Reference Manual

Graphs. The ultimate data structure. graphs 1

CS61BL. Lecture 5: Graphs Sorting

Graphs & Digraphs Tuesday, November 06, 2007

CSE 100: GRAPH ALGORITHMS

Lecture Summary CSC 263H. August 5, 2016

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

AE52/AC52/AT52 C & Data Structures JUNE 2014

Unweighted Graphs & Algorithms

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Final Examination CSE 100 UCSD (Practice)

Chapter 9. Priority Queue

Data Structures Brett Bernstein

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

Computer Science E-22 Practice Final Exam

Basic Graph Definitions

CS 310 Advanced Data Structures and Algorithms

12 Abstract Data Types

Binary Trees

) $ f ( n) " %( g( n)

Chapter 9 Graph Algorithms

11/22/2016. Chapter 9 Graph Algorithms. Introduction. Definitions. Definitions. Definitions. Definitions

CS302 - Data Structures using C++

Binary Representations and Arithmetic

CSE 332: Data Structures & Parallelism Lecture 7: Dictionaries; Binary Search Trees. Ruth Anderson Winter 2019

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College January 27, 2015

We would like to find the value of the right-hand side of this equation. We know that

Name: 1 stack<int> s; 2 s.push(9) 3 s.push(5) 4 s.push(10) 5 s.push(1) 6 s.pop() 7 s.pop() 8 s.push(0) 9 s.pop() 10 s.pop() 11 s.

CSE 373 Final Exam 3/14/06 Sample Solution

Week 9. CS 400 Programming III

Algorithm Design (8) Graph Algorithms 1/2

Unit #9: Graphs. CPSC 221: Algorithms and Data Structures. Will Evans 2012W1

Chapter 14. Graphs Pearson Addison-Wesley. All rights reserved 14 A-1

Algorithms: Design & Practice

CS 161 Lecture 11 BFS, Dijkstra s algorithm Jessica Su (some parts copied from CLRS) 1 Review

Outline. Graphs. Divide and Conquer.

Practice Problems for the Final

INF280 Graph Traversals & Paths

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

CSE100 Practice Final Exam Section C Fall 2015: Dec 10 th, Problem Topic Points Possible Points Earned Grader

CS 112 Final May 8, 2008 (Lightly edited for 2012 Practice) Name: BU ID: Instructions

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

UNIT 7A Data Representation: Numbers and Text. Digital Data

Data Structures Question Bank Multiple Choice

Lecture 4: Elementary Data Structures Steven Skiena

Graphs Data Structures

2. True or false: even though BFS and DFS have the same space complexity, they do not always have the same worst case asymptotic time complexity.

Spanning Trees, greedy algorithms. Lecture 22 CS2110 Fall 2017

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Algorithm Design and Analysis

COMP Data Structures

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

CS490: Problem Solving in Computer Science Lecture 6: Introductory Graph Theory

CS302 Data Structures using C++

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Exam 3 Practice Problems

( ) n 3. n 2 ( ) D. Ο

CSci 231 Final Review

CS 31: Intro to Systems Binary Representation. Kevin Webb Swarthmore College September 6, 2018

CIS 121 Data Structures and Algorithms Midterm 3 Review Solution Sketches Fall 2018

Spanning Trees 4/19/17. Prelim 2, assignments. Undirected trees

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Spanning Trees. Lecture 22 CS2110 Spring 2017

CS261 Data Structures. Ordered Array Dynamic Array Implementation

Merge Sort Goodrich, Tamassia Merge Sort 1

Minimum Spanning Tree

TU/e Algorithms (2IL15) Lecture 2. Algorithms (2IL15) Lecture 2 THE GREEDY METHOD

COMP2611: Computer Organization. Data Representation

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Data Structures and Algorithms

CSE 2123 Recursion. Jeremy Morris

The type of all data used in a C++ program must be specified

CS521 \ Notes for the Final Exam

COMP 251 Winter 2017 Online quizzes with answers

CS 315 Data Structures Spring 2012 Final examination Total Points: 80

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

SORTING AND SEARCHING

More about Binary 9/6/2016

Your favorite blog : (popularly known as VIJAY JOTANI S BLOG..now in facebook.join ON FB VIJAY

Ex: If you use a program to record sales, you will want to remember data:

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Transcription:

Data structures and libraries Bjarki Ágúst Guðmundsson Tómas Ken Magnússon Árangursrík forritun og lausn verkefna School of Computer Science Reykjavík University

Today we re going to cover Basic data types Big integers Why we need data structures Data structures you already know Sorting and searching Using bitmasks to represent sets Common applications of the data structures Augmenting binary search trees Representing graphs 2

Basic data types You should all be familiar with the basic data types: bool: a boolean (true/false) char: an 8-bit signed integer (often used to represent characters with ASCII) short: a 16-bit signed integer int: a 32-bit signed integer long long: a 64-bit signed integer float: a 32-bit floating-point number double: a 64-bit floating-point number long double: a 128-bit floating-point number string: a string of characters 3

Basic data types Type Bytes Min value Max value bool 1 char 1-128 127 short 2-32768 32767 int 4-2148364748 2147483647 long long 8-9223372036854775808 9223372036854775807 n 2 8n 1 2 8n 1 1 Type Bytes Min value Max value unsigned char 1 0 255 unsigned short 2 0 65535 unsigned int 4 0 4294967295 unsigned long long 8 0 18446744073709551615 n 0 2 8n 1 Type Bytes Min value Max value Precision float 4 3.4 10 38 3.4 10 38 7 digits double 8 1.7 10 308 1.7 10 308 14 digits long double 16 1.1 10 4932 1.1 10 4932 18 digits 4

Big integers What if we need to represent and do computations with very large integers, i.e. something that doesn t fit in a long long Simple idea: Store the integer as a string But how do we perform arithmetic on a pair of strings? We can use the same algorithms as we learned in elementary school Addition: Add digit-by-digit, and maintain the carry Subtraction: Similar to addition Multiplication: Long multiplication Division: Long division Modulo: Long division 5

Example problem: Simple Addition https://open.kattis.com/problems/simpleaddition 6

Why do we need data structures? Sometimes our data needs to be organized in a way that allows one or more of Efficient querying Efficient inserting Efficient deleting Efficient updating Sometimes we need a better way to represent our data How do we represent large integers? How do we represent graphs? Data structures help us achieve those things 7

Data structures you ve seen before Static arrays Dynamic arrays Linked lists Stacks Queues Priority queues Sets Maps 8

Data structures you ve seen before Static arrays - int arr[10] Dynamic arrays - vector<int> Linked lists - list<int> Stacks - stack<int> Queues - queue<int> Priority queues - priority_queue<int> Sets - set<int> Maps - map<int, int> 8

Data structures you ve seen before Static arrays - int arr[10] Dynamic arrays - vector<int> Linked lists - list<int> Stacks - stack<int> Queues - queue<int> Priority queues - priority_queue<int> Sets - set<int> Maps - map<int, int> Usually it s best to use the standard library implementations Almost surely bug-free and fast We don t need to write any code Sometimes we need our own implementation When we want more flexibility When we want to customize the data structure 8

Sorting and searching Very common operations: Sorting an array Searching an unsorted array Searching a sorted array Again, usually in the standard library We ll need different versions of binary search later which need custom code, but lower_bound is enough for now 9

Sorting and searching Very common operations: Sorting an array - sort(arr.begin(), arr.end()) Searching an unsorted array - find(arr.begin(), arr.end(), x) Searching a sorted array - lower_bound(arr.begin(), arr.end(), x) Again, usually in the standard library We ll need different versions of binary search later which need custom code, but lower_bound is enough for now 9

Representing sets We have a small (n 30) number of items We label them with integers in the range 0, 1,..., n 1 We can represent sets of these items as a 32-bit integer The ith item is in the set represented by the integer x if the ith bit in x is 1 Example: We have the set {0, 3, 4} int x = (1<<0) (1<<3) (1<<4); 10

Representing sets Empty set: 0 Single element set: 1<<i The universe set (i.e. all elements): (1<<n)-1 Union of sets: x y Intersection of sets: x&y Complement of a set: ~x & ((1<<n)-1) 11

Representing sets Check if an element is in the set: if (x & (1<<i)) { // yes } else { // no } 12

Representing sets Why do this instead of using set<int>? Very lightweight representation All subsets of the n elements can be represented by integers in the range 0... 2 n 1 Allows for easily iterating through all subsets (we ll see this later) Allows for easily using a set as an index of an array (we ll see this later) 13

Applications of Arrays and Linked Lists Too many to list Most problems require storing data, usually in an array 14

Applications of Stacks Processing events in a last-in first-out order Simulating recursion Depth-first search in a graph Reverse a sequence Matching brackets And a lot more 15

Example problem: Backspace https://open.kattis.com/problems/backspace 16

Applications of Queues Processing events in a first-in first-out order Breadth-first search in a graph And a lot more 17

Applications of Priority Queues Processing events in order of priority Finding a shortest path in a graph Some greedy algorithms And a lot more 18

Applications of Sets Keep track of distinct items Have we seen an item before? If implemented as a binary search tree: Find the successor of an element (the smallest element that is greater than the given element) Count how many elements are less than a given element Count how many elements are between two given elements Find the kth largest element And a lot more 19

Applications of Maps Associating a value with a key As a frequency table As a memory when we re doing Dynamic Programming (later) And a lot more 20

Augmenting Data Structures Sometimes we can store extra information in our data structures to gain more functionality Usually we can t do this to data structures in the standard library Need our own implementation that we can customize Example: Augmenting binary search trees 21

Augmenting Binary Search Trees We have a binary search tree and want to efficiently: Count number of elements < x Find the kth smallest element 10 15 20 33 38 47 51 Naive method is to go through all vertices, but that is slow: O(n) 5 18 36 34 37 39 49 22

Augmenting Binary Search Trees Idea: In each vertex store the size of the subtree This information can be maintained when we insert/delete elements without increasing time complexity 10, 2 5, 1 15, 5 20, 2 18, 1 33, 14 47, 8 38, 5 36, 3 39, 1 51, 2 49, 1 34, 1 37, 1 23

Augmenting Binary Search Trees Count number of elements < 38 33, 14 Search for 38 in the tree Count the vertices that we pass by that are less than x When we are at a vertex where we should go right, get the size of the left subtree and add it to our count 10, 2 5, 1 15, 5 20, 2 18, 1 47, 8 38, 5 36, 3 39, 1 34, 1 37, 1 51, 2 49, 1 24

Augmenting Binary Search Trees Count number of elements < 38 33, 14 Search for 38 in the tree Count the vertices that we pass by that are less than x When we are at a vertex where we should go right, get the size of the left subtree and add it to our count 10, 2 5, 1 15, 5 20, 2 18, 1 47, 8 38, 5 36, 3 39, 1 34, 1 37, 1 51, 2 49, 1 Time complexity O(log n) 25

Augmenting Binary Search Trees Find kth smallest element We re on a vertex whose left subtree is of size m If k = m + 1, we found it If k m, look for the kth smallest element in the left subtree If k > m + 1, look for the k m 1st smallest element in the right subtree 10, 2 5, 1 15, 5 20, 2 18, 1 33, 14 47, 8 38, 5 36, 3 39, 1 34, 1 37, 1 51, 2 49, 1 26

Augmenting Binary Search Trees Find kth smallest element We re on a vertex whose left subtree is of size m If k = m + 1, we found it If k m, look for the kth smallest element in the left subtree If k > m + 1, look for the k m 1st smallest element in the right subtree Example: k = 11 10, 2 5, 1 15, 5 20, 2 18, 1 33, 14 47, 8 38, 5 36, 3 39, 1 34, 1 37, 1 51, 2 49, 1 27

Representing graphs There are many types of graphs: Directed vs. undirected Weighted vs. unweighted Simple vs. non-simple Many ways to represent graphs Some special graphs (like trees) have special representations Most commonly used (general) representations: 1. Adjacency list 2. Adjacency matrix 3. Edge list 28

Adjacency list 0: 1, 2 1: 0, 2 2: 0, 1, 3 3: 2 vector<int> adj[4]; adj[0].push_back(1); adj[0].push_back(2); adj[1].push_back(0); adj[1].push_back(2); adj[2].push_back(0); adj[2].push_back(1); adj[2].push_back(3); adj[3].push_back(2); 0 1 2 3 29

Adjacency matrix 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 0 bool adj[4][4]; adj[0][1] = true; adj[0][2] = true; adj[1][0] = true; adj[1][2] = true; adj[2][0] = true; adj[2][1] = true; adj[2][3] = true; adj[3][2] = true; 0 1 2 3 30

Edge list 0, 1 0, 2 1, 2 2, 3 0 1 2 vector<pair<int, int> > edges; edges.push_back(make_pair(0, 1)); edges.push_back(make_pair(0, 2)); edges.push_back(make_pair(1, 2)); edges.push_back(make_pair(2, 3)); 3 31

Efficiency Adjacency list Adjacency matrix Edge list Storage O( V + E ) O( V 2 ) O( E ) Add vertex O(1) O( V 2 ) O(1) Add edge O(1) O(1) O(1) Remove vertex O( E ) O( V 2 ) O( E ) Remove edge O( E ) O(1) O( E ) Query: are u, v adjacent? O( V ) O(1) O( E ) Different representations are good for different situations 32

Example problem: Grandpa Bernie https://open.kattis.com/problems/grandpabernie 33