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

There are two commonly used techniques for representing graphs

- Adjacency Matrix
- Adjacency List

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;
this.adjMatrix = [];
}
initialize() {
for (let row = 0; row < this.nodes; row++) {
this.adjMatrix.push([]);
for (let column = 0; column < this.nodes; column++) {
this.adjMatrix[row][column] = 0
}
}
}
addEdge(source, destination) {
this.adjMatrix[source][destination] = 1;
this.adjMatrix[destination][source] = 1;
}
display() {
console.log(this.adjMatrix);
}
}
const totalNodes = 6
let graph = new Graph(totalNodes);
graph.initialize();
graph.display();
graph.addEdge(0, 1);
graph.addEdge(0, 3);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(2, 3);
graph.addEdge(2, 5);
graph.addEdge(3, 4);
graph.addEdge(3, 5);
graph.addEdge(4, 5);
console.log('\nAdjacency Matrix')
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() {
this.adjList = new Map();
}
initialize(nodes) {
for(let node of nodes){
this.addNode(node);
}
}
addNode(node) {
this.adjList.set(node, []);
}
addEdge(source, destination) {
this.adjList.get(source).push(destination);
}
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.addEdge("A", "E");
graph.addEdge("A", "D");
graph.addEdge("A", "B");
graph.addEdge("B", "A");
graph.addEdge("B", "D");
graph.addEdge("B", "C");
graph.addEdge("C", "B");
graph.addEdge("C", "D");
graph.addEdge("C", "F");
graph.addEdge("D", "A");
graph.addEdge("D", "B");
graph.addEdge("D", "C");
graph.addEdge("D", "E");
graph.addEdge("D", "F");
graph.addEdge("E", "A");
graph.addEdge("E", "D");
graph.addEdge("E", "F");
graph.addEdge("F", "E");
graph.addEdge("F", "D");
graph.addEdge("F", "C");
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
*/
```

]]>```
var favoriteNumber int = 7
```

a variable of the name "favoriteNumber" of a type integer that stores 7.

similarly

```
var favoriteColor string = "black"
```

a variable of the name "favoriteColor" of a type string that stores "black".

pretty simple right? 🤜

Computer store variable in the memory as block and block has an address and value. we can access and manipulate that value by address and here comes the pointer in the picture.

Pointer is a type of variable that contains the address of another variable.

```
// integer type variable
var num int = 7
// address of num stored in pointer p
var p *int = &num
fmt.Printf(p) // output: 0xc000018030
// yours will different
// access value of num by address
fmt.Printf(*p) // output: 7
// manipulate the value of num by address
*p = 18
fmt.Printf(num) // output: 18
```

For me, the pointer was a mysterious 👁🗨 topic. Share your experience in the comments

]]>A Graph is a collection of nodes and edges in a way that nodes are connected by an edge.

*fig01*

Every individual point that holds or represents some kind of data is called vertex or node.

In *fig01* points "A", "B", "C", "D", "E" are vertex/node. node and vertex both are the same terms.

A connection between two nodes is called an edge. In fig01 the connection between node B and node E is edge similarly A-B, A-C, A-C, A-D, B-D, C-D, D-E are edges.

*fig02*

A node that shares a common edge with other nodes is called "Adjacent" node.

Adjacents nodes of "A" mean all the nodes that share common edges with node "A".

so, Adjacent nodes of node "A" are "B", "D", "C".

Adjacent of all nodes respectively.

Nodes | Adjacents | ||

A | BDC | ||

B | ADE | ||

C | AD | ||

D | CABE | ||

E | BD |

*fig03*

The degree is the number of edges connected to a node.

for example, in *fig03* the node "D" has a degree of 4 while the "E" has a degree of 2.

*fig04*

**Disconnected graph**

In a disconnected graph, not all nodes have edges. nodes might be isolated.

If you see the above disconnected graph, there are three isolated regions. these three regions don't have a connection between them.

**Connected graph**

A graph is connected if all nodes have at least one edge.

**Undirected graph**

An undirected graph has no direction. The edges indicate a two-way relationship, edges can be traversed in both directions.

A good example would be a Facebook friend, When you accept a friend request a connection between you and your friend is undirected.

**Directed graph**

A directed graph has edges with direction. The edges indicate a one-way relationship, in that each edge can only be traversed in a single direction.

A good example would be Twitter, Instagram where either your friend follows you or you follow your friend or you both follow each other.

**Complete graph**

A graph is said to be complete if each node have a degree of n-1(n = total nodes)

sound like mathematics, in simple words

A graph is complete if each node has an edge with all other nodes except itself.

**Cyclic graph**

A graph can have cycles which means if you traverse through the node, you could get the same node more than once.

**Acyclic graph**

A graph is acyclic that means the graph must have at least one node with no targets (called a leaf).

in *fig04* acyclic graph, the orange node doesn't have any outgoing edge.

- Social media like Facebook, Linked In, Twitter, Instagram uses graphs that store users, groups, check-ins, likes, and more as nodes. -Google Maps, Apple Maps, Waze use graphs to treat all cities and places as nodes and routes between them as edges.
- Webgraph describes a directed graph between pages of the WWW. Each page is a vertex and the hyperlinks are edges. This is the basic idea behind the Google Page Ranking Algorithm.
- Uber, Ola, Lyft uses a graph to find the shortest and cheapest path for a car from one city to another.
- The graph is also used in a database for representing Entity-Relationship.
- Graph theory is also used to study molecules in chemistry and physics.

]]>

**Repositree** is a GitHub repository or file explorer that helps to navigate the files like your IDE.

**Features**

- IDE-like code tree for GitHub (Public and Private) repositories.
- Change the sidebar location to the right or left of the screen.
- Resize sidebar.

**Upcoming Features**

- Github Dark Mode
- File icons
- and many more

:D

]]>Generate auth URL

Get auth token

Get user info

So basically, it's just a wrapper that wraps all complex parts of google-auth-library. you can say it's a facade for google-auth-library.

Talk is cheap. Show me the code

```
const express = require('express');
const { GoogleAuth } = require('google-auth-simplify');
const app = express();
const googleAuth = new GoogleAuth({
clientId: GOOGLE_CLIENT_ID,
clientSecret: GOOGLE_CLIENT_SECRET,
redirectURI: GOOGLE_REDIRECT_URI,
});
app.get('/login', (req, res) => {
const authUrl = googleAuth.generateAuthUrl();// STEP 1
res.redirect(authUrl);
});
app.get('/google_redirect_url', async (req, res) => {
const code = req.query['code'];
const authToken = await googleAuth.getAuthToken(code);// STEP 2
const profile = await googleAuth.getUserInfo(authToken);// STEP 3
res.json({profile});
});
app.listen(3000, () => {
console.log('server started:.:.:');
});
```

I think it's easy to understand.

have any questions? let me know in the comments :D

BTW here is the repo: google-auth-simplify

]]>I have to just type `node project.js`

```
const { exec } = require('child_process');
const fullpath = '/desire/path';
main();
function main() {
launchVSCodeAt(fullpath);
launchTerminalAt(fullpath);
}
function launchVSCodeAt(path) {
exec(`cd ${path} && code .`, (err) => {
if (err) console.log(`failed to launch VSCcode at ${path}`);
console.log(`VSCode launched at ${path}`);
});
}
function launchTerminalAt(path) {
exec(`cd ${path} && gnome-terminal`, (error) => {
if (error) console.log(`failed to launch Terminal at ${path}`);
console.log(`Terminal launched at ${path}`);
});
}
```

**What you automate? Let me know in the comments.**

BTW I am new to HashNode.

]]>