SlideShare a Scribd company logo
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 ...

More Related Content

What's hot

test pre
test pretest pre
test pre
farazch
 
ADA complete notes
ADA complete notesADA complete notes
ADA complete notes
Vinay Kumar C
 
Dynamic Programming - Part 1
Dynamic Programming - Part 1Dynamic Programming - Part 1
Dynamic Programming - Part 1
Amrinder Arora
 
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
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
Computer Hardware & Trouble shooting
 
Introduction to dynamic programming
Introduction to dynamic programmingIntroduction to dynamic programming
Introduction to dynamic programming
Amisha Narsingani
 
Greedy Algoritham
Greedy AlgorithamGreedy Algoritham
Greedy Algoritham
RJ Mehul Gadhiya
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
smruti sarangi
 
Greedymethod
GreedymethodGreedymethod
Greedymethod
Meenakshi Devi
 
5.1 greedyyy 02
5.1 greedyyy 025.1 greedyyy 02
5.1 greedyyy 02
Krish_ver2
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Divide and conquer algorithm
Divide and conquer algorithmDivide and conquer algorithm
Divide and conquer algorithm
CHANDAN KUMAR
 
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
jayavignesh86
 
Algorithms : Introduction and Analysis
Algorithms : Introduction and AnalysisAlgorithms : Introduction and Analysis
Algorithms : Introduction and Analysis
Dhrumil Patel
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Sahil Kumar
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
Andres Mendez-Vazquez
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
CHANDAN KUMAR
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
rupali_2bonde
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
FellowBuddy.com
 

What's hot (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
 

Viewers also liked

Data Structures for Robotic Learning
Data Structures for Robotic LearningData Structures for Robotic Learning
Data Structures for Robotic Learning
Lesley Naleen Sarah Cooper
 
Algorithm & data structures lec1
Algorithm & data structures lec1Algorithm & data structures lec1
Algorithm & data structures lec1
Abdul Khan
 
Business Services - November 2015
Business Services - November 2015Business Services - November 2015
Business Services - November 2015
Linku2 North Shore
 
Brendan Murray Resume[1]
Brendan Murray Resume[1]Brendan Murray Resume[1]
Brendan Murray Resume[1]
JenMurray99
 
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
 
Wonder p owerpoint1
Wonder p owerpoint1Wonder p owerpoint1
Wonder p owerpoint1
22cartwrights
 
Accesibilidad de un material
Accesibilidad de un materialAccesibilidad de un material
Accesibilidad de un material
marset93
 
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
 
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
Linku2 North Shore
 
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...
DiegoFooter
 
StartUpSaturday_Deque
StartUpSaturday_DequeStartUpSaturday_Deque
StartUpSaturday_Deque
Srinivasu Chakravarthula
 
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)
Kuca Moraes
 
prueba
pruebaprueba
Lectura debate market share y posicionamiento
Lectura debate market share y posicionamientoLectura debate market share y posicionamiento
Lectura debate market share y posicionamiento
Maryfrancia Méndez Matheus
 
Powerpoint tema 1 (1)
Powerpoint tema 1 (1)Powerpoint tema 1 (1)
Powerpoint tema 1 (1)
Jessie86
 
Storyboard
StoryboardStoryboard
Storyboard
lshad24
 

Viewers also liked (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
 

Similar to Data structures and algorithms

01-algo.ppt
01-algo.ppt01-algo.ppt
01-algo.ppt
ssuser8d64ca
 
ALGO.ppt
ALGO.pptALGO.ppt
ALGO.ppt
PidoonEsm
 
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
Sheba41
 
CP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithmsCP4151 Advanced data structures and algorithms
CP4151 Advanced data structures and algorithms
Sheba41
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
Venkatesh Iyer
 
Cis435 week01
Cis435 week01Cis435 week01
Cis435 week01
ashish bansal
 
role of algo.pptx
role of algo.pptxrole of algo.pptx
role of algo.pptx
AmirZaman21
 
Chapter one
Chapter oneChapter one
Chapter one
mihiretu kassaye
 
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
TulasiramKandula1
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
Shaista Qadir
 
CS-323 DAA.pdf
CS-323 DAA.pdfCS-323 DAA.pdf
CS-323 DAA.pdf
priyadharshinic22
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
DAA - chapter 1.pdf
DAA - chapter 1.pdfDAA - chapter 1.pdf
DAA - chapter 1.pdf
ASMAALWADEE2
 
Algorithms
Algorithms Algorithms
Algorithms
yashodhaHR2
 
Segment_1_New computer algorithm for cse.pptx
Segment_1_New computer algorithm for cse.pptxSegment_1_New computer algorithm for cse.pptx
Segment_1_New computer algorithm for cse.pptx
fahmidasetu
 
Annotations.pdf
Annotations.pdfAnnotations.pdf
Annotations.pdf
GauravKumar295392
 
Alg1
Alg1Alg1
Data Structure: Algorithm and analysis
Data Structure: Algorithm and analysisData Structure: Algorithm and analysis
Data Structure: Algorithm and analysis
Dr. Rajdeep Chatterjee
 
Algorithm
AlgorithmAlgorithm
Algorithm
Anirban Sarkar
 
Algorithm
AlgorithmAlgorithm
Algorithm
Syam Kumar
 

Similar to 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
 
Segment_1_New computer algorithm for cse.pptx
Segment_1_New computer algorithm for cse.pptxSegment_1_New computer algorithm for cse.pptx
Segment_1_New computer algorithm for cse.pptx
 
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
 

More from Adriano Patrick Cunha

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

More from 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
 

Recently uploaded

UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
Peter Muessig
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
Green Software Development
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
Rakesh Kumar R
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
YousufSait3
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
rodomar2
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
Yara Milbes
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Envertis Software Solutions
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
Remote DBA Services
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
mz5nrf0n
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
Rakesh Kumar R
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
Drona Infotech
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
Peter Muessig
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
mz5nrf0n
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
Grant Fritchey
 
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
ssuserad3af4
 

Recently uploaded (20)

UI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design SystemUI5con 2024 - Bring Your Own Design System
UI5con 2024 - Bring Your Own Design System
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, FactsALGIT - Assembly Line for Green IT - Numbers, Data, Facts
ALGIT - Assembly Line for Green IT - Numbers, Data, Facts
 
How to write a program in any programming language
How to write a program in any programming languageHow to write a program in any programming language
How to write a program in any programming language
 
zOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL DifferenceszOS Mainframe JES2-JES3 JCL-JECL Differences
zOS Mainframe JES2-JES3 JCL-JECL Differences
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CDKuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
KuberTENes Birthday Bash Guadalajara - Introducción a Argo CD
 
SMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API ServiceSMS API Integration in Saudi Arabia| Best SMS API Service
SMS API Integration in Saudi Arabia| Best SMS API Service
 
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative AnalysisOdoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
Odoo ERP Vs. Traditional ERP Systems – A Comparative Analysis
 
Oracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptxOracle Database 19c New Features for DBAs and Developers.pptx
Oracle Database 19c New Features for DBAs and Developers.pptx
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
在线购买加拿大英属哥伦比亚大学毕业证本科学位证书原版一模一样
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
Fundamentals of Programming and Language Processors
Fundamentals of Programming and Language ProcessorsFundamentals of Programming and Language Processors
Fundamentals of Programming and Language Processors
 
Mobile app Development Services | Drona Infotech
Mobile app Development Services  | Drona InfotechMobile app Development Services  | Drona Infotech
Mobile app Development Services | Drona Infotech
 
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling ExtensionsUI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
UI5con 2024 - Boost Your Development Experience with UI5 Tooling Extensions
 
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
原版定制美国纽约州立大学奥尔巴尼分校毕业证学位证书原版一模一样
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Using Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query PerformanceUsing Query Store in Azure PostgreSQL to Understand Query Performance
Using Query Store in Azure PostgreSQL to Understand Query Performance
 
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
316895207-SAP-Oil-and-Gas-Downstream-Training.pptx
 

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 ...