public class Main { public static void main(String[] args) {
// Depth First Search = Pick a route, keep going. // If you reach a dead end, or an already visited node, // backtrack to a previous node with unvisited adjacent neighbors
I made this change to dFSHelper method to print statements to show how it travrese the matrix normally then recursivley private void dFSHelper(int src, boolean[] visited) { if (visited[src]) { return; } else { visited[src] = true; // System.out.println(nodes.get(src).data + " = visited"); } for (int i = 0; i < matrix[src].length; i++) { System.out.println(nodes.get(src).data + " = searching for neighbor to visit at " + "[" + src + ":" + i + "]"); if (matrix[src][i] == 1) { System.out.println(nodes.get(src).data + " = neighbor found at " + "[" + src + ":" + i + "]"); dFSHelper(i, visited); } } }
What is the chance of Bro Code uploading a video about DFS which is the EXACT topic that will be on my tomorrow's exam, dude... stop spying me... and thank u
I didn't get it, what exactly is the use of this code? I mean, you said that the print statement for the visited nodes is not necessary but besides that, I cant' understand what does the code do?
Bro what happens when I becomes 3 . The for completes the search and if condition doesn't execute since there is no connection from d i.e 3. The recursion should stop. But it executed for i=4 how
guys, I have been learning java for 2 months, but I can't understand why would you need an array of boolean for dfs in this case, can someone explain pls.
I don't think it has to anything with java or any other language. The logic is to keep track of visited nodes. if its true on a given index then the node on that index is visited or else it is not visited.
public class Main {
public static void main(String[] args) {
// Depth First Search = Pick a route, keep going.
// If you reach a dead end, or an already visited node,
// backtrack to a previous node with unvisited adjacent neighbors
Graph graph = new Graph(5);
graph.addNode(new Node('A'));
graph.addNode(new Node('B'));
graph.addNode(new Node('C'));
graph.addNode(new Node('D'));
graph.addNode(new Node('E'));
graph.addEdge(0, 1);
graph.addEdge(1, 2);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(2, 4);
graph.addEdge(4, 0);
graph.addEdge(4, 2);
graph.print();
graph.depthFirstSearch(0);
}
}
import java.util.*;
public class Graph {
ArrayList nodes;
int[][] matrix;
Graph(int size){
nodes = new ArrayList();
matrix = new int[size][size];
}
public void addNode(Node node) {
nodes.add(node);
}
public void addEdge(int src, int dst) {
matrix[src][dst] = 1;
}
public boolean checkEdge(int src, int dst) {
if(matrix[src][dst] == 1) {
return true;
}
else {
return false;
}
}
public void print() {
System.out.print(" ");
for(Node node : nodes) {
System.out.print(node.data + " ");
}
System.out.println();
for(int i = 0; i < matrix.length; i++) {
System.out.print(nodes.get(i).data + " ");
for(int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public void depthFirstSearch(int src) {
boolean[] visited = new boolean[matrix.length];
dFSHelper(src, visited);
}
private void dFSHelper(int src, boolean[] visited) {
if(visited[src]) {
return;
}
else {
visited[src] = true;
System.out.println(nodes.get(src).data + " = visited");
}
for(int i = 0; i < matrix[src].length; i++) {
if(matrix[src][i] == 1) {
dFSHelper(i, visited);
}
}
return;
}
}
public class Node {
char data;
Node(char data){
this.data = data;
}
}
I made this change to dFSHelper method to print statements to show how it travrese the matrix normally then recursivley
private void dFSHelper(int src, boolean[] visited) {
if (visited[src]) {
return;
} else {
visited[src] = true;
// System.out.println(nodes.get(src).data + " = visited");
}
for (int i = 0; i < matrix[src].length; i++) {
System.out.println(nodes.get(src).data + " = searching for neighbor to visit at " + "[" + src + ":" + i + "]");
if (matrix[src][i] == 1) {
System.out.println(nodes.get(src).data + " = neighbor found at " + "[" + src + ":" + i + "]");
dFSHelper(i, visited);
}
}
}
Practicing(coding line by line)
public class Main
{
public static void main (String[]args)
{
Graph graph = new Graph (5);
graph.addNode (new Node ('1'));
graph.addNode (new Node ('2'));
graph.addNode (new Node ('3'));
graph.addNode (new Node ('4'));
graph.addNode (new Node ('5'));
graph.addEdge (0, 1);
graph.addEdge (1, 2);
graph.addEdge (2, 3);
graph.addEdge (2, 4);
graph.addEdge (4, 0);
graph.addEdge (4, 2);
graph.print ();
graph.depthFirstSearch (0);
}
}
*****************************
import java.util.*;
public class Graph
{
ArrayList < Node > nodes;
int[][] matrix;
Graph (int size)
{
nodes = new ArrayList ();
matrix = new int[size][size];
}
public void addNode (Node node)
{
nodes.add (node);
}
public void addEdge (int src, int dst)
{
matrix[src][dst] = 1;
}
public boolean checkEdge (int src, int dst)
{
if (matrix[src][dst] == 1)
{
return true;
}
else
{
return false;
}
}
public void print ()
{
System.out.print (" ");
for (Node node:nodes)
{
System.out.print (node.data + " ");
}
System.out.println ();
for (int i = 0; i < matrix.length; i++)
{
System.out.print (nodes.get (i).data + " ");
for (int j = 0; j < matrix[i].length; j++)
{
System.out.print (matrix[i][j] + " ");
}
System.out.println ();
}
System.out.println ();
}
public void depthFirstSearch (int src)
{
boolean[]visited = new boolean[matrix.length];
dFSHelper (src, visited);
}
private void dFSHelper (int src, boolean[]visited)
{
if (visited[src])
{
return;
}
else
{
visited[src] = true;
System.out.println (nodes.get (src).data + " =visited");
}
for (int i = 0; i < matrix[src].length; i++)
{
if (matrix[src][i] == 1)
{
dFSHelper (i, visited);
}
}
return;
}
}
**********************************
public class Node{
char data;
Node(char data){
this.data = data;
}
}
can i write dfs like this:
public void DFS(int src)
{
System.out.print(nodes.get(src).data + " ");
for(int i=0; i
Java full lacture, python full lacture, and html css full lacture just by you man. Thanks
Dude you're not just bro you're a pro-bro...I understood dfs so easily 😃
great video, I've watched it all already
Happy Diwali Sir ❤️🎉🎉...Keep making videos!
Damn, bro. I really needed this video 3 weeks ago! Lol great video!
thanks man , help me a lot to deep understand
Hey bro awesome content ❤❤❤👌👌👌👌👌👌
Plz make data structure in C# also
Happy diwali 😊😊😊🙏
What is the chance of Bro Code uploading a video about DFS which is the EXACT topic that will be on my tomorrow's exam, dude... stop spying me... and thank u
mind blowing, 0:55 was good one :)
Love you vids bro
Love your content Bro :D
Thanks!
Nice Video :)
Happy Diwali bro 🔥
Thank you Bro🙂
I didn't get it, what exactly is the use of this code? I mean, you said that the print statement for the visited nodes is not necessary but besides that, I cant' understand what does the code do?
Gracias ✌️
heap sort bro , i lov u
Bro what happens when I becomes 3 . The for completes the search and if condition doesn't execute since there is no connection from d i.e 3. The recursion should stop. But it executed for i=4 how
👌👌👌
Hello Bro!! can u plss make these algorithms in C++ or Python??
Really love your content♥♥
maybe in the playlists. I have lot of material people want me to cover
Why not do it yourself?
guys, I have been learning java for 2 months, but I can't understand why would you need an array of boolean for dfs in this case, can someone explain pls.
I don't think it has to anything with java or any other language. The logic is to keep track of visited nodes. if its true on a given index then the node on that index is visited or else it is not visited.
can you make it list adj version?
Is anyone else confused why he used a stack for the visualisation and then recursion for the implementation?
Sir, if possible, kindly make a video on the implementation of priority queues & it's theory..😇🙏
I believe I have a video on those in this playlist
Happy Diwali Everyone 🙏🙏
Happy Diwali
Happy deepawali
Happy deepwebali
Happy Diwali again bruh
@@rahulchaudhary3508 Wtf is diwali
First?
yosh
:((
I think you’re missing out on a sure fire marketing opportunity: BRODER. You’re welcome.