#   # Graph Representation ☝️This is a graph. How? A graph is a collection of nodes and edges in a way that nodes are connected by edges.

How does the computer understand what is a graph? and what are nodes and edges?

There are two commonly used techniques for representing graphs An Adjacency matrix is the square matrix (same number of rows and columns) of size N x N where N is the number of the nodes. each vertex of a matrix is either 0 or 1 depending on whether there is an edge between nodes. If you look at the graph, node "A" has the edge with node "B" and vice versa. To represent this in the matrix we write 1 in the front row "A" and column "B" And if there is no edge between nodes we write 0.

``````class Graph {
constructor(totalNodes) {
this.nodes = totalNodes;
}
initialize() {
for (let row = 0; row < this.nodes; row++) {
for (let column = 0; column < this.nodes; column++) {
}
}
}
}
display() {
}
}

const totalNodes = 6
let graph = new Graph(totalNodes);
graph.initialize();
graph.display();

graph.display();

/*
OUTPUT:

[
[ 0, 1, 0, 1, 1, 0 ],
[ 1, 0, 1, 1, 0, 0 ],
[ 0, 1, 0, 1, 0, 1 ],
[ 1, 1, 1, 0, 1, 1 ],
[ 1, 0, 0, 1, 0, 1 ],
[ 0, 0, 1, 1, 1, 0 ]
]
*/
`````` An adjacency list is the list of adjacent nodes(nodes that shares an edge with another node). Node "B" & Node "D" shares and edge with Node "A". so the adjacency list of Node "A" is `[B, D]`

``````class Graph {
constructor() {
}
initialize(nodes) {
for(let node of nodes){
}
}
}
}
display() {
for (let [key, value] of this.adjList) {
console.log(`\${key}: \${value}`);
}
}
}

let nodes = ["A", "B", "C", "D", "E", "F"];

let graph = new Graph();

graph.initialize(nodes);

graph.display();

/*
OUTPUT:

A: E,D,B
B: A,D,C
C: B,D,F
D: A,B,C,E,F
E: A,D,F
F: E,D,C

*/
``````