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; } }
@@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.
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.
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;
}
}
why "nodes = new ArrayList();" , why not "nodes = new ArrayList();" in the Graph constructor?
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;
}
}
@@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.
Once again, an other masterpiece from our favorite bro. Keep up the excellent work bro!
thanks i learn alot about you front html css js full cource and now i learn dsa , thank again
bro youre carrying my comp sci exam in school thank you man
really really perfect approach 👍👍👍
Booleans are better because they take less memory
Thanks for this tutorial.👏
Nice class
very nice video thanks
Can you make two video series for Spring Boot(3.2+) and Angular (15+)
I had to do an assigment and this video saved me tyvm!!!!!
The video is extremely helpful, thank you Bro!
wonderful stuff
Great video bro
God bless you bro!
Thank you for the video
Sit back, relax and enjoy the show
God bess you bro
Great video. It would have been nice to add how to check if there is a cycle.
Great!!
here's a variable in java. 2 vids later. build a matrix why not... while your at it!
Thanks!
Would it be better to use a boolean matrix instead of an int one?
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.
Love you Bro
nice man
consumable
I stopped here 3:34.
why 1 is only between A and B, and not between B and A?
Because the first is the source while the second is the destination. The edge only goes from A to B, not B to A
@@anthonymartinez71604 thx!