Learn Adjacency Lists in 8 minutes 📑

  Рет қаралды 63,452

Bro Code

Bro Code

Күн бұрын

Пікірлер: 26
@BroCodez
@BroCodez 3 жыл бұрын
public class Main { public static void main(String[] args) { // Adjacency List = An array/arraylist of linkedlists. // Each LinkedList has a unique node at the head. // All adjacent neighbors to that node are added to that node's linkedlist // runtime complexity to check an Edge: O(v) // space complexity: O(v + e) Graph graph = new Graph(); 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(); //System.out.println(graph.checkEdge(0, 1)); } } import java.util.*; public class Graph { ArrayList alist; Graph(){ alist = new ArrayList(); } public void addNode(Node node) { LinkedList currentList = new LinkedList(); currentList.add(node); alist.add(currentList); } public void addEdge(int src, int dst) { LinkedList currentList = alist.get(src); Node dstNode = alist.get(dst).get(0); currentList.add(dstNode); } public boolean checkEdge(int src, int dst) { LinkedList currentList = alist.get(src); Node dstNode = alist.get(dst).get(0); for(Node node : currentList) { if(node == dstNode) { return true; } } return false; } public void print() { for(LinkedList currentList : alist) { for(Node node : currentList) { System.out.print(node.data + " -> "); } System.out.println(); } } } public class Node { char data; Node(char data){ this.data = data; } }
@masternobody1896
@masternobody1896 3 жыл бұрын
Thanks
@masternobody1896
@masternobody1896 3 жыл бұрын
... What is this
@joyceasante8292
@joyceasante8292 Жыл бұрын
Practicing... public class Main { public static void main (String[]args) { Graph graph = new Graph (); 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 (1, 4); graph.addEdge (2, 3); graph.addEdge (2, 4); graph.addEdge (4, 0); graph.addEdge (4, 2); graph.print (); } } ***************************** import java.util.*; public class Graph { ArrayList < LinkedList < Node >> alist; Graph () { alist = new ArrayList (); } public void addNode (Node node) { LinkedList < Node > currentList = new LinkedList (); currentList.add (node); alist.add (currentList); } public void addEdge (int src, int dst) { LinkedList < Node > currentList = alist.get (src); Node dstNode = alist.get (dst).get (0); currentList.add (dstNode); } public boolean checkEdge (int src, int dst) { LinkedList < Node > currentList = alist.get (src); Node dstNode = alist.get (dst).get (0); for (Node node:currentList) { if (node == dstNode) { return true; } } return false; } public void print () { for (LinkedList < Node > currentList:alist) { for (Node node:currentList) { System.out.print (node.data + " -> "); } System.out.println (); } } } ************************ public class Node { char data; Node (char data) { this.data = data; } }
@pfo9865
@pfo9865 3 жыл бұрын
Thank You so much for making these videos, they help me a lot during college :>
@Snowmanver2
@Snowmanver2 2 жыл бұрын
Thank you for yet another incredible video, Bro!
@user-zx7bq4lo1g
@user-zx7bq4lo1g 2 жыл бұрын
Great video man. A video about ortogonal lists would be great
@mateowang6570
@mateowang6570 3 жыл бұрын
Thanks for the great vid! Do you have a video explaining adjacency lists for undirected graphs?
@maruthichowdary8612
@maruthichowdary8612 2 жыл бұрын
how to add weights for this
@eduardoignacioroblessosa6349
@eduardoignacioroblessosa6349 7 ай бұрын
as a Node's property
@sibusisothabethe3109
@sibusisothabethe3109 8 ай бұрын
A legend 🔥🔥
@eduardoignacioroblessosa6349
@eduardoignacioroblessosa6349 7 ай бұрын
magnificent
@juanandresstingo
@juanandresstingo 9 ай бұрын
Could it be represented in a HashMap? so as Key you put the Node and in Value all the relations for the Node?
@nikavscode
@nikavscode Ай бұрын
Yes, that's another alternative to represent a Graph
@thalladasriram3936
@thalladasriram3936 2 жыл бұрын
can we write LinkedList [ ] ll =new LinkedList() ?
@RafaelSamuel-f9e
@RafaelSamuel-f9e Жыл бұрын
Great thank you!
@frazebean5117
@frazebean5117 Жыл бұрын
Thank you!!!
@MrLoser-ks2xn
@MrLoser-ks2xn Жыл бұрын
Thanks!
@eugenezuev7349
@eugenezuev7349 2 ай бұрын
a bit tough but still sweet
@sibusisothabethe3109
@sibusisothabethe3109 7 ай бұрын
Mine shows null values idk why somebody quickly help
@willywonka4761
@willywonka4761 2 жыл бұрын
Miss you, bro
@keenlearner1891
@keenlearner1891 2 жыл бұрын
same here ... great teacher always leave teaching in between ... broCode if u introduce paid course i will also join but plzz come for us and make full DSA course in java plzzz.... and guide us in becoming great programmer ..............
@tasneemayham974
@tasneemayham974 Жыл бұрын
@@keenlearner1891 No, you guys!! Some of us don't have the opportunity to pay. Don't spoil things!! Bro is great and wonderful. Don't turn everything to money. If money grows on your garden trees, donate with them. Don't tell people to change their courses to paid joins.
@keenlearner1891
@keenlearner1891 2 жыл бұрын
can anyone plzz explain what is NODE ?? why we are passing Node inside generics value ??
@pt_trainer9244
@pt_trainer9244 2 жыл бұрын
It represents the Node of a LinkedList(there is a data part which is the vertex, and a pointer pointing to the next vertex, which is the edge of the graph)
@tasneemayham974
@tasneemayham974 Жыл бұрын
We created a class called Node and passed in as arguments char data. We used that as a reference data type. If you hover over Node in your IDE, you see that it points to the class you made called Node, and that if you change the name of the class called Node, you will encounter an error in Graph and Main which means that here Node isn't a data type that is already created. We made it generic when we passed it in as value in the LinkedList. I hope my answer was useful and not too late.
Learn Depth First Search in 7 minutes ⬇️
7:41
Bro Code
Рет қаралды 92 М.
you will never ask about pointers again after watching this video
8:03
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 116 МЛН
Thank you Santa
00:13
Nadir Show
Рет қаралды 33 МЛН
Players vs Pitch 🤯
00:26
LE FOOT EN VIDÉO
Рет қаралды 137 МЛН
Learn Adjacency Matrix in 10 minutes ⬜
9:52
Bro Code
Рет қаралды 57 М.
Learn Linked Lists in 13 minutes 🔗
13:24
Bro Code
Рет қаралды 349 М.
How To Implement a Graph in C. (adjacency matrix version)
20:04
Jacob Sorber
Рет қаралды 48 М.
LinkedList vs ArrayList in Java Tutorial - Which Should You Use?
11:43
Coding with John
Рет қаралды 608 М.
Python: 2 Ways to Represent GRAPHS
9:13
Oggi AI - Artificial Intelligence Today
Рет қаралды 126 М.
Learn Binary search trees in 20 minutes 🔍
20:25
Bro Code
Рет қаралды 184 М.
Learn Queue data structures in 10 minutes 🎟️
10:07
Bro Code
Рет қаралды 134 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 116 МЛН