Learn Depth First Search in 7 minutes ⬇️

แชร์
ฝัง

ความคิดเห็น • 40

  • @BroCodez
    @BroCodez  2 ปีที่แล้ว +41

    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 ปีที่แล้ว

      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 13 วันที่ผ่านมา

      can i write dfs like this:
      public void DFS(int src)
      {
      System.out.print(nodes.get(src).data + " ");
      for(int i=0; i

  • @dheerajkumar824
    @dheerajkumar824 2 ปีที่แล้ว +4

    Java full lacture, python full lacture, and html css full lacture just by you man. Thanks

  • @preraksemwal
    @preraksemwal 2 ปีที่แล้ว +4

    Dude you're not just bro you're a pro-bro...I understood dfs so easily 😃

  • @doonk6004
    @doonk6004 2 ปีที่แล้ว

    Damn, bro. I really needed this video 3 weeks ago! Lol great video!

  • @joysaha3927
    @joysaha3927 2 ปีที่แล้ว +3

    Happy Diwali Sir ❤️🎉🎉...Keep making videos!

  • @enterb7643
    @enterb7643 2 ปีที่แล้ว

    great video, I've watched it all already

  • @Dq241
    @Dq241 2 ปีที่แล้ว +5

    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

  • @ujjwalabhishek39
    @ujjwalabhishek39 2 ปีที่แล้ว

    Hey bro awesome content ❤❤❤👌👌👌👌👌👌
    Plz make data structure in C# also
    Happy diwali 😊😊😊🙏

  • @soicooc3500
    @soicooc3500 หลายเดือนก่อน

    thanks man , help me a lot to deep understand

  • @tipster360
    @tipster360 9 หลายเดือนก่อน

    👌👌👌

  • @adheesh2secondsago630
    @adheesh2secondsago630 2 ปีที่แล้ว

    Love your content Bro :D

  • @AdityaKumar-vg3bp
    @AdityaKumar-vg3bp 2 ปีที่แล้ว +4

    Happy Diwali bro 🔥

  • @ShhFah
    @ShhFah 2 ปีที่แล้ว

    Love you vids bro

  • @truegrabbers
    @truegrabbers 2 ปีที่แล้ว

    mind blowing, 0:55 was good one :)

  • @sandracooper7256
    @sandracooper7256 2 ปีที่แล้ว

    Thank you Bro🙂

  • @huyngo2u924
    @huyngo2u924 2 ปีที่แล้ว

    heap sort bro , i lov u

  • @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?

  • @kunwardeepsingh9864
    @kunwardeepsingh9864 2 ปีที่แล้ว

    Nice Video :)

  • @harshitpandit188
    @harshitpandit188 2 ปีที่แล้ว

    Hello Bro!! can u plss make these algorithms in C++ or Python??
    Really love your content♥♥

    • @BroCodez
      @BroCodez  2 ปีที่แล้ว +7

      maybe in the playlists. I have lot of material people want me to cover

    • @Jukebox300Minecraft
      @Jukebox300Minecraft หลายเดือนก่อน

      Why not do it yourself?

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 10 หลายเดือนก่อน

    Thanks!

  • @DineshKumar-hh8pq
    @DineshKumar-hh8pq 5 หลายเดือนก่อน

    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

  • @joysaha3927
    @joysaha3927 2 ปีที่แล้ว +1

    Sir, if possible, kindly make a video on the implementation of priority queues & it's theory..😇🙏

    • @BroCodez
      @BroCodez  2 ปีที่แล้ว +7

      I believe I have a video on those in this playlist

  • @cadmium4113
    @cadmium4113 2 ปีที่แล้ว

    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 4 หลายเดือนก่อน

      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.

  • @OPGAMER.
    @OPGAMER. 2 ปีที่แล้ว +19

    Happy Diwali Everyone 🙏🙏

  • @IceMakesStuff
    @IceMakesStuff 2 ปีที่แล้ว +2

    First?

  • @mingming1869
    @mingming1869 11 หลายเดือนก่อน

    yosh

  • @fyrukmcoo100
    @fyrukmcoo100 2 ปีที่แล้ว

    :((