Git Product home page Git Product logo

dfs_example's Introduction

dfs_example

DFS example using a cyclist road network. Written with spaghetti code.

Quick Start

git clone https://github.com/marcus-hao/dfs_example
javac Main.java
java Main

Introduction

The standard DFS algorithm is modifed to terminate early when a destination vertex is reached. This implementation considers an unweighted digraph, where distance is measured by the number of edges required to reach the destination vertex. Therefore, the algorithm makes no assumption on "weights" when deciding which vertex to visit first, and a shortest path is not guaranteed.

Application Scenario

A pathfinding application for a cyclist road network is proposed. Due to poor city planning, the infrastructure for cyclists in the city is lacking. For example, some roads only have a cycling lane on one side of the road, forcing cyclists to take a different route on their return trip for safety. This application helps cyclists locate a path from their current location to ther destination that includes cycling lanes.

Choice of Data Structure

The data structure used to represent the graph is an adjacency list. The adjacency list is wrapped in a map structure, Map<Vertex, List<Vertex>>, where Vertex is a user-defined class for representing locations. This design was chosen because we wanted the flexibility to handle vertices of arbitrary data types during querying. For instance, our Vertex implementation uses strings instead of integers. Each key in the hash map represents a vertex, and its value is an ArrayList for its adjacent vertices. In the DFS algorithm, we used a Set data structure to keep track of the visited vertices during traversal. Because this data structure keeps track of unique values, we can determine whether a vertex has already been visited or not by checking if it is in the Set. If the vertex is already in the Set, then the DFS algorithm backtracks to the parent and visits other adjacent vertices.

General Outline of the Program

While the program makes no assumptions about distance when deciding which vertex to visit during DFS, it is determined by the order in which edges were added to the graph during construction.

For example, if E(u,v) was added before E(u,w), then we visit v first.

Conclusion

The application works as described in the specification of the assignment. For future improvements, a weighted graph implementation will be more suitable for a pathfinding application, as the current DFS algorithm may suggest longer paths than necessary.

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.