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; } }
@TarekBuhdeima2 жыл бұрын
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); } } }
@joyceasante8292 Жыл бұрын
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; } }
@TinyBoyy286 ай бұрын
can i write dfs like this: public void DFS(int src) { System.out.print(nodes.get(src).data + " "); for(int i=0; i
@preraksemwal3 жыл бұрын
Dude you're not just bro you're a pro-bro...I understood dfs so easily 😃
@dheerajkumar8243 жыл бұрын
Java full lacture, python full lacture, and html css full lacture just by you man. Thanks
@joysaha39273 жыл бұрын
Happy Diwali Sir ❤️🎉🎉...Keep making videos!
@matthews-carvalho3 жыл бұрын
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
@enterb76433 жыл бұрын
great video, I've watched it all already
@truegrabbers3 жыл бұрын
mind blowing, 0:55 was good one :)
@AdityaKumar-vg3bp3 жыл бұрын
Happy Diwali bro 🔥
@doonk60043 жыл бұрын
Damn, bro. I really needed this video 3 weeks ago! Lol great video!
@soicooc35007 ай бұрын
thanks man , help me a lot to deep understand
@andleebmiraan57162 жыл бұрын
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?
@ujjwalabhishek393 жыл бұрын
Hey bro awesome content ❤❤❤👌👌👌👌👌👌 Plz make data structure in C# also Happy diwali 😊😊😊🙏
@OPGAMER.3 жыл бұрын
Happy Diwali Everyone 🙏🙏
@DetCoAnimeFan3 жыл бұрын
Happy Diwali
@aditya_asundi3 жыл бұрын
Happy deepawali
@gerdsfargen66873 жыл бұрын
Happy deepwebali
@rahulchaudhary35082 жыл бұрын
Happy Diwali again bruh
@jubayeralam83588 ай бұрын
@@rahulchaudhary3508 Wtf is diwali
@adheesh2secondsago6303 жыл бұрын
Love your content Bro :D
@kunwardeepsingh98643 жыл бұрын
Nice Video :)
@ShhFah3 жыл бұрын
Love you vids bro
@huyngo2u9243 жыл бұрын
heap sort bro , i lov u
@Naufalmlns6 ай бұрын
can you make it list adj version?
@MrLoser-ks2xn Жыл бұрын
Thanks!
@DineshKumar-hh8pq Жыл бұрын
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
@sandracooper72563 жыл бұрын
Thank you Bro🙂
@harshitpandit1883 жыл бұрын
Hello Bro!! can u plss make these algorithms in C++ or Python?? Really love your content♥♥
@BroCodez3 жыл бұрын
maybe in the playlists. I have lot of material people want me to cover
@Jukebox300Minecraft7 ай бұрын
Why not do it yourself?
@joysaha39273 жыл бұрын
Sir, if possible, kindly make a video on the implementation of priority queues & it's theory..😇🙏
@BroCodez3 жыл бұрын
I believe I have a video on those in this playlist
@eltons.69164 ай бұрын
Is anyone else confused why he used a stack for the visualisation and then recursion for the implementation?
@tipster360 Жыл бұрын
👌👌👌
@cadmium41133 жыл бұрын
Gracias ✌️
@therushhourbrothers797 Жыл бұрын
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.
@DontAddMe10 ай бұрын
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.
@IceP673 жыл бұрын
First?
@eugenezuev73492 ай бұрын
sour-sweet
@mingming1869 Жыл бұрын
yosh
@fyrukmcoo1003 жыл бұрын
:((
@lakewobegonesbest87254 ай бұрын
I think you’re missing out on a sure fire marketing opportunity: BRODER. You’re welcome.