Skip to content

The simplest graph implementation in Java for DFS and BFS practise

License

Notifications You must be signed in to change notification settings

Alevs2R/simplest_graph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Simplest graph implementation in Java (30 lines)

This class Graph implements simplified Graph ADT without removals It can be used to practise in writing Depth first search (DFT) and Breadth first search (BFT)

List of nodes (vertices) have to be passed in constructor, but edges can be added later.

Only for educational purposes! :)

Usage

Create an instance

String[] nodes = {"A", "B", "C"};
List<String> nodesList = Arrays.asList(nodes);
Graph<String> graph = new Graph<>(nodesList);

Add edges

graph.addNeighbor("A", "B");
graph.addNeighbor("A", "C");
graph.addNeighbor("C", "A");

Do whatever you want

//print all nodes
for(Node<String> n: graph.getNodes()){
    System.out.println(n.element);
}

//print number of vertices that are adjacent to A
System.out.println(graph.getNode("A").neighbors.size());

//mark node as visited
graph.getNode("A").visited = true;

//maybe something like this
dfs(graph.getNodes().get(0));

About

The simplest graph implementation in Java for DFS and BFS practise

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages