Examples of Heuristics in Computer Science

This image shows how heuristics impact everyday life and computer science.

The post Examples of Heuristics in Computer Science first appeared on Qvault.

Heuristics in computer science and artificial intelligence are “rules of thumb” that are used in algorithms to assist in finding approximate solutions for complex problems. Often, there is simply too much data to sift through in order to come to a solution in a timely manner, and so, a heuristic algorithm is used to trade exactness for speed. However, because heuristics are based on individual rules unique to the problem they are solving, the specifics of the heuristics vary from problem to problem. Example problems and their heuristics are given below.

Traveling Salesperson Problem (TSP)

The TSP is a famous algorithm with a Big-O of O(n!) and asks the question:

Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?

For a low number of cities, this question could be reasonably brute-forced. However, as the number of cities increases, it becomes increasingly difficult to come to a solution.

The nearest-neighbor (NN) heuristic solves this problem nicely: the computer always picks the nearest unvisited city next on the path. NN does not always provide the best solution, but it is close enough to the best solution that the difference is often negligible for the purpose of answering the TSP. By using this heuristic, the Big-O complexity of TSP can be reduced from O(n!) to O(n^2).

Knapsack Problem

The knapsack problem poses the issue:

Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

An example heuristic for this problem is a greedy algorithm, which sorts the items in descending order of value per weight, and then proceeds to insert them into the “sack”. This ensures the most valuably “dense” items make it into the sack first.

Search Optimization

Search engine optimization has been sought after for as long as search engines have been around. Individuals using search engines want to find the information they are looking for as swiftly as possible. With such an incredible amount of information available, search engines must utilize heuristics in order to expedite the search process. At the start, a heuristic could try each possibility at each step, but as the search continues, it can choose to stop the search at any time if the current possibility is worse than the best solution already located. In this way, the search engine can be optimized for speed and correctness.

Applying Heuristics to Your Algorithms

In order to apply heuristics to your algorithms, you need to know the solution or goal you’re looking for ahead of time. If you know your end goal, you can specify rules that can help you achieve it. If the algorithm is being designed to find out how many moves a knight can make on a square, 8×8 chessboard while visiting every square, it’s possible to create a heuristic that causes the knight to always choose the path with the most available moves afterward. However, because we’re trying to create a specific path, it may be better to create a heuristic that causes the knight to choose the path with the fewest available moves afterward. Since the available decisions are much narrower, so too are the available solutions, and so they are found more quickly.

Thanks For Reading!

Take computer science courses on our new platform

Follow and hit us up on Twitter @q_vault if you have any questions or comments

Subscribe to our Newsletter for more programming articles



source https://qvault.io/2020/11/30/examples-of-heuristics-in-computer-science/

Comments

Popular posts from this blog

Why is Exclusive Or (XOR) Important in Cryptography?

Base64 vs Base58 Encoding

(Very) Basic Intro to Hash Functions (SHA-256, MD-5, etc)