SlideShare uma empresa Scribd logo
1 de 22
Baixar para ler offline
Data Structures and
Algorithms
Prof. Adriano Patrick Cunha
Prof. Adriano Patrick Cunha
Program
Introduction Algorithms.
Designing and Analyzing Algorithms.
Recursive technique.
Lists.
Trees.
Priority Lists.
Prof. Adriano Patrick Cunha
Data Structures and Algorithms
“I will, in fact, claim that the difference between
a bad programmer and a good one is whether
he considers his code or his data structures
more important. Bad programmers worry about
the code. Good programmers worry about data
structures and their relationships.”
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.
Prof. Adriano Patrick Cunha
Algorithms
Informally, an algorithm is any well-defined computational procedure that takes
some value, or set of values, as input and produces some value, or set of values,
as output. An algorithm is thus a sequence of computational steps that
transform the input into the output.
We can also view an algorithm as a tool for solving a well-specified
computational problem. The statement of the problem specifies in general terms
the desired input/output relationship. The algorithm describes a specific
computational procedure for achieving that input/output relationship.
Prof. Adriano Patrick Cunha
Problem:
Sorting
Input: sequence <a1, a2, ..., an) of numbers
Output: permutation <a'1, a'2, ..., a'n>
$(such that) -> $ a'1 <= a'2 <= ... <= a'n
E.g.
Input: 〈31, 41, 59, 26, 41, 58〉
Output: 〈26, 31, 41, 41, 58, 59〉
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
It works the way many people sort the cards by playing
cards
1. Letters initially on the table
2. One card at a time
3. Place it in the left hand, in the correct position
a. Find the right position from right to left
E.g. 〈5, 2, 4, 6, 1, 3〉
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
Is correct ?
Prof. Adriano Patrick Cunha
Solve:
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
Is correct ?
Is the best ?
Prof. Adriano Patrick Cunha
What is Correct?
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.
Contrary to what you might expect, incorrect algorithms can sometimes be
useful, if we can control their error rate.
Prof. Adriano Patrick Cunha
What is Correct?
Loop invariant
Property or statement that holds true for each loop iteration
Helps understand why an algorithm is correct
Prof. Adriano Patrick Cunha
Loop Invariant
Three details must be shown:
Initialization: the invariant is true before the first loop iteration
Maintenance: if the invariant is true before an iteration of the loop, it will
remain true before the next loop iteration
Termination: when the loop ends, the invariant gives us a useful property
that helps to show that the algorithm is correct
Prof. Adriano Patrick Cunha
What is being the best?
Insertion Sort
Number of statements executed c1
n2
Computer A: 1 billion (109
) instructions per second.
Great programmer: 2n2
instructions.
Intercalation Sort(Merge-Sort)
Number of statements executed c2
nlgn
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).
Prof. Adriano Patrick Cunha
Problem of the traveling salesman
4 15
10
9
16
20
8
14
127
A
B
CD
E
A -> E -> C -> D -> B -> A <56km>
B -> C -> E -> A -> D -> B <49km>
C -> E -> A -> D -> B -> C <49km>
D -> A -> E -> C -> B -> D <49km>
E -> A -> D -> C -> B -> E <44km>
N Alternativas (~n!) Tempo
5 120 0,00012 s
10 362880 3,62880 s
12 479001600 8 min
15 1307674368000 15 days
20 2432902008176640000 77.147 years
50 3,04 E+0064 ∞
100 9,33 E+0157 ∞
Prof. Adriano Patrick Cunha
What is being the best?
Running Time
- Depends on input (e.g. already sorted)
- Depends on input size (6 elements vs 6x10^9 elements)
- parametrize in input size
- Want upper bounds
- guarantee to user
Prof. Adriano Patrick Cunha
Analysis of Algorithms
Theoretical study of computer-program performance and resources usage.
What's more important than performance?
Why study algorithms and performance?
Determines something is feasible or infeasible
Performance enables the usability, security
Speed is fun!!
Prof. Adriano Patrick Cunha
Kinds of Analysis
- Woist-case(usually)
- T(n) = max time on any input of size n
- Average-case (sometimes)
- T(n) = expected time over all inputs of size n
- Need assumption of statistical distribution
- Best-case (bogus)
- Cheat
Prof. Adriano Patrick Cunha
Asympototic analysis
Ignore machine dependent constants
Look at GROWTH T(n) as n-> infinity
O Notation
- Drop low order terms
- Ignore leading constants
- E.g. 3n3
+ 90n2
- 5n + 6046 => O(n3
)
- As n -> infinity, O(n2
) algorithm always beats a O(n3
) algorithm.
Prof. Adriano Patrick Cunha
Insertion Sort - Asympototic analysis
Insertion-Sort(A, n) //Sort A[1..n]
for j <- 2 to n do
key <- A[ j ];
//Insert A[ j ] into the sorted sequence A[ 1 .. j -1]
i <- j - 1;
while(i > 0 and A[ i ] > key) do
A[ i + 1 ] <- A[ i ];
i <- j - 1;
A[ i + 1 ] <- key;
cost times
c1
c2
0
c3
c4
c5
c6
c7
n
n - 1
0
n - 1
∑n
j=2
Tj
∑n
j=2
(Tj
-1)
∑n
j=2
(Tj
-1)
n - 1
T(n) = c1n + c2(n-1) + c3(n-1) + c4∑n
j=2
Tj
+ c5∑n
j=2
(Tj
-1) + c6∑n
j=2
(Tj
-1) + c7(n-1)
Prof. Adriano Patrick Cunha
Insertion Sort - Asympototic analysis
Woist-case (Sequence in reverse order)
tj
= j, para j = 2, 3, ..., n
∑n
j=2
Tj
= ∑n
j=2
j = (∑n
k=1
k) - 1 = n(n + 1)/2 - 1
∑n
j=2
(Tj
-1) = ∑n
j=2
( j-1) = ∑n-1
k=1
k = n(n - 1)/2
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
)
Prof. Adriano Patrick Cunha
Recursive Technique
Continua ...

Mais conteúdo relacionado

Mais procurados

Design and analysis of computer algorithms
Design and analysis of computer algorithmsDesign and analysis of computer algorithms
Design and analysis of computer algorithms
Krishna Chaytaniah
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 

Mais procurados (20)

test pre
test pretest pre
test pre
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
 
Design and analysis of computer algorithms
Design and analysis of computer algorithmsDesign and analysis of computer algorithms
Design and analysis of computer algorithms
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
 
Greedy Algoritham
Greedy AlgorithamGreedy Algoritham
Greedy Algoritham
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
 
Lecture 2 role of algorithms in computing
Lecture 2   role of algorithms in computingLecture 2   role of algorithms in computing
Lecture 2 role of algorithms in computing
 
Algorithms : Introduction and Analysis
Algorithms : Introduction and AnalysisAlgorithms : Introduction and Analysis
Algorithms : Introduction and Analysis
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 

Destaque

Algorithm & data structures lec1
Algorithm & data structures lec1Algorithm & data structures lec1
Algorithm & data structures lec1
Abdul Khan
 
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Kuca Moraes
 
Kuca Moraes - O Blog Primordial - 8020mkt
Kuca Moraes - O Blog Primordial - 8020mktKuca Moraes - O Blog Primordial - 8020mkt
Kuca Moraes - O Blog Primordial - 8020mkt
Kuca Moraes
 
Powerpoint tema 1 (1)
Powerpoint tema 1 (1)Powerpoint tema 1 (1)
Powerpoint tema 1 (1)
Jessie86
 

Destaque (20)

Data Structures for Robotic Learning
Data Structures for Robotic LearningData Structures for Robotic Learning
Data Structures for Robotic Learning
 
Algorithm & data structures lec1
Algorithm & data structures lec1Algorithm & data structures lec1
Algorithm & data structures lec1
 
Business Services - November 2015
Business Services - November 2015Business Services - November 2015
Business Services - November 2015
 
Brendan Murray Resume[1]
Brendan Murray Resume[1]Brendan Murray Resume[1]
Brendan Murray Resume[1]
 
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
Kuca Moraes + Estevão Rizzo - Métricas em Mídias Sociais - Cases: Portal Educ...
 
Mari Asun Landa
Mari Asun LandaMari Asun Landa
Mari Asun Landa
 
Wonder p owerpoint1
Wonder p owerpoint1Wonder p owerpoint1
Wonder p owerpoint1
 
Accesibilidad de un material
Accesibilidad de un materialAccesibilidad de un material
Accesibilidad de un material
 
Kuca Moraes - O Blog Primordial - 8020mkt
Kuca Moraes - O Blog Primordial - 8020mktKuca Moraes - O Blog Primordial - 8020mkt
Kuca Moraes - O Blog Primordial - 8020mkt
 
Mari Asun Landa
Mari Asun LandaMari Asun Landa
Mari Asun Landa
 
Mari asun landa
Mari asun landaMari asun landa
Mari asun landa
 
L_m-frdn
  L_m-frdn  L_m-frdn
L_m-frdn
 
North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015
North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015
North Shore and Hibiscus Coast Things to Do and Places To Go - October 2015
 
Organizing for our Collective Success Presented by the National Young Farmers...
Organizing for our Collective Success Presented by the National Young Farmers...Organizing for our Collective Success Presented by the National Young Farmers...
Organizing for our Collective Success Presented by the National Young Farmers...
 
StartUpSaturday_Deque
StartUpSaturday_DequeStartUpSaturday_Deque
StartUpSaturday_Deque
 
Relatório de Twitter - @deppaulocorrea (11/2009)
Relatório de Twitter - @deppaulocorrea (11/2009)Relatório de Twitter - @deppaulocorrea (11/2009)
Relatório de Twitter - @deppaulocorrea (11/2009)
 
prueba
pruebaprueba
prueba
 
Lectura debate market share y posicionamiento
Lectura debate market share y posicionamientoLectura debate market share y posicionamiento
Lectura debate market share y posicionamiento
 
Powerpoint tema 1 (1)
Powerpoint tema 1 (1)Powerpoint tema 1 (1)
Powerpoint tema 1 (1)
 
Storyboard
StoryboardStoryboard
Storyboard
 

Semelhante a Data structures and algorithms

CP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsCP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithms
Sheba41
 

Semelhante a Data structures and algorithms (20)

01-algo.ppt
01-algo.ppt01-algo.ppt
01-algo.ppt
 
ALGO.ppt
ALGO.pptALGO.ppt
ALGO.ppt
 
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and AlgorithmsCP4151 ADSA unit1 Advanced Data Structures and Algorithms
CP4151 ADSA unit1 Advanced Data Structures and Algorithms
 
CP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsCP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithms
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
 
role of algo.pptx
role of algo.pptxrole of algo.pptx
role of algo.pptx
 
Chapter one
Chapter oneChapter one
Chapter one
 
Introduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfIntroduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdf
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
CS-323 DAA.pdf
CS-323 DAA.pdfCS-323 DAA.pdf
CS-323 DAA.pdf
 
chapter 1
chapter 1chapter 1
chapter 1
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
 
Algorithms
Algorithms Algorithms
Algorithms
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
 
Alg1
Alg1Alg1
Alg1
 
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of AlgorithmsCS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
 

Mais de Adriano Patrick Cunha (8)

Desenvolvimento web e mobile ifce
Desenvolvimento web e mobile   ifceDesenvolvimento web e mobile   ifce
Desenvolvimento web e mobile ifce
 
Recuperacao Falhas em Sistemas Workflow
Recuperacao Falhas em Sistemas WorkflowRecuperacao Falhas em Sistemas Workflow
Recuperacao Falhas em Sistemas Workflow
 
ETL DW-RealTime
ETL DW-RealTimeETL DW-RealTime
ETL DW-RealTime
 
Congresso TI - Qualidade de Código.
Congresso TI - Qualidade de Código.Congresso TI - Qualidade de Código.
Congresso TI - Qualidade de Código.
 
Concurrencyproblem
ConcurrencyproblemConcurrencyproblem
Concurrencyproblem
 
Article K-OPT in JSSP
Article K-OPT in JSSPArticle K-OPT in JSSP
Article K-OPT in JSSP
 
Natuurweb
NatuurwebNatuurweb
Natuurweb
 
Natuur mobile
Natuur mobileNatuur mobile
Natuur mobile
 

Último

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 

Último (20)

%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 

Data structures and algorithms

  • 1. Data Structures and Algorithms Prof. Adriano Patrick Cunha
  • 2. Prof. Adriano Patrick Cunha Program Introduction Algorithms. Designing and Analyzing Algorithms. Recursive technique. Lists. Trees. Priority Lists.
  • 3. Prof. Adriano Patrick Cunha Data Structures and Algorithms “I will, in fact, claim that the difference between a bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” Linus Torvalds
  • 4. 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.
  • 5. Prof. Adriano Patrick Cunha Algorithms Informally, an algorithm is any well-defined computational procedure that takes some value, or set of values, as input and produces some value, or set of values, as output. An algorithm is thus a sequence of computational steps that transform the input into the output. We can also view an algorithm as a tool for solving a well-specified computational problem. The statement of the problem specifies in general terms the desired input/output relationship. The algorithm describes a specific computational procedure for achieving that input/output relationship.
  • 6. Prof. Adriano Patrick Cunha Problem: Sorting Input: sequence <a1, a2, ..., an) of numbers Output: permutation <a'1, a'2, ..., a'n> $(such that) -> $ a'1 <= a'2 <= ... <= a'n E.g. Input: 〈31, 41, 59, 26, 41, 58〉 Output: 〈26, 31, 41, 41, 58, 59〉
  • 7. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; It works the way many people sort the cards by playing cards 1. Letters initially on the table 2. One card at a time 3. Place it in the left hand, in the correct position a. Find the right position from right to left E.g. 〈5, 2, 4, 6, 1, 3〉
  • 8. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; Is correct ?
  • 9. Prof. Adriano Patrick Cunha Solve: Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; Is correct ? Is the best ?
  • 10. Prof. Adriano Patrick Cunha What is Correct? 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. Contrary to what you might expect, incorrect algorithms can sometimes be useful, if we can control their error rate.
  • 11. Prof. Adriano Patrick Cunha What is Correct? Loop invariant Property or statement that holds true for each loop iteration Helps understand why an algorithm is correct
  • 12. Prof. Adriano Patrick Cunha Loop Invariant Three details must be shown: Initialization: the invariant is true before the first loop iteration Maintenance: if the invariant is true before an iteration of the loop, it will remain true before the next loop iteration Termination: when the loop ends, the invariant gives us a useful property that helps to show that the algorithm is correct
  • 13. Prof. Adriano Patrick Cunha What is being the best? Insertion Sort Number of statements executed c1 n2 Computer A: 1 billion (109 ) instructions per second. Great programmer: 2n2 instructions. Intercalation Sort(Merge-Sort) Number of statements executed c2 nlgn Computer B: 10 millions (107 ) instructions per second. Regular programmer: 50nlgn instructions. Time to sort 1 million (106 ) elements of a set?
  • 14. 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).
  • 15. Prof. Adriano Patrick Cunha Problem of the traveling salesman 4 15 10 9 16 20 8 14 127 A B CD E A -> E -> C -> D -> B -> A <56km> B -> C -> E -> A -> D -> B <49km> C -> E -> A -> D -> B -> C <49km> D -> A -> E -> C -> B -> D <49km> E -> A -> D -> C -> B -> E <44km> N Alternativas (~n!) Tempo 5 120 0,00012 s 10 362880 3,62880 s 12 479001600 8 min 15 1307674368000 15 days 20 2432902008176640000 77.147 years 50 3,04 E+0064 ∞ 100 9,33 E+0157 ∞
  • 16. Prof. Adriano Patrick Cunha What is being the best? Running Time - Depends on input (e.g. already sorted) - Depends on input size (6 elements vs 6x10^9 elements) - parametrize in input size - Want upper bounds - guarantee to user
  • 17. Prof. Adriano Patrick Cunha Analysis of Algorithms Theoretical study of computer-program performance and resources usage. What's more important than performance? Why study algorithms and performance? Determines something is feasible or infeasible Performance enables the usability, security Speed is fun!!
  • 18. Prof. Adriano Patrick Cunha Kinds of Analysis - Woist-case(usually) - T(n) = max time on any input of size n - Average-case (sometimes) - T(n) = expected time over all inputs of size n - Need assumption of statistical distribution - Best-case (bogus) - Cheat
  • 19. Prof. Adriano Patrick Cunha Asympototic analysis Ignore machine dependent constants Look at GROWTH T(n) as n-> infinity O Notation - Drop low order terms - Ignore leading constants - E.g. 3n3 + 90n2 - 5n + 6046 => O(n3 ) - As n -> infinity, O(n2 ) algorithm always beats a O(n3 ) algorithm.
  • 20. Prof. Adriano Patrick Cunha Insertion Sort - Asympototic analysis Insertion-Sort(A, n) //Sort A[1..n] for j <- 2 to n do key <- A[ j ]; //Insert A[ j ] into the sorted sequence A[ 1 .. j -1] i <- j - 1; while(i > 0 and A[ i ] > key) do A[ i + 1 ] <- A[ i ]; i <- j - 1; A[ i + 1 ] <- key; cost times c1 c2 0 c3 c4 c5 c6 c7 n n - 1 0 n - 1 ∑n j=2 Tj ∑n j=2 (Tj -1) ∑n j=2 (Tj -1) n - 1 T(n) = c1n + c2(n-1) + c3(n-1) + c4∑n j=2 Tj + c5∑n j=2 (Tj -1) + c6∑n j=2 (Tj -1) + c7(n-1)
  • 21. Prof. Adriano Patrick Cunha Insertion Sort - Asympototic analysis Woist-case (Sequence in reverse order) tj = j, para j = 2, 3, ..., n ∑n j=2 Tj = ∑n j=2 j = (∑n k=1 k) - 1 = n(n + 1)/2 - 1 ∑n j=2 (Tj -1) = ∑n j=2 ( j-1) = ∑n-1 k=1 k = n(n - 1)/2 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 )
  • 22. Prof. Adriano Patrick Cunha Recursive Technique Continua ...