SlideShare a Scribd company logo
Algorithms - A Sneak Peek
Algorithms
A Sneak Peek
Definition
A broad definition: A set of steps to accomplish a task
● You use algorithms daily
○ Brush your teeth
○ Drive to work
○ ...
Replace the steps with code, and you get our algorithms
Ants (UVa)
Description
● n ants walk on L cm long pole. n and l are both known
● Speed of each ant is 1 cm/s
● When an ant reaches the end of the pole, it falls
● If two ants meet, they change direction. Otherwise,
they never change direction
● We know the original positions
● Assume that each ant can start in either direction
● Get the earliest time and latest time possible for all
ants to fall down
L
Ants (UVa)
Conflict
All ants are the same.
Color only means different
direction.
Change direction
Falls down
Ants (UVa)
● Earliest time for all to fall down?
● Just head to the nearest end
max(min(p0, L-p0), min(p1, L-p1), … min(pn-1, L-pn-1))
Answer
Ants (UVa)
● Latest time for all to fall down?
● Head to the farthest end
max(max(p0, L-p0), max(p1, L-p1), … max(pn-1, L-pn-1))
Answer
Ants (UVa)
● But what if two ants meet?
○ Each one should invert direction
● Should that matter?
Both invert direction Same length, same speed …
same time
How to Describe Algorithms
● Correctness
○ Some algorithms are expected to give one and exact correct
answer
■ Shortest driving route by GPS
○ Irrelevant from bad input
■ GPS not being fed by real-time data
○ We may tolerate a chance of error as long as we control it
■ Prime numbers test in RSA with Fermat Little Theorem
○ Nondeterministic
■ Even with the same input, the output can be different
How to Describe Algorithms
● Resource usage
○ Mainly CPU cycles, a.k.a time
■ Also, memory, network bandwidth, random bits, disk operations
○ How can we measure time performance for an algorithm?
■ e.g. If it takes t to solve n, then to solve an, it takes at?
Asymptotic notation
1. Measure time in terms of input
● Specifically, how many instructions per input size
2. Measure the rate of growth
● How fast it grows as input grows
● More: We’re interested in the dominant form (Order of
Growth)
t(n) = 25n + 500 25n dominates 500
Order of Growth
6n2
+ 100n + 300
Then, the algorithm grows as n2
Need more proof?
Order of Growth
0.6n2
+ 1000n + 3000
Order of Growth:
● Drop the less significant terms
● Drop the coefficients
Order of Growth - Different Notations
no
t(n) = Θ(n); then
k1
n < t(n) < k2
n; for all n > no
for some constants k1
, k2
and no
Big Theta
Order of Growth - Different Notations[4]
t(n) = O(f(n)); then
kf(n) < t(n); for all n > no
for some constants k and no
Big O
no
no
t(n) = Ω(f(n)); then
t(n) > kf(n); for all n > no
for some constants k and no
Big Omega
Order of Growth - Bubble Sort
func bubblesort( var a as array )
for i from 1 to N
for j from 0 to N - 1
if a[j] > a[j + 1]
swap( a[j], a[j + 1] )
end func
t(n) = 2 * (1 + 2 + … + (N - 1)) = 2 * (N * (N - 1) / 2)
t(n) = O(N2
)
Different Domains
● Sorting
● Binary search (Or searching generally)
● Graphs
● Strings
● Cryptography
● Data Compression
● Dynamic Programming
● Math
● Simulation
● Geography
Algorithms and You - Why You Should Care?
● It helps you become a better coder
● It’s not the complicated algorithms, it’s the skills
● Rarely would you encounter a problem that requires a
sophisticated algorithm
● Even in software giants
Algorithms and You - What Skills? [5]
● Fast coding, but careful coding
● Know the default language framework better
● Converting that mental model into something
● That’s what it’s all about
● Get in the code mode
● DEBUGGING
● ...
Algorithms and You - Why You Should Care?
● It would ease your way into the big companies and
more
● Interviews contain much more than algorithms, BTW…
● Some of the big companies host their own
competitions or watch closely on another
● Code Jam, Hacker Cup, ACM ICPC, Top Coder …
Managers! Please look away for a second!
Algorithms and You - Why You Should Care?
It’s fun and a great brain exercise.
Lastly,
Graphs - A Sneak Peek
● A graph is a set of vertices V and edges E
● An edge connects two vertices
● The edges can be directed or not
1
2
3
4
1
2
3
4
Graphs - A Sneak Peek
Everywhere in life
● Computers as vertices and wires as edges
● Road intersections and roads
● People as vertices and relationships as edges
● …
Graphs - A Sneak Peek
Representations
● Adjacency matrix
○ 2D boolean array, where matrix[i][j] = true, if there’s an
edge from node i to node j
● Adjacency list
○ Each node maintains a list, of all adjacent nodes
● Edge list
○ Store edges as pairs of nodes
Graphs - A Sneak Peek
Algorithms
● Searching
○ Breadth-first search, depth-first search
● Traversal
● Flood fill
● Shortest path
○ Dijkstra, Bellman-Ford, Floyd-Warshall
● Max-flow
● Bipartite graphs
● ...
Graphs - Sneak Peek
DFS using adjacency list
procedure DFS(G,v):
label v as discovered
for all edges w in G.adjacentEdges(v) do
if vertex w is not labeled as discovered then
recursively call DFS(G,w)
Complexity: O(n + m)
A Bug’s Life (SPOJ)
● Researching the sexual behaviour of a rare bug species
○ Note: Believe me. It’s a good problem! :D
● Assumingly, they have two genders
● We can identify all bugs by numbers, and we know all
interactions that happened
● Verify that the assumption is true.
○ i.e. There are no homsexual bugs
A Bug’s Life (SPOJ)
1
2
3
1
2
3
1
2
3
“Suspicious” bugs found
1
2
3
1
2
3
1
2
3
All good
A Bug’s Life (SPOJ)
Solution:
Bipartite graph check / Graph two-colors problem
A Bug’s Life (SPOJ)
hasSuspeciuosBugs(V):
for v in V:
if v is unmarked:
mark v with color1
if (visit(v))
return true
visit(v):
otherColor = //
for av in v.adjacencyList:
if av is marked:
if av.mark != otherColor:
return true
else:
av.mark = otherColor
if (visit(av))
return true
return false
References
1. Algorithms Unlocked by Thomas Cormen
2. Ants (UVa)
3. Nondeterministic Algorithms (WIkipedia)
4. Asymptotic Notation (Khan Academy)
5. Regarding the smug opposition to programming competitions
6. DFS (Wikipedia)
7. A Bug’s Life (SPOJ)
8. Bureaucracy (SPOJ)
9. Paradox (SPOJ)

More Related Content

Viewers also liked (10)

PDF
2016 07 燦爛時光 東南亞主題書店
Evan Hsiang
 
PPT
Virus INFORMATICOS
yanipa
 
PDF
KlientBoost and Kissmetrics Present: PPC Analytics [infographic]
KlientBoost
 
PDF
البابا شنودة والقدس الحقيقي والمعلن
Mamdouh Al-Shaikh
 
PPTX
Children’s rights
ADISH SUBHASH
 
PDF
Master en Marketing Digital fundesem 2012
Abserver
 
PDF
productlist
Ruoyu Wang
 
DOC
Nitin Mandloi
Nitin Mandloi
 
PDF
How To: Designing a Room with Metallics
Virgie Vincent
 
PPTX
Las tic nadia liliana mina bedoya.pptx28
nadialilianaminabedoya
 
2016 07 燦爛時光 東南亞主題書店
Evan Hsiang
 
Virus INFORMATICOS
yanipa
 
KlientBoost and Kissmetrics Present: PPC Analytics [infographic]
KlientBoost
 
البابا شنودة والقدس الحقيقي والمعلن
Mamdouh Al-Shaikh
 
Children’s rights
ADISH SUBHASH
 
Master en Marketing Digital fundesem 2012
Abserver
 
productlist
Ruoyu Wang
 
Nitin Mandloi
Nitin Mandloi
 
How To: Designing a Room with Metallics
Virgie Vincent
 
Las tic nadia liliana mina bedoya.pptx28
nadialilianaminabedoya
 

Similar to Algorithms - A Sneak Peek (20)

PDF
Chord DHT
John-Alan Simmons
 
PDF
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Dharmalingam Ganesan
 
PDF
Dependency Analysis of RSA Private Variables
Dharmalingam Ganesan
 
PDF
RSA cracking puzzle
Dharmalingam Ganesan
 
PPTX
Deep Learning Tutorial
Ligeng Zhu
 
PDF
An Analysis of RSA Public Exponent e
Dharmalingam Ganesan
 
PPTX
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
PDF
Cyclic Attacks on the RSA Trapdoor Function
Dharmalingam Ganesan
 
PDF
Security of RSA and Integer Factorization
Dharmalingam Ganesan
 
PDF
Convolutional and Recurrent Neural Networks
Ramesh Ragala
 
PDF
Solutions to online rsa factoring challenges
Dharmalingam Ganesan
 
PPTX
Algo complexity
ZÅhid IslÅm
 
PDF
Asymptotic Notation
sohelranasweet
 
PDF
PKC&RSA
Anver S R
 
PDF
Distributed computing with spark
Javier Santos Paniego
 
PDF
Chromatic Sparse Learning
Databricks
 
PPTX
Converting High Dimensional Problems to Low Dimensional Ones
Strand Life Sciences Pvt Ltd
 
PDF
Transactional Data Mining Ted Dunning 2004
MapR Technologies
 
PDF
Pathfinding in games
Adrian Popovici
 
PDF
Asymptotic notation
mustafa sarac
 
Computing the Square Roots of Unity to break RSA using Quantum Algorithms
Dharmalingam Ganesan
 
Dependency Analysis of RSA Private Variables
Dharmalingam Ganesan
 
RSA cracking puzzle
Dharmalingam Ganesan
 
Deep Learning Tutorial
Ligeng Zhu
 
An Analysis of RSA Public Exponent e
Dharmalingam Ganesan
 
1_Asymptotic_Notation_pptx.pptx
pallavidhade2
 
Cyclic Attacks on the RSA Trapdoor Function
Dharmalingam Ganesan
 
Security of RSA and Integer Factorization
Dharmalingam Ganesan
 
Convolutional and Recurrent Neural Networks
Ramesh Ragala
 
Solutions to online rsa factoring challenges
Dharmalingam Ganesan
 
Algo complexity
ZÅhid IslÅm
 
Asymptotic Notation
sohelranasweet
 
PKC&RSA
Anver S R
 
Distributed computing with spark
Javier Santos Paniego
 
Chromatic Sparse Learning
Databricks
 
Converting High Dimensional Problems to Low Dimensional Ones
Strand Life Sciences Pvt Ltd
 
Transactional Data Mining Ted Dunning 2004
MapR Technologies
 
Pathfinding in games
Adrian Popovici
 
Asymptotic notation
mustafa sarac
 
Ad

More from BADR (15)

PDF
Sunspot - The Ruby Way into Solr
BADR
 
PDF
Docker up and Running For Web Developers
BADR
 
PDF
Vue.js
BADR
 
PDF
There and Back Again - A Tale of Programming Languages
BADR
 
PDF
Take Pride in Your Code - Test-Driven Development
BADR
 
PDF
Single Responsibility Principle
BADR
 
PDF
NoSQL Databases
BADR
 
PDF
Explicit Semantic Analysis
BADR
 
PDF
Getting some Git
BADR
 
PDF
ReactiveX
BADR
 
PDF
Android from A to Z
BADR
 
PDF
Apache Hadoop - Big Data Engineering
BADR
 
PDF
MySQL Indexing
BADR
 
PDF
Duckville - The Strategy Design Pattern
BADR
 
PDF
The Perks and Perils of the Singleton Design Pattern
BADR
 
Sunspot - The Ruby Way into Solr
BADR
 
Docker up and Running For Web Developers
BADR
 
Vue.js
BADR
 
There and Back Again - A Tale of Programming Languages
BADR
 
Take Pride in Your Code - Test-Driven Development
BADR
 
Single Responsibility Principle
BADR
 
NoSQL Databases
BADR
 
Explicit Semantic Analysis
BADR
 
Getting some Git
BADR
 
ReactiveX
BADR
 
Android from A to Z
BADR
 
Apache Hadoop - Big Data Engineering
BADR
 
MySQL Indexing
BADR
 
Duckville - The Strategy Design Pattern
BADR
 
The Perks and Perils of the Singleton Design Pattern
BADR
 
Ad

Recently uploaded (20)

PDF
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
PDF
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
PDF
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
PDF
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
PDF
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
PPTX
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
PPTX
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PPTX
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
PDF
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
PDF
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
PPTX
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
PDF
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
PPTX
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
PDF
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
IDM Crack with Internet Download Manager 6.42 Build 43 with Patch Latest 2025
bashirkhan333g
 
Empower Your Tech Vision- Why Businesses Prefer to Hire Remote Developers fro...
logixshapers59
 
Driver Easy Pro 6.1.1 Crack Licensce key 2025 FREE
utfefguu
 
SciPy 2025 - Packaging a Scientific Python Project
Henry Schreiner
 
Open Chain Q2 Steering Committee Meeting - 2025-06-25
Shane Coughlan
 
Help for Correlations in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Agentic Automation: Build & Deploy Your First UiPath Agent
klpathrudu
 
Finding Your License Details in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Change Common Properties in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
Foundations of Marketo Engage - Powering Campaigns with Marketo Personalization
bbedford2
 
AI + DevOps = Smart Automation with devseccops.ai.pdf
Devseccops.ai
 
The 5 Reasons for IT Maintenance - Arna Softech
Arna Softech
 
ChiSquare Procedure in IBM SPSS Statistics Version 31.pptx
Version 1 Analytics
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Agentic Automation Journey Series Day 2 – Prompt Engineering for UiPath Agents
klpathrudu
 
AOMEI Partition Assistant Crack 10.8.2 + WinPE Free Downlaod New Version 2025
bashirkhan333g
 
In From the Cold: Open Source as Part of Mainstream Software Asset Management
Shane Coughlan
 
MiniTool Power Data Recovery 8.8 With Crack New Latest 2025
bashirkhan333g
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 

Algorithms - A Sneak Peek

  • 3. Definition A broad definition: A set of steps to accomplish a task ● You use algorithms daily ○ Brush your teeth ○ Drive to work ○ ... Replace the steps with code, and you get our algorithms
  • 4. Ants (UVa) Description ● n ants walk on L cm long pole. n and l are both known ● Speed of each ant is 1 cm/s ● When an ant reaches the end of the pole, it falls ● If two ants meet, they change direction. Otherwise, they never change direction ● We know the original positions ● Assume that each ant can start in either direction ● Get the earliest time and latest time possible for all ants to fall down L
  • 5. Ants (UVa) Conflict All ants are the same. Color only means different direction. Change direction Falls down
  • 6. Ants (UVa) ● Earliest time for all to fall down? ● Just head to the nearest end max(min(p0, L-p0), min(p1, L-p1), … min(pn-1, L-pn-1)) Answer
  • 7. Ants (UVa) ● Latest time for all to fall down? ● Head to the farthest end max(max(p0, L-p0), max(p1, L-p1), … max(pn-1, L-pn-1)) Answer
  • 8. Ants (UVa) ● But what if two ants meet? ○ Each one should invert direction ● Should that matter? Both invert direction Same length, same speed … same time
  • 9. How to Describe Algorithms ● Correctness ○ Some algorithms are expected to give one and exact correct answer ■ Shortest driving route by GPS ○ Irrelevant from bad input ■ GPS not being fed by real-time data ○ We may tolerate a chance of error as long as we control it ■ Prime numbers test in RSA with Fermat Little Theorem ○ Nondeterministic ■ Even with the same input, the output can be different
  • 10. How to Describe Algorithms ● Resource usage ○ Mainly CPU cycles, a.k.a time ■ Also, memory, network bandwidth, random bits, disk operations ○ How can we measure time performance for an algorithm? ■ e.g. If it takes t to solve n, then to solve an, it takes at?
  • 11. Asymptotic notation 1. Measure time in terms of input ● Specifically, how many instructions per input size 2. Measure the rate of growth ● How fast it grows as input grows ● More: We’re interested in the dominant form (Order of Growth) t(n) = 25n + 500 25n dominates 500
  • 12. Order of Growth 6n2 + 100n + 300 Then, the algorithm grows as n2 Need more proof?
  • 13. Order of Growth 0.6n2 + 1000n + 3000 Order of Growth: ● Drop the less significant terms ● Drop the coefficients
  • 14. Order of Growth - Different Notations no t(n) = Θ(n); then k1 n < t(n) < k2 n; for all n > no for some constants k1 , k2 and no Big Theta
  • 15. Order of Growth - Different Notations[4] t(n) = O(f(n)); then kf(n) < t(n); for all n > no for some constants k and no Big O no no t(n) = Ω(f(n)); then t(n) > kf(n); for all n > no for some constants k and no Big Omega
  • 16. Order of Growth - Bubble Sort func bubblesort( var a as array ) for i from 1 to N for j from 0 to N - 1 if a[j] > a[j + 1] swap( a[j], a[j + 1] ) end func t(n) = 2 * (1 + 2 + … + (N - 1)) = 2 * (N * (N - 1) / 2) t(n) = O(N2 )
  • 17. Different Domains ● Sorting ● Binary search (Or searching generally) ● Graphs ● Strings ● Cryptography ● Data Compression ● Dynamic Programming ● Math ● Simulation ● Geography
  • 18. Algorithms and You - Why You Should Care? ● It helps you become a better coder ● It’s not the complicated algorithms, it’s the skills ● Rarely would you encounter a problem that requires a sophisticated algorithm ● Even in software giants
  • 19. Algorithms and You - What Skills? [5] ● Fast coding, but careful coding ● Know the default language framework better ● Converting that mental model into something ● That’s what it’s all about ● Get in the code mode ● DEBUGGING ● ...
  • 20. Algorithms and You - Why You Should Care? ● It would ease your way into the big companies and more ● Interviews contain much more than algorithms, BTW… ● Some of the big companies host their own competitions or watch closely on another ● Code Jam, Hacker Cup, ACM ICPC, Top Coder … Managers! Please look away for a second!
  • 21. Algorithms and You - Why You Should Care? It’s fun and a great brain exercise. Lastly,
  • 22. Graphs - A Sneak Peek ● A graph is a set of vertices V and edges E ● An edge connects two vertices ● The edges can be directed or not 1 2 3 4 1 2 3 4
  • 23. Graphs - A Sneak Peek Everywhere in life ● Computers as vertices and wires as edges ● Road intersections and roads ● People as vertices and relationships as edges ● …
  • 24. Graphs - A Sneak Peek Representations ● Adjacency matrix ○ 2D boolean array, where matrix[i][j] = true, if there’s an edge from node i to node j ● Adjacency list ○ Each node maintains a list, of all adjacent nodes ● Edge list ○ Store edges as pairs of nodes
  • 25. Graphs - A Sneak Peek Algorithms ● Searching ○ Breadth-first search, depth-first search ● Traversal ● Flood fill ● Shortest path ○ Dijkstra, Bellman-Ford, Floyd-Warshall ● Max-flow ● Bipartite graphs ● ...
  • 26. Graphs - Sneak Peek DFS using adjacency list procedure DFS(G,v): label v as discovered for all edges w in G.adjacentEdges(v) do if vertex w is not labeled as discovered then recursively call DFS(G,w) Complexity: O(n + m)
  • 27. A Bug’s Life (SPOJ) ● Researching the sexual behaviour of a rare bug species ○ Note: Believe me. It’s a good problem! :D ● Assumingly, they have two genders ● We can identify all bugs by numbers, and we know all interactions that happened ● Verify that the assumption is true. ○ i.e. There are no homsexual bugs
  • 28. A Bug’s Life (SPOJ) 1 2 3 1 2 3 1 2 3 “Suspicious” bugs found 1 2 3 1 2 3 1 2 3 All good
  • 29. A Bug’s Life (SPOJ) Solution: Bipartite graph check / Graph two-colors problem
  • 30. A Bug’s Life (SPOJ) hasSuspeciuosBugs(V): for v in V: if v is unmarked: mark v with color1 if (visit(v)) return true visit(v): otherColor = // for av in v.adjacencyList: if av is marked: if av.mark != otherColor: return true else: av.mark = otherColor if (visit(av)) return true return false
  • 31. References 1. Algorithms Unlocked by Thomas Cormen 2. Ants (UVa) 3. Nondeterministic Algorithms (WIkipedia) 4. Asymptotic Notation (Khan Academy) 5. Regarding the smug opposition to programming competitions 6. DFS (Wikipedia) 7. A Bug’s Life (SPOJ) 8. Bureaucracy (SPOJ) 9. Paradox (SPOJ)