Skip to content
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,17 @@ Graph traversals are a way to travel and access/print the nodes in the graph. Th

When you pick up a node and travel all the way down, and then check for any other node.

- [DFS in Graph using Adjacency Matrix](GraphDFSAdjMat.java)
- [DFS in Graph using Adjacency List](GraphDFSAdjList.java)
- [DFS in Graph using Adjacency Matrix](GraphDFSAdjMat.md)
- [DFS in Graph using Adjacency List](GraphDFSAdjList.md)

**Breadth First Search (BFS)**

When you pick up a node and traverse it's neighbours at the present depth before moving in deep.

- [BFS in Graph using Adjacency Matrix](GraphBFSAdjMat.java)
- [DFS in Graph using Adjacency List](GraphBFSAdjList.java)
- [BFS in Graph using Adjacency Matrix](GraphBFSAdjMat.md)
- [DFS in Graph using Adjacency List](GraphBFSAdjList.md)


_Additonal Resources_
[Implementing BFS In Java](https://favtutor.com/blogs/breadth-first-search-java)
[Implementing DFS In Java](https://favtutor.com/blogs/depth-first-search-java)
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Breadth First Search Using Adjacency List
## BFS Algorithm

The general process of exploring a graph using breadth-first search includes the following steps:-

-Take the input for the adjacency matrix or adjacency list for the graph.
-Initialize a queue.
-Enqueue the root node (in other words, put the root node into the beginning of the queue.
-Dequeue the head (or first element) of the queue, then enqueue all of its neighboring nodes, starting from left to right.
-If a node has no neighboring nodes which need to be explored, simply dequeue the head and continue the process.
-(Note: If a neighbor which is already explored or in the queue appears, don’t enqueue it – simply skip it
-Keep repeating this process till the queue is empty.



```java


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
Expand Down Expand Up @@ -59,4 +77,11 @@ public static void main(String[] args) {
System.out.println("BFS:");
g.bfs(2);
}
}
}


```
# Time & Space Complexity
The running time complexity of the BFS in Java is O(V+E) where V is the number of nodes in the graph, and E is the number of edges.

Since the algorithm requires a queue for storing the nodes that need to be traversed at any point in time, the space complexity is O(V).
20 changes: 20 additions & 0 deletions Traversals/GraphBFSAdjMat.java → Traversals/GraphBFSAdjMat.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# Breadth First Search Using Adjacency Matrix

## BFS Algorithm
The general process of exploring a graph using breadth-first search includes the following steps:-

-Take the input for the adjacency matrix or adjacency list for the graph.
-Initialize a queue.
-Enqueue the root node (in other words, put the root node into the beginning of the queue).
-Dequeue the head (or first element) of the queue, then enqueue all of its neighboring nodes, starting from left to right. If a node has no neighboring nodes which need to be explored, simply dequeue the head and continue the process.
-(Note: If a neighbor which is already explored or in the queue appears, don’t enqueue it – simply skip it.)
-Keep repeating this process till the queue is empty.

```java
import java.util.LinkedList;
import java.util.Queue;


public class GraphBFSAdjMat {
private int vertices; // Number of vertices in the graph
private int[][] adjacencyMatrix; // Adjacency matrix to store graph edges
Expand Down Expand Up @@ -56,3 +69,10 @@ public static void main(String[] args) {
g.bfs(2);
}
}
```
## Time & Space Complexity


The running time complexity of the BFS in Java is O(V+E) where V is the number of nodes in the graph, and E is the number of edges.

Since the algorithm requires a queue for storing the nodes that need to be traversed at any point in time, the space complexity is O(V).
21 changes: 21 additions & 0 deletions Traversals/GraphDFSAdjList.java → Traversals/GraphDFSAdjList.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# Depth First Search Using Adjacency List </br>
## DFS Algorithm:

The general process of exploring a graph using depth first search includes the following steps:-

- Take the input for the adjacency matrix or adjacency list for the graph.
- Initialize a stack.
- Push the root node (in other words, put the root node into the beginning of the stack).
- If the root node has no neighbors, stop here. Else push the leftmost neighboring node which hasn’t already been explored into the stack. Continue this process till a node is encountered which has no neighbors (or whose neighbors have all been added to the stack already) – stop the process then, pop the head, and then continue the process for the node which is popped.
- Keep repeating this process till the stack becomes empty.

```java

import java.util.*;

public class GraphDFSAdjList {
Expand Down Expand Up @@ -62,3 +75,11 @@ public static void main(String[] args) {
g.dfs(2);
}
}


```

## Time & Space Complexity
The running time complexity of the DFS algorithm in java is O(V+E) where V is the number of nodes in the graph, and E is the number of edges.

Since the algorithm requires a stack for storing the nodes that need to be traversed at any point in time, the space complexity is the maximum size of the stack at any point of time. Since this can extend to V slots for a linear graph, the maximum space complexity is O(V).
23 changes: 23 additions & 0 deletions Traversals/GraphDFSAdjMat.java → Traversals/GraphDFSAdjMat.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# Depth First Search Using Adjacency Matrix

## DFS Algorithm

The general process of exploring a graph using depth first search includes the following steps:-

-Take the input for the adjacency matrix or adjacency list for the graph.
-Initialize a stack.
-Push the root node (in other words, put the root node into the beginning of the stack).
-If the root node has no neighbors, stop here.
-Else push the leftmost neighboring node which hasn’t already been explored into the stack.
-Continue this process till a node is encountered which has no neighbors (or whose neighbors have all been added to the stack already)
– stop the process then, pop the head, and then continue the process for the node which is popped.
-Keep repeating this process till the stack becomes empty.

```java

import java.util.Stack;

public class GraphDFSAdjMat {
Expand Down Expand Up @@ -60,3 +77,9 @@ public static void main(String[] args) {
}

}
```
## Time & space Complexity

The running time complexity of the DFS algorithm in java is O(V+E) where V is the number of nodes in the graph, and E is the number of edges.

Since the algorithm requires a stack for storing the nodes that need to be traversed at any point in time, the space complexity is the maximum size of the stack at any point of time. Since this can extend to V slots for a linear graph, the maximum space complexity is O(V).