lecture 01 intro

33
ITCS-6114/8114: Algorithms and Data Structures Fall 2010 Srinivas Akella Department of Computer Science University of North Carolina, Charlotte Acknowledgments: Richard Souvenir

Upload: krishna-kant

Post on 08-Apr-2015

81 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Lecture 01 Intro

ITCS-6114/8114: Algorithms and Data Structures

Fall 2010

Srinivas AkellaDepartment of Computer Science

University of North Carolina, Charlotte

Acknowledgments: Richard Souvenir

Page 2: Lecture 01 Intro

Algorithms & Data StructuresITCS 6114 / 8114

• Instructor: Srinivas Akella– Office: 410E Woodward– Email: [email protected]– Office hours: Wed 2:30-3:30 or by appointment

• TA: Ning Zhou– Office: 401 Woodward– Email: [email protected]– Office hours: Monday 1:00-3:00pm

• Course website: www.cs.uncc.edu/~sakella/courses/ads/• Course information via Moodle

Page 3: Lecture 01 Intro

Welcome and Administrivia

• This is a course about data structures and algorithms

• What does that mean?

Page 4: Lecture 01 Intro

What is a data structure?

• A data structure is a representation of a dynamic set of data items.– Dynamic sets can grow, shrink, and otherwise

change over time.

Page 5: Lecture 01 Intro

What is an algorithm?

• An algorithm is a sequence of unambiguous instructions for solving a problem, i.e., for obtaining a required output for any legitimate input in a finite amount of time.

“computer”

problem

algorithm

input output

Page 6: Lecture 01 Intro

Algorithm

• Word derived from Al Khwarzimi (~780-850 AD), Persian mathematician in Baghdad

• Books: “On the Calculation with Hindu Numerals” (Latin: Algoritmi de numero Indorum) and“Kitab al-Jabr wa-l-Muqabala” (Latin: Liber algebrae et almucabala)

• His book described methods to compute: add, multiply, divide, find square roots, compute pi

• Methods were precise, unambiguous, correct, efficient• Techniques were popularized by Leonardo Fibonacci

(1170-1250 AD) in his book “Liber abaci”

Source: Wikipedia

Page 7: Lecture 01 Intro

Greatest Common Divisor

• Euclid’s GCD algorithm (~300 BC)• Possibly oldest algorithm

• GCD (a, b)if b==0

return aelse return GCD (b, a mod b)

Page 8: Lecture 01 Intro

8

Some Well-known Computational Problems

• Sorting*• Searching*• Shortest paths in a

graph*• Minimum spanning tree*• Primality testing

• * will cover in this course

• Traveling salesman problem*

• Knapsack problem*• Chess• Towers of Hanoi• Program termination

Page 9: Lecture 01 Intro

9

Two main issues related to algorithms

• How to design algorithms

• How to analyze algorithm efficiency

Page 10: Lecture 01 Intro

10

Design of Algorithms

• Many design strategies– We will cover the most popular strategies

used in CS.

• The idea is to determine best strategy given a problem (type)– A mix of art & science

Page 11: Lecture 01 Intro

11

Analysis of Algorithms

• How good is the algorithm?– Correctness– Time efficiency– Space efficiency

• Does there exist a better algorithm?– Lower bounds– Optimality

Page 12: Lecture 01 Intro

Example: Fibonacci numbers

• F_0 = 0• F_1 = 1• F_n = F_(n-1) + F_(n-2), for n >= 2

• Write a function to compute the n-thFibonacci number

Page 13: Lecture 01 Intro

Fibonacci Algorithms

• Recursive version• Complexity: ~O(1.618^n)

• Memoized version: Stores state (using an array, or even just two variables) to reduce time complexity

• Complexity: O(n)

Page 14: Lecture 01 Intro

Does Algorithm Efficiency Really Matter?

Source: Programming Pearls, 2nd ed., Bentley

Page 15: Lecture 01 Intro

15

Why study algorithms?

• Theoretical importance

– the core of computer science

• Practical importance

– A practitioner’s toolkit of known algorithms

– Framework for designing and analyzing algorithms for new problems

Page 16: Lecture 01 Intro

16

Data Structures & Algorithms?

• Data structures &

algorithms are related

– Algorithms need data

structures

– Data Structures need

algorithms

• Data Structures

– array

– linked list

– Stack / queue

– Hashtable*

– Priority queue*

– Graph*

– Trees (BST, Red-Black)*

Page 17: Lecture 01 Intro

Course Information

• Syllabus can be found on course web page and Moodle

• We will cover the highlights now

Page 18: Lecture 01 Intro

18

Course Philosophy

• This course will introduce topics that are at

the core of computer science

– Help you think and talk like a Computer Scientist

• Auxiliary Goals

– critical thinking

– problem solving

– creativity

Page 19: Lecture 01 Intro

19

Attendance Policy

• Attendance is not mandatory, but strongly recommended.

• Most of the material that is presented in class can be found in the course textbook

• You are responsible for all the material and administrative announcements presented during class.

Page 20: Lecture 01 Intro

20

Textbook

• The required textbook is:

Introduction to Algorithms, 3rd edition, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein, MIT Press, 2009.

Page 21: Lecture 01 Intro

Homework & Exams

• Homeworks (4)– Written answers to material covered in class

• Programming Projects (2)– Use one of C++, Java, Python– Must be able to read/write files– Must write commented and documented code

• Exams (2)

Page 22: Lecture 01 Intro

22

Academic Integrity Policy

• All submitted solutions to homeworks and programming projects must be your own work.

• See syllabus for policy on discussion of homeworks and projects

• Report any assistance you receive.• Violations of any of the academic

integrity rules will be dealt with harshly!

Page 23: Lecture 01 Intro

Ok, let’s get started…

• You own a large space telescope• Lots of astronomers want to use it• Each astronomer’s project pi requires use of

the telescope starting at a fixed time si (when their grant starts) and running for li days.

• Only one project can use a telescope at a time.

• Your goal: Justify yourself to NASA by scheduling as many projects as possible

Page 24: Lecture 01 Intro

Formally…

• Given a set P of projects pi, each occupying the half-open interval [si, si + li) …

• Choose a subset ¦ µ P of project for which– No two projects intervals overlap– The number of projects in ¦ is maximized

• This is one of many variants of scheduling or activity selection

Page 25: Lecture 01 Intro

Suggestion #1

Page 26: Lecture 01 Intro

Suggestion #2

Page 27: Lecture 01 Intro

Suggestion #3

Page 28: Lecture 01 Intro

Common Plan

• Common themes:– Repeatedly pick an element until no more

feasible choices remain– Among all feasible choices, we always pick

the one that minimizes (or maximizes) some property (job length, start time, # conflicts)

– Such algorithms are called greedy

Page 29: Lecture 01 Intro

Greedy Algorithms

• So far, greedy algorithms don’t look so good.

• Perhaps, we’ve been using the wrong property

Page 30: Lecture 01 Intro

Another Approach

• Intuition– For each project pi,

define its finishing time fi to be si + li

– Sort the projects in increasing order of finishing time

– Repeatedly pick nonconflicting, unscheduled job with earliest finishing time

Page 31: Lecture 01 Intro

How did we do?

• Is this correct?• Is this fast?• Can we do better?

• By the end of the semester, we’ll be able to answer these questions.

Page 32: Lecture 01 Intro

Quiz

• Yes, I have a short quiz for you now.

Page 33: Lecture 01 Intro

Reading

• CLRS

• Background reading: Appendices A to D, Chapter 10

• Today’s class: Chapter 1

• Next class: Chapters 2 and 3