Data structure design Jordi Cortadella Department of Computer Science.

Slides:



Advertisements
Similar presentations
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Advertisements

Introduction to Programming (in C++) Subprograms: procedures and functions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Introduction to Programming (in C++) Advanced examples Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Chapter 2: Algorithm Discovery and Design
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Computer Science 1620 Programming & Problem Solving.
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Introduction to Programming (in C++) Vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Recursion Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Data and statements Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Introduction to Programming (in C++) Conclusions Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Sorting and Vectors Mechanism for representing lists JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
CS161 Topic #15 1 Today in CS161 Lecture #15 Practicing! Writing Programs to Practice Write a game program (1 player) of Mad Math Reuse the functions to.
Reasoning with invariants Jordi Cortadella Department of Computer Science.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chars and strings Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Data structures Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Matrices Jordi Cortadella Department of Computer Science.
Prime numbers Jordi Cortadella Department of Computer Science.
Gaussian elimination Jordi Cortadella Department of Computer Science.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Sequences Jordi Cortadella Department of Computer Science.
Search algorithms for vectors Jordi Cortadella Department of Computer Science.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Sudoku Jordi Cortadella Department of Computer Science.
CSC 211 Data Structures Lecture 13
First steps Jordi Cortadella Department of Computer Science.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Introduction to Programming (in C++) Numerical algorithms Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
Data Structures & Algorithms
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
1 CS161 Introduction to Computer Science Topic #8.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Vectors Jordi Cortadella Department of Computer Science.
HANGMAN OPTIMIZATION Kyle Anderson, Sean Barton and Brandyn Deffinbaugh.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
Parameter passing Jordi Cortadella Department of Computer Science.
Functions Jordi Cortadella Department of Computer Science.
Combinatorial problems Jordi Cortadella Department of Computer Science.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Jordi Cortadella, Ricard Gavaldà, Fernando Orejas
Introduction to Programming (in C++) Data structures
Reasoning with invariants
Jordi Cortadella Department of Computer Science
Combinatorial problems
Introduction to C++ Programming
Jordi Cortadella Department of Computer Science
Let’s all Repeat Together
Search algorithms for vectors
Jordi Cortadella and Jordi Petit Department of Computer Science
Jordi Cortadella Department of Computer Science
Life is Full of Alternatives
CS150 Introduction to Computer Science 1
Chapter 18 Recursion.
COMS 261 Computer Science I
Introduction to Programming (in C++) Advanced examples
Presentation transcript:

Data structure design Jordi Cortadella Department of Computer Science

Data structure design Up to now, designing a program (or a procedure or a function) has meant designing an algorithm. The structure of the data on which the algorithm operates was part of the problem statement. However, when we create a program, we often need to design data structures to store data and intermediate results. The design of appropriate data structures is often critical: – to be able to solve the problem – to provide a more efficient solution Introduction to Programming© Dept. CS, UPC2

Sports tournament Design a program that reads the participants in a knockout tournament and the list of results for each round. The program must write the name of the winner. Assumptions: – The number of participants is a power of two. – The list represents the participation order, i.e. in the first round, the first participant plays with the second, the third with the fourth, etc. In the second round, the winner of the first match plays against the winner of the second match, the winner of the third match plays against the winner of the fourth match, etc. At the end, the winner of the first semi-final will play against the winner of the second semi- final. The specification of the program could be as follows: // Pre: the input contains the number of players, // the players and the results of the tournament. // Post: the winner has been written at the output. Introduction to Programming© Dept. CS, UPC3

Sports tournament Nadal – Djokovic 3-0 Nadal – Berdych 3-1 Nadal – Murray 2-0 Berdych – Soderling 3-1 Federer – Djokovic 2-3 Federer – Ferrer 3-1 Djokovic – Roddick 3-2 Introduction to Programming© Dept. CS, UPC4 Input (example): 8 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick

Sports tournament A convenient data structure that would enable an efficient solution would be a vector with 2n-1 locations (n is the number of participants): – The first n locations would store the participants. – The following n/2 locations would store the winners of the first round. – The following n/4 locations would store the winners of the second round, etc. – The last location would store the name of the winner. Introduction to Programming© Dept. CS, UPC5

Sports tournament Input: 8 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal Introduction to Programming© Dept. CS, UPC6 First round Second round Third round Winner

Sports tournament The algorithm could run as follows: – First, it reads the number of participants and their names. They will be stored in the locations 0…n-1 of the vector. – Next, it fills up the rest of the locations. Two pointers might be used. The first pointer (j) points at the locations of the players of a match. The second pointer (k) points at the location where the winner will be stored. // Inv: players[n..k-1] contains the // winners of the matches stored // in players[0..j-1] Introduction to Programming© Dept. CS, UPC7

Sports tournament Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Introduction to Programming© Dept. CS, UPC8 Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal Murray Berdych Soderling Federer Ferrer Djokovic Roddick Nadal Berdych Federer Djokovic Nadal Djokovic Nadal

Sports tournament int main() { int n; cin >> n; // Number of participants vector players(2n - 1); // Read the participants for (int i = 0; i > players[i]; int j = 0; // Read the results and calculate the winners for (int k = n; k < 2n - 1; ++k) { int score1, score2; cin >> score1 >> score2; if (score1 > score2) players[k] = players[j]; else players[k] = players[j + 1]; j = j + 2; } cout << players[2n - 2] << endl; } Introduction to Programming© Dept. CS, UPC9

Sports tournament Exercise: Modify the previous algorithm using only a vector with n strings, i.e., vector players(n) Introduction to Programming© Dept. CS, UPC10

Most frequent letter Problem: design a function that reads a text and reports the most frequent letter in the text and its frequency (as a percentage). The letter case is ignored. That is: struct Res { char letter; // letter is in ‘a’..‘z’ double freq; // 0 <= freq <= 100 }; // Pre: the input contains a text // Returns the most frequent letter in the text // and its frequency, as a percentage, // ignoring the letter case Res most_freq_letter(); Introduction to Programming© Dept. CS, UPC11

Most frequent letter The obvious algorithm is to sequentially read the characters of the text and keep a record of how many times we have seen each letter. Once we have read all the text, we compute the letter with the highest frequency, and report it with the frequency divided by the text length  100. To do this process efficiently, we need fast access to the number of occurrences of each letter seen so far. Introduction to Programming© Dept. CS, UPC12

Most frequent letter Solution: keep a vector of length N, where N is the number of distinct letters. The i-th component contains the number of occurrences of the i-th letter so far. Observation: the problem specification did not mention any vectors. We introduce one to solve the problem efficiently. const int N = int('z') - int('a') + 1; vector occs(N, 0); int n_letters; // Inv: n_letters is the number of letters read // so far, occs[i] is the number of occurrences // of letter ‘a’ + i in the text read so far Introduction to Programming© Dept. CS, UPC13

Most frequent letter Res most_freq_letter() { const int N = int('z') - int('a') + 1; vector occs(N, 0); int n_letters = 0; char c; // n_letters contains the number of letters in the text, and occs[i] // contains the number of occurrences of letter ‘a’ + i in the text while (cin >> c) { if (c >= 'A' and c = 'a' and c <= 'z') { ++n_letters; ++occs[int(c) - int('a')]; } } int imax = 0; // imax = the index of the highest value in occs[0..i-1] for (int i = 1; i occs[imax]) imax = i; } Res r; r.letter = 'a' + imax; if (n_letters > 0) r.freq = double(occs[imax])100/n_letters; else r.freq = 0; // 0% if no letters in the text return r; } Introduction to Programming© Dept. CS, UPC14

Pangrams A pangram is a sentence containing all the letters in the alphabet. An English pangram: The quick brown dog jumps over the lazy fox A Catalan pangram: Jove xef, porti whisky amb quinze glaçons d’hidrogen, coi! Problem: design a function that reads a sentence and says whether it is a pangram. That is, // Pre: the input contains a sentence. // Returns true if the input sentence is a pangram // and false otherwise. bool is_pangram(); Introduction to Programming© Dept. CS, UPC15

Pangrams The algorithm is similar to previous the problem: – Use a vector with one position per letter as a data structure. – Read the sentence and keep track of the number of occurrences of each letter. – Then check that each letter appeared at least once. Introduction to Programming© Dept. CS, UPC16

Pangrams bool is_pangram() { const int N = int('z') - int('a') + 1; vector appear(N, false); char c; // Inv: appear[i] indicates whether the letter // ‘a’ + i has already appeared in the text. while (cin >> c) { if (c >= 'A' and c = 'a' and c <= 'z') appear[int(c) - int('a')] = true; } // Check that all letters appear for (int i = 0; i < N; ++i) { if (not appear[i]) return false; } return true; } Introduction to Programming© Dept. CS, UPC17

Brackets A number of characters go in pairs, one used to “open” a part of a text and the other to “close” it. Some examples are: ( ) (parenthesis), [ ] (square brackets) { } (curly brackets) (angle brackets) ¿ ? (question marks - Spanish) ¡ ! (exclamation marks - Spanish) “ ” (double quotes) ‘ ’ (single quotes) Introduction to Programming© Dept. CS, UPC18

Brackets The correct use of brackets can be defined by three rules: 1.Every opening bracket is followed in the text by a matching closing bracket of the same type – though not necessarily immediately. 2.Vice versa, every closing bracket is preceded in the text by a matching opening bracket of the same type. 3.The text between an opening bracket and its matching closing bracket must include the closing bracket of every opening bracket it contains, and the opening bracket of every closing bracket it contains (It’s ok if you need to read this more than once) Introduction to Programming© Dept. CS, UPC19

Brackets Exercise: design a function that reads a sequence of bracket characters of different kinds, and tells whether the sequence respects the bracketing rules. ([][{}]¿¡¡!!?)[]  (([][{][}]¿¡¡!!?)[]   (([][{}]¿¡¡   ([]){})  Introduction to Programming© Dept. CS, UPC20

Brackets That is, we want: // Pre: the input contains a nonempty sequence // of bracket chars // Returns true if the sequence is correctly // bracketed, and false otherwise. bool brackets(); Suppose we use the following functions: bool is_open_br(char c); // Is c an opening bracket? bool is_clos_br(char c); // Is c a closing bracket? char match(char c); // Returns the match of c Introduction to Programming© Dept. CS, UPC21

Brackets Strategy: keep a vector of unclosed open brackets. When we see an opening bracket in the input, we store it in unclosed (its matching closing bracket should arrive later). When we see a closing bracket in the input, either its matching opening bracket must be the last element in unclosed (and we can remove both), or we know the sequence is incorrect. At the end of the sequence unclosed should be empty. Introduction to Programming© Dept. CS, UPC22

Brackets // Pre: the input contains a nonempty sequence of bracket chars // Returns true if the sequence is correctly bracketed, // and false otherwise. bool brackets() { vector unclosed; char c; while (cin >> c) { if (is_open_br(c)) unclosed.push_back(c); else if (unclosed.size() == 0) return false; else if (match(c) != unclosed[unclosed.size()-1]) return false; else unclosed.pop_back(); } // Check that no bracket has been left open return unclosed.size() == 0; } Introduction to Programming© Dept. CS, UPC23

Summary As programming problems become complex, it is important to first define the data structures required to represent data. When defining the data structures, think of the operations you will have to perform. If necessary, change the data structure. Introduction to Programming© Dept. CS, UPC24