Learn Adjacency Matrix in 10 minutes ⬜

Поделиться
HTML-код
  • Опубликовано: 8 янв 2025
  • adjacency matrix data structures and algorithms tutorial example explained java
    #adjacency #matrix #tutorial

Комментарии • 40

  • @BroCodez
    @BroCodez  3 года назад +46

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

    // Adjacency Matrix = An array to store 1's/0's to represent edges
    // # of rows = # of unique nodes
    // # of columns = # of unique nodes

    // runtime complexity to check an Edge: O(1)
    // space complexity: O(v^2)

    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); // I forgot this line in the video
    graph.addEdge(2, 3);
    graph.addEdge(2, 4);
    graph.addEdge(4, 0);
    graph.addEdge(4, 2);

    graph.print();

    //System.out.println(graph.checkEdge(0, 1));
    }
    }
    import java.util.ArrayList;
    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();
    }
    }
    }
    public class Node {
    char data;

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

    • @sriramarajuchintalapati1304
      @sriramarajuchintalapati1304 2 года назад

      why "nodes = new ArrayList();" , why not "nodes = new ArrayList();" in the Graph constructor?

    • @joyceasante8292
      @joyceasante8292 Год назад

      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();
      System.out.println(graph.checkEdge(4,1));
      }
      }
      ******************************************
      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();
      }
      }
      }
      ********************************
      public class Node{
      char data;
      Node(char data){
      this.data = data;
      }
      }

    • @duy6930
      @duy6930 4 месяца назад

      @@sriramarajuchintalapati1304 In Java, the syntax nodes = new ArrayList(); is a shorthand introduced in Java 7, known as the diamond operator. It allows you to omit the generic type on the right-hand side of the assignment when the compiler can infer it from the left-hand side.

  • @fredericoamigo
    @fredericoamigo Год назад +6

    Once again, an other masterpiece from our favorite bro. Keep up the excellent work bro!

  • @soicooc3500
    @soicooc3500 8 месяцев назад

    thanks i learn alot about you front html css js full cource and now i learn dsa , thank again

  • @shinakuma6986
    @shinakuma6986 28 дней назад +1

    why
    public boolean checkEdge(int src, int dst) {
    if(matrix[src][dst] == 1) {
    return true;
    }
    else {
    return false;
    }
    }
    you could change the
    int[ ][ ] matrix;
    line into
    boolean[ ][ ] matrix;
    in the graph data structure and the checkEdge( ) function into
    public boolean checkEdge(boolean src, boolean dst) {
    return matrix[src][dst];
    }
    which would be a lot easier and the data structure would take a lot less space like instead of n^2 * sizeof(int) which is 4*n^2 bytes to n^2 * sizeof(boolean) which is 1 * n^2, other than that, a good video

    • @cardsfanlv9371
      @cardsfanlv9371 18 дней назад

      or if keeping the 0s and 1s are preferred could use byte data type.
      public boolean checkEdge(int src, int dest) {
      return matrix[src][dest] == 1;
      }

  • @yunanistan2364
    @yunanistan2364 Год назад +1

    really really perfect approach 👍👍👍

  • @wikkichris
    @wikkichris Год назад +1

    bro youre carrying my comp sci exam in school thank you man

  • @Snowmanver2
    @Snowmanver2 3 года назад +1

    The video is extremely helpful, thank you Bro!

  • @cardsfanlv9371
    @cardsfanlv9371 18 дней назад

    The missing piece to this is - determining the index of the node. How would we know 'A' is node 0, 'B' is node 1, etc without some sort of lookup. Would probably store these in a hash map or table.

    • @cardsfanlv9371
      @cardsfanlv9371 18 дней назад

      in current setup you could do something like
      int getIndexForNode(Node n) {
      return nodes.indexOf(n);
      }

  • @georgekon69
    @georgekon69 Год назад +6

    Booleans are better because they take less memory

  • @ReviewGame10
    @ReviewGame10 2 года назад

    I had to do an assigment and this video saved me tyvm!!!!!

  • @elionayzuridasilveira4140
    @elionayzuridasilveira4140 2 месяца назад

    Thanks for this tutorial.👏

  • @yunanistan2364
    @yunanistan2364 Год назад

    very nice video thanks

  • @nik1andr22
    @nik1andr22 3 года назад +1

    Would it be better to use a boolean matrix instead of an int one?

    • @silverseltzer2739
      @silverseltzer2739 Год назад

      Booleans in Java are printed as true or false. A boolean matrix would display “true” or “false” instead of the zeros and ones we want in our adjacency matrix.

  • @emreotu4464
    @emreotu4464 20 дней назад

    Really nice!

  • @berryallen5556
    @berryallen5556 3 года назад +1

    God bless you bro!

  • @Md.FirozeHossain
    @Md.FirozeHossain 8 месяцев назад

    Can you make two video series for Spring Boot(3.2+) and Angular (15+)

  • @one111won
    @one111won Год назад

    wonderful stuff

  • @CrazyD4RKiller1
    @CrazyD4RKiller1 3 года назад +1

    Great video bro

  • @victorrezende6002
    @victorrezende6002 Год назад

    Nice class

  • @Memes_uploader
    @Memes_uploader 2 года назад

    Thank you for the video

  • @tarekghosn3648
    @tarekghosn3648 2 года назад

    here's a variable in java. 2 vids later. build a matrix why not... while your at it!

  • @mmm6231
    @mmm6231 3 года назад

    Great!!

  • @alaeeddinehajji
    @alaeeddinehajji 2 года назад

    Sit back, relax and enjoy the show

  • @nailcutter8920
    @nailcutter8920 Год назад

    why 1 is only between A and B, and not between B and A?

    • @anthonymartinez71604
      @anthonymartinez71604 Год назад

      Because the first is the source while the second is the destination. The edge only goes from A to B, not B to A

    • @nailcutter8920
      @nailcutter8920 Год назад

      @@anthonymartinez71604 thx!

  • @wWvwvV
    @wWvwvV Год назад +1

    I stopped here 3:34.

  • @berryallen5556
    @berryallen5556 3 года назад +1

    God bess you bro

  • @TheNicoya77
    @TheNicoya77 Год назад

    Great video. It would have been nice to add how to check if there is a cycle.

  • @bulentoral1177
    @bulentoral1177 2 года назад

    Love you Bro

  • @musicsadboizz
    @musicsadboizz Год назад

    nice man

  • @MrLoser-ks2xn
    @MrLoser-ks2xn Год назад

    Thanks!

  • @eugenezuev7349
    @eugenezuev7349 4 месяца назад

    consumable