Informula

Informula

Share this post

Informula
Informula
How to Build a Simple Routing Tool via NN Algorithm and Python OOP? Part 1

How to Build a Simple Routing Tool via NN Algorithm and Python OOP? Part 1

Informula's avatar
Informula
Nov 08, 2023
∙ Paid

Share this post

Informula
Informula
How to Build a Simple Routing Tool via NN Algorithm and Python OOP? Part 1
Share

The Nearest Neighbour (NN) algorithm was one of the first algorithms used to solve the travelling salesman problem approximately. In that problem, the salesman starts at a random city and repeatedly visits the nearest city until all have been visited. The algorithm quickly yields a short tour, but usually not the optimal one.

In this set of articles, we are going to demonstrate how to build a routing tool via NN algorithm, which allows you to add the locations with x, y coordinates, define the starting point, plan route, and plot the routes with annotations. We will cover routing part in this one and finish the demo in the next part.

Step 0: Import packages.

import math
import matplotlib.pyplot as plt

Step 1: Create Class Locations.

  • Initialization with instance variables x coordinate, y coordinate and loc name.

class Location:
    def __init__(self, name, x, y):
        self.name = name
        self.x = x
        self.y = y

Keep reading with a 7-day free trial

Subscribe to Informula to keep reading this post and get 7 days of free access to the full post archives.

Already a paid subscriber? Sign in
© 2025 Informula
Privacy ∙ Terms ∙ Collection notice
Start writingGet the app
Substack is the home for great culture

Share