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; } }
@masternobody18963 жыл бұрын
Thanks
@masternobody18963 жыл бұрын
... What is this
@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; } }
@pfo98653 жыл бұрын
Thank You so much for making these videos, they help me a lot during college :>
@Snowmanver22 жыл бұрын
Thank you for yet another incredible video, Bro!
@user-zx7bq4lo1g2 жыл бұрын
Great video man. A video about ortogonal lists would be great
@mateowang65703 жыл бұрын
Thanks for the great vid! Do you have a video explaining adjacency lists for undirected graphs?
@maruthichowdary86122 жыл бұрын
how to add weights for this
@eduardoignacioroblessosa63497 ай бұрын
as a Node's property
@sibusisothabethe31098 ай бұрын
A legend 🔥🔥
@eduardoignacioroblessosa63497 ай бұрын
magnificent
@juanandresstingo9 ай бұрын
Could it be represented in a HashMap? so as Key you put the Node and in Value all the relations for the Node?
@nikavscodeАй бұрын
Yes, that's another alternative to represent a Graph
@thalladasriram39362 жыл бұрын
can we write LinkedList [ ] ll =new LinkedList() ?
@RafaelSamuel-f9e Жыл бұрын
Great thank you!
@frazebean5117 Жыл бұрын
Thank you!!!
@MrLoser-ks2xn Жыл бұрын
Thanks!
@eugenezuev73492 ай бұрын
a bit tough but still sweet
@sibusisothabethe31097 ай бұрын
Mine shows null values idk why somebody quickly help
@willywonka47612 жыл бұрын
Miss you, bro
@keenlearner18912 жыл бұрын
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 Жыл бұрын
@@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.
@keenlearner18912 жыл бұрын
can anyone plzz explain what is NODE ?? why we are passing Node inside generics value ??
@pt_trainer92442 жыл бұрын
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 Жыл бұрын
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.