• No results found

Data Structures and Algorithms

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures and Algorithms"

Copied!
36
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures and Algorithms

COMP 103 - 2021 T2

.

Peter Andreae

( “Pondy” )

Hui Ma

Thomas K

ühne

Marcus Frean (CC)

(2)

Welcome!

COMP 103 = 2nd core course for COMP, CGRA, SWEN, CYBR, ECEN, NWEN

Core principles of Computer Science and essential programming skills

Builds on COMP 102/112

(3)

What’s the course about?

Using and understanding Collections

• lists, sets, maps, stacks, queues, …

Writing code with Trees and linked structures.

How to write code using collections efficiently

Designing and implementing algorithms

Using recursion

Recurring theme: Efficiency

• How fast is it?

(4)

Concepts

Fundamental principles:

• collections, linked structures, algorithms, recursion, efficiency,

Focusing on:

• Using collections, rather than implementing collections.

• Becoming good, effective, efficient programmers.

(5)

Observations from COMP 102

Not engaging in lectures is dangerous for your learning!

Many were uncomfortable about asking questions in 102:

• in lectures, in labs, on the forum

because other students put them down

Putting people down for asking questions or for trying to learn is

unprofessional

obnoxious

unacceptable

(6)

Admin: People

Coordinator:

Marcus Frean [Cotton 337] (from week 4) [email protected]

Lecturers:

Peter Andreae ("Pondy") [Cotton 336] [email protected]

Hui Ma [Cotton 259] [email protected]

Thomas Kühne [Cotton 233] [email protected]

Tutors:

Ghassem Narimani [Cotton 251] [email protected]

Programmer:

Monique Damito [Cotton327] [email protected]

Course URL:

http://ecs.victoria.ac.nz/Courses/COMP103_2021T2

(7)

Prereqs

Prerequisite for COMP 103

You need to have passed

COMP102 or COMP112

If you did not meet this,

you will need to take 102/112 again

Passing COMP 102/112 also required for ENGR 110 and CGRA151

COMP 132 is a course on programming with data this trimester

(8)

Prereqs

What is assumed from COMP102/112?

• Programming in Java

• Methods, parameters and variables

• Conditionals, loops (if, if...else, for, while)

• UI for getting input, output, drawing shapes

• Files

• Classes, Objects, Fields, Methods

• Using GUI’s (Graphical User Interface) with buttons, mouse, etc

• ArrayLists and arrays

(9)

Lectures

Mon, Thu, Fri, 2pm in KK LT303

Slides

• on course webpage

Video recordings on blackboard

Questions:

Goals

• Provide a framework for your learning

• Provide key content and explanations

(10)

Assessment

6 Assignments

[30%]

Mid-term test

[15%]

50 mins, week 7

Tutorials

[ 5%]

(group mark)

Final Test

[50%]

2 hours, in exam period (does NOT boost test 1!)

Mandatory Requirements

• submit reasonable attempts for

at least 5 out of 6 Assignments

(may also be used for borderline cases and aegrotats)

• Note: You have 48 "late hours" in total. Use them carefully!

• If you are late, have used up your late hours and don't have an approved extension. • DO NOT SUBMIT!

(11)

Tutorials

No scheduled labs in COMP103; Weekly tutorials instead:

6 options: Mon: 3pm Tue: 12, 4pm, 5pm(zoom) Wed: 11am, 1pm

10 tutorials in total

Starting next week Sign up now!

(zoom session available only if outside Wellington Region)

What will happen?

• Work in groups on problems, with tutor support

• Answers get handed in (worth 5% in total, group mark)

• Aim: help you to better understand both lectures and assignments

(12)

Assignments

Critical for your learning!

• 6 Assignments  total of 30%

• 2 weeks each

due: Tuesday 10am (Assig1&2)

Friday 10am (4&5)

Sunday 11:50pm (3 & 6)

• Video Solutions will be available.

• Late assignments cannot be marked. Remember the 48 late hours.

Must be

individual

work. (not like COMP 102)

• We will talk about how to collaborate and help each other without plagiarism

Won’t be as constraining as COMP 102 assignments

• You may need to do more of the design of the structure of the program

Programming Style will be a component of the marks (up to 5% off for bad style)

(13)

Getting Help.

[email protected]

• individual help where you can submit your code and questions to a tutor

• online help-desk

Physical & Zoom Help desks

• help in person for individuals or small groups

(14)

Workshops

Workshops (optional) covering all 1st year subjects with tutors to support.

• Excellent for working with discussion and interaction with other students.

(15)

JAVA FOUNDATIONS:

Introduction to Program Design & Data Structures

by Lewis, DePasquale and Chase

• 4th edition is best

• Same textbook as comp 102/112 ☺

(16)

PLAGIARISM UNACCEPTABLE

We want you to LEARN, TALK to each other, learn TOGETHER, and

HELP each other, but

PLAGIARISM is UNACCEPTABLE!

Got help from anybody other than lecturer or tutor?

STATE IT ON THE ASSIGNMENT!

Copied bits of code from anywhere other than lecture slides or

textbook?

(17)

Admin

Class rep

Workshops

(18)

Course Structure

Topics:

• Programming with unstructured and linear collections

• Different Kinds of collections:

• Lists, Sets, Bags, Maps, Stacks, Queues, Priority Queues

• Algorithms using collections.

• Complexity

• Recursion

• Programming with Linked collections

• Building, traversing tree structured collections

• Building, traversing graph/network structured collections

(19)

Recurring Themes

Good Design:

• Which choices should you make?

• Choosing appropriate implementations for collections

• Making the right choice the first time

Efficiency:

• How fast is it?

• How much memory does it take?

• By analysis, and by benchmarking

Testing:

(20)

Programming with Libraries

Modern programs (especially GUI and network)

are too big to build from scratch.

Have to reuse code written by other people

Libraries are collections of code designed for reuse.

• Java has a huge collection of standard libraries….

(21)

Libraries for COMP 103

• ecs100

UI, etc

• java.io, java.nio

Classes for dealing with files

• java.util

Collection classes (you've used ArrayList, now …….)

Other utility classes

We will use these libraries in almost every program

(22)

Collections of information

Lots of information that computers deal with comes in Collections

• Need to be able to store information in a way that reflects its structure

• Need to be able to manipulate the information in lots of different ways

(23)

Collections in Java

What is a Collection in Java?

An object that contains other objects

What defines the collection type?

• What you can do to it

• What structure it imposes on its contents

• What properties it ensures

Mostly we won’t care much it works inside:

• As long as it has the right behaviour, the inside doesn't matter?

(24)

“Standard” Collections

Bag

Set

Map

List

Queue

Stack

Tree

Graph

Collection

What's the difference?

(25)

Collections: What are the differences?

Different structures

• No structure – just a collection of values

• Linear structure of values – the order matters

• Set of key-value pairs

• Grid/table

• Hierarchical structures

• networked structures

Different constraints

• duplicates allowed/not allowed

• get, put, remove anywhere

• get, put, remove only at the ends, or only at the top, or …

• get, put, remove by position, or by value, or by key, or …

(26)

Abstract Data Types

Set, Bag, Queue, List, Stack, Map, etc are

Abstract Data Types

an ADT is a type of data, described at an abstract level:

• Specifies the operations that can be done to an object of this type

• Specifies how it will behave.

Doesn’t specify how it is implemented underneath – “black box”

(27)

Eg: Set ADT

(simple version)

Conceptual:

• Collection of items with no structure and no duplicates.

Operations:

add(value ),

remove(value ),

contains(value )→boolean

Behaviour:

• A new set contains no values.

A set will contain a value iff the value has been added to the set and it has not been removed since adding it.

(28)

Java Collections Library (in java.util)

Defines interfaces ⇒

Abstract Data Types

and classes:

Collection Set SortedSet List Queue HashSet LinkedHash

Set TreeSet ArrayList LinkedList PriorityQueue

implements

interfaces ≈ ADTs

(not ordinary classes)

classes

Deque

Collections Arrays

(29)

Java Collections library

Interfaces:

Collection

= Bag (most general)

List

= ordered collection

Set

= unordered, no duplicates

Queue

ordered collection, limited access (add at one end, remove from other)

Map

= key-value pairs (or mapping)

• …

Specify the Types:

Classes

• List classes:

ArrayList, LinkedList, Vector

• Set classes:

HashSet, TreeSet, EnumSet, LinkedHashSet,…

• Map classes:

EnumMap, HashMap, TreeMap,

LinkedHashMap, WeakHashMap, …

• …

Implement the interfaces

(30)

Java Interfaces and ADT’s

A Java Interface corresponds to an Abstract Data Type

• Specifies what methods can be called on objects of this type

(specifies name, parameters and types, and type of return value)

• Behaviour of methods is only given in comments (but cannot be enforced)

No constructors - can’t make an instance: new Set() new List()

No fields - doesn’t say how to store the data

public interface Set <E> {

public boolean add(E item); /*…description…*/

public boolean remove(E item); /*…description…*/

public boolean contains(E item); /*…description…*/

// (plus lots more methods in the Java Set interface)

(31)

Using Java Collection library

Your program can

• Declare a variable, parameter, or field of the interface type

private List <String> images; // defined using the ADT – interface

Set <Student> students;

• Create a collection object

images= new ArrayList <String> (); // constructed using a class

students= new HashSet <Student> ();

• Call methods on that variable, parameter, or field

images.add(UIFileChooser.open("Choose an image file"); images.set(i, images.remove(j));

(32)

Why?

Why use the Interface type to declare the field/variable then use a class to make

the object?

Can’t make an object of the interface type:

List <Double> myNumbers = new List <Double>();

Why not just use the class?

ArrayList <Double> myNumbers = new ArrayList <Double>();

More flexible design to use the Interface type:

List <Double> myNumbers = new ArrayList <Double>();

(33)

Stack is an exception

There is no Interface for Stack, just a class:

Stack <Action> undoStack = new Stack <Action> (); // constructed using a class

Stacks have four stack operations:

• empty() -> boolean is the stack empty or not

• push(item) push an item onto the top of the stack

• pop() -> item removes and returns the item at the top of the stack (error if the stack is empty)

(34)

Comments on code style for 103

I will drop “this.” except when needed.

• instead of this.loadFromFile(fname) just loadFromFile(fname)

• instead of this.shapes.addShape(shape) just shapes.addShape(shape)

I may leave out { } when surrounding just one statement

• instead of if (i < name.length) { name[i] = null;

}

(35)

Undo

How does it work?

Have to keep a record of all the actions as they are done

• Have to keep enough information to be able to undo them later

Undo button steps backwards through the record of actions, undoing the next one.

What kind of collection do we need for the record of actions?

(36)

Brick Builder

What are the actions to remember?

What info do we need to remember for each action?

To incorporate undo:

• make a stack of undo record

• at each action in the program, add a new record to the stack

• add an undo button and method which pops the top record from the stack and undoes the action

• create a new class to store all the information for each undo record

• fields

• constructors

References

Related documents

When policymakers act under discretion, multiple equilibria are possible because, by determining the equilibrium costs of issuing public debt, agents expectations impact on the

The template versions of these files included in the ModusOne Web Client installation package ('ModusOneWebClient.zip') can be used as references for the changes you need to make

If the political attack against the MBH98 study was rooted in synecdochally situat­ ing the hockey-stick article as representative of all global climate change studies, the

The histological evaluation of the brainstem nuclei, according to the guidelines established by the Italian law, are focused on identifying the hypoglossus, the

included their study in our review because of the large population followed up and the finding of 3 risk factors for general mortality in epilepsy coinciding with risk factors

Graph analysis based on a measure that favored the interaction between regions with a similar degree of atrophy was used to characterize the impact of the mesencephalic volume loss

Tüm bunların yanı sıra cümle başında verilen bir boşluk sıfat ya da zarf ile devam ediyorsa cevap “However/No matter how” olabilir. “Once” bağlacı “present” ya

Journal of Applied and Natural Science 11(1): 23- 34 (2019) ISSN : 0974-9411 (Print), 2231-5209 (Online) journals.ansfoundation.org Composition, richness and floristic