Learn Breadth First Search in 6 minutes ↔️

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 ม.ค. 2025

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

  • @BroCodez
    @BroCodez  3 ปีที่แล้ว +24

    public class Main {
    public static void main(String[] args) {

    // Breadth FS = Traverse a graph level by level
    // Utilizes a Queue
    // Better if destination is on average close to start
    // Siblings are visited before children

    // Depth FS = Traverse a graph branch by branch
    // Utilizes a Stack
    // Better if destination is on average far from the start
    // Children are visited before siblings
    // More popular for games/puzzles

    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.breadthFirstSearch(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 breadthFirstSearch(int src) {

    Queue queue = new LinkedList();
    boolean[] visited = new boolean[matrix.length];

    queue.offer(src);
    visited[src] = true;

    while(queue.size() != 0) {

    src = queue.poll();
    System.out.println(nodes.get(src).data + " = visited");

    for(int i = 0; i < matrix[src].length; i++) {
    if(matrix[src][i] == 1 && !visited[i]) {
    queue.offer(i);
    visited[i] = true;
    }
    }
    }
    }
    }
    public class Node {
    char data;

    Node(char data){
    this.data = data;
    }
    }

    • @doublemand3078
      @doublemand3078 3 ปีที่แล้ว

      F

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

      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.breadthFirstSearch(2);
      }
      }
      ***************************
      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 breadthFirstSearch(int src){
      Queuequeue = new LinkedList();
      boolean[] visited = new boolean[matrix.length];
      queue.offer(src);
      visited[src] = true;
      while(queue.size() != 0) {
      src = queue.poll();
      System.out.println(nodes.get(src).data + "= visited");
      for(int i = 0; i < matrix[src].length; i++){
      if(matrix[src][i]== 1 && !visited[i]){
      queue.offer(src);
      visited[i] = true;
      }
      }
      }
      }
      }
      ***********************************
      public class Node{
      char data;
      Node(char data){
      this.data = data;
      }
      }

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

      @@joyceasante8292 can u make for list adj?

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

    Most underrated coding channel on TH-cam

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

    I've been trying to figure out how to implement this for a couple of days now until your video popped up, thank you so much. So many explanations are incredibly technical and don't really explain the practicality of it very well

  • @micahjacobson8533
    @micahjacobson8533 ปีที่แล้ว

    Been watching your videos all year and now you're really saving me from my Discrete math class.

  • @LukaS-uu4vv
    @LukaS-uu4vv ปีที่แล้ว

    Informative and easy to understand! Good points in the end too

  • @flammabletfm3405
    @flammabletfm3405 3 ปีที่แล้ว

    Thanks for the tutorial bro. 😇

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

    Bro please make a design and analysis of algorithms, playlist

  • @hermansiisengbae
    @hermansiisengbae 3 ปีที่แล้ว

    Good 👍👍

  • @robinsonlanda9364
    @robinsonlanda9364 3 ปีที่แล้ว

    You are the best:)

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

    my bro your student is here

  • @preraksemwal8768
    @preraksemwal8768 3 ปีที่แล้ว

    you're really good !!

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

    Hey Make React Js Course bro with node js backend

  • @harishgunasekera
    @harishgunasekera ปีที่แล้ว

    Can you do a tutorial for Dijkstra's Algorithm

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

    Super

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

    great vid!

  • @r66112d
    @r66112d 3 ปีที่แล้ว

    Hey Bro can you do some more C# videos? For example LINQ queries ?

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

    sweeeet

  • @scottzeta3067
    @scottzeta3067 3 ปีที่แล้ว +1

    I am confused with why does my lecture needs 40minutes to explain this and I could not understand anything he talked about.

  • @doublemand3078
    @doublemand3078 3 ปีที่แล้ว

    Bro code, I'm learning python ( watching your 12h course) and I got an idea to do a brute force attack on a website, I searched on github and youtube but when I try the code it doesn't work. Can you do a video on how to do a brute force attack and what are python libraries and how they work?

    • @BroCodez
      @BroCodez  3 ปีที่แล้ว

      I don't know anything about hacking, unfortunately xD

    • @doublemand3078
      @doublemand3078 3 ปีที่แล้ว

      @@BroCodez can you do a video on python libraries

    • @doublemand3078
      @doublemand3078 3 ปีที่แล้ว

      @@BroCodez Also, im 15 and I want to learn a programming language but I dont what language and should I learn multiple languages? Am i too late to learn programming

    • @doublemand3078
      @doublemand3078 3 ปีที่แล้ว

      @@adheesh2secondsago630 Thank you, it means a lot!

  • @nonconsensualopinion
    @nonconsensualopinion 3 ปีที่แล้ว +6

    I'm a bit heartbroken. This video didn't start with "Hey everyone, it's your bro". :(

  • @siraj522
    @siraj522 3 ปีที่แล้ว

    Hey bro, can you make a guide for JPackage (making an installable .exe out of a .jar)
    I've seen many tutorials and docs but still have problems with it

  • @gerdsfargen6687
    @gerdsfargen6687 3 ปีที่แล้ว

    Another session with the Brofessor. Class is in Session.

  • @PhongNguyenCS
    @PhongNguyenCS 3 ปีที่แล้ว

    Hello bro, really appreciate what you are doing. I want to ask if you have any knowledge about machine learning field, please make video about it. Thank you

  • @MrLoser-ks2xn
    @MrLoser-ks2xn ปีที่แล้ว

    Thanks!

  • @doublemand3078
    @doublemand3078 3 ปีที่แล้ว

    Where did my comment go?

  • @ngstudentsvlog113
    @ngstudentsvlog113 3 ปีที่แล้ว

    Hello

  • @Sh11roe
    @Sh11roe 3 ปีที่แล้ว

    I'm early today

  • @ngstudentsvlog113
    @ngstudentsvlog113 3 ปีที่แล้ว

    ഒരു ഹലോ തരാമോ 🌚

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

    :((