SlideShare a Scribd company logo
1 of 35
Download to read offline
F O U R T H E D I T I O N
ALGORITHMS: THEORY, DESIGN
AND IMPLEMENTATION
‣ overview
‣ why study algorithms?
‣ resources
3
Their impact is broad and far-reaching.
Internet. Web search, packet routing, distributed file sharing, ...
Biology. Human genome project, protein folding, ...
Computers. Circuit layout, file system, compilers, ...
Computer graphics. Movies, video games, virtual reality, ...
Security. Cell phones, e-commerce, voting machines, ...
Multimedia. MP3, JPG, DivX, HDTV, face recognition, ...
Social networks. Recommendations, news feeds, advertisements, ...
Physics. N-body simulation, particle collision simulation, ...
Why study algorithms?
Old roots, new opportunities. 300 BCE
・Study of algorithms dates at least to Euclid.
・Formalized by Church and Turing in 1930s.
・Some important algorithms may be discovered
by yourself!
1920s
1930s
1940s
1950s
1960s
1970s
1980s
1990s
2000s
4
Why study algorithms?
5
To solve problems that could not otherwise be addressed.
Ex. Network connectivity. [stay tuned]
Why study algorithms?
T
6
“ For me, great algorithms are the poetry of computation. Just like
verse, they can be terse, allusive, dense, and even mysterious.
But once unlocked, they cast a brilliant new light on some
aspect of computing. ” — Francis Sullivan
“ An algorithm must be seen to be believed. ” — Donald Knuth
2 COMPUTING
IN
SCIENCE & ENGINEERING
rithms have advanced in startling and unexpected ways in the
20th century—at least it looks that way to us now. The algo-
rithms we chose for this issue have been essential for progress
in communications, health care, manufacturing, economics,
weather prediction, defense, and fundamental science. Con-
versely, progress in these areas has stimulated the search for
ever-better algorithms. I recall one late-night bull session on
the Maryland Shore when someone asked, “Who first ate a
crab? After all, they don’t look very appetizing.’’ After the usual
speculations about the observed behavior of sea gulls, someone
gave what must be the right answer—namely, “A very hungry
person first ate a crab.”
The flip side to “necessity is the mother of invention’’ is “in-
vention creates its own necessity.’’ Our need for powerful ma-
chines always exceeds their availability. Each significant com-
putation brings insights that suggest the next, usually much
larger,computationtobedone.Newalgorithmsareanattempt
to bridge the gap between the demand for cycles and the avail-
able supply of them. We’ve become accustomed to gaining the
Moore’s Law factor of two every 18 months. In effect, Moore’s
Law changes the constant in front of the estimate of running
time as a function of problem size. Important new algorithms
do not come along every 1.5 years, but when they do, they can
change the exponent of the complexity!
For me, great algorithms are the poetry of computation.
Just like verse, they can be terse, allusive, dense, and even
not have paid off.
Researchers have cracked many hard problems since 1 Jan-
uary 1900, but we are passing some even harder ones on to the
next century. In spite of a lot of good work, the question of
how to extract information from extremely large masses of
data is still almost untouched. There are still very big chal-
lenges coming from more “traditional” tasks, too. For exam-
ple, we need efficient methods to tell when the result of a large
floating-point calculation is likely to be correct. Think of the
way that check sums function. The added computational cost
is very small, but the added confidence in the answer is large.
Is there an analog for things such as huge, multidisciplinary
optimizations? At an even deeper level is the issue of reason-
able methods for solving specific cases of “impossible’’ prob-
lems. Instances of NP-complete problems crop up in at-
tempting to answer many practical questions. Are there
efficient ways to attack them?
I suspect that in the 21st century, things will be ripe for an-
other revolution in our understanding of the foundations of
computational theory. Questions already arising from quan-
tum computing and problems associated with the generation
of random numbers seem to require that we somehow tie to-
gether theories of computing, logic, and the nature of the
physical world.
The new century is not going to be very restful for us, but it
is not going to be dull either!
Francis Sullivan, Associate Editor-in-Chief
HE THEME OF THIS FIRST-OF-THE-CENTURY ISSUE OF COMPUTING IN
SCIENCE & ENGINEERING IS ALGORITHMS. IN FACT, WE WERE BOLD
ENOUGH—AND PERHAPS FOOLISH ENOUGH—TO CALL THE 10 EXAMPLES WE’VE SE-
LECTED “THE TOP 10 ALGORITHMS OF THE CENTURY.”
Computationalalgorithmsareprobablyasoldascivilization. mysterious. But once unlocked, they cast a brilliant new light
Sumerian cuneiform, one of the most ancient written records, on some aspect of computing. A colleague recently claimed
consists partly of algorithm descriptions for reckoning in base that he’d done only 15 minutes of productive work in his
60. And I suppose we could claim that the Druid algorithm for whole life. He wasn’t joking, because he was referring to the
estimating the start of summer is embodied in Stonehenge. 15 minutes during which he’d sketched out a fundamental op-
(That’s really hard hardware!) timization algorithm. He regarded the previous years of
Like so many other things that technology affects, algo- thought and investigation as a sunk cost that might or might
Why study algorithms?
For intellectual stimulation.
F R O M TH E
E D I T O R S
THE JOY OF ALGORITHMS
7
Why study algorithms?
To become a proficient programmer.
“ 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 (creator of Linux)
“ Algorithms + Data Structures = Programs. ” — Niklaus Wirth
𝐸 = 𝑚𝑐2
𝐹 = 𝑚𝑎
F = Gm1m2
r2
Why study algorithms?
They may unlock the secrets of life and of the universe.
Computational models are replacing math models in scientific inquiry.
“ Algorithms: a common language for nature, human, and computer. ” — Avi Wigderson
8
for (double t = 0.0; true; t = t + dt)
for (int i = 0; i < N; i++)
{
bodies[i].resetForce();
for (int j = 0; j < N; j++)
if (i != j)
bodies[i].addForce(bodies[j]);
21st century science
(algorithm based)
}
For fun and profit.
9
Why study algorithms?
・Their impact is broad and far-reaching.
・Old roots, new opportunities.
・To solve problems that could not otherwise be addressed.
・For intellectual stimulation.
・To become a proficient programmer.
・They may unlock the secrets of life and of the universe.
・For fun and profit.
10
Why study algorithms?
Why study anything else?
INDICATIVE ROAD MAP FORTHE
STRUCTURE OF CONTENTS
Brute-force
Divide-and-
conquer
Decrease-
and-conquer
Transform-
and-conquer
Greedy
Dynamic
programming
Sorting
algorithms
√ (LW3) √ (LW4) √ (LW5) √ (LW6)
Search
algorithms
√ √ (LW8) √ (LW8)
Graph
algorithms
√ (LW9) √ (LW9) √ √ √ (LW9)
Path finding
algorithms
√ √ (LW10) √
Network
flow
algorithms
√ √ (LW11) √
Computation
al geometry
algorithms
√ √ (LW12) √
SOME LOGISTICS ABOUT THE MODULE
• Case studies, i.e., problems and algorithmic solutions, will be
discussed in groups during Q&A sessions, as part of the
Problem Based Learning approach to our teaching.
Therefore, please come prepared to these sessions.
• Surveys may be contacted in order to reflect on your
perception of difficulty to algorithmically resolve the
problem.
SOME LOGISTICS ABOUT THE MODULE
• Programming exercises for the tutorials will address
implementation of algorithms in Java and within a
framework, which is already prepared for you.
• Two assessment components, a course work and an
exam. Please check details on Blackboard’s module site.
KARATSUBA MULTIPLICATION OF
INTEGER NUMBERS
• Having studied the plain text notes or having watched the
recommended videos for LW1, please apply the Karatsuba
Algorithm for the multiplication of the numbers:
• 2019
• 1963
CASE STUDY FOR
TUTORIALS
UNION-FIND ALGORITHMS ANDTHE CONNECTIVITY
PROBLEM
4
Dynamic connectivity
Given a set of N objects.
・Union command: connect two objects.
・Find/connected query: is there a path connecting the two objects?
union(4, 3)
union(3, 8)
union(6, 5)
union(9, 4)
union(2, 1)
connected(0, 7)
connected(8, 9)
union(5, 0)
union(7, 2)
union(6, 1)
union(1, 0)
connected(0, 7)
0 1 2 3 4
5 6 7 8 9

✔
✔
Q. Is there a path connecting p and q ?
A. Yes.
5
Connectivity example
p
q
Applications involve manipulating objects of all types.
・Pixels in a digital photo.
・Computers in a network.
・Friends in a social network.
・Transistors in a computer chip.
・Elements in a mathematical set.
・Variable names in Fortran program.
・Metallic sites in a composite system.
When programming, convenient to name objects 0 to N –1.
・Use integers as array index.
・Suppress details not relevant to union-find.
6
Modeling the objects
can use symbol table to translate from site
names to integers: stay tuned (Chapter 3)
Modeling the connections
We assume "is connected to" is an equivalence relation:
・Reflexive: p is connected to p.
・Symmetric: if p is connected to q, then q is connected to p.
・Transitive: if p is connected to q and q is connected to r,
then p is connected to r.
Connected components. Maximal set of objects that are mutually
connected.
{ 0 } { 1 4 5 } { 2 3 6 7 }
3 connected components
7
0
4
1
5
2
6
3
7
8
Implementing the operations
Find query. Check if two objects are in the same component.
Union command. Replace components containing two objects
with their union.
{ 0 } { 1 4 5 } { 2 3 6 7 }
3 connected components
0 1 2 3
4 5 6 7
union(2, 5)
{ 0 } { 1 2 3 4 5 6 7 }
2 connected components
0 1 2 3
4 5 6 7
9
Union-find data type (API)
Goal. Design efficient data structure for union-find.
・Number of objects N can be huge.
・Number of operations M can be huge.
・Find queries and union commands may be intermixed.
public class UF
UF(int N)
void union(int p, int q)
boolean connected(int p, int q)
int find(int p)
int count()
initialize union-find data structure with
N objects (0 to N – 1)
add connection between p and q
are p and q in the same component?
component identifier for p (0 to N – 1)
number of components
Dynamic-connectivity client
・Read in number of objects N from standard input.
・Repeat:
– read in pair of integers from standard input
– if they are not yet connected, connect them and print out pair
public static void main(String[] args)
{
int N = StdIn.readInt();
UF uf = new UF(N);
while (!StdIn.isEmpty())
{
int p = StdIn.readInt();
int q = StdIn.readInt();
if (!uf.connected(p, q))
{
uf.union(p, q);
StdOut.println(p + " " + q);
}
}
}
10
% more tinyUF.txt
10
4 3
3 8
6 5
9 4
2 1
8 9
5 0
7 2
6 1
1 0
6 7
13
0, 5 and 6 are connected
1, 2, and 7 are connected
3, 4, 8, and 9 are connected
0
5
1
6
2
7
3
8
4
9
0 1
0 1
1 8
2 3
8 0
4 5
0 1
6 7
8 8
8 9
id[]
Quick-find [eager approach]
Data structure.
if and only if
・Integer array id[] of size N.
・Interpretation: p and q are connected iff they have the same id.
Find. Check if p and q have the same id.
after union of 6 and 1
problem: many values can change
14
Quick-find [eager approach]
Data structure.
・Integer array id[] of size N.
・Interpretation: p and q are connected iff they have the same id.
id[6] = 0; id[1] = 1
6 and 1 are not connected
Union. To merge components containing p and q, change all entries
whose id equals id[p] to id[q].
0 1
0 1
1 8
2 3
8 0
4 5
0 1
6 7
8 8
8 9
1 1
0 1
1 8
2 3
8 1
4 5
1 1
6 7
8 8
8 9
id[]
id[]
15
Quick-find demo
0
5
1
6
2
7
3
8
4
9
0 1
0 1
2 3
2 3
4 5
4 5
6 7
6 7
8 9
8 9
id[]
Quick-find demo
0
5
1
6
2
7
3
8
4
9
1 1
0 1
1 8
2 3
8 1
4 5
1 1
6 7
8 8
8 9
id[]
public boolean connected(int p, int q)
{ }
public
{
void union(int p, int q)
}
}
17
Quick-find: Java implementation – Your first exercise 
public class QuickFindUF
{
private int[] id;
public QuickFindUF(int N)
{
set id of each object to itself
(N array accesses)
change all entries with id[p] to id[q]
(at most 2N + 2 array accesses)
}
check whether p and q
are in the same component
(2 array accesses)
Quick-find is too slow
Cost model. Number of array accesses (for read or write).
algorithm
quick-find
initialize
N
union
N
find
1
order of growth of number of array accesses
Quick-find defect. Union too expensive? More on these aspects of performance
next week…..
quadratic
Ex. Takes N 2 array accesses to process sequence of N union commands
on N objects.
18
・Integer array id[] of size N.
・Interpretation: id[i] is parent of i.
・Root of i is id[id[id[...id[i]...]]].
Quick-union [lazy approach]
Data structure.
0 1
0 1
9 4
2 3
9 6
4 5
6 7
6 7
8 9
8 9
id[]
3
root of 3 is 9
22
5
4
7
keep going until it doesn’t change
(algorithm ensures no cycles)
0 1 9 6 8
2
・Integer array id[] of size N.
・Interpretation: id[i] is parent of i.
・Root of i is id[id[id[...id[i]...]]].
Find. Check if p and q have the same root.
set the id of p's root to the id of q's root.
23
Quick-union [lazy approach]
Data structure.
0 1
0 1
9 4
2 3
9 6
4 5
6 7
6 7
8 9
8 9
id[]
3
4
7
0 1
9
6 8
2
only one value changes
p
q
0 1
0 1
9 4
2 3
9 6
4 5
6 7
6 7
8 6
8 9
id[]
5
3
5
4
7
0 1 9 6 8
2
p
q
root of 3 is 9
root of 5 is 6
3 and 5 are not connected
Union. To merge components containing p and q,
24
Quick-union demo
0 1 2 3 4 5 6 7 8 9
0 1
0 1
2 3
2 3
4 5
4 5
6 7
6 7
8 9
8 9
id[]
0
1
2
5
6
7
3
4
Quick-union demo
8
9
1 8
0 1
1 8
2 3
3 0
4 5
5 1
6 7
8 8
8 9
id[]
Quick-union: Java implementation – Your second exercise 
public class QuickUnionUF
{
private int[] id;
public QuickUnionUF(int N)
{
}
private int root(int i)
{
}
public boolean connected(int p, int q)
{
}
public void union(int p, int q)
{
set id of each object to itself
(N array accesses)
chase parent pointers until reach root
(depth of i array accesses)
check if p and q have same root
(depth of p and q array accesses)
change root of p to point to root of q
(depth of p and q array accesses)
}
}
26
worst case
† includes cost of finding roots
Quick-find defect.
・Union too expensive (N array accesses).
・Trees are flat, but too expensive to keep them flat.
Quick-union defect.
・Trees can get tall.
・Find too expensive (could be N array accesses).
27
Quick-union is also too slow
Cost model. Number of array accesses (for read or write).
algorithm
quick-find
initialize
N
union
N
find
1
quick-union N N † N
48
・Percolation.
・Games (Go, Hex).
✓ Dynamic connectivity.
・Least common ancestor.
・Equivalence of finite state automata.
・Hoshen-Kopelman algorithm in physics.
・Hinley-Milner polymorphic type inference.
・Kruskal's minimum spanning tree algorithm.
・Compiling equivalence statements in Fortran.
・Morphological attribute openings and closings.
・Matlab's bwlabel() function in image processing.
Union-find applications
Steps to developing a usable algorithm.
・Model the problem.
・Find an algorithm to solve it.
・Fast enough? Fits in memory?
・If not, figure out why.
・Find a way to address the problem.
・Iterate until satisfied.
63
In a nutshell

More Related Content

Similar to Lecture 1 Slides -Introduction to algorithms.pdf

A New Year in Data Science: ML Unpaused
A New Year in Data Science: ML UnpausedA New Year in Data Science: ML Unpaused
A New Year in Data Science: ML UnpausedPaco Nathan
 
Discrete Mathematics Cse131
Discrete Mathematics Cse131Discrete Mathematics Cse131
Discrete Mathematics Cse131ashikul akash
 
Unit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxUnit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxDr.M.Karthika parthasarathy
 
SoftComputing.pdf
SoftComputing.pdfSoftComputing.pdf
SoftComputing.pdfktosri
 
Introduction.doc
Introduction.docIntroduction.doc
Introduction.docbutest
 
Informatics is a natural science
Informatics is a natural scienceInformatics is a natural science
Informatics is a natural scienceFrank van Harmelen
 
Introduction to Artificial Intelligences
Introduction to Artificial IntelligencesIntroduction to Artificial Intelligences
Introduction to Artificial IntelligencesMeenakshi Paul
 
computer science engineering spe ialized in artificial Intelligence
computer science engineering spe ialized in artificial Intelligencecomputer science engineering spe ialized in artificial Intelligence
computer science engineering spe ialized in artificial IntelligenceKhanKhaja1
 
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...YanNaingSoe33
 
Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402vrij
 

Similar to Lecture 1 Slides -Introduction to algorithms.pdf (20)

A New Year in Data Science: ML Unpaused
A New Year in Data Science: ML UnpausedA New Year in Data Science: ML Unpaused
A New Year in Data Science: ML Unpaused
 
Discrete Mathematics Cse131
Discrete Mathematics Cse131Discrete Mathematics Cse131
Discrete Mathematics Cse131
 
1. The Game Of The Century
1. The Game Of The Century1. The Game Of The Century
1. The Game Of The Century
 
Data mining BY Zubair Yaseen
Data mining BY Zubair YaseenData mining BY Zubair Yaseen
Data mining BY Zubair Yaseen
 
Unit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptxUnit 1 Introduction to Artificial Intelligence.pptx
Unit 1 Introduction to Artificial Intelligence.pptx
 
Artificial intelligence
Artificial intelligenceArtificial intelligence
Artificial intelligence
 
1. Cyber and Intelligence
1. Cyber and Intelligence1. Cyber and Intelligence
1. Cyber and Intelligence
 
SoftComputing.pdf
SoftComputing.pdfSoftComputing.pdf
SoftComputing.pdf
 
Deep learning
Deep learningDeep learning
Deep learning
 
Introduction.doc
Introduction.docIntroduction.doc
Introduction.doc
 
Informatics is a natural science
Informatics is a natural scienceInformatics is a natural science
Informatics is a natural science
 
ai.ppt
ai.pptai.ppt
ai.ppt
 
ai.ppt
ai.pptai.ppt
ai.ppt
 
Introduction to Artificial Intelligences
Introduction to Artificial IntelligencesIntroduction to Artificial Intelligences
Introduction to Artificial Intelligences
 
ai.ppt
ai.pptai.ppt
ai.ppt
 
computer science engineering spe ialized in artificial Intelligence
computer science engineering spe ialized in artificial Intelligencecomputer science engineering spe ialized in artificial Intelligence
computer science engineering spe ialized in artificial Intelligence
 
ai.ppt
ai.pptai.ppt
ai.ppt
 
What iscs
What iscsWhat iscs
What iscs
 
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...
The Design and Analysis of Computer Algorithms [Aho, Hopcroft & Ullman 1974-0...
 
Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402
 

Recently uploaded

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 

Recently uploaded (20)

Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 

Lecture 1 Slides -Introduction to algorithms.pdf

  • 1. F O U R T H E D I T I O N ALGORITHMS: THEORY, DESIGN AND IMPLEMENTATION ‣ overview ‣ why study algorithms? ‣ resources
  • 2. 3 Their impact is broad and far-reaching. Internet. Web search, packet routing, distributed file sharing, ... Biology. Human genome project, protein folding, ... Computers. Circuit layout, file system, compilers, ... Computer graphics. Movies, video games, virtual reality, ... Security. Cell phones, e-commerce, voting machines, ... Multimedia. MP3, JPG, DivX, HDTV, face recognition, ... Social networks. Recommendations, news feeds, advertisements, ... Physics. N-body simulation, particle collision simulation, ... Why study algorithms?
  • 3. Old roots, new opportunities. 300 BCE ・Study of algorithms dates at least to Euclid. ・Formalized by Church and Turing in 1930s. ・Some important algorithms may be discovered by yourself! 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 4 Why study algorithms?
  • 4. 5 To solve problems that could not otherwise be addressed. Ex. Network connectivity. [stay tuned] Why study algorithms?
  • 5. T 6 “ For me, great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even mysterious. But once unlocked, they cast a brilliant new light on some aspect of computing. ” — Francis Sullivan “ An algorithm must be seen to be believed. ” — Donald Knuth 2 COMPUTING IN SCIENCE & ENGINEERING rithms have advanced in startling and unexpected ways in the 20th century—at least it looks that way to us now. The algo- rithms we chose for this issue have been essential for progress in communications, health care, manufacturing, economics, weather prediction, defense, and fundamental science. Con- versely, progress in these areas has stimulated the search for ever-better algorithms. I recall one late-night bull session on the Maryland Shore when someone asked, “Who first ate a crab? After all, they don’t look very appetizing.’’ After the usual speculations about the observed behavior of sea gulls, someone gave what must be the right answer—namely, “A very hungry person first ate a crab.” The flip side to “necessity is the mother of invention’’ is “in- vention creates its own necessity.’’ Our need for powerful ma- chines always exceeds their availability. Each significant com- putation brings insights that suggest the next, usually much larger,computationtobedone.Newalgorithmsareanattempt to bridge the gap between the demand for cycles and the avail- able supply of them. We’ve become accustomed to gaining the Moore’s Law factor of two every 18 months. In effect, Moore’s Law changes the constant in front of the estimate of running time as a function of problem size. Important new algorithms do not come along every 1.5 years, but when they do, they can change the exponent of the complexity! For me, great algorithms are the poetry of computation. Just like verse, they can be terse, allusive, dense, and even not have paid off. Researchers have cracked many hard problems since 1 Jan- uary 1900, but we are passing some even harder ones on to the next century. In spite of a lot of good work, the question of how to extract information from extremely large masses of data is still almost untouched. There are still very big chal- lenges coming from more “traditional” tasks, too. For exam- ple, we need efficient methods to tell when the result of a large floating-point calculation is likely to be correct. Think of the way that check sums function. The added computational cost is very small, but the added confidence in the answer is large. Is there an analog for things such as huge, multidisciplinary optimizations? At an even deeper level is the issue of reason- able methods for solving specific cases of “impossible’’ prob- lems. Instances of NP-complete problems crop up in at- tempting to answer many practical questions. Are there efficient ways to attack them? I suspect that in the 21st century, things will be ripe for an- other revolution in our understanding of the foundations of computational theory. Questions already arising from quan- tum computing and problems associated with the generation of random numbers seem to require that we somehow tie to- gether theories of computing, logic, and the nature of the physical world. The new century is not going to be very restful for us, but it is not going to be dull either! Francis Sullivan, Associate Editor-in-Chief HE THEME OF THIS FIRST-OF-THE-CENTURY ISSUE OF COMPUTING IN SCIENCE & ENGINEERING IS ALGORITHMS. IN FACT, WE WERE BOLD ENOUGH—AND PERHAPS FOOLISH ENOUGH—TO CALL THE 10 EXAMPLES WE’VE SE- LECTED “THE TOP 10 ALGORITHMS OF THE CENTURY.” Computationalalgorithmsareprobablyasoldascivilization. mysterious. But once unlocked, they cast a brilliant new light Sumerian cuneiform, one of the most ancient written records, on some aspect of computing. A colleague recently claimed consists partly of algorithm descriptions for reckoning in base that he’d done only 15 minutes of productive work in his 60. And I suppose we could claim that the Druid algorithm for whole life. He wasn’t joking, because he was referring to the estimating the start of summer is embodied in Stonehenge. 15 minutes during which he’d sketched out a fundamental op- (That’s really hard hardware!) timization algorithm. He regarded the previous years of Like so many other things that technology affects, algo- thought and investigation as a sunk cost that might or might Why study algorithms? For intellectual stimulation. F R O M TH E E D I T O R S THE JOY OF ALGORITHMS
  • 6. 7 Why study algorithms? To become a proficient programmer. “ 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 (creator of Linux) “ Algorithms + Data Structures = Programs. ” — Niklaus Wirth
  • 7. 𝐸 = 𝑚𝑐2 𝐹 = 𝑚𝑎 F = Gm1m2 r2 Why study algorithms? They may unlock the secrets of life and of the universe. Computational models are replacing math models in scientific inquiry. “ Algorithms: a common language for nature, human, and computer. ” — Avi Wigderson 8 for (double t = 0.0; true; t = t + dt) for (int i = 0; i < N; i++) { bodies[i].resetForce(); for (int j = 0; j < N; j++) if (i != j) bodies[i].addForce(bodies[j]); 21st century science (algorithm based) }
  • 8. For fun and profit. 9 Why study algorithms?
  • 9. ・Their impact is broad and far-reaching. ・Old roots, new opportunities. ・To solve problems that could not otherwise be addressed. ・For intellectual stimulation. ・To become a proficient programmer. ・They may unlock the secrets of life and of the universe. ・For fun and profit. 10 Why study algorithms? Why study anything else?
  • 10. INDICATIVE ROAD MAP FORTHE STRUCTURE OF CONTENTS Brute-force Divide-and- conquer Decrease- and-conquer Transform- and-conquer Greedy Dynamic programming Sorting algorithms √ (LW3) √ (LW4) √ (LW5) √ (LW6) Search algorithms √ √ (LW8) √ (LW8) Graph algorithms √ (LW9) √ (LW9) √ √ √ (LW9) Path finding algorithms √ √ (LW10) √ Network flow algorithms √ √ (LW11) √ Computation al geometry algorithms √ √ (LW12) √
  • 11. SOME LOGISTICS ABOUT THE MODULE • Case studies, i.e., problems and algorithmic solutions, will be discussed in groups during Q&A sessions, as part of the Problem Based Learning approach to our teaching. Therefore, please come prepared to these sessions. • Surveys may be contacted in order to reflect on your perception of difficulty to algorithmically resolve the problem.
  • 12. SOME LOGISTICS ABOUT THE MODULE • Programming exercises for the tutorials will address implementation of algorithms in Java and within a framework, which is already prepared for you. • Two assessment components, a course work and an exam. Please check details on Blackboard’s module site.
  • 13. KARATSUBA MULTIPLICATION OF INTEGER NUMBERS • Having studied the plain text notes or having watched the recommended videos for LW1, please apply the Karatsuba Algorithm for the multiplication of the numbers: • 2019 • 1963
  • 14. CASE STUDY FOR TUTORIALS UNION-FIND ALGORITHMS ANDTHE CONNECTIVITY PROBLEM
  • 15. 4 Dynamic connectivity Given a set of N objects. ・Union command: connect two objects. ・Find/connected query: is there a path connecting the two objects? union(4, 3) union(3, 8) union(6, 5) union(9, 4) union(2, 1) connected(0, 7) connected(8, 9) union(5, 0) union(7, 2) union(6, 1) union(1, 0) connected(0, 7) 0 1 2 3 4 5 6 7 8 9  ✔ ✔
  • 16. Q. Is there a path connecting p and q ? A. Yes. 5 Connectivity example p q
  • 17. Applications involve manipulating objects of all types. ・Pixels in a digital photo. ・Computers in a network. ・Friends in a social network. ・Transistors in a computer chip. ・Elements in a mathematical set. ・Variable names in Fortran program. ・Metallic sites in a composite system. When programming, convenient to name objects 0 to N –1. ・Use integers as array index. ・Suppress details not relevant to union-find. 6 Modeling the objects can use symbol table to translate from site names to integers: stay tuned (Chapter 3)
  • 18. Modeling the connections We assume "is connected to" is an equivalence relation: ・Reflexive: p is connected to p. ・Symmetric: if p is connected to q, then q is connected to p. ・Transitive: if p is connected to q and q is connected to r, then p is connected to r. Connected components. Maximal set of objects that are mutually connected. { 0 } { 1 4 5 } { 2 3 6 7 } 3 connected components 7 0 4 1 5 2 6 3 7
  • 19. 8 Implementing the operations Find query. Check if two objects are in the same component. Union command. Replace components containing two objects with their union. { 0 } { 1 4 5 } { 2 3 6 7 } 3 connected components 0 1 2 3 4 5 6 7 union(2, 5) { 0 } { 1 2 3 4 5 6 7 } 2 connected components 0 1 2 3 4 5 6 7
  • 20. 9 Union-find data type (API) Goal. Design efficient data structure for union-find. ・Number of objects N can be huge. ・Number of operations M can be huge. ・Find queries and union commands may be intermixed. public class UF UF(int N) void union(int p, int q) boolean connected(int p, int q) int find(int p) int count() initialize union-find data structure with N objects (0 to N – 1) add connection between p and q are p and q in the same component? component identifier for p (0 to N – 1) number of components
  • 21. Dynamic-connectivity client ・Read in number of objects N from standard input. ・Repeat: – read in pair of integers from standard input – if they are not yet connected, connect them and print out pair public static void main(String[] args) { int N = StdIn.readInt(); UF uf = new UF(N); while (!StdIn.isEmpty()) { int p = StdIn.readInt(); int q = StdIn.readInt(); if (!uf.connected(p, q)) { uf.union(p, q); StdOut.println(p + " " + q); } } } 10 % more tinyUF.txt 10 4 3 3 8 6 5 9 4 2 1 8 9 5 0 7 2 6 1 1 0 6 7
  • 22. 13 0, 5 and 6 are connected 1, 2, and 7 are connected 3, 4, 8, and 9 are connected 0 5 1 6 2 7 3 8 4 9 0 1 0 1 1 8 2 3 8 0 4 5 0 1 6 7 8 8 8 9 id[] Quick-find [eager approach] Data structure. if and only if ・Integer array id[] of size N. ・Interpretation: p and q are connected iff they have the same id.
  • 23. Find. Check if p and q have the same id. after union of 6 and 1 problem: many values can change 14 Quick-find [eager approach] Data structure. ・Integer array id[] of size N. ・Interpretation: p and q are connected iff they have the same id. id[6] = 0; id[1] = 1 6 and 1 are not connected Union. To merge components containing p and q, change all entries whose id equals id[p] to id[q]. 0 1 0 1 1 8 2 3 8 0 4 5 0 1 6 7 8 8 8 9 1 1 0 1 1 8 2 3 8 1 4 5 1 1 6 7 8 8 8 9 id[] id[]
  • 24. 15 Quick-find demo 0 5 1 6 2 7 3 8 4 9 0 1 0 1 2 3 2 3 4 5 4 5 6 7 6 7 8 9 8 9 id[]
  • 25. Quick-find demo 0 5 1 6 2 7 3 8 4 9 1 1 0 1 1 8 2 3 8 1 4 5 1 1 6 7 8 8 8 9 id[]
  • 26. public boolean connected(int p, int q) { } public { void union(int p, int q) } } 17 Quick-find: Java implementation – Your first exercise  public class QuickFindUF { private int[] id; public QuickFindUF(int N) { set id of each object to itself (N array accesses) change all entries with id[p] to id[q] (at most 2N + 2 array accesses) } check whether p and q are in the same component (2 array accesses)
  • 27. Quick-find is too slow Cost model. Number of array accesses (for read or write). algorithm quick-find initialize N union N find 1 order of growth of number of array accesses Quick-find defect. Union too expensive? More on these aspects of performance next week….. quadratic Ex. Takes N 2 array accesses to process sequence of N union commands on N objects. 18
  • 28. ・Integer array id[] of size N. ・Interpretation: id[i] is parent of i. ・Root of i is id[id[id[...id[i]...]]]. Quick-union [lazy approach] Data structure. 0 1 0 1 9 4 2 3 9 6 4 5 6 7 6 7 8 9 8 9 id[] 3 root of 3 is 9 22 5 4 7 keep going until it doesn’t change (algorithm ensures no cycles) 0 1 9 6 8 2
  • 29. ・Integer array id[] of size N. ・Interpretation: id[i] is parent of i. ・Root of i is id[id[id[...id[i]...]]]. Find. Check if p and q have the same root. set the id of p's root to the id of q's root. 23 Quick-union [lazy approach] Data structure. 0 1 0 1 9 4 2 3 9 6 4 5 6 7 6 7 8 9 8 9 id[] 3 4 7 0 1 9 6 8 2 only one value changes p q 0 1 0 1 9 4 2 3 9 6 4 5 6 7 6 7 8 6 8 9 id[] 5 3 5 4 7 0 1 9 6 8 2 p q root of 3 is 9 root of 5 is 6 3 and 5 are not connected Union. To merge components containing p and q,
  • 30. 24 Quick-union demo 0 1 2 3 4 5 6 7 8 9 0 1 0 1 2 3 2 3 4 5 4 5 6 7 6 7 8 9 8 9 id[]
  • 31. 0 1 2 5 6 7 3 4 Quick-union demo 8 9 1 8 0 1 1 8 2 3 3 0 4 5 5 1 6 7 8 8 8 9 id[]
  • 32. Quick-union: Java implementation – Your second exercise  public class QuickUnionUF { private int[] id; public QuickUnionUF(int N) { } private int root(int i) { } public boolean connected(int p, int q) { } public void union(int p, int q) { set id of each object to itself (N array accesses) chase parent pointers until reach root (depth of i array accesses) check if p and q have same root (depth of p and q array accesses) change root of p to point to root of q (depth of p and q array accesses) } } 26
  • 33. worst case † includes cost of finding roots Quick-find defect. ・Union too expensive (N array accesses). ・Trees are flat, but too expensive to keep them flat. Quick-union defect. ・Trees can get tall. ・Find too expensive (could be N array accesses). 27 Quick-union is also too slow Cost model. Number of array accesses (for read or write). algorithm quick-find initialize N union N find 1 quick-union N N † N
  • 34. 48 ・Percolation. ・Games (Go, Hex). ✓ Dynamic connectivity. ・Least common ancestor. ・Equivalence of finite state automata. ・Hoshen-Kopelman algorithm in physics. ・Hinley-Milner polymorphic type inference. ・Kruskal's minimum spanning tree algorithm. ・Compiling equivalence statements in Fortran. ・Morphological attribute openings and closings. ・Matlab's bwlabel() function in image processing. Union-find applications
  • 35. Steps to developing a usable algorithm. ・Model the problem. ・Find an algorithm to solve it. ・Fast enough? Fits in memory? ・If not, figure out why. ・Find a way to address the problem. ・Iterate until satisfied. 63 In a nutshell