Skip to content

A project created for demonstrating three different ways of implementing a depth first search algorithm.

Notifications You must be signed in to change notification settings

FabioCingottini/depth-first-search-implementations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Description

Given the following graph:

Graph image

Represented by the following javascript data structure:

const graph = {
  a: ["b", "e"],
  b: ["c", "d"],
  c: [],
  d: [],
  e: ["f"],
  f: [],
};

Three possible way could be used to iterate through its nodes. They are explained below.

Recursive depth first search

In index.js one can find the function recursiveDFS.
Given a graph parameter it will iterate over its nodes by using recursion and returns an array representing the iteration order.
The function called in the following way:

recursiveDFS(graph);

Will have the following returned value:

['a', 'b', 'c', 'd', 'e', 'f']

Iterative depth first search

In index.js one can find the function iterativeDFS.
Given a graph parameter it will iterate over its nodes by using iteration and returns an array representing the iteration order.
The function called in the following way:

iterativeDFS(graph);

Will have the following returned value:

['a', 'e', 'f', 'b', 'd', 'c']

Iterative depth first search with recursive-like order

In index.js one can find the function orderedIterativeDFS.
Given a graph parameter it will iterate over its nodes by using iteration and returns an array representing the iteration order.
The difference between iterativeDFS and orderedIterativeDFS is that the orderedIterativeDFS iterate over the nodes in the same order as recursiveDFS.
The function called in the following way:

orderedIterativeDFS(graph);

Will have the following returned value:

['a', 'b', 'c', 'd', 'e', 'f']

About

A project created for demonstrating three different ways of implementing a depth first search algorithm.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published