Giter VIP home page Giter VIP logo

algorithmtermproject's Introduction

Algorithm Term Project - Group 8

Maze creation and path finding algorithm source code written by Java.


Maze creation algorithm

The maze creation algorithm uses the binary tree algorithm to create a maze.

Binary tree algorithm pseudo code

  1. Designate all edges as unreachable walls 2. All even-numbered tiles are made into walls.
    1. If you do this, all non-wall areas (green tiles) will be surrounded by walls on all sides.
    2. Edge (1) is also blocked by a wall (because it’s an even number).
    3. In this state, the wall in one of the two directions, either to the right or downward based on the tile rather than the wall, will be made green and drilled through.

Summary: From the perspective of one accessible tile, one of the two walls to the right and bottom is randomly selected and changed into the accessible tile. In other words, it randomly selects one of two directions and clears the path.

Referenced document: Maze creation algorithm (Binary Tree, Side Winder) with C#


Path finding algorithm

Breadth-First Search (BFS)

BFS (G, s) //Where G is the graph and s is the source node.
    let Q be queue.
    Q.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.

    mark s as visited.
    while ( Q is not empty)
        //Removing that vertex from queue,whose neighbour will be visited now
        v  =  Q.dequeue( )

        //processing all the neighbours of v  
        for all neighbours w of v in Graph G
            if (w is not visited) and (w is a passable node) and (w is in corret range)
                Q.enqueue( w ) //Stores w in Q to further visit its neighbour
                mark w as visited.

Referenced document: Breadth First Search Tutorial

Depth-First Search (DFS)

DFS (S, s) //Where G is the graph and s is the source node.
    let S be stack.
    S.push( s ) //Inserting s in stack until all its neighbour vertices are marked.

    mark s as visited.
    while ( S is not empty)
        //Removing that vertex from stack, whose neighbour will be visited now
        v  =  S.pop( )

        //processing all the neighbours of v  
        for all neighbours w of v in Graph G
            if (w is not visited) and (w is a passable node) and (w is in corret range)
                S.push( w ) //Stores w in Q to further visit its neighbour
                mark w as visited.

Referenced document: Breadth First Search Tutorial

A* algorithm with Euclidean distance as heuristic function

Let us call the heuristic weight function $f(x)$ and assume that it consists of $h\left( x \right) = g\left( x \right) + h\left( x \right)$.
At this time, $g\left( x \right)$ is the Euclidean distance from the start node to the x node, and h\left( x \right) is the Euclidean distance from the x node to the destination node.

Euclidean distance from $\left( x_1, y_1 \right)$ to $\left( x_2, y_2 \right)$ = $\sqrt{\left( x_1-x_2 \right)^2 + \left( y_1-y_2 \right)^2}$ Thus, $f\left( x \right) = g\left( x \right) + h\left( x \right)$ $$f\left( x \right) = \sqrt{\left( x_1 - x_2 \right)^2 + \left( y_1 - y_2 \right)^2} + \sqrt{\left( x_2 - x_3 \right)^2+\left( y_2 - y_3 \right)^2}$$

A* (G, s) //Where G is the graph and s is the source node.
    let PQ be priority queue. //And PQ is sorted in ascending order based on the heuristic function f(x).
    PQ.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.

    mark s as visited.
    while ( PQ is not empty)
        //Removing that vertex from queue,whose neighbour will be visited now
        v  =  PQ.dequeue( )

        //processing all the neighbours of v  
        for all neighbours w of v in Graph G
            if (w is not visited) and (w is a passable node) and (w is in corret range)
                PQ.enqueue( w ) //Stores w in Q to further visit its neighbour
                mark w as visited.

A* algorithm with Manhattan distance as heuristic function

Let us call the heuristic weight function $f(x)$ and assume that it consists of $h\left( x \right) = g\left( x \right) + h\left( x \right)$.
At this time, $g\left( x \right)$ is the Manhattan distance from the start node to the x node, and h\left( x \right) is the Manhattan distance from the x node to the destination node.

Manhattan distance from $\left( x_1, y_1 \right)$ to $\left( x_2, y_2 \right)$ = $\left| x_1 - x_2 \right| + \left| y_1 - y_2 \right|$
Thus, $f\left( x \right) = g\left( x \right) + h\left( x \right)$ $$f\left( x \right) = \left( \left| x_1 - x_2 \right| + \left| y_1 - y_2 \right| \right) + \left( \left| x_2 - x_3 \right| + \left| y_2 - y_3 \right| \right)$$

A* (G, s) //Where G is the graph and s is the source node.
    let PQ be priority queue. //And PQ is sorted in ascending order based on the heuristic function f(x).
    PQ.enqueue( s ) //Inserting s in queue until all its neighbour vertices are marked.

    mark s as visited.
    while ( PQ is not empty)
        //Removing that vertex from queue,whose neighbour will be visited now
        v  =  PQ.dequeue( )

        //processing all the neighbours of v  
        for all neighbours w of v in Graph G
            if (w is not visited) and (w is a passable node) and (w is in corret range)
                PQ.enqueue( w ) //Stores w in Q to further visit its neighbour
                mark w as visited.

Referenced document: A* search algorithm

algorithmtermproject's People

Contributors

salkcoding avatar

Watchers

 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.