Skip to content

This is a Java implementation of the Depth-First-Search algorithm. It is a recursive algorithm that traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.

License

Notifications You must be signed in to change notification settings

BaseMax/DepthFirstSearchJava

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

Depth-First-Search Java

This is a Java implementation of the Depth-First-Search algorithm. It is a recursive algorithm that traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.

Usage

To use this algorithm, you need to create a graph and add vertices and edges to it. Then, you need to create a DepthFirstSearch object and pass the graph to it. Finally, you can call the search method to get the result.

Example

Graph theGraph = new Graph();
theGraph.addVertex('A'); // 0 (start for dfs)
theGraph.addVertex('B'); // 1
theGraph.addVertex('C'); // 2
theGraph.addVertex('D'); // 3
theGraph.addVertex('E'); // 4

theGraph.addEdge(0, 1); // AB
theGraph.addEdge(1, 2); // BC
theGraph.addEdge(0, 3); // AD
theGraph.addEdge(3, 4); // DE

System.out.print("Visits: ");
theGraph.dfs(); // depth-first search
System.out.println();

Copyright (c) 2022, Max Base

About

This is a Java implementation of the Depth-First-Search algorithm. It is a recursive algorithm that traverses a graph in a depthward motion and uses a stack to remember to get the next vertex to start a search, when a dead end occurs in any iteration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages