diff --git a/README.md b/README.md index 9e9ebf2..2e580de 100644 --- a/README.md +++ b/README.md @@ -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) \ No newline at end of file +- [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) diff --git a/Traversals/GraphBFSAdjList.java b/Traversals/GraphBFSAdjList.md similarity index 65% rename from Traversals/GraphBFSAdjList.java rename to Traversals/GraphBFSAdjList.md index 6903813..c897c06 100644 --- a/Traversals/GraphBFSAdjList.java +++ b/Traversals/GraphBFSAdjList.md @@ -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; @@ -59,4 +77,11 @@ public static void main(String[] args) { System.out.println("BFS:"); g.bfs(2); } -} \ No newline at end of file +} + + +``` +# 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). diff --git a/Traversals/GraphBFSAdjMat.java b/Traversals/GraphBFSAdjMat.md similarity index 63% rename from Traversals/GraphBFSAdjMat.java rename to Traversals/GraphBFSAdjMat.md index 6b7a24e..5e41b9f 100644 --- a/Traversals/GraphBFSAdjMat.java +++ b/Traversals/GraphBFSAdjMat.md @@ -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 @@ -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). diff --git a/Traversals/GraphDFSAdjList.java b/Traversals/GraphDFSAdjList.md similarity index 62% rename from Traversals/GraphDFSAdjList.java rename to Traversals/GraphDFSAdjList.md index b6a488c..ba6ae1a 100644 --- a/Traversals/GraphDFSAdjList.java +++ b/Traversals/GraphDFSAdjList.md @@ -1,3 +1,16 @@ +# Depth First Search Using Adjacency List
+## 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 { @@ -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). diff --git a/Traversals/GraphDFSAdjMat.java b/Traversals/GraphDFSAdjMat.md similarity index 60% rename from Traversals/GraphDFSAdjMat.java rename to Traversals/GraphDFSAdjMat.md index b4d56c5..d71ef67 100644 --- a/Traversals/GraphDFSAdjMat.java +++ b/Traversals/GraphDFSAdjMat.md @@ -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 { @@ -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).