Coder Social home page Coder Social logo

viktoo / shortest-path-2d Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 34.91 MB

Shortest path solutions ๐Ÿ›ฃ๏ธ for 2D arrays using Breadth-First Search (BFS) Algorithm and Dijkstra's Algorithm. Implemented in Python.

License: MIT License

Python 97.24% Shell 0.02% HTML 0.24% CSS 0.04% JavaScript 0.16% Jupyter Notebook 0.10% C++ 0.77% C 1.37% Fortran 0.05%

shortest-path-2d's Introduction

Shortest Path 2D

Shortest path solutions for 2D arrays using Breadth-First Search (BFS) Algorithm and Dijkstra's Algorithm. Implemented in Python.

Setup

On Mac/Linux:

source venv/bin/activate

On Windows:

.\\venv\\Scripts\\activate

Usage

python unw.py runs Breadth-First Search Algorithm (on stage 1 by default). Unweighted graphs only.

python w.py runs Dijkstra's Algorithm (on stage 2 by default). Considers weighted edges.

CSV files:

  • Stage 1 - Original first stage (unweighted)
  • Stage 2 - Original second stage (weighted)
  • Stage 3 - Weighted edges with clear varying paths for testing.
  • tiny.csv - A small 3x3 matrix on which my first iteration of Dijkstra's was built testing on.

BFS Algorithm

The Breadth-First Search Algorithm explores nodes in layers. Take a node and explore all of its neighbors. Then take all the nodes you've seen, and explore all their neighbors, then take all those nodes, and so on. Time complexity O(V+E).

It's particularly useful for finding shortest path on an unweighted graph

Dijkstra's Algorithm

This is one of the most important algorithms in graph theory for finding shortest path. It is similar to BFS as it's exploring the nodes, with added functionality of utilizing a priority queue with distances(weights). Because of this, the graph must only contain non-negative edge weights. Single Source Shortest Path (SSSP) with time complexity O(E*Log(V)).

Particularly useful for weighted graphs

This is the pen-and-paper version of Dijkstra's if ran against tiny.csv

Optimization

The time complexity of Dijkstra's Algorithm gives it an advantage over BFS while expanding the functionality to include graphs with weighted edges. Dijkstra's can be used to replace BFS in this case.

This implementation of Dijkstra's is "lazy" meaning it lazily deletes outdated (key, value) pairs. The "eager" version uses an Indexed Priority Queue (IPQ) that avoids duplicate (key, value) pairs. It allows access to pairs in the queue at constant time and updates in log time (if binary heap).

We can potentially further optimize by changing the heap we're using. Indexed Binary Heap โ†’ D-ary Heap.

This use-case of an apartment/house is particularly well-suited for an algorithm that takes direction into account such as A*. In this context we know the position of the end node, but even if we didn't, gearing a search algorithm around doorways would offer very efficient traversal of the building.

shortest-path-2d's People

Contributors

viktoo avatar

Stargazers

Ishaan Sehgal avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.