data structures and algorithm analysis introduction lecturer: ligang dong, egan email:...

22
Data Structures and Algorithm Analysis Introduction Lecturer: Ligang Dong, egan Email: [email protected] Tel: 28877721 13306517055 Office: SIEE Building 305

Upload: allan-moody

Post on 13-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Data Structures and Algorithm AnalysisIntroduction

Lecturer: Ligang Dong, egan 

Email: [email protected]: 28877721 , 13306517055Office: SIEE Building 305

Textbook

Mark Allen Weiss, Data Structures and Algorithm Analysis in C, China Machine Press.

E-book is available. http://netcom.zjsu.edu.cn/person/dlg/data_structure.htm

Scheduling Instruction Lectures: Tuesday 8:05am Programming Practices: Friday 9:50am

Grading Final exam: 55% Weekly Homeworks:45%(15*3%)

Posted every Tuesday Due Next Tuesday before class

(solution posted after class) If you have difficulties in solving the

homework, please ask for help during Programming Practices in Friday.

No late submission accepted

Why Learn Data Structures and Algorithms? One of the basic rules concerning

programming is to break the program down into modules.

Each module is a logical unit and does a specific job. Its size is kept small by calling other modules.

Two types of modules “data” module ------ data structure “procedure” module ------ algorithm

Why Learn Data Structures and Algorithms? Modularity has several advantages.

It is much easier to debug small routines than large routines;

It is easier for several people to work on a modular program simultaneously;

A well-written modular program places certain dependencies in only one routing, making changes easier.

What are Data Structures and Algorithms? Data Structures are methods of

organizing large amounts of data.

An algorithm is a procedure that consists of finite set of instructions which, given an input from some set of possible inputs, enables us to obtain an output through a systematic execution of the instructions.

ADT(Abstract Data Types)

Data Structure

Algorithms

Software

Programs

Software Engineering

Programming Languages

Abstract Data Types (ADTs) An abstract data type (ADT) is a set of

objects such as lists, sets, and graphs, along with their operations (e.g., insert, delete)

Integers, reals, and booleans are data types. Integers, reals, and booleans have operations associated with them, and so do ADTs.

Nowhere in an ADT’s definition is there any mention of how the set of operations is implemented.

Example Problem: Given 10 test results

(80, 85, 77, 56, 68, 83, 90, 92, 80, 98) of C language programming of Tom, what is the average score?

Example Solutionmain()

{

int sum, average;

int t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;

t1=80;t2=85;t3=77;t4=56;t5=68;

t6=83;t7=90;t8=92;t9=80;t10=98;

sum= t1+t2+t3+t4+t5+t6+t7+t8+t9+t10;

average=sum/10;

printf(“Sum=%d\n”,sum);

printf(“Average=%d\n”, average);

}

Example Better Solutionmain()

{

int sum, average; int i;

int t[10] =80, 85, 77, 56, 68, 83, 90, 92, 80, 98}

sum=0;

for(i=0; i<10; i++)

sum=sum+t[i];

average=sum/10;

printf(“Sum=%d\n”,sum);

printf(“Average=%d\n”, average);

}

By using an array instead of n variables, with a loop instead of n assignments, the program is simpler.

Contents

Chapter 1 Introduction ( 0.5 Week )C Review ( 1.5 Week )Chapter 2 Algorithm Analysis ( 1 Week )Chapter 3 Lists, Stacks, and Queues ( 1 Week )Chapter 4 Trees ( 2 Weeks )Chapter 5 Hashing ( 1 Week )Chapter 6 Priority Queues (Heaps) ( 1 Week )Chapter 7 Sorting ( 2 Weeks )Chapter 8 Disjoint Set ADT ( 1 Week )Chapter 9 Graph Algorithms ( 2 Weeks )Chapter 10 Algorithm Design Techniques ( 2 Weeks )

Mathematical Foundation

Series and summation:

1 + 2 + 3 + ……. N = N(N+1)/2 (arithmetic series)

1 + r2 + r3 +………rN-1 = (1- rN)/(1-r), (geometric series)

1/(1-r) , r < 1, large N

Sum of squares:

1 + 22 + 32 +………N2 = N(N + 1)(2N + 1)/6

Mathematical Foundation

logxa = b if xb = a

we will use base 2 mostly, but may use other bases occasionallyWill encounter log functions again and again!

log (ab ) = log a + log b

log (a/b ) = log a - log b

log ab = b log a

logba = logca/ logcb

alog n = nlog a

amn = (am )n = (an)m

am+n = am an

(2n)0.5 (n/e)n n (2n)0.5 (n/e)n + (1/12n)

Mathematical Foundation Proof Method1: Proof By Induction

Prove that a property holds for input size 1

Assume that the property holds for input size n. Show that the property holds for input size n+1.

Then, the property holds for all input sizes, n.

Mathematical Foundation Example of Proof By Induction: Prove that

the sum of 1+2+…..+n = n(n+1)/2 Holds for n = 1 Let it hold for n

1+2+….+n = n(n+1)/2 When n+1

1+2+….+n+(n+1) = n(n+1)/2 + (n+1) = (n+1)(n+2)/2.

Mathematical Foundation Proof Method2: Proof By Counter

Example Want to prove something is not true! Give an example to show that it does not

hold!

Mathematical Foundation Proof Method3: Proof By Contradiction

Suppose, you want to prove something. Assume that what you want to prove does

not hold. Then show that you arrive at an

impossiblity.

Mathematical Foundation Example of Proof By Contradiction:The

number of prime numbers is not finite! Suppose there are finite number of primes, k primes. (we

do not include 1 in primes here)

We know k2.

Let the primes be P1, P2 , ……… Pk

Z = P1 P2 ……… Pk + 1

Z is not divisible by P1, P2 , ……… Pk

Z is greater than P1, P2 , ……… Pk Thus Z is not a prime.

We know that a number is either prime or divisible by primes. Hence contradiction.

So our assumption that there are finite number of primes is not true.

Homework #1

At least complete two of the following: 1.1 Prove the following formulas

1.2 Write a program to solve the selection problem. (Suppose you have a group of n numbers and would like to determine the kth largest.)1.3 Based on 1.2, let k = n/2, show the original location of kth largest. And draw a table showing the running time of your program for various values of n (4<=n<=20).

Hint You should submit the homework using the WS Word file

through email. Your program must be submitted with output. DO NOT write any code until you understand what the

program does, the design of the program, and the output that it produces.  Writing code before understanding the program will simply be a waste of your time, and will result in a program that does not work!!

As with any large program, it is probably a good idea to finish the easy parts first and then slowly complete the harder parts.

Develop your code in small pieces, and test each part of your code as you write it. 

Your program is graded according to correctness, readability, robustness, efficiency.