Professional Documents
Culture Documents
syste m l i fe c yc l e
2
ti
ti
syste m l i fe c yc l e
requirements
de ne the purpose of the project
describe informa on including input and output
analysis
break the problems into manageable pieces
bo om-up vs. top-down
design
view the system as both data objects and opera ons
for example, scheduling system for a university
objects: students, courses, professors…
opera ons: inser ng, removing, and searching each object…
3
tt
fi
ti
ti
ti
ti
syste m l i fe c yc l e
coding
choose representa ons for data objects and write algorithms for each
opera on
veri ca on
correctness proofs
can select algorithms that have been proven correct
tes ng
with working code and sets of test data
include all possible scenarios (more than syntax error)
running me should be considered
4
ti
fi
ti
ti
ti
ti
a l go r i t h m
an algorithm is a nite set of instruc ons that accomplishes a par cular task
algorithms sa sfy the following criteria
zero or more inputs
at least one output
de niteness (clear, unambiguous)
niteness (terminates a er a nite number of steps)
e ec veness
5
fi
ff
fi
ti
ti
fi
ft
fi
ti
ti
a l go r i t h m : s e l e c t i o n s o r t
6
a l go r i t h m : s e l e c t i o n s o r t
From the unsorted integers, nd the smallest and place it next to the sorted list.
sorted unsorted
sorted unsorted
sorted unsorted
sorted unsorted
7
fi
a l go r i t h m : s e l e c t i o n s o r t
From the unsorted integers, nd the smallest and place it next to the sorted list.
8
fi
fi
a l go r i t h m : s e l e c t i o n s o r t
From the unsorted integers, nd the smallest and place it next to the sorted list.
a l go r i t h m : s e l e c t i o n s o r t
10
a l go r i t h m s p e c i f i cat i o n : b i n a r y s e a rc h
middle = (start + end) / 2; [0] [1] [2] [3] [4] [5] [6]
8 14 26 30 43 50 52
compare list[middle] with query
1) query < list[middle] start end middle list[middle] : searchnum
set end to middle-1 0 6 3 30 < 43
4 6 5 50 > 43
2) query = list[middle] 4 4 4 43 == 43
return middle
start end middle list[middle] : searchnum
3) query > list[middle] 0 6 3 30 > 18
0 2 1 14 < 18
set start to middle+1
2 2 2 26 > 18
2 1 -
11
fi
ti
a l go r i t h m s p e c i f i cat i o n : b i n a r y s e a rc h
int compare(int x, int y) {
if (x < y) return -1;
if (x == y) return 0;
return 1;
}
int binsearch(int list[], int query, int start, int end) {
int middle;
while (start <= end) {
middle = (start + end) / 2;
switch (compare(list[middle], query)) {
case -1: {
start = middle + 1;
break;
}
case 0:
return middle;
case 1:
end = middle - 1;
}
}
return -1;
} 12
rec u rs i ve a l go ri t h ms
recursion
direct recursion: call themselves
indirect recursion: call other func ons that invoke the calling func on again
recursive mechanism
extremely powerful
allows us to express a complex process in very clear terms
13
ti
ti
rec u rs i ve a l go ri t h ms :b i n a r y s ea rc h
establish boundary condi on that terminates the recursive call
1) success
list[middle] = query
2) failure
start & end indices cross
ti
given a set of n(≥1) elements, print out all possible permuta ons of this set
if set {a,b,c} is given, then set of permuta ons is
(a, b, c) (a, c, b)
(b, a, c) (b, c, a)
(c, b, a) (c, a, b)
15
ti
ti
given a set of n(≥1) elements, print out all possible permuta ons of this set
if a set {a,b,c} is given, then the set of permuta ons is
(a, b, c) (a, c, b)
(b, a, c) (b, c, a)
(c, b, a) (c, a, b)
16
ti
ti
ti
ti
ti
ti
given a set of n(≥1) elements, print out all possible permuta ons of this set
17
ti
ti
ti
ti
ti
ti
ti
void main() {
:
perm(list, 0, n-1);
}
18
d ata a bst ra ct i o n
a data type is a collec on of objects and a set of opera ons that act on
those objects
the data type int consists of the objects {0, +1, -1, +2, -2, …, INT_MAX,
INT_MIN} and the opera ons {+, -, *, /, and %}
19
ff
fi
ti
ti
fl
ti
d ata a bst ra ct i o n
an abstract data type (ADT) is a data type that is organized in such a way that
the speci ca on of the objects and their opera ons is separated from
the implementa on of the objects and opera ons
20
ti
fi
ti
fi
ti
ti
ti
ti
ti
ti
ti
ti
ti
22
ti
ti
ti
ti
s p a c e co m p l ex i t y
S(P) = C + SP(I)
23
fi
fi
ti
ti
ti
fi
ti
24
fi
ti
ti
Example: itera ve func on for summing a list of numbers
25
ti
fi
ti
compiler must save parameters, local variables, return address for each recursive call
ti
t i m e co m p l ex i t y
me T(P), taken by a program P, is the sum of its compile me and its run (or
execu on) me
compile me is similar to the xed space component
A program step is a syntac cally or seman cally meaningful program segment whose
execu on me is independent of the instance characteris cs
27
ti
ti
ti
ti
ti
ti
ti
ti
fi
ti
ti
ti
ti
ti
ti
ti
steps/
statement total steps
execu on
total 2n+3
28
ti
Example: recursive summing of a list of numbers
total 2n+2
29
Example: matrix addi on
2 * rows * cols +
total
2 * rows + 1
30
ti
Asympto c nota on: big-O nota on
De ni on [big-O]
f(n) = O(g(n)) i there exist posi ve constants c and n0
such that f(n) ≤ c·g(n) for all n, n ≥ n0
g(n)
f(n)
n031 n
fi
ti
ti
ff
ti
ti
ti
big-O nota on
32
ti
big-O nota on
33
ti
Ω nota on
De ni on [Omega]
f(n) = Ω(g(n)) i there exist posi ve constants c and n0
such that f(n) ≥ c·g(n) for all n, n ≥ n0
34
fi
ti
ti
ff
ti
Θ nota on
De ni on [Theta]
f(n) = Θ(g(n)) i there exist posi ve constants c1, c2, and n0
such that c1·g(n) ≤ f(n) ≤ c2·g(n) for all n, n ≥ n0
more precise than both the “big oh” and “big omega” nota ons
g(n) is both an upper and lower bound on f(n)
35
fi
ti
ti
ff
ti
ti
Θ nota on
total Ɵ (rows*cols)
36
ti
a sy m pto t i c n o tat i o n
37
me complexity of algorithms
38
ti
a sy m pto t i c n o tat i o n
39
ti
ti