Coder Social home page Coder Social logo

dijkstra-astar-problem's Introduction

Problem Statement

You’re in a grid at a starting position (0, 0). You want to reach a target position (target_x, target_y). You can only move to the top, left, bottom or right cell from a certain cell position.

The grid’s lower left is (0, 0) and upper right is (max_x, max_y)

Each cell is either ‘E’ or ‘O’. E represents an empty cell, ‘O’ represents an obstacle. You can’t move into an obstacle. Find the shortest path to reach the target from the starting position. using Dijkstra’s algorithm and A* then find out how these two differ in terms of result and/or time in milliseconds? Were you able to reach there?

Use Manhattan distance as the heuristic. Manhattan distance = absolute difference between the x coordinates + absolute difference between the y coordinates.

Input:

Starting position: (0,0)
Target position: (4,2)

O E E E E
E E E O E
E E E O E
E E E O E
E E E O E

Note:

You may take the input in a 2D matrix like this grid =
[
['O', 'E', 'E', 'E', 'E'],
['E', 'E', 'E', 'O', 'E'],
['E', 'E', 'E', 'O', 'E'],
['E', 'E', 'E', 'O', 'E'],
['E', 'E', 'E', 'O', 'E']
]

Look if you access using indices, the first element i.e. grid[0][0] is ‘O’ the (0, 0) position isn’t matched with the index, (0, 0) position is actually grid[4][0]. You may use a helper method to convert the index pair to position & vice versa.

Hint:

Subtraction / Addition might help with the row index. Look, the row index corresponds to the y coordinate & the column index corresponds to the x coordinate. Do you need to convert the row or the column?

Sample Output:

A * path: (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2) -> (2,3) -> (2,4) -> (3, 4) -> (4,4) -> (4,3) -> (4,2)
A * time: X.XXXX sec

Dijkstra path: (0,0) -> (1,0) -> (2,0) -> (2,1) -> (2,2) -> (2,3) -> (2,4) -> (3, 4) -> (4,4) -> (4,3) -> (4,2)
Dijkstra time: X.XXXX sec

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.