Anda di halaman 1dari 33

Welcome to EE 232

Data Structures
Kashif Javed
kashifjaved.uet.edu.pk
Department of Electrical Engineering
UET Lahore

Copyright © Kashif Javed Introduction 1-1


Course Overview
 A fundamental computer science course

 Essential for programming


 Prerequisite for “design and analysis of
Algorithms” taught in 6th term

 A challenging course

 Intensive programming
 Intensive (mathematical and logic) thinking

Copyright © Kashif Javed Introduction 1-2


Copyright © Kashif Javed Introduction 1-3
Prerequisites
 Computer Fundamentals

 C++ language programming


• Functions, Pointers, Arrays , Structures, Recursion

 Editors, Compilers , Linkers, Debuggers


 User-level knowledge of Operating Systems
• Windows
• Linux

 Elementary Calculus and Algebra

Copyright © Kashif Javed Introduction 1-4


First some course terminology

 A data structure is a way to store and organize data


in order to facilitate access and modifications

 No single data structure works well for all purposes,


so it is important to know the strengths and limitations
of several of them

Copyright © Kashif Javed Introduction 1-5


First some course terminology

 An algorithm is a well-defined computational


procedure that takes some values, as input and
produces some value, or set of values, as output

 Computing time of an algorithm and the space it


requires in the memory determine its efficiency

 These are bounded resources and therefore should


be used wisely

Copyright © Kashif Javed Introduction 1-6


Course Objectives
This course will help the students

 To understand the fundamental ways of organizing


collections of data to be processed by a program
 focus is on main-memory data structures

 To evaluate the efficiency of data structures and their


algorithms

 To define, implement and use Abstract Data Types


(ADTs)

Copyright © Kashif Javed Introduction 1-7


Course Outline
 Introduction (1)
 Algorithms and Analysis (2)
 Lists, Stacks, Queues (3)
 Trees (4)
 Hashing (5)
 Priority Queues (6)
 Sorting (7)

Copyright © Kashif Javed Introduction 1-8


EE 232 Data Structures
Session-11 , Spring-13

Chapter 1
Introduction

Copyright © Kashif Javed Introduction 1-9


Chapter 1: Introduction
Our goals: we shall

 discuss the aims and  also briefly review


goals of this course recursion
and briefly review
programming concepts

 summarize the basic


mathematical
background

Copyright © Kashif Javed Introduction 1-10


Chapter 1: roadmap
1.1 What’s the book about?
1.2 Mathematics review
1.3 A Brief Introduction to Recursion

Copyright © Kashif Javed Introduction 1-11


What's the book about?
Selection Problem
 Given a list of N numbers, determine the kth largest,
where k  N

 Algorithm 1
• Read N numbers into an array
• Sort the array in decreasing order by some
simple algorithm
• Return the element in position k

Copyright © Kashif Javed Introduction 1-12


What's the book about?...
 Algorithm 2
• Read the first k elements into an array and sort
them in decreasing order
• Each remaining element is read one by one
• If smaller than the kth element, then it is ignored
• Otherwise, it is placed in its correct spot in the
array, bumping one element out of the array

• The element in the kth position is returned


as the answer

Copyright © Kashif Javed Introduction 1-13


What's the book about?...
 Considering large input values such as N = 1million
and k = 500,000 , both the algorithms do not
terminate in a reasonable amount of time ->
impractical
 A better algorithm can solve this in a second!

What should we conclude:


1) Writing a working program is not good enough!
2) When a program is to be run on a large data set,
then the running time becomes an issue

Copyright © Kashif Javed Introduction 1-14


What's the book about?...
 We will learn

 How to estimate the running time of a program for


large inputs
 How to compare the running times of two
programs without actually coding them

 Techniques for drastically improving the speed of


a program

 Techniques for determining program bottlenecks

Copyright © Kashif Javed Introduction 1-15


Chapter 1: roadmap
1.1 What’s the book about?
1.2 Mathematics review
1.3 A Brief Introduction to Recursion

Copyright © Kashif Javed Introduction 1-16


Mathematics review
To describe portions of an algorithm and to
analyse the performance characteristics of an
algorithm

Exponents Logarithms
 XA•XB = X A+B  All logarithms are to the
 XA  XB = X A-B base 2, unless otherwise
specified
 (XA) B = X A•B
 Definition:
 XA + XA = 2•XA  X2•A
 XA = B if and only if
 2N + 2N = 2•2N = 2N+1
logX B = A

Copyright © Kashif Javed Introduction 1-17


Mathematics review…
Properties of logarithms

logC B
 logAB = ; A,B,C > 0 , A  1
logC A

 log AB = log A + log B ; A, B > 0


 log(A/B) = log(A) - log(B)
 log(AB) = B log(A)
 log(X) < X for all X > 0
 log(1)=0, log(2)=1, log(1,024)=10,
 log(1,048,576)=20

Copyright © Kashif Javed Introduction 1-18


Mathematics review…
Geometric Series

  2i = 2N+1 - 1
i=0..N

AN+1 - 1
  Ai =
i=0..N A -1

1
 If 0 < A < 1,  Ai 
i=0..N
A-1
1
 If N  ,  Ai =
i=0..N
A-1
Copyright © Kashif Javed Introduction 1-19
Mathematics review…
Arithmetic Series
N(N+1)
 i =
2
i=1..N

Copyright © Kashif Javed Introduction 1-20


Mathematics review…
Modular Arithmetic
 The number-theoretical concept of congruence
is
• A  B (mod N)
• A is congruent to B modulo N , if N divides
(A-B)

 In other words, the remainder is the same if


either A or B are divided by N
• e.g. 81  61  1 (mod 10)
 Similarly,
• A+C  B+C (mod N) and AD  BD (mod N)
Copyright © Kashif Javed Introduction 1-21
Mathematics review…
Proving the statements in data structure analysis

Proof By Induction
 Given a theorem

1) First prove a base case


 Show the theorem is true for some small value(s)

2) Next assume an inductive hypothesis


 Assume the theorem is true for all cases up to
some limit k
 Using this assumption, prove that the theorem
holds for the next value (k+1)
Copyright © Kashif Javed Introduction 1-22
Mathematics review…
Proof By Induction – example
 Fibonacci Series
 F0 = 1, F1 = 1, Fi = Fi-1 + Fi-2 , for i > 1

 Show that
 Fi < (5/3) i , for i  1

 Base case: F1 = 1 < 5/3

 Inductive Hypothesis:
 Fi < (5/3) i , for i = 1, 2, ..., k
Copyright © Kashif Javed Introduction 1-23
Mathematics review…
Proof By Induction – example…
 Now prove that Fk+1 < (5/3)k+1

 From definition of Fibonacci Series


Fk+1 = Fk + Fk-1

 Using inductive Hypothesis


Fk+1 < (5/3) k + (5/3) k-1
< (3/5)(5/3) k+1 + (3/5)2(5/3) k+1
< (5/3) k+1 [ 3/5 + (3/5)2]
< (5/3) k+1 [24/25]
< (5/3) k+1

Copyright © Kashif Javed Introduction 1-24


Mathematics review…
Proof by Contradiction
 Assume a theorem is false

 Show that this assumption implies that some


known property is false

 Hence, the original assumption is erroneous

Proof by Contradiction – example


 “There is an infinite number of prime numbers”
 Proof:
• Assume the theorem is false and there is
some largest prime Pk

Copyright © Kashif Javed Introduction 1-25


Mathematics review…
Proof by Contradiction – example…
 Let P1, P2, …,Pk be all the primes in the increasing
order
 Consider N = P1P2P3…Pk + 1
 Since N > Pk, so N is not a prime
 However, none of P1, P2, …,Pk divides N exactly,
because there will always be a remainder of 1
 This is a contradiction, because every number is
either a prime or a product of primes
 Hence the original assumption is false  the theorem
is true
Copyright © Kashif Javed Introduction 1-26
Chapter 1: roadmap
1.1 What’s the book about?
1.2 Mathematics review
1.3 A Brief Introduction to Recursion

Copyright © Kashif Javed Introduction 1-27


A brief introduction to recursion

 Recursive function
 a function that is defined in terms of itself

 e.g. a factorial function:


6! = 6 * 5 * 4 * 3 * 2 * 1
 could be written as:
6! = 6 * 5!

 In general, we can express the factorial function as


follows:
n! = n * (n-1)!

Copyright © Kashif Javed Introduction 1-28


A brief introduction to recursion…

 The factorial function is only defined for positive


integers. So we should be a bit more precise:
n! = 1 (if n is equal to 1)
n! = n * (n-1)! (if n is larger than 1)

 The C equivalent of this definition:


int fac(int numb){
if(numb <=1)
Base Case
return 1;
else Recursive Call
return numb * fac(numb-1);
}
Copyright © Kashif Javed Introduction 1-29
A brief introduction to recursion…
 Lets find the factorial of 3, that is, numb=3
fac(3) :
3 <= 1 ? No
fac(3) = 3 * fac(2)
fac(2) :
2 <= 1 ? No
fac(2) = 2 * fac(1)
fac(1) :
1 <= 1 ? Yes
return 1
fac(2) = 2 * 1 = 2 int fac(int numb){
return fac(2) if(numb<=1)
return 1;
fac(3) = 3 * 2 = 6 else
return fac(3) return numb * fac(numb-1);
fac(3) has the value 6 }
Copyright © Kashif Javed Introduction 1-30
A brief introduction to recursion…
 While using recursion, we must be careful not to
create an infinite chain of function calls:
int fac(int numb){
return numb * fac(numb-1); Oops!
} No termination
or condition
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
} Oops!
Copyright © Kashif Javed Introduction 1-31
A brief introduction to recursion…

 Two rules for writing correct recursive programs


 Define a base case
• A base case can be solved without recursion
and it terminates recursion

 Making progress
• For cases that are to be solved recursively, the
recursive call must always be to a case that
makes progress toward the base case

Copyright © Kashif Javed Introduction 1-32


Introduction: Summary
Covered the basics You now have:
 What's the book about?  Overview of this course

 In the mathematical
review,
 Logarithms
 Arithmetic series
 Geometric Series
 Mathematical induction

 We also have learnt how


to write correct recursive
programs

Copyright © Kashif Javed Introduction 1-33

Anda mungkin juga menyukai