Data Structures and Algorithms 1

Similar documents
AP Computer Science 4325

AP Computer Science AB

Quantitative Finance COURSE NUMBER: 22:839:615 COURSE TITLE: Special Topics Oriented Programming 2

Topics for CSCI 151 Final Exam Wednesday, May 10

QUIZ. 0] Define arrays 1] Define records 2] How are arrays and records: (a) similar? (b) different?

SYLLABUS Type of evaluation

CSC2100-Data Structures

Samples of Evidence to Satisfy the AP Computer Science AB Curricular Requirements

About this exam review

CSE373: Data Structure & Algorithms Lecture 18: Comparison Sorting. Dan Grossman Fall 2013

CSE 373: Data Structures and Algorithms

CSE 373: Data Structures and Algorithms

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

CS 445: Data Structures Final Examination: Study Guide

CSE 2123 Sorting. Jeremy Morris

School of Computing and Information Sciences. Course Title: Data Structures Date: 3/30/2010 Course Number: COP 3530 Number of Credits: 3

CSc 225 Algorithms and Data Structures I Algorithm Analysis

Lecture 9: Sorting Algorithms

CS 483. Jana Kosecka CS Dept Eng. Building

CS2013 Course Syllabus Spring 2018 Lecture: Mon/Wed 2:00 P.M. 2:50 P.M. SH C259 Lab: Mon/Wed 2:50 P.M. 4:00 P.M. SH C259

Data Structures and Algorithms

Announcements. Last modified: Thu Nov 29 16:15: CS61B: Lecture #40 1

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

Priority queues. Priority queues. Priority queue operations

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

CSE 332: Data Structures & Parallelism Lecture 12: Comparison Sorting. Ruth Anderson Winter 2019

CS301 - Data Structures Glossary By

Introduction p. 1 Pseudocode p. 2 Algorithm Header p. 2 Purpose, Conditions, and Return p. 3 Statement Numbers p. 4 Variables p. 4 Algorithm Analysis

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Total Score /15 /20 /30 /10 /5 /20 Grader

Administrivia. HW on recursive lists due on Wednesday. Reading for Wednesday: Chapter 9 thru Quicksort (pp )

Lecture 13: AVL Trees and Binary Heaps

Boca Raton Community High School AP Computer Science A - Syllabus 2009/10

CS61BL. Lecture 5: Graphs Sorting

1) What is the primary purpose of template functions? 2) Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

Unit 6 Chapter 15 EXAMPLES OF COMPLEXITY CALCULATION

Total No. of Questions :09] [Total No. of Pages : 02. II/IV B.Tech. DEGREE EXAMINATIONS, NOV/DEC First Semester CSE/IT DATA STRUCTURES USING C

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

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

Course Review for Finals. Cpt S 223 Fall 2008

12 Abstract Data Types

Syllabus for Bachelor of Technology. Computer Engineering. Subject Code: 01CE0301. Subject Name: Data Structure. B.Tech. Year - II

CS/COE 1501 cs.pitt.edu/~bill/1501/ Introduction

CS/COE 1501

Data Structures and Algorithms

COT 6405: Analysis of Algorithms. Giri Narasimhan. ECS 389; Phone: x3748

CS2 Algorithms and Data Structures Note 1

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Prelim 2 Solution. CS 2110, November 19, 2015, 5:30 PM Total. Sorting Invariants Max Score Grader

Home Works and Assignments

cs2010: algorithms and data structures

INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad

Algorithms and Data Structures

CS2013 Course Syllabus Spring 2017 Lecture: Friday 8:00 A.M. 9:40 A.M. Lab: Friday 9:40 A.M. 12:00 Noon

Data_Structures - Hackveda

Programming and Data Structures Prof. N.S. Narayanaswamy Department of Computer Science and Engineering Indian Institute of Technology, Madras

Lecture 7. Transform-and-Conquer

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid. Fall 2018

Announcements HEAPS & PRIORITY QUEUES. Abstract vs concrete data structures. Concrete data structures. Abstract data structures

CPSC 2380 Data Structures and Algorithms

CS 206 Introduction to Computer Science II

Sorting and Searching Algorithms

Course Review. Cpt S 223 Fall 2010

Unit 10: Sorting/Searching/Recursion

A loose end: binary search

CS2102, B11 Exam 1. Name:

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 25: Review and Open Problems

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

COMP251: Algorithms and Data Structures. Jérôme Waldispühl School of Computer Science McGill University

COS 226 Midterm Exam, Spring 2009

Algorithm Design and Analysis

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN. Sec. Name Office Office hour Mail id

Algorithms: Design & Practice

Sorting. CPSC 259: Data Structures and Algorithms for Electrical Engineers. Hassan Khosravi

Lecture #40: Course Summary

Draw a diagram of an empty circular queue and describe it to the reader.

COMP Data Structures

CS 270 Algorithms. Oliver Kullmann. Binary search. Lists. Background: Pointers. Trees. Implementing rooted trees. Tutorial

Course Review. Cpt S 223 Fall 2009

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Overview of Sorting Algorithms

Programming II (CS300)

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

SOLUTIONS. COMP103 Introduction to Data Structures and Algorithms

CSCE 210/2201 Data Structures and Algorithms. Prof. Amr Goneid

CS302 Data Structures using C++

SARDAR RAJA COLLEGE OF ENGINEERING

OF VICTORIA EXAMINATIONS- DECEMBER 2010 CSC

CHAPTER 7 Iris Hui-Ru Jiang Fall 2008

Lecture 6 Sorting and Searching

PROGRAMMING IN C++ (Regulation 2008) Answer ALL questions PART A (10 2 = 20 Marks) PART B (5 16 = 80 Marks) function? (8)

CS 210 Algorithms and Data Structures College of Information Technology and Engineering Weisberg Division of Engineering and Computer Science

CS210 (161) with Dr. Basit Qureshi Final Exam Weight 40%

Prelim 2 Solution. CS 2110, November 19, 2015, 7:30 PM Total. Sorting Invariants Max Score Grader

Outline. Computer Science 331. Three Classical Algorithms. The Sorting Problem. Classical Sorting Algorithms. Mike Jacobson. Description Analysis

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Lecture 10: Recursion vs Iteration

( ) + n. ( ) = n "1) + n. ( ) = T n 2. ( ) = 2T n 2. ( ) = T( n 2 ) +1

Advanced Algorithms and Data Structures

Transcription:

CS Content Academy: Data Structures Outline 1. How Does Data Structures Fit into CS What Is CS? 2. What Are Data Structures and Algorithms? 3. The Structure of Data Structures and Algorithms 3.1 Part A: Container Hierarchy 3.2 Part B: Algorithm Analysis 3.3 Part C: Searching and Sorting Algorithms 3.4 Part D: Advanced Topics 4. Themes 4.1 ADTs and Contiguous vs. Linked Data Structures 4.2 Recursion vs. Stacks 4.3 Trade-Offs 5. Pedagogy 5.1 Projects 5.2 Execution by Hand 5.3 Visualization 5.4 Active Learning 6. Where to Go from Here Data Structures and Algorithms 1

1. What is Computer Science? Computer Science is the discipline that answers the question What can we compute and how can we compute it? What can we compute? Theory What counts as computing computability theory (automata, etc.) What can be computed in principle computability, decidability What computations are tractable abstract complexity theory Practice Applications of automata Artificial Intelligence Halting problem, etc. Cryptography How can we compute things? Hardware versus software we only deal with software How can we compute things, from most to least general Theory Algorithms abstract complexity theory (P, NP, etc.) Languages Formal language theory Graphs Graph theory Logic Practice Algorithms and data structures Algorithm analysis concrete complexity theory Programming languages Compilers and interpreters Networking Artificial Intelligence Program Validation Operating Systems, Database Systems, Games and Simulations, Graphics and Media, Business Applications, Scientific and Engineering Applications, etc. Computing as a human activity: Software engineering Societal and ethical issues Cyber security and cyber war Data Structures and Algorithms 2

2. What Are Data Structures and Algorithms? Data structure an arrangement of data in memory. Examples: arrays, singly linked lists, linked trees, hash tables, etc. Algorithm a finite sequence of steps for accomplishing some computational task. Examples: division algorithm, factorial function, binary search, insertion sort, etc. Abstract data type (ADT) a set of values (the carrier set), and operations (the method set) on those values. Examples: integers, strings, stacks, queues, etc. Data type an implementation of an abstract data type on a computer. Remarks Examples: Java int, String, ArrayList, etc. 1. Virtually every program stores some data, and it is de facto arranged in memory, so virtually every program uses data structures. 2. Virtually every program has a finite sequence of steps for doing something, so virtually every program has algorithms. 3. Implementing an ADT (making a data type) requires figuring out how to represent values of its carrier set on the computer and how to realize the elements of its method set in subprograms. 4 Representing values requires arranging data in memory, so implementing ADTs requires data structures. 5. Realizing operations on a computer requires specifying a finite sequence of steps to carry out the operation, so implementing ADTs requires algorithms. 6. Every class embodies an ADT. 7. Programming is largely the design and implementation of ADTs, typically (nowadays) as classes in an OO language, so programming essentially requires using data structures and algorithms. 8. Computer scientists have been studying data structures for over 60 now, so there is a wealth of material on this topic. Data Structures and Algorithms 3

3.1 Structure of DS&A: A Container Hierarchy Data Structures and Algorithms 4

Framework 3.2 Structure of DS&A: Algorithm Analysis 1. Choose a measure for the size of the input. 2. Choose a basic operation to count. 3. Determine whether the algorithm has different complexity for various inputs of size n; if so, then derive measures for the best case complexity B(n), worst case complexity W(n), and average case complexity A(n) as functions of the size of the input; if not, then derive a measure for the every case complexity C(n) as a function of the size of the input. 4. Determine the order of growth of the complexity measures for the algorithm. Orders of Growth n lg n n n lg n n² n³ 2n n! 10 3.3 10 33 100 1000 1024 3,628,800 100 6.6 100 660 10,000 1,000,000 1.3 10³0 9.3 10¹57 1000 10 1000 10,000 1,000,000 109 10,000 13 10,000 130,000 108 10¹² 100,000 17 100,000 1,700,000 10¹0 10¹5 1,000,000 20 1,000,000 2 107 10¹² 10¹8 Example: Find the Minimum Value in an Array Strategy: Examine every element of the array and remember the smallest so far. 1 public int selectionsort( int[] A ) { 2 int min = A[0]; 3 for ( int i = 1; i < A.length; i++ ) 4 if ( A[i] < min ) min = A[i]; 5 return min; 6 } 1. Input size measure: size of the array n 2. Basic operation: comparison of keys 3. Always does the same thing for inputs of size n, so figure out C(n) 4. Order of growth: O(n) Data Structures and Algorithms 5

Sorting Algorithms 3.3 Structure of DS&A: Searching and Sorting Algorithms Consider and analyze (time permitting) several standard sorting algorithms, including bubble sort, selection sort, insertion sort, Shell sort, merge sort, quicksort, and heapsort. Example: Selection Sort Strategy: repeatedly find the smallest element in the array and place it at the start of the unsorted portion. 1 public void selectionsort( int[] A ) { 2 for ( int i = 0; i < A.length-1; i++ ) { 3 int minindex = i; 4 for ( int j = i+1; j < A.length; j++ ) 5 if ( A[j] < A[minIndex] ) minindex = j; 6 int tmp = A[i]; A[i] = A[minIndex]; A[minIndex] = tmp; 7 } 8 } 1. Input size measure: size of the array n 2. Basic operation: comparison of keys 3. Always does the same thing for inputs of size n, so figure out C(n) 4. Order of growth: O(n²) Searching Algorithms Consider and analyze sequential search and binary search. Introduce binary search trees and consider algorithms on binary trees, including traversal, insertion, deletion, and search. Example: Sequential Search Strategy: examine each element until the key is found or the end of the array or list is reached. 1 public int ssearch( int[] A, int key ) { 2 for ( int i = 0; i < A.length-1; i++ ) 3 if ( key == A[i] ) return i; 4 return -1; 5 } 1. Input size measure: size of the array n 2. Basic operation: comparison of keys 3. Behavior varies depending on the key and the array, so figure out B(n), W(n), and A(n) 4. B(n) = 1, W(n) = n, A(n) n/2, which are all O(n) Data Structures and Algorithms 6

3.4 Structure of DS&A: Advanced Topics More containers: Bags Dequeues Randomizer (random queue) Quad trees Etc. Graphs Matrix vs adjacency list representations Directed vs undirected graphs Graph ADT Breadth-first vs depth-first search Graph algorithms: spanning trees, minmum paths, etc. Balanced Trees AVL trees 2-3 trees and red-black trees B-trees Strings Sorting Tries String search algorithms Regular expressions Data compression More... Data Structures and Algorithms 7

4.1 Themes: ADTs and Contiguous vs. Linked Data Structures Every container is first considered as an ADT, and then we tink about how to represent carrier set elements contiguously or using linked structures. Algorithms for these structures are then considered/analyzed to decide when each is best to use. Example: HashSets vs TreeSets HashSets can store, remove, and find data a little more quickly than TreeSets, but TreeSets can be traversed in order and HashSets can t. 4.2 Themes: Recursion vs. Stacks Every algorithm using stacks can be replaced with one using recursion and vice-versa. Sometimes a job is easier to do with a stack, sometimes it is easier with recursion, and sometimes it doesn t matter. Also, recursion can be eliminated without a stack for tail-recursive algorithms. We consider stack-based vs recursive algorithms and elimination of tail-recursion as we study various containers and searching and sorting algorithms. Example: Recursion Elimination from Binary Search 01 public int bsearchrecursive( int[] A, int key ) { 02 return bsearchhelper(a, key, 0, A.length-1); 03 } 04 05 private int bsearchhelper( int[] A, int key, int lo, int hi ) { 06 if ( hi < lo ) return -1; 07 int m := (lo+hi)/2; 08 if ( key == A[m] ) return m; 09 else if ( key < A[m] ) return bsearchhelper(a, key, lo, m-1); 10 else return BSearchHelper(A, key, m+1, hi); 11 } 12 13 public int bsearch( int[] A, int key ) { 14 int lo = 0; 15 int hi = A.length-1; 16 while (lo <= hi) { 17 int m := (lo+hi)/2; 18 if ( key == A[m] ) return m; 19 else if ( key < A[m] ) hi = m-1; 20 else lo = m+1; 21 } 22 return -1; 23 } Recursive binary search is tail recursive, so recursion can be eliminated, speeding up the algorithm slightly and using less memory to run it. Time vs Space Efficiency vs Complexity/Reliability Needed vs Unneeded Operations 4.3 Themes: Trade-Offs Data Structures and Algorithms 8

5.1 Pedagogy: Projects Data structures and algorithms are essential to programming, so learning about them is also about learning how to program. The best way to learn to program is to do it. Example Projects Implement the container hierarchy Write a program to compare the time to sort various lists with algorithms Comparison of all sorts on small slices of random data. N Bubble Select Insert Shell Merge QBasic QImprove Heap Inspct 10000 0.282 0.148 0.074 0.001 0.001 0.001 0.001 0.002 0.001 20000 1.127 0.592 0.298 0.003 0.003 0.002 0.002 0.005 0.002 40000 4.515 2.365 1.190 0.007 0.006 0.004 0.004 0.010 0.004 80000 18.003 9.453 4.725 0.015 0.012 0.008 0.008 0.022 0.008 Use containers to do something, like a checkout simulation using queues, or a concordance using tree maps Write a simple game that uses interesting data structures, like Boggle 5.2 Pedagogy: Execution by Hand This is an essential skill that students need to be taught. Example 01 public int fib( int n ) { 02 if ( n <= 1 ) return 1; 03 return fib(n-1) 04 + fib(n-2); 05 } Compute fib(4) Exercise Compute bsearch(new int[] {1, 3, 6, 7, 12, 19, 23, 25}, 5) Compute bsearchrecursive(new int[] {1, 3, 6, 7, 12, 19, 23, 25}, 23) 5.3 Pedagogy: Visualization Visualization can sometimes help students understand how an algorithm works. Visualization can also demonstrate how algorithms to accomplish the same task differ. Investigate Sorting Algorithm Animations: http://www.sorting-algorithms.com Data Structure Visualizations: http://www.cs.usfca.edu/~galles/visualization Data Structures and Algorithms 9

Labs POGIL 5.4 Pedagogy: Active Learning Labs are a valuable way to teach programming and to give students a supportive environment for code development POGIL stands for Process Oriented Guided Inquiry Learning POGIL is very popular and widely used in Chemistry My in-class activities are based on this approach Process 1. Students prepare before class (read) 2. Students take a graded readiness quiz individually 3. Students take a graded IFAT-form quiz as a group 4. Students work through the activity in class in groups 5. Often, students do homework individually Examples: Attached Books 6. Where to Go From Here Most college data structure and algorithms textbooks are perfectly fine They mainly differ in the languages used and the level of presentation An advanced book using Java: Robert Sedgewick and Kevin Wayne, Algorithms, 4th Edition, Addison-Wesley, 2011 A less advanced book using Ruby (and it s free): Christopher Fox. Concise Notes on Data Structures and Algorithms: Ruby Edition, Ventus Publishing ApS, 2012 (http://bookboon.com/en/concise-notes-on-data-structures-and-algorithms-ebook) The Web MOOCS (Coursera) Various (Stanford CS Education Library, etc.) Free video lectures (YouTube, freevideolectures.com). Data Structures and Algorithms 10