From 0039bb90ff5ea89d7ae60207cba0492b86574323 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:45:57 +0530 Subject: [PATCH 01/11] Update GraphDFSAdjList.java --- Traversals/GraphDFSAdjList.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Traversals/GraphDFSAdjList.java b/Traversals/GraphDFSAdjList.java index b6a488c..1742106 100644 --- a/Traversals/GraphDFSAdjList.java +++ b/Traversals/GraphDFSAdjList.java @@ -1,3 +1,18 @@ +#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. + +The following is the implementation of BFS: + +'''java + import java.util.*; public class GraphDFSAdjList { @@ -62,3 +77,9 @@ public static void main(String[] args) { g.dfs(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). From e249473cc7e6b50d525ffa40c911e90e7899ee3a Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:49:06 +0530 Subject: [PATCH 02/11] Update GraphBFSAdjMat.java --- Traversals/GraphBFSAdjMat.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Traversals/GraphBFSAdjMat.java b/Traversals/GraphBFSAdjMat.java index 6b7a24e..fb81fb1 100644 --- a/Traversals/GraphBFSAdjMat.java +++ b/Traversals/GraphBFSAdjMat.java @@ -1,4 +1,13 @@ +#Breadth First Search Uisng 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. import java.util.LinkedList; import java.util.Queue; @@ -56,3 +65,9 @@ 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). From 884c4351b6781b394d31e90c75e2899d3811f5d2 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:52:40 +0530 Subject: [PATCH 03/11] Update and rename GraphDFSAdjList.java to GraphDFSAdjList.md --- ...raphDFSAdjList.java => GraphDFSAdjList.md} | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) rename Traversals/{GraphDFSAdjList.java => GraphDFSAdjList.md} (68%) diff --git a/Traversals/GraphDFSAdjList.java b/Traversals/GraphDFSAdjList.md similarity index 68% rename from Traversals/GraphDFSAdjList.java rename to Traversals/GraphDFSAdjList.md index 1742106..06bd49b 100644 --- a/Traversals/GraphDFSAdjList.java +++ b/Traversals/GraphDFSAdjList.md @@ -1,15 +1,13 @@ -#Breadth First Search Using Adjacency List -##BFS Algorithm +#Depth First Search Using Adjacency List +##DFS Algorithm: -The general process of exploring a graph using breadth-first search includes the following steps:- +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 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. - -The following is the implementation of BFS: +-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 @@ -80,6 +78,6 @@ public static void main(String[] args) { ##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. +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 queue for storing the nodes that need to be traversed at any point in time, the space complexity is O(V). +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). From d6767eaa1e37bc613b0fbb21544fb77a86479f02 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:53:11 +0530 Subject: [PATCH 04/11] Update GraphDFSAdjList.md --- Traversals/GraphDFSAdjList.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Traversals/GraphDFSAdjList.md b/Traversals/GraphDFSAdjList.md index 06bd49b..a743e66 100644 --- a/Traversals/GraphDFSAdjList.md +++ b/Traversals/GraphDFSAdjList.md @@ -1,4 +1,4 @@ -#Depth First Search Using Adjacency List +#Depth First Search Using Adjacency List
##DFS Algorithm: The general process of exploring a graph using depth first search includes the following steps:- From 19eb8a66af6951597a79c33b76a2bad0031ff152 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:58:41 +0530 Subject: [PATCH 05/11] Update GraphDFSAdjList.md --- Traversals/GraphDFSAdjList.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/Traversals/GraphDFSAdjList.md b/Traversals/GraphDFSAdjList.md index a743e66..ba6ae1a 100644 --- a/Traversals/GraphDFSAdjList.md +++ b/Traversals/GraphDFSAdjList.md @@ -1,15 +1,15 @@ -#Depth First Search Using Adjacency List
-##DFS Algorithm: +# 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. +- 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 +```java import java.util.*; @@ -77,7 +77,9 @@ public class GraphDFSAdjList { } -##Time & Space Complexity +``` + +## 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). From 1cf852c695dec23314ea0dd18e8ced062e98b070 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:00:21 +0530 Subject: [PATCH 06/11] Update and rename GraphBFSAdjMat.java to GraphBFSAdjMat.md --- ...{GraphBFSAdjMat.java => GraphBFSAdjMat.md} | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) rename Traversals/{GraphBFSAdjMat.java => GraphBFSAdjMat.md} (76%) diff --git a/Traversals/GraphBFSAdjMat.java b/Traversals/GraphBFSAdjMat.md similarity index 76% rename from Traversals/GraphBFSAdjMat.java rename to Traversals/GraphBFSAdjMat.md index fb81fb1..5a8961e 100644 --- a/Traversals/GraphBFSAdjMat.java +++ b/Traversals/GraphBFSAdjMat.md @@ -1,16 +1,19 @@ -#Breadth First Search Uisng Adjacency Matrix +# Breadth First Search Uisng Adjacency Matrix -##BFS Algorithm +## 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. +-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 @@ -65,7 +68,8 @@ public static void main(String[] args) { g.bfs(2); } } -##Time & Space Complexity +``` +## 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. From e59fdababeee94c4d3d7d816f726c91abb8164eb Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:01:39 +0530 Subject: [PATCH 07/11] Update GraphBFSAdjMat.md --- Traversals/GraphBFSAdjMat.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Traversals/GraphBFSAdjMat.md b/Traversals/GraphBFSAdjMat.md index 5a8961e..e497992 100644 --- a/Traversals/GraphBFSAdjMat.md +++ b/Traversals/GraphBFSAdjMat.md @@ -3,11 +3,12 @@ ## 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. +-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; From 0eb0f06994ef657a1b5612918f38816b89cf117d Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:02:06 +0530 Subject: [PATCH 08/11] Update GraphBFSAdjMat.md --- Traversals/GraphBFSAdjMat.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Traversals/GraphBFSAdjMat.md b/Traversals/GraphBFSAdjMat.md index e497992..5e41b9f 100644 --- a/Traversals/GraphBFSAdjMat.md +++ b/Traversals/GraphBFSAdjMat.md @@ -1,4 +1,4 @@ -# Breadth First Search Uisng Adjacency Matrix +# Breadth First Search Using Adjacency Matrix ## BFS Algorithm The general process of exploring a graph using breadth-first search includes the following steps:- From b113bccfedf0352bdadb876509720b7935dd1790 Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:13:37 +0530 Subject: [PATCH 09/11] Update and rename GraphBFSAdjList.java to GraphBFSAdjList.md --- ...raphBFSAdjList.java => GraphBFSAdjList.md} | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) rename Traversals/{GraphBFSAdjList.java => GraphBFSAdjList.md} (65%) 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). From 8f1db042fa677c2b6a24a9636f3d914a83aff71d Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:17:24 +0530 Subject: [PATCH 10/11] Update and rename GraphDFSAdjMat.java to GraphDFSAdjMat.md --- ...{GraphDFSAdjMat.java => GraphDFSAdjMat.md} | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) rename Traversals/{GraphDFSAdjMat.java => GraphDFSAdjMat.md} (60%) 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). From 4d91fa6e28528f2f935ed1bc29642b82e80a2afa Mon Sep 17 00:00:00 2001 From: Noob-Master-22 <124292057+Noob-Master-22@users.noreply.github.com> Date: Sat, 25 Feb 2023 19:23:33 +0530 Subject: [PATCH 11/11] Update README.md --- README.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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)