Title: Principles of Data Structures
1Principles of Data Structures
- Instructor Yonit Kesten
- Tutor Sarah
- Books
- Data Structures and Problem Solving using
Java,Mark Allen Weiss, Addison-Wesley, second
edition. - Algorithms, Cormen, Leiserson, Rivest, McGraw
Hill. - The same, translated by the Open University.
- Data Structures and Algorithm Analysis in Java,
Weiss, Addison wesley, 2000. - Course Web Page
- http//www.ise.bgu.ac.il/yonit/courses00/ds02
2What is the course all aboutorWho needs to
know about Data Structures??
3Computer Science The mechanization of
Abstraction
- Sciences (physics, biology) - understanding the
universe as it is. - Computer Science - a science of abstraction.
- Creating the right model for reasoning about a
problem and devising the appropriate mechanizable
techniques to solve it.Or - Create abstractions of real world problems that
can be understood by the user and, at the same
time, can be represented and manipulated
efficiently by the computer.
4Example
world university, students, courses,
exams. problem scheduling exams with no
students conflicts abstraction course
conflict Graph nodes represent
courses arcs represent courses taken by
the same student (conflicting
courses), i.e., each arc
connects two courses taken by the
same student. Algorithm identify maximal
independent sets of nodes. 1) databases,
Java, English. 2) math. 3) data
structures What is not modeled ?
math
English
data structures
databases
Java
5What is the Course About
- Three problem-solving tools
- Data Models - The abstractions used to solve
problems (graphs, lists, trees, sets, finite
automata, logic, etc) - Data Structures - The programming language
constructs used to represent data models. We
study methods to represent data models with the
data structures of the programming language. - Algorithms - Techniques used to obtain solutions
by manipulating data as represented by the
abstractions of a data model.
6Data Models
- Abstractions used to help formulate solutions
to problems. - Lists
- Trees
- Sets, Relations and Equivalence Classes
- Graphs
- Other abstractions not handled in this course
- Automata
- Logics
- Grammars
- ..
7Data Structures
- When the data model of the programming language
lacks a built-in representation for the
data-model we want to use - represent the data model using abstractions
supported by the language. - Classes and Objects (or OOP) are the main tools
given in Java to define user-defined data types,
with data and behaviors. - Yet, the abstract models are not part of the
language. The models need to be built within the
language. - Different programming languages may have
different data models - Lisp - supports trees directly.
- Prolog - logic built into its data model.
8Algorithms
- An algorithm is a recipe or well defined
procedure for transforming some input into a
desired output mechanically. - Searching, Sorting
- Some well known algorithms are those for adding
and multiplying numbers.
9Example
- A different multiplication algorithm
- write the two numbers side by side
- Repeat until the first column 1
- divide left column by 2 and throw out the
fractional part - multiply second column by 2
- erase rows in which first column is even
- sum up all non-erased rows of the second
row.This is the result of the multiplication.
10Algorithms (cont)
- Relevant questions
- Does it halt ?
- Is it correct ?
- Is it fast ?
- How much memory does it use ? This is called
algorithm analysis.
11Algorithms - Running Time
- The main analysis we will consider is to
determine the amount of r time it requires.
12What is Algorithm Analysis
- Running time, almost always depends on size of
input Example Sorting 10,000 as opposed to
10 elements. - Exact running time depends on other factors
host machine, compiler, programming style. - Given a program can plot running time versus
input size.
13Running Time Analysis
- Not interested in actual running time but in
growth rate with input size. - Use Big-O notation to capture the most dominant
term in a function Ex linear growth O(n)
logarithmic growth O(lg n) - Functions in order of increasing growth rate
- c lt lg n lt log2 n lt n lt n lg n lt n2 lt n3 lt 2n
14Some Important Questions
- Is it always important to be on the most
efficient curve - How much better is one curve than another
- How do you decide on which curve a given
algorithm lies on - How do you design an algorithm that avoid being
on bad curves
15Examples of Algorithm Running Times
- Find minimal element in an array of size ngiven
an array on n elements, find the smallest
element. - A naïve algorithm is T(n) ?. Cant do better.
- Closest points in the planeGiven n points in the
plane,find the pair of points with minimal
distance. - Naïve algorithm for every 2 points evaluate
distance - T(n) ?
- Can do better (n lg n) (not evaluating distance
of all two points) - Coolinear points in the planeGiven n points in a
plane, determine if any three are on a straight
line. - Naïve algorithm ? enumerating all groups of three
- T(n) ?
- Can do better (O(n2 ))
16Example The Maximum Contiguous Subsequence Sum
Problem
- Give a list of (possibly negative) integers
- a1, a2, .., an
- Find lti, jgt such that (ai, ai1, , aj) is
maximal. - Example (-2, 11, -4, 13, -5, 2) i2, j4,
sum 20
17Maximum Contiguous SubsequenceO(n3)
- public static int maxSubSum1( int a )
- int maxSum 0
- for( int i 0 i lt a.length i )
- for( int j i j lt a.length j )
-
- int thisSum 0
- for( int k i k lt j k )
- thisSum a k
- if( thisSum gt maxSum )
- maxSum thisSum
- seqStart i
- seqEnd j
-
-
- return maxSum
-
18Maximum Contiguous Subsequence O(n2)
- Quadratic maximum contiguous subsequence sum
algorithm. - seqStart and seqEnd represent the actual
best sequence. - /
- public static int maxSubSum2( int a )
-
- int maxSum 0
- for( int i 0 i lt a.length i )
-
- int thisSum 0
- for( int j i j lt a.length j )
-
- thisSum a j
- if( thisSum gt maxSum )
-
- maxSum thisSum
- seqStart i
- seqEnd j
19Maximum Contiguous Subsequence O(n)
- Linear-time maximum contiguous subsequence sum
algorithm. - seqStart and seqEnd represent the actual
best sequence. - /
- public static int maxSubSum3( int a )
- int maxSum 0
- int thisSum 0
- for( int i 0, j 0 j lt a.length j
) -
- thisSum a j
- if( thisSum gt maxSum )
-
- maxSum thisSum
- seqStart i
- seqEnd j
-
- else if( thisSum lt 0 )
-
20Example Sorting
- Given a sequence of numbers a1, a2, ...,
an - Construct a permutation a1, a2, , an,
such that a1 lt a2 lt lt
an. - Example input 34, 40, 10, 15,
2 output 2, 10, 15, 34, 40
21Insertion-Sort
key
A - an array of integers.
Insertion-Sort (A) for j 2,
length(A) key Aj k j-1
while k gt 0 and Akgt key
Ak1 Ak k k-1
Ak1 key
sorted
unsorted
sorted
unsorted
j
n
1
T(n) ?
22Another Sorting Algorithm
- Using a divide and conquer technique
- split (divide) the problem into a set of smaller
problems - solve the smaller problems
- combine the solutions of the smaller problems
- Combined with recursion
- split (divide) the problem into a set of smaller
problems - solve the smaller problems by splitting
recursively, until the remaining problem is small
enough to be solved trivially - combine the solutions of the smaller problems
23MergeSort
MergeSort(A, p, r) if p lt r
q _(pr)/2_ MergeSort(A, p,
q) MergeSort(A, q1,
r) Merge(A,p,q,r)
Replaces sorting by splitting merging
MergeSort(A, 1, n) (initiation)