Learn Depth First Search in 7 minutes ⬇️

  Рет қаралды 81,638

Bro Code

Bro Code

Күн бұрын

Depth first search data structures and algorithms tutorial example explained java
#depth #first #search

Пікірлер: 45
@BroCodez
@BroCodez 2 жыл бұрын
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; } }
@TarekBuhdeima
@TarekBuhdeima 2 жыл бұрын
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
@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; } }
@TinyBoyy28
@TinyBoyy28 4 ай бұрын
can i write dfs like this: public void DFS(int src) { System.out.print(nodes.get(src).data + " "); for(int i=0; i
@preraksemwal
@preraksemwal 2 жыл бұрын
Dude you're not just bro you're a pro-bro...I understood dfs so easily 😃
@matthews-carvalho
@matthews-carvalho 2 жыл бұрын
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
@andleebmiraan5716
@andleebmiraan5716 Жыл бұрын
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?
@joysaha3927
@joysaha3927 2 жыл бұрын
Happy Diwali Sir ❤️🎉🎉...Keep making videos!
@dheerajkumar824
@dheerajkumar824 2 жыл бұрын
Java full lacture, python full lacture, and html css full lacture just by you man. Thanks
@OPGAMER.
@OPGAMER. 2 жыл бұрын
Happy Diwali Everyone 🙏🙏
@DetCoAnimeFan
@DetCoAnimeFan 2 жыл бұрын
Happy Diwali
@aditya_asundi
@aditya_asundi 2 жыл бұрын
Happy deepawali
@gerdsfargen6687
@gerdsfargen6687 2 жыл бұрын
Happy deepwebali
@rahulchaudhary3508
@rahulchaudhary3508 Жыл бұрын
Happy Diwali again bruh
@jubayeralam8358
@jubayeralam8358 5 ай бұрын
​@@rahulchaudhary3508 Wtf is diwali
@AdityaKumar-vg3bp
@AdityaKumar-vg3bp 2 жыл бұрын
Happy Diwali bro 🔥
@doonk6004
@doonk6004 2 жыл бұрын
Damn, bro. I really needed this video 3 weeks ago! Lol great video!
@enterb7643
@enterb7643 2 жыл бұрын
great video, I've watched it all already
@ujjwalabhishek39
@ujjwalabhishek39 2 жыл бұрын
Hey bro awesome content ❤❤❤👌👌👌👌👌👌 Plz make data structure in C# also Happy diwali 😊😊😊🙏
@DineshKumar-hh8pq
@DineshKumar-hh8pq 10 ай бұрын
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
@Naufalmlns
@Naufalmlns 4 ай бұрын
can you make it list adj version?
@soicooc3500
@soicooc3500 5 ай бұрын
thanks man , help me a lot to deep understand
@kunwardeepsingh9864
@kunwardeepsingh9864 2 жыл бұрын
Nice Video :)
@truegrabbers
@truegrabbers 2 жыл бұрын
mind blowing, 0:55 was good one :)
@adheesh2secondsago630
@adheesh2secondsago630 2 жыл бұрын
Love your content Bro :D
@therushhourbrothers797
@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.
@DontAddMe
@DontAddMe 8 ай бұрын
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.
@harshitpandit188
@harshitpandit188 2 жыл бұрын
Hello Bro!! can u plss make these algorithms in C++ or Python?? Really love your content♥♥
@BroCodez
@BroCodez 2 жыл бұрын
maybe in the playlists. I have lot of material people want me to cover
@Jukebox300Minecraft
@Jukebox300Minecraft 5 ай бұрын
Why not do it yourself?
@joysaha3927
@joysaha3927 2 жыл бұрын
Sir, if possible, kindly make a video on the implementation of priority queues & it's theory..😇🙏
@BroCodez
@BroCodez 2 жыл бұрын
I believe I have a video on those in this playlist
@ShhFah
@ShhFah 2 жыл бұрын
Love you vids bro
@eltons.6916
@eltons.6916 2 ай бұрын
Is anyone else confused why he used a stack for the visualisation and then recursion for the implementation?
@huyngo2u924
@huyngo2u924 2 жыл бұрын
heap sort bro , i lov u
@IceMakesStuff
@IceMakesStuff 2 жыл бұрын
First?
@MrLoser-ks2xn
@MrLoser-ks2xn Жыл бұрын
Thanks!
@sandracooper7256
@sandracooper7256 2 жыл бұрын
Thank you Bro🙂
@tipster360
@tipster360 Жыл бұрын
👌👌👌
@cadmium4113
@cadmium4113 2 жыл бұрын
Gracias ✌️
@eugenezuev7349
@eugenezuev7349 15 күн бұрын
sour-sweet
@AdelaBourne-c3q
@AdelaBourne-c3q 8 күн бұрын
Martinez Linda Brown Helen Lee Sarah
@mingming1869
@mingming1869 Жыл бұрын
yosh
@fyrukmcoo100
@fyrukmcoo100 2 жыл бұрын
:((
@lakewobegonesbest8725
@lakewobegonesbest8725 2 ай бұрын
I think you’re missing out on a sure fire marketing opportunity: BRODER. You’re welcome.
Learn Breadth First Search in 6 minutes ↔️
6:41
Bro Code
Рет қаралды 38 М.
小丑妹妹插队被妈妈教训!#小丑#路飞#家庭#搞笑
00:12
家庭搞笑日记
Рет қаралды 36 МЛН
Пришёл к другу на ночёвку 😂
01:00
Cadrol&Fatich
Рет қаралды 10 МЛН
Touching Act of Kindness Brings Hope to the Homeless #shorts
00:18
Fabiosa Best Lifehacks
Рет қаралды 19 МЛН
Depth First Search DFS
8:39
Anitha Ramesh
Рет қаралды 13 М.
Learn Merge Sort in 13 minutes 🔪
13:45
Bro Code
Рет қаралды 298 М.
Depth First & Tree Traversals (Pre, In, Post) Explained
28:43
Coderbyte
Рет қаралды 27 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 384 М.
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Codebagel
Рет қаралды 185 М.
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Рет қаралды 337 М.
小丑妹妹插队被妈妈教训!#小丑#路飞#家庭#搞笑
00:12
家庭搞笑日记
Рет қаралды 36 МЛН