Learn Depth First Search in 7 minutes ⬇️

  Рет қаралды 92,957

Bro Code

Bro Code

Күн бұрын

Пікірлер: 44
@BroCodez
@BroCodez 3 жыл бұрын
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 6 ай бұрын
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 3 жыл бұрын
Dude you're not just bro you're a pro-bro...I understood dfs so easily 😃
@dheerajkumar824
@dheerajkumar824 3 жыл бұрын
Java full lacture, python full lacture, and html css full lacture just by you man. Thanks
@joysaha3927
@joysaha3927 3 жыл бұрын
Happy Diwali Sir ❤️🎉🎉...Keep making videos!
@matthews-carvalho
@matthews-carvalho 3 жыл бұрын
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
@enterb7643
@enterb7643 3 жыл бұрын
great video, I've watched it all already
@truegrabbers
@truegrabbers 3 жыл бұрын
mind blowing, 0:55 was good one :)
@AdityaKumar-vg3bp
@AdityaKumar-vg3bp 3 жыл бұрын
Happy Diwali bro 🔥
@doonk6004
@doonk6004 3 жыл бұрын
Damn, bro. I really needed this video 3 weeks ago! Lol great video!
@soicooc3500
@soicooc3500 7 ай бұрын
thanks man , help me a lot to deep understand
@andleebmiraan5716
@andleebmiraan5716 2 жыл бұрын
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?
@ujjwalabhishek39
@ujjwalabhishek39 3 жыл бұрын
Hey bro awesome content ❤❤❤👌👌👌👌👌👌 Plz make data structure in C# also Happy diwali 😊😊😊🙏
@OPGAMER.
@OPGAMER. 3 жыл бұрын
Happy Diwali Everyone 🙏🙏
@DetCoAnimeFan
@DetCoAnimeFan 3 жыл бұрын
Happy Diwali
@aditya_asundi
@aditya_asundi 3 жыл бұрын
Happy deepawali
@gerdsfargen6687
@gerdsfargen6687 3 жыл бұрын
Happy deepwebali
@rahulchaudhary3508
@rahulchaudhary3508 2 жыл бұрын
Happy Diwali again bruh
@jubayeralam8358
@jubayeralam8358 8 ай бұрын
​@@rahulchaudhary3508 Wtf is diwali
@adheesh2secondsago630
@adheesh2secondsago630 3 жыл бұрын
Love your content Bro :D
@kunwardeepsingh9864
@kunwardeepsingh9864 3 жыл бұрын
Nice Video :)
@ShhFah
@ShhFah 3 жыл бұрын
Love you vids bro
@huyngo2u924
@huyngo2u924 3 жыл бұрын
heap sort bro , i lov u
@Naufalmlns
@Naufalmlns 6 ай бұрын
can you make it list adj version?
@MrLoser-ks2xn
@MrLoser-ks2xn Жыл бұрын
Thanks!
@DineshKumar-hh8pq
@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
@sandracooper7256
@sandracooper7256 3 жыл бұрын
Thank you Bro🙂
@harshitpandit188
@harshitpandit188 3 жыл бұрын
Hello Bro!! can u plss make these algorithms in C++ or Python?? Really love your content♥♥
@BroCodez
@BroCodez 3 жыл бұрын
maybe in the playlists. I have lot of material people want me to cover
@Jukebox300Minecraft
@Jukebox300Minecraft 7 ай бұрын
Why not do it yourself?
@joysaha3927
@joysaha3927 3 жыл бұрын
Sir, if possible, kindly make a video on the implementation of priority queues & it's theory..😇🙏
@BroCodez
@BroCodez 3 жыл бұрын
I believe I have a video on those in this playlist
@eltons.6916
@eltons.6916 4 ай бұрын
Is anyone else confused why he used a stack for the visualisation and then recursion for the implementation?
@tipster360
@tipster360 Жыл бұрын
👌👌👌
@cadmium4113
@cadmium4113 3 жыл бұрын
Gracias ✌️
@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 10 ай бұрын
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.
@IceP67
@IceP67 3 жыл бұрын
First?
@eugenezuev7349
@eugenezuev7349 2 ай бұрын
sour-sweet
@mingming1869
@mingming1869 Жыл бұрын
yosh
@fyrukmcoo100
@fyrukmcoo100 3 жыл бұрын
:((
@lakewobegonesbest8725
@lakewobegonesbest8725 4 ай бұрын
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
Рет қаралды 42 М.
From Small To Giant 0%🍫 VS 100%🍫 #katebrush #shorts #gummy
00:19
When u fight over the armrest
00:41
Adam W
Рет қаралды 33 МЛН
coco在求救? #小丑 #天使 #shorts
00:29
好人小丑
Рет қаралды 24 МЛН
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 184 М.
DFS vs BFS, When to Use Which?
9:25
AlgoMonster
Рет қаралды 6 М.
Learn Hash Tables in 13 minutes #️⃣
13:26
Bro Code
Рет қаралды 387 М.
Graph Search Algorithms in 100 Seconds - And Beyond with JS
10:30
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 441 М.
Learn Insertion Sort in 7 minutes 🧩
7:05
Bro Code
Рет қаралды 254 М.
Breadth First Search (BFS): Visualized and Explained
10:41
Reducible
Рет қаралды 221 М.
From Small To Giant 0%🍫 VS 100%🍫 #katebrush #shorts #gummy
00:19