ENS Lyon Camp. Day 5. Basic group. C October
|
|
- Donald Osborne
- 6 years ago
- Views:
Transcription
1 ENS Lyon Camp. Day 5. Basic group. C October Contents 1 Input/Output C-style C++-style Stack Overflow Runtime Error 3 STL containers 3.1 Pair Vector Queue Deque Set Map STL sort 8 1 Input/Output 1.1 C-style 1 #i n c l u d e <s t d i o. h> 3 i n t main ( ) 4 { 5 i n t a ; 6 FILE * f i n, * f o u t ; 7 f i n = fopen ( " example. in ", " r " ) ; 8 f o u t = fopen ( " example. out ", "w" ) ; f s c a n f ( f i n, "%i ", &a ) ; 10 f p r i n t f ( fout, "%i ", a ) ; 11 f c l o s e ( f i n ) ; 1 f c l o s e ( f o u t ) ; 13 } There is an issue with long long variables. You should use specifier "%I64d" for Windows and specifier "%lld" for all other operating systems. To prevent errors with this issue you could define the following macros: 1
2 1 #i f d e f WIN3 p r i n t f ( "%I64d \n", ans ) ; 3 #e l s e 4 p r i n t f ( "%l l d \n", ans ) ; 5 #e n d i f 1. C++-style Another possibility to avoid this issue is usage of C++. 1 #i n c l u d e <fstream> 3 i n t main ( ) 4 { 5 long a ; 6 i f s t r e a m f i n ( " example. in " ) ; 7 ofstream f o u t ( " example. out " ) ; 8 f i n >> a ; f o u t << a << endl ; 10 f i n. c l o s e ( ) ; 11 f o u t. c l o s e ( ) ; 1 } Stack Overflow Runtime Error All variables store in two different places in the memory. One of it is called stack and another one is called heap (In this context there is no connection between them and data structures with the same names). Heap is a region of memory that stores all global variables, memory that allocates using new(), malloc(), etc. functions. Stack is a special region of memory that stores temporary variables created by each function (including the main() function). Stack variables only exist while the function that created them, is running. You should keep in mind, that there is a limit (varies with OS) on the size of variables that can be store on the stack. So if you declare large array in the main function, you program crash. If you have a lot of recursive calls, you program crash. You can set size of stack manually as follows: #pragma comment(linker, "/STACK: "). The size is specified in bytes. 3 STL containers 3.1 Pair This class couples together a pair of values, which may be of different types. 1 #i n c l u d e <u t i l i t y > // std : : p a i r 3 i n t main ( ) { 4 // d e c l a r a t i o n 5 std : : p a i r <int, double> p ;
3 6 7 // c r e a t e p a i r 8 p = std : : make_pair ( 1 0, ) ; 10 // output f i r s t and second elements o f p a i r 11 std : : cout << p. f i r s t << ", " << p. second << \n ; 1 } 3. Vector Vectors represent arrays that can change in size. Vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. You can create vectors with the following constructors: 1 #i n c l u d e <vector > 3 i n t main ( ) 4 { 5 std : : vector <int > f i r s t ; // empty v e c t o r o f i n t s 6 std : : vector <int > second ( 4, ) ; // f o u r i n t s with value std : : vector <int > t h i r d ( second. begin ( ), second. end ( ) ) ; // i t e r a t i n g second 8 std : : vector <int > f o u r t h ( t h i r d ) ; // a copy o f t h i r d 10 i n t myints [ ] = { 1 6,, 7 7, } ; 11 std : : vector <int > f i f t h ( myints, myints + s i z e o f ( myints ) / s i z e o f ( i n t ) ) ; 1 } Here is the examples of vector usage: 1 i n t main ( ) { 3 std : : vector <int > v ; 4 5 // Returns whether the v e c t o r i s empty 6 bool isempty = v. empty ( ) ; 7 8 // Returns the number o f elements in the v e c t o r. i n t s i z e = v. s i z e ( ) ; // Adds a new element at the end o f the vector, a f t e r i t s c u r r e n t l a s t element. 1 v. push_back ( 5 ) ; // Removes the l a s t element in the v e c t o r. 15 v. pop_back ( ) ; // Returns a r e f e r e n c e to the f i r s t element in the v e c t o r. 18 i n t f i r s t = v. f r o n t ( ) ; 1 0 // Returns a r e f e r e n c e to the l a s t element in the v e c t o r. 1 i n t l a s t = v. back ( ) ; 3 // Returns a r e f e r e n c e to the element at p o s i t i o n i in the v e c t o r c o n t a i n e r. 3
4 4 i n t x = v [ i ] ; 5 } Time complexity of all operation is O(1). 3.3 Queue Queues operates in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other. You can create it in the same way as vector. Here is the examples of queue usage: 1 #i n c l u d e <queue> i n t main ( ) 3 { 4 std : : queue<int > q ; 5 6 // Adds a new element at the end o f the queue, a f t e r i t s c u r r e n t l a s t element. 7 q. push ( 5 ) ; 8 // Remove the " o l d e s t " element in the queue 10 q. pop ( ) ; 11 1 // Returns whether t h e queue i s empty 13 bool isempty = q. empty ( ) ; // Returns the number o f elements in the queue 16 i n t s i z e = q. s i z e ( ) ; // Returns a r e f e r e n c e " o l d e s t " element in the queue 1 i n t f i r s t = q. f r o n t ( ) ; 0 1 // Returns a r e f e r e n c e to the the " newest " element in the queue i n t l a s t = q. back ( ) ; 3 } Deque Deque is a double-ended queue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). It constructs in the same way. Here is the examples of deque usage: 1 #i n c l u d e <deque> i n t main ( ) 3 { 4 std : : deque<int > q ; 5 6 // Returns whether t h e queue i s empty 7 bool isempty = q. empty ( ) ; 8 4
5 // Returns the number o f elements in the queue 10 i n t s i z e = q. s i z e ( ) ; 11 1 // Adds a new element at the end o f the deque c o n t a i n e r 13 q. push_back ( 5 ) ; // Removes the l a s t element in the deque 16 q. pop_back ( ) ; // I n s e r t s a new element at the beginning o f the deque 1 q. push_front ( ) ; 0 1 // Removes the f i r s t element in the deque q. pop_front ( ) ; 3 4 // Returns a r e f e r e n c e to the f i r s t element in the deque 5 i n t f i r s t = q. f r o n t ( ) ; 6 7 // Returns a r e f e r e n c e to the l a s t element in the c o n t a i n e r. 8 i n t l a s t = q. back ( ) ; 30 // Returns a r e f e r e n c e to the element at p o s i t i o n i in the deque. 31 i n t x = v [ i ] ; 3 } Set Sets are containers that store unique elements following a specific order (elements must be comparable). Set implementation uses binary search trees. There is two way to redefine comparator of elements 1 bool cmpfunction ( i n t l, i n t r ) { return r < l ; } 3 s t r u c t cmpclass { 4 bool o perator ( ) ( const i n t& l, const i n t& r ) const 5 { return r < l ; } 6 } ; 7 8 i n t main ( ) { 10 std : : set <int > f i r s t ; // empty s e t o f i n t s 11 1 i n t myints []= { 1 0, 0, 3 0, 4 0, 5 0 } ; 13 std : : set <int > second ( myints, myints+5) ; // range std : : set <int > t h i r d ( second ) ; // a copy o f second std : : set <int > f o u r t h ( second. begin ( ), second. end ( ) ) ; // i t e r a t o r 18 1 std : : set <int, cmpclass> f i f t h ; // r e d e f i n e comparator using c l a s s 5
6 0 1 bool (* fn_pt ) ( int, i n t ) = cmpfunction ; // d e c l a t e p o i n t e r to cmpfunction std : : set <int, bool ( * ) ( int, i n t )> s i x t h ( fn_pt ) ; // r e d e f i n e comparator 3 } 4 Here is the examples of set usage: 1 i n t main ( ) { 3 std : : set <int > s ; 4 5 // Returns whether the s e t i s empty 6 bool isempty = s. empty ( ) ; 7 8 // Returns the number o f elements in the queue i n t s i z e = s. s i z e ( ) ; // Returns a pair, with i t s member p a i r : : f i r s t s e t to an i t e r a t o r 1 // p o i n t i n g to e i t h e r the newly i n s e r t e d element 13 // or to the e q u i v a l e n t element already in the s e t. 14 // The p a i r : : second element in the p a i r i s s e t to true i f a new element 15 // was i n s e r t e d or f a l s e i f an e q u i v a l e n t element a lready e x i s t e d. 16 s. i n s e r t ( 0) ; i n t a []= { 5, 1 0, 1 5 } ; 1 s. i n s e r t ( myints, myints+3) ; 0 1 // Removes from the s e t c o n t a i n e r e i t h e r a s i n g l e element // or a range o f elements ( [ f i r s t, l a s t ) ). 3 i t = s. begin ( ) ; 4 s. e r a s e ( i t ) ; // e r a s i n g by value 5 s. e r a s e (0) ; // e r a s i n g by key 6 7 // Searches the c o n t a i n e r f o r an element e q u i v a l e n t to val and r e t u r n s 8 // an i t e r a t o r to i t i f found, o t h e r w i s e i t r e t u r n s an i t e r a t o r to s e t : : end. s. f i n d (0) ; // Returns an i t e r a t o r p o i n t i n g to the f i r s t element in the c o n t a i n e r 3 // which i s not c o n s i d e r e d to go b e f o r e val 33 // ( i. e., e i t h e r i t i s e q u i v a l e n t or goes a f t e r ). 34 std : : set <int >:: i t e r a t o r lb = s. lower_bound (0) ; // Returns an i t e r a t o r p o i n t i n g to the f i r s t element in the c o n t a i n e r 37 // which i s c o n s i d e r e d to go a f t e r val. 38 std : : set <int >:: i t e r a t o r ub = s. upper_bound (0) ; 3 } 40 There is similar container multiset, that allows store the equivalent values with the same methods; 1 #i n c l u d e <set > 3 typedef std : : multiset <int >:: i t e r a t o r s e t I t ; 6
7 4 5 i n t main ( ) 6 { 7 std : : multiset <int > s ; 8 // Count elements with a s p e c i f i c key 10 i n t cnt = s. count (4) ; 11 1 // Get range o f equal elements 13 std : : pair <s e t I t, s e t I t > r e t = mymultiset. equal_range (4) ; 14 } 15 Time complexity is O(log n). In C++11 there exists unordered set (hash set), that stores unordered value and time complexity is approximately O(1) (#include <unordered_set>). 3.6 Map Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. You can redefined compare operator in the same way as it was redefined in set. Here is example of usage map. 1 #i n c l u d e <map> 3 i n t main ( ) 4 { 5 std : : map<char, int > m; 6 7 // Returns whether t h e map i s empty 8 bool isempty = m. empty ( ) ; 10 // Returns the number o f elements in the map 11 i n t s i z e = m. s i z e ( ) ; 1 13 // Returns a pair, with i t s member p a i r : : f i r s t s e t to an i t e r a t o r p o i n t i n g 14 // to e i t h e r the newly i n s e r t e d element 15 // or to the e q u i v a l e n t element already in the map. 16 // The p a i r : : second element in the p a i r i s s e t to true 17 // i f a new element was i n s e r t e d 18 // or f a l s e i f an e q u i v a l e n t element already e x i s t e d. 1 m. i n s e r t ( std : : pair <char, int >( a,100) ) ; 0 1 // Removes from the map c o n t a i n e r e i t h e r a s i n g l e element // or a range o f elements ( [ f i r s t, l a s t ) ). 3 i t = m. begin ( ) ; 4 m. e r a s e ( i t ) ; // e r a s i n g by i t e r a t o r 5 s. e r a s e ( a ) ; // e r a s i n g by key 6 7 // Searches the c o n t a i n e r f o r an element e q u i v a l e n t to val 8 // and r e t u r n s an i t e r a t o r to i t i f found, // o t h e r w i s e i t r e t u r n s an i t e r a t o r to map : : end. 7
8 30 s. f i n d ( a ) ; 31 3 // Returns an i t e r a t o r p o i n t i n g to the f i r s t element in the c o n t a i n e r 33 // which i s not c o n s i d e r e d to go b e f o r e val 34 std : : map<char, int >:: i t e r a t o r lb = m. lower_bound ( a ) ; // Returns an i t e r a t o r p o i n t i n g to the f i r s t element in the c o n t a i n e r 37 // which i s c o n s i d e r e d to go a f t e r val. 38 std : : map<char, int >:: i t e r a t o r ub = m. upper_bound ( b ) ; 3 40 // I f k matches the key o f an element in the container, 41 // the f u n c t i o n r e t u r n s a r e f e r e n c e to i t s mapped value. 4 // I f k does not match the key o f any element in the container, 43 // the f u n c t i o n i n s e r t s a new element with that key 44 // and r e t u r n s a r e f e r e n c e to i t s mapped value. 45 mymap[ a ] = 5 ; 46 } 47 Similary there exists multimap that allows store the equivalent values with the same methods; In C++11 there exists unordered map (hash map) that stores unordered value and time complexity is approximately O(1) (#include <unordered_map>). 4 STL sort 1 #i n c l u d e <algorithm > // std : : s o r t #i n c l u d e <vector > 3 4 bool cmpfunction ( i n t l, i n t r ) { return ( r < l ) ; } 5 6 s t r u c t cmpclass { 7 bool o perator ( ) ( i n t l, i n t r ) { return ( r < l ) ; } 8 } myobject ; 10 i n t main ( ) { 11 i n t myints [ ] = { 3, 7 1, 1, 4 5, 6, 8 0, 5 3, 3 3 } ; 1 std : : vector <int > myvector ( myints, myints+8) ; // using d e f a u l t comparison ( operator <) : 15 std : : s o r t ( myvector. begin ( ), myvector. begin ( ) +4) ; // using f u n c t i o n as comp 18 std : : s o r t ( myvector. begin ( ) +4, myvector. end ( ), myfunction ) ; 1 0 // using o b j e c t as comp 1 std : : s o r t ( myvector. begin ( ), myvector. end ( ), myobject ) ; } 3 8
9 There is function std :: stable_sort that preserves the relative order of the elements with equivalent values.
import java. u t i l. ;... Scanner sc = new Scanner ( System. in ) ;
CPSC 490 Input Input will always arrive on stdin. You may assume input is well-formed with respect to the problem specification; inappropriate input (e.g. text where a number was specified, number out
More informationENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October
ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October Contents 1 Cartesian Tree. Definition. 1 2 Cartesian Tree. Construction 1 3 Cartesian Tree. Operations. 2 3.1 Split............................................
More informationSolution suggestions for examination of Logic, Algorithms and Data Structures,
Department of VT12 Software Engineering and Managment DIT725 (TIG023) Göteborg University, Chalmers 24/5-12 Solution suggestions for examination of Logic, Algorithms and Data Structures, Date : April 26,
More informationQueues. Principles of Computer Science II. Basic features of Queues
Queues Principles of Computer Science II Abstract Data Types Ioannis Chatzigiannakis Sapienza University of Rome Lecture 9 Queue is also an abstract data type or a linear data structure, in which the first
More informationInsert Sorted List Insert as the Last element (the First element?) Delete Chaining. 2 Slide courtesy of Dr. Sang-Eon Park
1617 Preview Data Structure Review COSC COSC Data Structure Review Linked Lists Stacks Queues Linked Lists Singly Linked List Doubly Linked List Typical Functions s Hash Functions Collision Resolution
More informationTutorial 4. Dynamic Set: Amortized Analysis
Tutorial 4 Dynamic Set: Amortized Analysis Review Binary tree Complete binary tree Full binary tree 2-tree P115 Unlike common binary tree, the base case is not an empty tree, but a external node Heap Binary
More information2. Write a recursive function to add the first n terms of the alternating harmonic series:
CS 7B - Spring 2014 - Midterm 2. 5/8/14 Write responses on separate paper. 1. Write a recursive method that uses only addition, subtraction, and comparison to multiply two numbers. The basic engine for
More informationC++ For Science and Engineering Lecture 14
C++ For Science and Engineering Lecture 14 John Chrispell Tulane University Monday September 27, 2010 File Input and Output Recall writing text to standard out You must include the iostream header file.
More informationCS 7B - Fall Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm
CS 7B - Fall 2017 - Final Exam (take-home portion). Due Wednesday, 12/13 at 2 pm Write your responses to following questions on separate paper. Use complete sentences where appropriate and write out code
More informationPriority queues implemented via heaps
Priority queues implemented via heaps Comp Sci 1575 Data s Outline 1 2 3 Outline 1 2 3 Priority queue: most important first Recall: queue is FIFO A normal queue data structure will not implement a priority
More informationAmortized analysis. Amortized analysis
In amortized analysis the goal is to bound the worst case time of a sequence of operations on a data-structure. If n operations take T (n) time (worst case), the amortized cost of an operation is T (n)/n.
More informationData Structures and Algorithms Winter Semester
Page 0 German University in Cairo December 26, 2015 Media Engineering and Technology Faculty Prof. Dr. Slim Abdennadher Dr. Wael Abouelsadaat Data Structures and Algorithms Winter Semester 2015-2016 Final
More informationCS 5321: Advanced Algorithms Amortized Analysis of Data Structures. Motivations. Motivation cont d
CS 5321: Advanced Algorithms Amortized Analysis of Data Structures Ali Ebnenasir Department of Computer Science Michigan Technological University Motivations Why amortized analysis and when? Suppose you
More informationCS361 Homework #3 Solutions
CS6 Homework # Solutions. Suppose I have a hash table with 5 locations. I would like to know how many items I can store in it before it becomes fairly likely that I have a collision, i.e., that two items
More informationAlgorithms. Jordi Planes. Escola Politècnica Superior Universitat de Lleida
Algorithms Jordi Planes Escola Politècnica Superior Universitat de Lleida 2016 Syllabus What s been done Formal specification Computational Cost Transformation recursion iteration Divide and conquer Sorting
More informationData Structures and Algorithm. Xiaoqing Zheng
Data Structures and Algorithm Xiaoqing Zheng zhengxq@fudan.edu.cn MULTIPOP top[s] = 6 top[s] = 2 3 2 8 5 6 5 S MULTIPOP(S, x). while not STACK-EMPTY(S) and k 0 2. do POP(S) 3. k k MULTIPOP(S, 4) Analysis
More informationAMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM
AMORTIZED ANALYSIS binary counter multipop stack dynamic table Lecture slides by Kevin Wayne http://www.cs.princeton.edu/~wayne/kleinberg-tardos Last updated on 1/24/17 11:31 AM Amortized analysis Worst-case
More informationCSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )
CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26 Homework #2 ( Due: Nov 8 ) Task 1. [ 80 Points ] Average Case Analysis of Median-of-3 Quicksort Consider the median-of-3 quicksort algorithm
More informationChapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn
Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn Priority Queue / Heap Stores (key,data) pairs (like dictionary) But, different set of operations: Initialize-Heap: creates new empty heap
More informationAbstract Data Types. EECS 214, Fall 2017
Abstract Data Types EECS 214, Fall 2017 2 What is an ADT? An ADT defines: A set of (abstract) values A set of (abstract) operations on those values 2 What is an ADT? An ADT defines: A set of (abstract)
More informationCS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community
CS-141 Exam 2 Review October 19, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu Linked Lists 1. You are given the linked list: 1 2 3. You may assume that each node has one field
More information10:00 12:30. Do not open this problem booklet until the start of the examination is announced.
21 I 20 8 26 10:00 12:30 (1),. Do not open this problem booklet until the start of the examination is announced. (2) 4.. Answer the following 4 problems. Use the designated answer sheet for each problem.
More informationPart I: Definitions and Properties
Turing Machines Part I: Definitions and Properties Finite State Automata Deterministic Automata (DFSA) M = {Q, Σ, δ, q 0, F} -- Σ = Symbols -- Q = States -- q 0 = Initial State -- F = Accepting States
More informationLecture 7: Amortized analysis
Lecture 7: Amortized analysis In many applications we want to minimize the time for a sequence of operations. To sum worst-case times for single operations can be overly pessimistic, since it ignores correlation
More information! Insert. ! Remove largest. ! Copy. ! Create. ! Destroy. ! Test if empty. ! Fraud detection: isolate $$ transactions.
Priority Queues Priority Queues Data. Items that can be compared. Basic operations.! Insert.! Remove largest. defining ops! Copy.! Create.! Destroy.! Test if empty. generic ops Reference: Chapter 6, Algorithms
More informationStacks. Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks
Stacks Definitions Operations Implementation (Arrays and Linked Lists) Applications (system stack, expression evaluation) Data Structures 1 Stacks Stacks: Definitions and Operations A LIFO list: Last In,
More informationProgramming Abstractions
Programming Abstractions C S 1 0 6 X Cynthia Lee Today s topics: Performance issues in recursion Big-O performance analysis 2 Announcement: Recursive art contest! Go to http://recursivedrawing.com/ Make
More informationAssignment 5: Solutions
Comp 21: Algorithms and Data Structures Assignment : Solutions 1. Heaps. (a) First we remove the minimum key 1 (which we know is located at the root of the heap). We then replace it by the key in the position
More informationINTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.
C PROGRAMMING 1 INTRODUCTION This is not a full c-programming course. It is not even a full 'Java to c' programming course. 2 LITTERATURE 3. 1 FOR C-PROGRAMMING The C Programming Language (Kernighan and
More informationIntroduction to Computing II (ITI 1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Introduction
More informationINF2220: algorithms and data structures Series 1
Universitetet i Oslo Institutt for Informatikk I. Yu, D. Karabeg INF2220: algorithms and data structures Series 1 Topic Function growth & estimation of running time, trees (Exercises with hints for solution)
More informationAmortized Complexity Main Idea
omp2711 S1 2006 Amortized Complexity Example 1 Amortized Complexity Main Idea Worst case analysis of run time complexity is often too pessimistic. Average case analysis may be difficult because (i) it
More informationCS 7B - Spring Assignment: Adapting the calculator for bitwise expressions. due 2/21/18
CS 7B - Spring 2018 - Assignment: Adapting the calculator for bitwise expressions. due 2/21/18 Background Theory A bitwise number is a number in base 2. In base 2, place values are either a 1 or 0, depending
More informationITI Introduction to Computing II
(with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 11, 2015 Please don t print these lecture notes unless you really need to!
More informationCS213d Data Structures and Algorithms
CS21d Data Structures and Algorithms Heaps and their Applications Milind Sohoni IIT Bombay and IIT Dharwad March 22, 2017 1 / 18 What did I see today? March 22, 2017 2 / 18 Heap-Trees A tree T of height
More informationIntroduction to Computing II (ITI 1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Identification
More informationSearching. Sorting. Lambdas
.. s Babes-Bolyai University arthur@cs.ubbcluj.ro Overview 1 2 3 Feedback for the course You can write feedback at academicinfo.ubbcluj.ro It is both important as well as anonymous Write both what you
More information1 ListElement l e = f i r s t ; / / s t a r t i n g p o i n t 2 while ( l e. next!= n u l l ) 3 { l e = l e. next ; / / next step 4 } Removal
Präsenzstunden Today In the same room as in the first week Assignment 5 Felix Friedrich, Lars Widmer, Fabian Stutz TA lecture, Informatics II D-BAUG March 18, 2014 HIL E 15.2 15:00-18:00 Timon Gehr (arriving
More informationINTRODUCTION TO HASHING Dr. Thomas Hicks Trinity University. Data Set - SSN's from UTSA Class
Dr. Thomas E. Hicks Data Abstractions Homework - Hashing -1 - INTRODUCTION TO HASHING Dr. Thomas Hicks Trinity University Data Set - SSN's from UTSA Class 467 13 3881 498 66 2055 450 27 3804 456 49 5261
More informationSolutions to EoPL3 Exercises
Solutions to EoPL3 Exercises Release 0.1.0 Cheng Lian May 16, 2017 Contents 1 Contents 3 2 Overview 29 i ii Author Cheng Lian Contents 1 2 Contents CHAPTER 1 Contents Chapter 1.
More informationFlow Interfaces Compositional Abstractions of Concurrent Data Structures. Siddharth Krishna, Dennis Shasha, and Thomas Wies
Flow Interfaces Compositional Abstractions of Concurrent Data Structures Siddharth Krishna, Dennis Shasha, and Thomas Wies Background Verifying programs, separation logic, inductive predicates Verifying
More informationLecture Notes for Chapter 17: Amortized Analysis
Lecture Notes for Chapter 17: Amortized Analysis Chapter 17 overview Amortized analysis Analyze a sequence of operations on a data structure. Goal: Show that although some individual operations may be
More informationRound 5: Hashing. Tommi Junttila. Aalto University School of Science Department of Computer Science
Round 5: Hashing Tommi Junttila Aalto University School of Science Department of Computer Science CS-A1140 Data Structures and Algorithms Autumn 017 Tommi Junttila (Aalto University) Round 5 CS-A1140 /
More informationFast Sorting and Selection. A Lower Bound for Worst Case
Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 0 Fast Sorting and Selection USGS NEIC. Public domain government image. A Lower Bound
More informationRandomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th
CSE 3500 Algorithms and Complexity Fall 2016 Lecture 10: September 29, 2016 Quick sort: Average Run Time In the last lecture we started analyzing the expected run time of quick sort. Let X = k 1, k 2,...,
More informationITI Introduction to Computing II
(with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 9, 2019 Please don t print these lecture notes unless you really need to!
More information/463 Algorithms - Fall 2013 Solution to Assignment 4
600.363/463 Algorithms - Fall 2013 Solution to Assignment 4 (30+20 points) I (10 points) This problem brings in an extra constraint on the optimal binary search tree - for every node v, the number of nodes
More informationSorting Algorithms. We have already seen: Selection-sort Insertion-sort Heap-sort. We will see: Bubble-sort Merge-sort Quick-sort
Sorting Algorithms We have already seen: Selection-sort Insertion-sort Heap-sort We will see: Bubble-sort Merge-sort Quick-sort We will show that: O(n log n) is optimal for comparison based sorting. Bubble-Sort
More informationStatic Program Analysis
Static Program Analysis Xiangyu Zhang The slides are compiled from Alex Aiken s Michael D. Ernst s Sorin Lerner s A Scary Outline Type-based analysis Data-flow analysis Abstract interpretation Theorem
More informationConstructors - Cont. must be distinguished by the number or type of their arguments.
Constructors - Cont. 1 Constructors can be overloaded! must be distinguished by the number or type of their arguments. 2 When no constructor is defined, there is a default constructor with no arguments.
More informationFlow Interfaces Compositional Abstractions of Concurrent Data Structures. Siddharth Krishna, Dennis Shasha, and Thomas Wies
Flow Interfaces Compositional Abstractions of Concurrent Data Structures Siddharth Krishna, Dennis Shasha, and Thomas Wies Background Verifying programs, separation logic, inductive predicates Slides courtesy
More informationAn Introduction to Z3
An Introduction to Z3 Huixing Fang National Trusted Embedded Software Engineering Technology Research Center April 12, 2017 Outline 1 SMT 2 Z3 Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 2
More informationPartha Sarathi Mandal
MA 252: Data Structures and Algorithms Lecture 32 http://www.iitg.ernet.in/psm/indexing_ma252/y12/index.html Partha Sarathi Mandal Dept. of Mathematics, IIT Guwahati The All-Pairs Shortest Paths Problem
More information1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;
1 Trees The next major set of data structures belongs to what s called Trees. They are called that, because if you try to visualize the structure, it kind of looks like a tree (root, branches, and leafs).
More informationData Structures and Algorithms " Search Trees!!
Data Structures and Algorithms " Search Trees!! Outline" Binary Search Trees! AVL Trees! (2,4) Trees! 2 Binary Search Trees! "! < 6 2 > 1 4 = 8 9 Ordered Dictionaries" Keys are assumed to come from a total
More informationIntroduction to Computing II (ITI1121) FINAL EXAMINATION
Université d Ottawa Faculté de génie École de science informatique et de génie électrique University of Ottawa Faculty of engineering School of Electrical Engineering and Computer Science Identification
More informationDivide-and-Conquer Algorithms Part Two
Divide-and-Conquer Algorithms Part Two Recap from Last Time Divide-and-Conquer Algorithms A divide-and-conquer algorithm is one that works as follows: (Divide) Split the input apart into multiple smaller
More informationFundamental Algorithms
Fundamental Algorithms Chapter 5: Searching Michael Bader Winter 2014/15 Chapter 5: Searching, Winter 2014/15 1 Searching Definition (Search Problem) Input: a sequence or set A of n elements (objects)
More informationAlgorithm Analysis Divide and Conquer. Chung-Ang University, Jaesung Lee
Algorithm Analysis Divide and Conquer Chung-Ang University, Jaesung Lee Introduction 2 Divide and Conquer Paradigm 3 Advantages of Divide and Conquer Solving Difficult Problems Algorithm Efficiency Parallelism
More informationAmortized Analysis (chap. 17)
Amortized Analysis (chap. 17) Not just consider one operation, but a sequence of operations on a given data structure. Average cost over a sequence of operations. Probabilistic analysis: Average case running
More informationPremaster Course Algorithms 1 Chapter 3: Elementary Data Structures
Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures Christian Scheideler SS 2018 23.04.2018 Chapter 3 1 Overview Basic data structures Search structures (successor searching) Dictionaries
More informationHeaps and Priority Queues
Heaps and Priority Queues Motivation Situations where one has to choose the next most important from a collection. Examples: patients in an emergency room, scheduling programs in a multi-tasking OS. Need
More informationComp 11 Lectures. Mike Shah. July 12, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 12, / 33
Comp 11 Lectures Mike Shah Tufts University July 12, 2017 Mike Shah (Tufts University) Comp 11 Lectures July 12, 2017 1 / 33 Please do not distribute or host these slides without prior permission. Mike
More informationStructuring the verification of heap-manipulating programs
Structuring the verification of heap-manipulating programs Aleksandar Nanevski (IMDEA Madrid) Viktor Vafeiadis (MSR / Univ. of Cambridge) Josh Berdine (MSR Cambridge) Hoare/Separation Logic Hoare logic
More informationAccumulators. A Trivial Example in Oz. A Trivial Example in Prolog. MergeSort Example. Accumulators. Declarative Programming Techniques
Declarative Programming Techniques Accumulators, Difference Lists (VRH 3.4.3-3.4.4) Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL September 13, 2007 Accumulators Accumulator
More informationFunctional Data Structures
Functional Data Structures with Isabelle/HOL Tobias Nipkow Fakultät für Informatik Technische Universität München 2017-2-3 1 Part II Functional Data Structures 2 Chapter 1 Binary Trees 3 1 Binary Trees
More informationProblem Set 4 Solutions
Introduction to Algorithms October 8, 2001 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 18
More informationC++ For Science and Engineering Lecture 17
C++ For Science and Engineering Lecture 17 John Chrispell Tulane University Monday October 4, 2010 Functions and C-Style Strings Three methods for representing the C-style string: An array of char A quoted
More informationCS 7B - Spring Assignment: Ramsey Theory with Matrices and SFML. due 5/23/18
CS 7B - Spring 2018 - Assignment: Ramsey Theory with Matrices and SFML. due 5/23/18 Background Theory Ramsey theory, named after the British mathematician and philosopher Frank P. Ramsey, is a branch of
More informationBig-O Notation and Complexity Analysis
Big-O Notation and Complexity Analysis Jonathan Backer backer@cs.ubc.ca Department of Computer Science University of British Columbia May 28, 2007 Problems Reading: CLRS: Growth of Functions 3 GT: Algorithm
More informationProblem-Solving via Search Lecture 3
Lecture 3 What is a search problem? How do search algorithms work and how do we evaluate their performance? 1 Agenda An example task Problem formulation Infrastructure for search algorithms Complexity
More informationECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University
ECEN 651: Microprogrammed Control of Digital Systems Department of Electrical and Computer Engineering Texas A&M University Prof. Mi Lu TA: Ehsan Rohani Laboratory Exercise #4 MIPS Assembly and Simulation
More informationNumerical differentiation
Chapter 3 Numerical differentiation 3.1 Introduction Numerical integration and differentiation are some of the most frequently needed methods in computational physics. Quite often we are confronted with
More informationCMSC 132, Object-Oriented Programming II Summer Lecture 12
CMSC 132, Object-Oriented Programming II Summer 2016 Lecturer: Anwar Mamat Lecture 12 Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 12.1 Trees
More informationNormalization by Evaluation
Normalization by Evaluation Andreas Abel Department of Computer Science and Engineering Chalmers and Gothenburg University PhD Seminar in Mathematical Engineering EAFIT University, Medellin, Colombia 9
More informationLists, Stacks, and Queues (plus Priority Queues)
Lists, Stacks, and Queues (plus Priority Queues) The structures lists, stacks, and queues are composed of similar elements with different operations. Likewise, with mathematics: (Z, +, 0) vs. (Z,, 1) List
More informationTensor calculus with Lorene
Tensor calculus with Lorene Eric Gourgoulhon Laboratoire de l Univers et de ses Théories (LUTH) CNRS / Observatoire de Paris F-92195 Meudon, France eric.gourgoulhon@obspm.fr based on a collaboration with
More informationMath 391: Midterm 1.0 Spring 2016
Math 391: Midterm 1.0 Spring 2016 James Skon skonjp@kenyon.edu Test date: October 5 Submission Instructions: Give all your assumptions. Show all your work. Be as complete as possible. Grading Problem 1
More informationFundamental Algorithms
Chapter 5: Hash Tables, Winter 2018/19 1 Fundamental Algorithms Chapter 5: Hash Tables Jan Křetínský Winter 2018/19 Chapter 5: Hash Tables, Winter 2018/19 2 Generalised Search Problem Definition (Search
More informationSolutions. Problem 1: Suppose a polynomial in n of degree d has the form
Assignment 1 1. Problem 3-1 on p. 57 2. Problem 3-2 on p. 58 3. Problem 4-5 on p. 86 4. Problem 6-1 on p. 142 5. Problem 7-4 on p. 162 6. Prove correctness (including halting) of SelectionSort (use loop
More informationHW #4. (mostly by) Salim Sarımurat. 1) Insert 6 2) Insert 8 3) Insert 30. 4) Insert S a.
HW #4 (mostly by) Salim Sarımurat 04.12.2009 S. 1. 1. a. 1) Insert 6 2) Insert 8 3) Insert 30 4) Insert 40 2 5) Insert 50 6) Insert 61 7) Insert 70 1. b. 1) Insert 12 2) Insert 29 3) Insert 30 4) Insert
More informationCSE373: Data Structures and Algorithms Lecture 2: Proof by & Algorithm Analysis. Lauren Milne Summer 2015
CSE373: Data Structures and Algorithms Lecture 2: Proof by Induc@on & Algorithm Analysis Lauren Milne Summer 2015 Today Did everyone get email sent on Monday about TA Sec@ons star@ng on Thursday? Homework
More informationSearching. Constant time access. Hash function. Use an array? Better hash function? Hash function 4/18/2013. Chapter 9
Constant time access Searching Chapter 9 Linear search Θ(n) OK Binary search Θ(log n) Better Can we achieve Θ(1) search time? CPTR 318 1 2 Use an array? Use random access on a key such as a string? Hash
More informationUniversity of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003
University of New Mexico Department of Computer Science Midterm Examination CS 361 Data Structures and Algorithms Spring, 2003 Name: Email: Print your name and email, neatly in the space provided above;
More informationGraduate Analysis of Algorithms Dr. Haim Levkowitz
UMass Lowell Computer Science 9.53 Graduate Analysis of Algorithms Dr. Haim Levkowitz Fall 27 Lecture 5 Tuesday, 2 Oct 27 Amortized Analysis Overview Amortize: To pay off a debt, usually by periodic payments
More informationBloom Filter Redux. CS 270 Combinatorial Algorithms and Data Structures. UC Berkeley, Spring 2011
Bloom Filter Redux Matthias Vallentin Gene Pang CS 270 Combinatorial Algorithms and Data Structures UC Berkeley, Spring 2011 Inspiration Our background: network security, databases We deal with massive
More informationSorting and Searching. Tony Wong
Tony Wong 2017-03-04 Sorting Sorting is the reordering of array elements into a specific order (usually ascending) For example, array a[0..8] consists of the final exam scores of the n = 9 students in
More informationBin Sort. Sorting integers in Range [1,...,n] Add all elements to table and then
Sorting1 Bin Sort Sorting integers in Range [1,...,n] Add all elements to table and then Retrieve in order 1,2,3,...,n Stable Sorting Method (repeated elements will end up in their original order) Numbers
More informationMATHEMATICAL OBJECTS in
MATHEMATICAL OBJECTS in Computational Tools in a Unified Object-Oriented Approach Yair Shapira @ CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor & Francis
More informationPart IA Algorithms Notes
Part IA Algorithms Notes 1 Sorting 1.1 Insertion Sort 1 d e f i n s e r t i o n S o r t ( a ) : 2 f o r i from 1 i n c l u d e d to l e n ( a ) excluded : 3 4 j = i 1 5 w h i l e j >= 0 and a [ i ] > a
More informationQuiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)
Introduction to Algorithms October 14, 2009 Massachusetts Institute of Technology 6.006 Spring 2009 Professors Srini Devadas and Constantinos (Costis) Daskalakis Quiz 1 Solutions Quiz 1 Solutions Problem
More informationIntroduction to Hash Tables
Introduction to Hash Tables Hash Functions A hash table represents a simple but efficient way of storing, finding, and removing elements. In general, a hash table is represented by an array of cells. In
More informationToday: Amortized Analysis
Today: Amortized Analysis COSC 581, Algorithms March 6, 2014 Many of these slides are adapted from several online sources Today s class: Chapter 17 Reading Assignments Reading assignment for next class:
More informationData structures Exercise 1 solution. Question 1. Let s start by writing all the functions in big O notation:
Data structures Exercise 1 solution Question 1 Let s start by writing all the functions in big O notation: f 1 (n) = 2017 = O(1), f 2 (n) = 2 log 2 n = O(n 2 ), f 3 (n) = 2 n = O(2 n ), f 4 (n) = 1 = O
More informationDeclarative Programming Techniques
Declarative Programming Techniques Accumulators (CTM 3.4.3) Difference Lists (CTM 3.4.4) Carlos Varela RPI Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL December 1, 2015 C. Varela; Adapted
More informationGreedy. Outline CS141. Stefano Lonardi, UCR 1. Activity selection Fractional knapsack Huffman encoding Later:
October 5, 017 Greedy Chapters 5 of Dasgupta et al. 1 Activity selection Fractional knapsack Huffman encoding Later: Outline Dijkstra (single source shortest path) Prim and Kruskal (minimum spanning tree)
More informationAmortized Analysis. DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person
Amortized Analysis DistributeMoney(n, k) 1 Each of n people gets $1. 2 for i = 1to k 3 do Give a dollar to a random person What is the maximum amount of money I can receive? Amortized Analysis DistributeMoney(n,
More informationIn this approach, the ground state of the system is found by modeling a diffusion process.
The Diffusion Monte Carlo (DMC) Method In this approach, the ground state of the system is found by modeling a diffusion process. Diffusion and random walks Consider a random walk on a lattice with spacing
More informationAdvanced Implementations of Tables: Balanced Search Trees and Hashing
Advanced Implementations of Tables: Balanced Search Trees and Hashing Balanced Search Trees Binary search tree operations such as insert, delete, retrieve, etc. depend on the length of the path to the
More informationReversing Lists Haskell: The craft of functional programming, by Simon Thompson, page 140
Reversing Lists Haskell: The craft of functional programming, by Simon Thompson, page 14 Reverse.hs rev :: [t] -> [t] rev [] = [] rev (e:l) = (rev l) ++ [e] 6 5 rev [1..n] n**2/2 4 3 2 1 2 4 6 8 1 1 Array
More information