SlideShare a Scribd company logo
Exploring Algorithms Traveling Salesperson Problem I: Brute Force, Greedy, and Heuristics Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
Traveling Salesperson Problem http://creativecommons.org/licenses/by-nc/2.0/ Photo by: maureen sill  The Traveling Salesperson Problem (TSP):  he has the unfortunate job of traveling to 10 different towns in his area each month in order to deliver something important. Each town is a different distance away from his town and from each other town. How do you figure out a route that will minimize the distance traveled?
Brute Force One way to solve this problem (and any other NP-complete problem) is to enumerate all possible routes, of which there are 10! (3,628,800) for 10 towns, and then choose the shortest. This is a  brute force  algorithm . It would only take a couple seconds on a typical PC to compute all the possible routes and distances for 10 towns. And you would only need to do it once.  By:mellomango http://creativecommons.org/licenses/by-nd/2.0/deed.en
How to Solve It?
Still More Ways to Solve It: Heuristics The  2-opt  technique. We randomly pick two links between cities in our best random solution. We then remove those links and replace them with others that keep the route connected.
A Greedy Example A greedy algorithm is one where we make the best choice at each stage of an algorithm given the immediate information available. These choices do not take into account all the data available from all stages of the algorithm. Sometimes the immediate information is enough and an optimal solution is found; sometimes it's not enough, and non-optimal solutions are found.
Traveling Salesperson II: Divide and Conquer You may have seen this approach in binary search:  int binsearch(int x, int v[], int low, int high) /* recursive binary search: find x in v[low]..v[high]; return index of location */ {  int mid, loc;  mid = (low + high) / 2;  if (x == v[mid])   return(mid);  else if ((x < v[mid]) && (low < mid))   return(binsearch(x, v, low, mid-1));  else if ((x > v[mid]) && (high > mid))   return(binsearch(x, v, mid+1, high));  else   return(-1); }
TSP: Divide and Conquer And recursive sorting algorithms such as mergesort, or quicksort.  MergeSort(L) if (length of L > 1) {   Split list into first half and second half   MergeSort(first half)   MergeSort(second half)   Merge first half and second half into sorted list }
TSP: Divide and Conquer There is a divide and conquer heuristic for TSP. This is a common approach for fleet and transportation companies who have to solve TSP all the time! Basically, they take the route map and divide the stops into smaller groups. Then, they build routes between these groups. Note that this application of TSP removes the requirement of visiting each stop exactly once.  Streets often divide into regions naturally, particularly if a highway, river or some other natural barrier cuts through a region. It's also common to assign all the stops in a region to one driver. This gives the driver an opportunity to become familiar with the area, which increases speed and efficiency.
Traveling Salesperson III: Branch and Bound A search tree is a way of representing the execution of a backtracking algorithm. We start at the root and then add nodes to the tree to represent our exploration as we work towards a solution. If we find we reach a dead-end or the leaf of the tree, we backtrack to explore other paths. The classic example is a maze-search where the nodes generated from a parent represent an attempt to move up, down, right, or left.
Branch and Bound Searching Branch and bound searching  is a variation of backtracking for problems where we are looking for an optimal solution. The trick is to calculate for each new node in a search tree a bound on what solutions this node will produce.
A Bounding Function This algorithm produces the best possible solution to the traveling salesperson problem. Since TSP is known to be NP-complete, no algorithm to solve TSP could run in polynomial time. Although this algorithm is not polynomial, it is usually much faster than simply exploring every possible circuit. In the worst case, however, this algorithm could be exponential and mirror a brute-force approach.
Dynamic Programming Techniques The Fibonacci Sequence is often used to illustrate the power of dynamic programming. The sequence is defined by the following recurrence relation: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2 This very easily translates into a recursive function:  int Fibonacci(int n) {  if ((n == 0) || (n == 1))   return n;  else   return (Fibonacci(n-1) + Fibonacci(n-2)); }
What is the running time of Fibonacci()? Consider the call Fibonacci(4).  Here is how Fibonacci would be written using dynamic programming: int fib(int n) {  int f[n+1];  f[1] = f[2] = 1;  for (int i = 3; i <= n; i++)   f[i] = f[i-1] + f[i-2];  return f[n]; }
Problem #1 Let's start with a problem that allows us to use common algorithms. The problem is to find the k th  smallest element in a list of integers. Here are some possible algorithms that solve this problem:   Sorting: We could just sort the list and then extract the kth smallest element from the start of the list. The running time for the most efficient sort is O( n log n ) (QuickSort, MergeSort, HeapSort). We can iterate over the entire list keeping track of the smallest element found thus far. The running time for this is O( n ). Do an incomplete selection sort. That is, find the smallest value and move it to the beginning of the list. Find the next smallest value and move it to the second position. Keep doing this until you have moved the k th  element into position. The running time is O( k*n ). Use the  Hoare Selection Algorithm . This is based on QuickSort:
- function select(list, k, left, right) {   - choose a pivot value list[pivotIndex];   - pivotNewIndex := partition(list, left, right, pivotIndex)  - if k = pivotNewIndex   - return list[k]   - else if k < pivotNewIndex   - return select(list, k, left, pivotNewIndex-1) - else   - return select(list, k-pivotNewIndex, pivotNewIndex+1,  - right)   -  } The running time is just like QuickSort which is usually O( n log n ), but can run in O( n2) time if bad partition values are consistently chosen.  Use a data structure, like a binary search tree to minimize the search time for the k th  element. The running time here is O( log n ), but we have the overhead of inserting and deleting such that the tree remains balanced and in search tree form.
Problem #2 Here is an example of how binomial coefficients are used in combinatorics. Let's say there are n ice cream toppings to choose from. If one wishes to create an ice cream sundae with exactly k toppings, then the binomial coefficient expresses how many different types of such k-topping ice cream sundaes are possible.
We are interested in designing an algorithm to calculate a binomial coefficient. This time, we present the solutions in order of increasing efficiency.  Just apply the formula for  C( n,k ): n! / ((n - k)! * k!). If we only have to compute one binomial coefficient, brute force calculation works fine. Another idea, if we don't have to do the calculation often, is to use the recursive definition, assuming we have a factorial function: choose(m, n) = fact( m ) / (fact( n ) * fact(m-n))   As you can imagine, this is extremely slow, particularly if we use the recursive version of factorial!  If we have to do it frequently, we can compute Pascal's triangle once and do searches. This is a dynamic programming approach. Finally, the most elegant solution of all is one defined by Lilavati, over 850 years ago. This runs in O( n ) time.
Ad

Recommended

Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
Priyanka Rana
 
Machine learning (11)
Machine learning (11)
NYversity
 
Perform brute force
Perform brute force
SHC
 
CS8451 - Design and Analysis of Algorithms
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Recursion and Sorting Algorithms
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
L1803016468
L1803016468
IOSR Journals
 
Bruteforce algorithm
Bruteforce algorithm
Rezwan Siam
 
I1803014852
I1803014852
IOSR Journals
 
Travelling salesman problem
Travelling salesman problem
Dimitris Mavrommatis
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
Edward Blurock
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Travelling Salesman Problem
Travelling Salesman Problem
Shikha Gupta
 
Recursion
Recursion
Nalin Adhikari
 
03 notes
03 notes
Andres Mendez-Vazquez
 
Brute force method
Brute force method
priyankabhansali217
 
Line Search Techniques by Fibonacci Search
Line Search Techniques by Fibonacci Search
inventionjournals
 
Recursion | C++ | DSA
Recursion | C++ | DSA
Sumit Pandey
 
String matching algorithms
String matching algorithms
Dr Shashikant Athawale
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer
Andres Mendez-Vazquez
 
String kmp
String kmp
thinkphp
 
Data Representation of Strings
Data Representation of Strings
Prof Ansari
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
Amrinder Arora
 
algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Seminar Report (Final)
Seminar Report (Final)
Aruneel Das
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
06. string matching
06. string matching
Onkar Nath Sharma
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 
Travelling Salesman Problem
Travelling Salesman Problem
Daniel Raditya
 
ABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSP
gauravon
 

More Related Content

What's hot (20)

Travelling salesman problem
Travelling salesman problem
Dimitris Mavrommatis
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
Edward Blurock
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Travelling Salesman Problem
Travelling Salesman Problem
Shikha Gupta
 
Recursion
Recursion
Nalin Adhikari
 
03 notes
03 notes
Andres Mendez-Vazquez
 
Brute force method
Brute force method
priyankabhansali217
 
Line Search Techniques by Fibonacci Search
Line Search Techniques by Fibonacci Search
inventionjournals
 
Recursion | C++ | DSA
Recursion | C++ | DSA
Sumit Pandey
 
String matching algorithms
String matching algorithms
Dr Shashikant Athawale
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer
Andres Mendez-Vazquez
 
String kmp
String kmp
thinkphp
 
Data Representation of Strings
Data Representation of Strings
Prof Ansari
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
Amrinder Arora
 
algorithm Unit 2
algorithm Unit 2
Monika Choudhery
 
Seminar Report (Final)
Seminar Report (Final)
Aruneel Das
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
06. string matching
06. string matching
Onkar Nath Sharma
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 
Computability, turing machines and lambda calculus
Computability, turing machines and lambda calculus
Edward Blurock
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Travelling Salesman Problem
Travelling Salesman Problem
Shikha Gupta
 
Line Search Techniques by Fibonacci Search
Line Search Techniques by Fibonacci Search
inventionjournals
 
Recursion | C++ | DSA
Recursion | C++ | DSA
Sumit Pandey
 
01 Notes Introduction Analysis of Algorithms Notes
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
String kmp
String kmp
thinkphp
 
Data Representation of Strings
Data Representation of Strings
Prof Ansari
 
Asymptotic Notation and Data Structures
Asymptotic Notation and Data Structures
Amrinder Arora
 
Seminar Report (Final)
Seminar Report (Final)
Aruneel Das
 
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Divide and Conquer - Part II - Quickselect and Closest Pair of Points
Amrinder Arora
 
Graph Traversal Algorithms - Breadth First Search
Graph Traversal Algorithms - Breadth First Search
Amrinder Arora
 

Viewers also liked (20)

Travelling Salesman Problem
Travelling Salesman Problem
Daniel Raditya
 
ABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSP
gauravon
 
Matrix multiplicationdesign
Matrix multiplicationdesign
Respa Peter
 
Matrix mult class-17
Matrix mult class-17
Kumar
 
2-Approximation Vertex Cover
2-Approximation Vertex Cover
Kowshik Roy
 
Traveling salesman problem(tsp)
Traveling salesman problem(tsp)
Viraj Patil
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Nguyễn Công Hoàng
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
Nguyễn Công Hoàng
 
Giraph Travelling Salesman Example
Giraph Travelling Salesman Example
EliasDaboussi
 
Basic Problems and Solving Algorithms
Basic Problems and Solving Algorithms
Nopadon Juneam
 
Greedy Algorithms with examples' b-18298
Greedy Algorithms with examples' b-18298
LGS, GBHS&IC, University Of South-Asia, TARA-Technologies
 
The Brute Force and Ignorance Approach: Writing when you have no plan, no plo...
The Brute Force and Ignorance Approach: Writing when you have no plan, no plo...
Vincent O'Neil
 
Analysis of Algorithm
Analysis of Algorithm
أحلام انصارى
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem - by Y Achchuthan
Achchuthan Yogarajah
 
Traveling salesman problem__theory_and_applications
Traveling salesman problem__theory_and_applications
Sachin Kheveria
 
Quicksort
Quicksort
maamir farooq
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
Tsp branch and-bound
Tsp branch and-bound
Saravanan Natarajan
 
04 search heuristic
04 search heuristic
Nour Zeineddine
 
Vertex cover Problem
Vertex cover Problem
Gajanand Sharma
 
Travelling Salesman Problem
Travelling Salesman Problem
Daniel Raditya
 
ABC-GSX:Hybrid method to solve TSP
ABC-GSX:Hybrid method to solve TSP
gauravon
 
Matrix multiplicationdesign
Matrix multiplicationdesign
Respa Peter
 
Matrix mult class-17
Matrix mult class-17
Kumar
 
2-Approximation Vertex Cover
2-Approximation Vertex Cover
Kowshik Roy
 
Traveling salesman problem(tsp)
Traveling salesman problem(tsp)
Viraj Patil
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Giáo trình Phân tích và thiết kế giải thuật - CHAP 8
Nguyễn Công Hoàng
 
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
Giáo trình Phân tích và thiết kế giải thuật - CHAP 3
Nguyễn Công Hoàng
 
Giraph Travelling Salesman Example
Giraph Travelling Salesman Example
EliasDaboussi
 
Basic Problems and Solving Algorithms
Basic Problems and Solving Algorithms
Nopadon Juneam
 
The Brute Force and Ignorance Approach: Writing when you have no plan, no plo...
The Brute Force and Ignorance Approach: Writing when you have no plan, no plo...
Vincent O'Neil
 
Greedy Knapsack Problem - by Y Achchuthan
Greedy Knapsack Problem - by Y Achchuthan
Achchuthan Yogarajah
 
Traveling salesman problem__theory_and_applications
Traveling salesman problem__theory_and_applications
Sachin Kheveria
 
Dynamic Programming - Matrix Chain Multiplication
Dynamic Programming - Matrix Chain Multiplication
Pecha Inc.
 
Ad

Similar to Exploring Algorithms (20)

Particle Swarm Optimization to Solve Multiple Traveling Salesman Problem
Particle Swarm Optimization to Solve Multiple Traveling Salesman Problem
IRJET Journal
 
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
woolryvoloh
 
Parallel search
Parallel search
Md. Mahedi Mahfuj
 
ETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptx
RahulSingh190790
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
Algorithm in computer science
Algorithm in computer science
Riazul Islam
 
Unit 2 in daa
Unit 2 in daa
Nv Thejaswini
 
Dynamic programming
Dynamic programming
Jay Nagar
 
Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
Parallel searching
Parallel searching
Md. Mahedi Mahfuj
 
An Implementational approach to genetic algorithms for TSP
An Implementational approach to genetic algorithms for TSP
Sougata Das
 
A* Algorithm
A* Algorithm
Dr. C.V. Suresh Babu
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
dickonsondorris
 
Lecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Ada notes
Ada notes
VIKAS SINGH BHADOURIA
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf
whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf
hassankhan978073
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
Numerical differentation with c
Numerical differentation with c
Yagya Dev Bhardwaj
 
Particle Swarm Optimization to Solve Multiple Traveling Salesman Problem
Particle Swarm Optimization to Solve Multiple Traveling Salesman Problem
IRJET Journal
 
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
woolryvoloh
 
ETCS262A-Analysis of design Algorithm.pptx
ETCS262A-Analysis of design Algorithm.pptx
RahulSingh190790
 
Are there trends, changes in the mi.pptx
Are there trends, changes in the mi.pptx
priyaaajadhav31
 
Algorithm in computer science
Algorithm in computer science
Riazul Islam
 
Dynamic programming
Dynamic programming
Jay Nagar
 
Sure interview algorithm-1103
Sure interview algorithm-1103
Sure Interview
 
An Implementational approach to genetic algorithms for TSP
An Implementational approach to genetic algorithms for TSP
Sougata Das
 
Unit 2 Modeling of Programs A function maps inputs to out.docx
Unit 2 Modeling of Programs A function maps inputs to out.docx
dickonsondorris
 
Lecture 8 dynamic programming
Lecture 8 dynamic programming
Oye Tu
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Design and analysis of algorithm in Computer Science
Design and analysis of algorithm in Computer Science
secularistpartyofind
 
whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf
whhhhhhhhhhhhhhhhhhhhhhhhhhhhheek 8 Cc.pdf
hassankhan978073
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
Techglyphs
 
Numerical differentation with c
Numerical differentation with c
Yagya Dev Bhardwaj
 
Ad

More from Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radar
Sri Prasanna
 
Qr codes para tech radar 2
Qr codes para tech radar 2
Sri Prasanna
 
assds
assds
Sri Prasanna
 
assds
assds
Sri Prasanna
 
asdsa
asdsa
Sri Prasanna
 
dsd
dsd
Sri Prasanna
 
About stacks
About stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
About Stacks
About Stacks
Sri Prasanna
 
Network and distributed systems
Network and distributed systems
Sri Prasanna
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
Sri Prasanna
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementation
Sri Prasanna
 
Other distributed systems
Other distributed systems
Sri Prasanna
 

Recently uploaded (20)

CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Hurricane Helene Application Documents Checklists
Hurricane Helene Application Documents Checklists
Mebane Rash
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Values Education 10 Quarter 1 Module .pptx
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
ENGLISH_Q1_W1 PowerPoint grade 3 quarter 1 week 1
jutaydeonne
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
How payment terms are configured in Odoo 18
How payment terms are configured in Odoo 18
Celine George
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
Code Profiling in Odoo 18 - Odoo 18 Slides
Code Profiling in Odoo 18 - Odoo 18 Slides
Celine George
 
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
Q1_ENGLISH_PPT_WEEK 1 power point grade 3 Quarter 1 week 1
jutaydeonne
 

Exploring Algorithms

  • 1. Exploring Algorithms Traveling Salesperson Problem I: Brute Force, Greedy, and Heuristics Except as otherwise noted, the content of this presentation is licensed under the Creative Commons Attribution 2.5 License.
  • 2. Traveling Salesperson Problem http://creativecommons.org/licenses/by-nc/2.0/ Photo by: maureen sill The Traveling Salesperson Problem (TSP): he has the unfortunate job of traveling to 10 different towns in his area each month in order to deliver something important. Each town is a different distance away from his town and from each other town. How do you figure out a route that will minimize the distance traveled?
  • 3. Brute Force One way to solve this problem (and any other NP-complete problem) is to enumerate all possible routes, of which there are 10! (3,628,800) for 10 towns, and then choose the shortest. This is a brute force algorithm . It would only take a couple seconds on a typical PC to compute all the possible routes and distances for 10 towns. And you would only need to do it once. By:mellomango http://creativecommons.org/licenses/by-nd/2.0/deed.en
  • 5. Still More Ways to Solve It: Heuristics The 2-opt technique. We randomly pick two links between cities in our best random solution. We then remove those links and replace them with others that keep the route connected.
  • 6. A Greedy Example A greedy algorithm is one where we make the best choice at each stage of an algorithm given the immediate information available. These choices do not take into account all the data available from all stages of the algorithm. Sometimes the immediate information is enough and an optimal solution is found; sometimes it's not enough, and non-optimal solutions are found.
  • 7. Traveling Salesperson II: Divide and Conquer You may have seen this approach in binary search: int binsearch(int x, int v[], int low, int high) /* recursive binary search: find x in v[low]..v[high]; return index of location */ { int mid, loc; mid = (low + high) / 2; if (x == v[mid]) return(mid); else if ((x < v[mid]) && (low < mid)) return(binsearch(x, v, low, mid-1)); else if ((x > v[mid]) && (high > mid)) return(binsearch(x, v, mid+1, high)); else return(-1); }
  • 8. TSP: Divide and Conquer And recursive sorting algorithms such as mergesort, or quicksort. MergeSort(L) if (length of L > 1) { Split list into first half and second half MergeSort(first half) MergeSort(second half) Merge first half and second half into sorted list }
  • 9. TSP: Divide and Conquer There is a divide and conquer heuristic for TSP. This is a common approach for fleet and transportation companies who have to solve TSP all the time! Basically, they take the route map and divide the stops into smaller groups. Then, they build routes between these groups. Note that this application of TSP removes the requirement of visiting each stop exactly once. Streets often divide into regions naturally, particularly if a highway, river or some other natural barrier cuts through a region. It's also common to assign all the stops in a region to one driver. This gives the driver an opportunity to become familiar with the area, which increases speed and efficiency.
  • 10. Traveling Salesperson III: Branch and Bound A search tree is a way of representing the execution of a backtracking algorithm. We start at the root and then add nodes to the tree to represent our exploration as we work towards a solution. If we find we reach a dead-end or the leaf of the tree, we backtrack to explore other paths. The classic example is a maze-search where the nodes generated from a parent represent an attempt to move up, down, right, or left.
  • 11. Branch and Bound Searching Branch and bound searching is a variation of backtracking for problems where we are looking for an optimal solution. The trick is to calculate for each new node in a search tree a bound on what solutions this node will produce.
  • 12. A Bounding Function This algorithm produces the best possible solution to the traveling salesperson problem. Since TSP is known to be NP-complete, no algorithm to solve TSP could run in polynomial time. Although this algorithm is not polynomial, it is usually much faster than simply exploring every possible circuit. In the worst case, however, this algorithm could be exponential and mirror a brute-force approach.
  • 13. Dynamic Programming Techniques The Fibonacci Sequence is often used to illustrate the power of dynamic programming. The sequence is defined by the following recurrence relation: F0 = 0 F1 = 1 Fn = Fn-1 + Fn-2 This very easily translates into a recursive function: int Fibonacci(int n) { if ((n == 0) || (n == 1)) return n; else return (Fibonacci(n-1) + Fibonacci(n-2)); }
  • 14. What is the running time of Fibonacci()? Consider the call Fibonacci(4). Here is how Fibonacci would be written using dynamic programming: int fib(int n) { int f[n+1]; f[1] = f[2] = 1; for (int i = 3; i <= n; i++) f[i] = f[i-1] + f[i-2]; return f[n]; }
  • 15. Problem #1 Let's start with a problem that allows us to use common algorithms. The problem is to find the k th smallest element in a list of integers. Here are some possible algorithms that solve this problem: Sorting: We could just sort the list and then extract the kth smallest element from the start of the list. The running time for the most efficient sort is O( n log n ) (QuickSort, MergeSort, HeapSort). We can iterate over the entire list keeping track of the smallest element found thus far. The running time for this is O( n ). Do an incomplete selection sort. That is, find the smallest value and move it to the beginning of the list. Find the next smallest value and move it to the second position. Keep doing this until you have moved the k th element into position. The running time is O( k*n ). Use the Hoare Selection Algorithm . This is based on QuickSort:
  • 16. - function select(list, k, left, right) { - choose a pivot value list[pivotIndex]; - pivotNewIndex := partition(list, left, right, pivotIndex) - if k = pivotNewIndex - return list[k] - else if k < pivotNewIndex - return select(list, k, left, pivotNewIndex-1) - else - return select(list, k-pivotNewIndex, pivotNewIndex+1, - right) - } The running time is just like QuickSort which is usually O( n log n ), but can run in O( n2) time if bad partition values are consistently chosen. Use a data structure, like a binary search tree to minimize the search time for the k th element. The running time here is O( log n ), but we have the overhead of inserting and deleting such that the tree remains balanced and in search tree form.
  • 17. Problem #2 Here is an example of how binomial coefficients are used in combinatorics. Let's say there are n ice cream toppings to choose from. If one wishes to create an ice cream sundae with exactly k toppings, then the binomial coefficient expresses how many different types of such k-topping ice cream sundaes are possible.
  • 18. We are interested in designing an algorithm to calculate a binomial coefficient. This time, we present the solutions in order of increasing efficiency. Just apply the formula for C( n,k ): n! / ((n - k)! * k!). If we only have to compute one binomial coefficient, brute force calculation works fine. Another idea, if we don't have to do the calculation often, is to use the recursive definition, assuming we have a factorial function: choose(m, n) = fact( m ) / (fact( n ) * fact(m-n)) As you can imagine, this is extremely slow, particularly if we use the recursive version of factorial! If we have to do it frequently, we can compute Pascal's triangle once and do searches. This is a dynamic programming approach. Finally, the most elegant solution of all is one defined by Lilavati, over 850 years ago. This runs in O( n ) time.