Top Banner
COP 3503C: Data Structures and Algorithm Analysis Analysis
36
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS2

COP 3503C: Data Structures and Algorithm AnalysisAnalysis

Page 2: CS2

Instructor Info•Name: Dr. Haiyan Nancy Hu•Research: Bioinformatics

•Create algorithms, computational and statistical techniques, and theory •For management and analysis of biological data.

•Contact:•Contact: •Email: [email protected]•Office: ENG3-412Office: ENG3 412•Phone: 407-882-0134•Office hours: MWF:9:30AM-10:30AM or by

7 January 2009 COP3503C spring09- Introduction 2appointment

Page 3: CS2

TA infoTA infoRosa EncisoEmail: [email protected] Hours and Location: To be announced

Greg TenerEmail: [email protected] Hours and Location: To be announced

7 January 2009 COP3503C spring09- Introduction 3

Page 4: CS2
Page 5: CS2

General InfoGeneral Info

• All info is on webcourses@UCFAll info is on webcourses@UCF• Teaching style:

I t ti t hi l ti i ti i– Interactive teaching, class participation is highly encouragedCollaborative learning encourage learning– Collaborative learning, encourage learning from your peer

7 January 2009 COP3503C spring09- Introduction 5

Page 6: CS2

Meeting TimeMeeting Time

• Lecture: MWF 10:30PM - 11:20PM ENG2 0102• Lab:

Th 10 00AM 10 50AM HEC 0302• Th 10:00AM – 10:50AM HEC 0302• Th 11:00AM – 11:50AM HEC 0302• Fr 8:30AM - 9:20AM ENGR 0383• Fr 8:30AM - 9:20AM ENGR 0383 • Fr 9:30AM - 10:20AM HEC 0302

7 January 2009 COP3503C spring09- Introduction 6

Page 7: CS2

TextbookTextbook

• Data Structures and Algorithm Analysis inData Structures and Algorithm Analysis in Java by Mark Allen Weiss (ISBN: 0-321-37013-9)37013 9)

7 January 2009 COP3503C spring09- Introduction 7

Page 8: CS2

LabLab

• TA lecture• Quizzes• Small exercises

Participation is counted toward finalgradegrade.

7 January 2009 COP3503C spring09- Introduction 8

Page 9: CS2

Assignments• Use Java for the programming assignments.

– Supports sharing on the web (with applets),– Makes it easy to display data structures graphically.y p y g p y

• Submitted over webourses.http://webcourses.ucf.edu

• Four assignments including individual or groupprojects So please form groups as soon asprojects. So please form groups as soon aspossible (each group 5/6 people). Eachassignment will be introduced in class and then

t d th l bposted on the class web page.• Requirements for group project

– Major distribution and member exchange policyMajor distribution and member exchange policy– Work assignment policy: working as a team, each one

should contribute enough for any credit– Report problems when encountering mismatch

7 January 2009 COP3503C spring09- Introduction 9

– Report problems when encountering mismatch

Page 10: CS2

GradingAssignments 40%

Two Midterms 20% (each 10%)

Final exam 20%

Lecture & Lab Participation 10%Lecture & Lab Participation 10%

Quizzes 10%Quizzes 10%

7 January 2009 COP3503C spring09- Introduction 10

Page 11: CS2

Deadlines and Late PolicyDeadlines and Late Policy

• Due date/timeDue date/time– Will be specified as the assignments posted.

Late Policy• Late PolicyIf you missed the due date/time, you have an additional 24 hours to submit with a 10% late-penalty reduction. After 24 hours, no submission is accepted.

• No make-up midterms or assignments7 January 2009 COP3503C spring09- Introduction 11

Page 12: CS2

Academic Misconducthttp://www goldenrule sdes ucf edu/2ehttp://www.goldenrule.sdes.ucf.edu/2e_

Rules.html• Unauthorized assistance: communication to another through g

written, visual, electronic, or oral means. The presentation of material which has not been studied or learned, but rather was obtained solely through someone else’s efforts and used as part of an examination course assignment orused as part of an examination, course assignment or project. The unauthorized possession or use of examination or course related material may also constitute cheating.

• Plagiarism: whereby another’s work is used or appropriated without any indication of the source, thereby attempting to convey the impression that such work is the student’s ownconvey the impression that such work is the student s own.

• Any student who knowingly helps another violate academic behavior standards is also in violation of the standards

7 January 2009 COP3503C spring09- Introduction 12

behavior standards is also in violation of the standards.

Page 13: CS2

• What you have learned in the prerequisiteWhat you have learned in the prerequisite class?

• What you expect to learn in this class?

7 January 2009 COP3503C spring09- Introduction 13

Page 14: CS2

Class OverviewClass Overview• Introduce a variety of basic data structures

and algorithms used in computer software.• Learn how to implement them.p• Practice design and analysis of these data

structuresstructures.• Learn when to apply them and practice

using these data structures by problemusing these data structures by problem solving.

7 January 2009 COP3503C spring09- Introduction 14

Page 15: CS2

ObjectiveObjective• You will understand

– what the tools are for storing and processing common data types

– which tools are appropriate for which needwhich tools are appropriate for which need• So that you can

– make good design choices as a developer, project manager or system customermanager, or system customer

• You will be able to– Justify your design decisions via formal reasoning– Communicate ideas about programs clearly and

precisely

7 January 2009 COP3503C spring09- Introduction 15

Page 16: CS2

Picking the best D S f h j bData Structure for the job

• The data structure you pick needs to support the operations you needpp p y

• Ideally it supports the operations you will use most often in an efficient manneruse most often in an efficient manner

• Examples of operations:A List with operations i t and d l t– A List with operations insert and delete

– A Stack with operations push and pop

7 January 2009 COP3503C spring09- Introduction 16

Page 17: CS2

Course Topics: class calendarCourse Topics: class calendar

• Introduction to Algorithm AnalysisIntroduction to Algorithm Analysis• Search Algorithms and Trees• Hashing and HeapsHashing and Heaps• Sorting• Disjoint Sets• Disjoint Sets• Graph Algorithms• Algorithm design techniques• Algorithm design techniques

– Greedy, Divide and Conquer, Dynamic Programming…

7 January 2009 COP3503C spring09- Introduction 17

Programming…

Page 18: CS2

TerminologyTerminology• Abstract Data Type (ADT)• Algorithm• Algorithm• Data structure• Implementation of data structure• Implementation of data structure

7 January 2009 COP3503C spring09- Introduction 18

Page 19: CS2

TerminologyTerminology• Abstract Data Type (ADT)

Mathematical description of an object with set of– Mathematical description of an object with set of operations on the object. Useful building block.

• Algorithm– A high level, language independent, description of a

step-by-step process• Data structure• Data structure

– A specific family of algorithms for implementing an abstract data type.

• Implementation of data structure– A specific implementation in a specific language

7 January 2009 COP3503C spring09- Introduction 19

Page 20: CS2

In simple wordsIn simple words…Abstract Data Types (ADT)› Objects + operations Use your intuition!j p› E.g., a stack that allows push and pop

Data StructuresData Structures› A step-by-step description of how an ADT is realized in pseudo code

E g sing an arra or a list Proof by Induction &› E.g., using an array or a list

ProgramsA t l i l t ti f ADT b d

Proof by Induction & Asymptotic Analysis

› An actual implementation of an ADT based on particular data structures› E.g., java.util.Stack Test the program with

l d t !7 January 2009 COP3503C spring09- Introduction 20

g j real data!

Page 21: CS2

Why So Many Data Structures?Ideal data structure:Ideal data structure:

“fast”, “elegant”, memory efficientGenerates tensions:Generates tensions:

– time vs. space– performance vs elegance– performance vs. elegance– generality vs. simplicity– one operation’s performance vs another’sone operation s performance vs. another s

The study of data structures is the study of

7 January 2009 COP3503C spring09- Introduction 21

tradeoffs. That’s why we have so many of them!

Page 22: CS2

First Example: Queue ADTFirst Example: Queue ADT

• FIFO: First In First Out• Queue operations

createdestroy F E D C Benqueue dequeueG Aenqueuedequeue

F E D C BG A

is_empty

7 January 2009 COP3503C spring09- Introduction 22

Page 23: CS2

Circular Array Queue DataCircular Array Queue Data Structure

Qb c d e f

Q0 size - 1

front back

enqueue(Object x) {Q[back] = x ;b k (b k 1) % i

front back

How test for empty list?

How to find K-thback = (back + 1) % size}

dequeue() {

How to find K th element in the queue?

What is complexity of x = Q[front] ;front = (front + 1) % size;return x ;

p ythese operations?

Limitations of this

7 January 2009 COP3503C spring09- Introduction 23

return x ;}

structure?

Page 24: CS2

Linked List Queue Data Structureb c d e f

front back

void enqueue(Object x) {if (is_empty())

front = back = new Node(x)

Object dequeue() {assert(!is_empty)return_data = front->data

elseback->next = new Node(x)back = back->next

temp = frontfront = front->nextdelete temp

}bool is_empty() {

return front == null

return return_data}

7 January 2009 COP3503C spring09- Introduction 24

}

Page 25: CS2

Circular Array vs Linked ListCircular Array vs. Linked List

• Too much space • Can grow as neededToo much space• Kth element accessed

“easily”

Can grow as needed• Can keep growing• No back loopingy

• Not as complex• Could make array

No back looping around to front

• Linked list code moreCould make array more robust

Linked list code more complex

7 January 2009 COP3503C spring09- Introduction 25

Page 26: CS2

Second Example: Stack ADTSecond Example: Stack ADT• LIFO: Last In First Out• Stack operations

– create– destroy– push

pop

A

B

E D C B A

– pop– top– is_empty

BCDE

_ p yEF F

7 January 2009 COP3503C spring09- Introduction 26

Page 27: CS2

Stacks in PracticeStacks in Practice

• Function call stackFunction call stack• Removing recursion

B l i b l ( th )• Balancing symbols (parentheses)• Evaluating Reverse Polish Notation

7 January 2009 COP3503C spring09- Introduction 27

Page 28: CS2

Algorithm Analysis: Why?Algorithm Analysis: Why?

7 January 2009 COP3503C spring09- Introduction 28

Page 29: CS2

Algorithm Analysis: Why?Algorithm Analysis: Why?• Correctness:

– Does the algorithm do what is intended.– How well does the algorithm complete its goal

• Performance:– What is the running time of the algorithm.

How much storage does it consume– How much storage does it consume.• Different algorithms may correctly solve a given

task– Which should I use?

7 January 2009 COP3503C spring09- Introduction 29

Page 30: CS2

Algorithm for sumAlgorithm for sum

• Find the sum of the first n integers storedFind the sum of the first n integers stored in an array v.

7 January 2009 COP3503C spring09- Introduction 30

Page 31: CS2

Iterative algorithm for sumIterative algorithm for sum

• Find the sum of the first n integersFind the sum of the first n integers stored in an array v.

sum(integer array v, integer n) returns i tintegerlet sum = 0for i = 1...nsum = sum + ith number

return sum

• Note the use of pseudocode

7 January 2009 COP3503C spring09- Introduction 31

Page 32: CS2

Recursive algorithm for sumRecursive algorithm for sum

• Write a recursive function to find the sumWrite a recursive function to find the sum of the first n integers stored in array v.

sum(integer array v, integer n) returns integerif n = 0 thenif n 0 thensum = 0

elsesum = nth number + sum of first n-1

numbersreturn sum

7 January 2009 COP3503C spring09- Introduction 32

return sum

Page 33: CS2

Proof by InductionProof by Induction

• Basis Step: The algorithm is correct for aBasis Step: The algorithm is correct for a base case or two by inspection.

• Inductive Hypothesis (n=k): Assume thatInductive Hypothesis (n k): Assume that the algorithm works correctly for the first k cases.

• Inductive Step (n=k+1): Given the hypothesis above, show that the k+1 case ypwill be calculated correctly.

7 January 2009 COP3503C spring09- Introduction 33

Page 34: CS2

Program Correctness by InductionProgram Correctness by Induction

• Basis Step: sum(v,0) = 0.Basis Step: sum(v,0) 0. • Inductive Hypothesis (n=k): Assume sum(v,k)

correctly returns sum of first k elements of v, i.e. y ,v[0]+v[1]+…+v[k-1]

• Inductive Step (n=k+1): sum(v,n) returnsv[k]+sum(v,k)= (by inductive hyp.)v[k]+(v[0]+v[1]+…+v[k-1])=v[0]+v[1]+…+v[k-1]+v[k]

7 January 2009 COP3503C spring09- Introduction 34

Page 35: CS2

Algorithms vs ProgramsAlgorithms vs Programs

• Proving correctness of an algorithm is veryProving correctness of an algorithm is very important– a well designed algorithm is guaranteed to work

correctly and its performance can be estimated• Proving correctness of a program (an

i l t ti ) i f ht ith i d bimplementation) is fraught with weird bugs– Abstract Data Types are a way to bridge the gap

between mathematical algorithms and programsbetween mathematical algorithms and programs

7 January 2009 COP3503C spring09- Introduction 35

Page 36: CS2

Quiz #11. Name and PID:2. Java experience:

no experienceone or two programs, but have not written appletsone or two programs, including an applet or twothree or more programs, with or without applets

3 Uni e perience3. Unix experience:4. Major subject at UCF:5. When did you take COP3502?

Grade received in COP3502 (if you took it):Grade received in COP3502 (if you took it): 6. Any special interest related to the course: 7. Any special interest not necessarily related to the course: 8. If you have a personal web page, you are encouraged to share it8. If you have a personal web page, you are encouraged to share it here by giving the URL:9. Optional comments:

7 January 2009 COP3503C spring09- Introduction 36