Learn Adjacency Matrix in 10 minutes ⬜

  Рет қаралды 46,490

Bro Code

Bro Code

2 жыл бұрын

adjacency matrix data structures and algorithms tutorial example explained java
#adjacency #matrix #tutorial

Пікірлер: 32
@BroCodez
@BroCodez 2 жыл бұрын
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 Жыл бұрын
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; } }
@fredericoamigo
@fredericoamigo 6 ай бұрын
Once again, an other masterpiece from our favorite bro. Keep up the excellent work bro!
@ReviewGame10
@ReviewGame10 2 жыл бұрын
I had to do an assigment and this video saved me tyvm!!!!!
@Snowmanver2
@Snowmanver2 2 жыл бұрын
The video is extremely helpful, thank you Bro!
@wikkichris6434
@wikkichris6434 Жыл бұрын
bro youre carrying my comp sci exam in school thank you man
@soicooc3500
@soicooc3500 2 ай бұрын
thanks i learn alot about you front html css js full cource and now i learn dsa , thank again
@yunanistan2364
@yunanistan2364 Жыл бұрын
really really perfect approach 👍👍👍
@berryallen5556
@berryallen5556 2 жыл бұрын
God bless you bro!
@FukSN
@FukSN 2 жыл бұрын
Bro'vo 👏👏👏 Bro'vo Edit: Also if you want to make the matrix exactly like the last vid (Graph), you need to add one more edge - graph.addEdge(1, 4); Not sure why I needed to add this but I did 😁
@Memes_uploader
@Memes_uploader 2 жыл бұрын
Thank you for the video
@one111won
@one111won Жыл бұрын
wonderful stuff
@CrazyD4RKiller1
@CrazyD4RKiller1 2 жыл бұрын
Great video bro
@yunanistan2364
@yunanistan2364 Жыл бұрын
very nice video thanks
@mmm6231
@mmm6231 2 жыл бұрын
Great!!
@victorrezende6002
@victorrezende6002 8 ай бұрын
Nice class
@berryallen5556
@berryallen5556 2 жыл бұрын
God bess you bro
@MrLoser-ks2xn
@MrLoser-ks2xn 11 ай бұрын
Thanks!
@georgekon69
@georgekon69 11 ай бұрын
Booleans are better because they take less memory
@bulentoral1177
@bulentoral1177 Жыл бұрын
Love you Bro
@user-vs5rp4fw3r
@user-vs5rp4fw3r Ай бұрын
Can you make two video series for Spring Boot(3.2+) and Angular (15+)
@musicsadboizz
@musicsadboizz 7 ай бұрын
nice man
@TheNicoya77
@TheNicoya77 Жыл бұрын
Great video. It would have been nice to add how to check if there is a cycle.
@alaeeddinehajji
@alaeeddinehajji Жыл бұрын
Sit back, relax and enjoy the show
@tarekghosn3648
@tarekghosn3648 Жыл бұрын
here's a variable in java. 2 vids later. build a matrix why not... while your at it!
@nik1andr22
@nik1andr22 2 жыл бұрын
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.
@nailcutter8920
@nailcutter8920 7 ай бұрын
why 1 is only between A and B, and not between B and A?
@anthonymartinez7614
@anthonymartinez7614 7 ай бұрын
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 7 ай бұрын
@@anthonymartinez7614 thx!
@wWvwvV
@wWvwvV Жыл бұрын
I stopped here 3:34.
Learn Adjacency Lists in 8 minutes 📑
8:29
Bro Code
Рет қаралды 53 М.
Introduction to Graph Theory: A Computer Science Perspective
16:26
Just try to use a cool gadget 😍
00:33
123 GO! SHORTS
Рет қаралды 66 МЛН
Чай будешь? #чайбудешь
00:14
ПАРОДИИ НА ИЗВЕСТНЫЕ ТРЕКИ
Рет қаралды 2,9 МЛН
Final muy inesperado 🥹
00:48
Juan De Dios Pantoja
Рет қаралды 12 МЛН
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 141 М.
Learn Quick Sort in 13 minutes ⚡
13:49
Bro Code
Рет қаралды 288 М.
Learn Hash Tables in 13 minutes #️⃣
13:26
Bro Code
Рет қаралды 317 М.
How To Implement a Graph in C. (adjacency matrix version)
20:04
Jacob Sorber
Рет қаралды 44 М.
Breadth First Search grid shortest path | Graph Theory
16:51
WilliamFiset
Рет қаралды 324 М.
Learn Queue data structures in 10 minutes 🎟️
10:07
Bro Code
Рет қаралды 105 М.
Vectors in Java: The 1 Situation You Might Want To Use Them
16:13
Coding with John
Рет қаралды 76 М.
ПОКУПКА ТЕЛЕФОНА С АВИТО?🤭
1:00
Корнеич
Рет қаралды 1,8 МЛН
Iphone or nokia
0:15
rishton vines😇
Рет қаралды 1,7 МЛН
Карточка Зарядка 📱 ( @ArshSoni )
0:23
EpicShortsRussia
Рет қаралды 788 М.