Data Structures and Algorithms
Data Structures and Algorithms
Algorithms
Prof. Adriano Patrick Cunha
Program
Introduction Algorithms.
Recursive technique.
Lists.
Trees.
Priority Lists.
Linus Torvalds
Prof. Adriano Patrick Cunha
Data Structures and Algorithms
Problems solve by algorithms and data structures
The Human Genome Project
The Internet enables people all around the world to quickly access and retrieve large amounts of information.
Electronic commerce enables goods and services to be negotiated and exchanged electronically, and it depends on the privacy of
personal information such as credit card numbers, passwords, and bank statements.
Manufacturing and other commercial enterprises often need to allocate scarce resources in the most beneficial way.
We are given a road map on which the distance between each pair of adjacent intersections is marked, and we wish to determine the
shortest route from one intersection to another.
We are given two ordered sequences of symbols, X = (x1 ; x2 ;... ; xm) and Y = (y1 ; y2 ;... ; yn), and we wish to find a longest common
subsequence of X and Y
We are given a mechanical design in terms of a library of parts, where each part may include instances of other parts, and we need to list
the parts in order so that each part appears before any part that uses it.
We are given n points in the plane, and we wish to find the convex hull of these points. The convex hull is the smallest convex polygon
containing the points.
Sorting
$(such that) -> $ a'1 <= a'2 <= ... <= a'n
E.g.
i <- j - 1;
A[ i + 1 ] <- key;
key <- A[ j ];
i <- j - 1;
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
key <- A[ j ];
i <- j - 1;
i <- j - 1;
A[ i + 1 ] <- key;
An algorithm is said to be correct if, for every input instance, it halts with the
correct output. We say that a correct algorithm solves the given computational
problem.
An incorrect algorithm might not halt at all on some input instances, or it might
halt with an incorrect answer.
Loop invariant
Termination: when the loop ends, the invariant gives us a useful property
that helps to show that the algorithm is correct
Insertion Sort
Number of statements executed c1n2
Computer A: 1 billion (109) instructions per second.
Great programmer: 2n2 instructions.
Intercalation Sort(Merge-Sort)
Number of statements executed c2nlgn
Computer B: 10 millions (107 ) instructions per second.
Regular programmer: 50nlgn instructions.
Time to sort 1 million (106) elements of a set?
Prof. Adriano Patrick Cunha
Problem of the traveling salesman
A traveling salesman has to visit a certain number of cities and each move between two cities involves
a certain cost. What will be the most economic return, visiting each of the cities only once and
returning the one from where you left? The optimal solution for this type of problem is to find a
Hamilton circuit of minimum length.
Hamilton (or Hamiltonian) Circuit It is a path that begins and ends at the same vertex running through
all the vertices once (except the last which is also the first).
Running Time
- guarantee to user
Speed is fun!!
- Woist-case(usually)
- Average-case (sometimes)
- Best-case (bogus)
- Cheat
O Notation
for j <- 2 to n do c1 n
i <- j - 1; c3 n-1
c5 nj=2(Tj-1)
A[ i + 1 ] <- A[ i ];
i <- j - 1; c6 nj=2(Tj-1)
tj = j, para j = 2, 3, ..., n
T(n) = c1n + c2(n-1) + c3(n-1) + c4[n(n + 1)/2 - 1] + c5[n(n - 1)/2] + c6[n(n - 1)/2] + c7(n-1)
Logo, O(n2)
Continua ...