https://github.com/basemax/depthfirstsearchjava
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.
https://github.com/basemax/depthfirstsearchjava
data-structure datastructure depth-first-search dfs dfs-algorithm dfs-java ds java java-dfs
Last synced: 2 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/basemax/depthfirstsearchjava
- Owner: BaseMax
- License: gpl-3.0
- Created: 2022-12-19T09:32:32.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-02-03T17:43:13.000Z (over 2 years ago)
- Last Synced: 2025-07-19T06:32:16.061Z (3 months ago)
- Topics: data-structure, datastructure, depth-first-search, dfs, dfs-algorithm, dfs-java, ds, java, java-dfs
- Language: Java
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```java
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'); // 4theGraph.addEdge(0, 1); // AB
theGraph.addEdge(1, 2); // BC
theGraph.addEdge(0, 3); // AD
theGraph.addEdge(3, 4); // DESystem.out.print("Visits: ");
theGraph.dfs(); // depth-first search
System.out.println();
```Copyright (c) 2022, Max Base