Code Monkey home page Code Monkey logo

breadth-first-search-depth-first-search-graphs's Introduction

Graphs Algorithms: Breadth First Search & Depth First Search

  • Simple implementation of BFS & DFS in Java using Adjacency List for Graph Representation
  • Uses almost identical code for BFS & DFS. BFS uses a LIFO Queue (LinkedList) while DFS uses a FIFO Queue (Stack)
  • You can pick any node in the graph & run DFS or BFS from that node
  • Ability to repeat & choose a different node or algorithm

Pseudocode

Breadth First Search

Depth First Search

Breadth First Search With Layers

Notes

  • Constructor automatically creates a graph like this: starting-graph
  • CODE IS IDENTICAL! The only difference between BFS & DFS is the type of Queue
    BFS uses LinkedList (LIFO) while DFS uses Stack (FIFO)

    Some code specific details:
    • BFS: Queue<Integer> nodeQueue = new LinkedList<Integer>();
      vs DFS: Stack<Integer> nodeQueue = new Stack<Integer>();
    • BFS: int nodeRemovedFromQueue = nodeQueue.remove();
      vs DFS: int nodeRemovedFromQueue = nodeQueue.pop();
      remove() vs pop()
  • I used a while loop and an iterator instead of an inner for loop since I'm using LinkList for each row in the Adjacency List
  • Calling either method will print the nodes in the order visited by the algorithm

BFS Layers breadthFirstSearchLayers()

  • Uses a different approach for Breadth first search to display the actual layers as BFS ripples outward
  • The line layers.add(new ArrayList<Integer>()); create empty layer each iteration, but results in an extra layer when algorithm terminate.
    • This extra layer is ignored in printBfsLayers()
      if(!layers.get(i).isEmpty()){
      System.out.print("Layer "+i+": ");
      }

BFS Layers shows the layers like this

breadth-first-search-depth-first-search-graphs's People

Contributors

sleekpanther avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

breadth-first-search-depth-first-search-graphs's Issues

The DFS method is incorrect.

The original output for the DFS if the node starts at 2 is as follows,
Depth-first search visitation order
[2, 5, 6, 4, 3, 8, 7, 1]

It should be,
[2, 1, 3, 5, 4, 6, 7, 8]

I've created a pull request for the correct method.

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.