Graphs
Lets make an abstraction: a set of nodes connected with edges
This is a graph !
A Graph consists of a finite set of vertices(or nodes) and set of Edges which connect a pair of nodes.
The best way of drawing subway maps can be easily solved using graph theory.
Most widely used chemical models are graph models
Facebook (and almost all other social networks) model the users and their connections with their friends in a graph structure. And, they use this graph model to do their business by applying graph theory ideas, including how to populate your timeline, what advertisements to show, etc.
Google, uses a graph representation to store/retreive the semantic relationship between different entities, which they call it, Knowledge Graph, as well as providing you with the top relevant webpages for your query, using PageRank algorithm, which is a graph theorethic algorithm.
including the ones used by Amazon for recommending you the relevant books to buy, or Netflix for recommending you what to watch use graph theoretic algorithms.
Your entire Internet works on the principle of Graph Theory.
In directed graphs, edges point from the node at one end to the node at the other end. In undirected graphs, the edges simply connect the nodes at each end.
graph is cyclic if it has a cycle—an unbroken series of nodes with no repeating nodes or edges that connects back to itself. Graphs without cycles are acyclic.
If a graph is weighted, each edge has a "weight." The weight could, for example, represent the distance between two locations, or the cost or time it takes to travel between the locations.
Lots of graph problems can be solved using just these traversals:
Is there a path between two nodes in this undirected graph?
Run DFS or BFS from one node and see if you reach the other one.
What's the shortest path between two nodes in this undirected, unweighted graph?
Run BFS from one node and backtrack once you reach the second. Note: BFS always finds the shortest path, assuming the graph is undirected and unweighted. DFS does not always find the shortest path.
Does this undirected graph have a cycle?
Run BFS, keeping track of the number of times we're visiting each node. If we ever visit a node twice, then we have a cycle.
Adjacency list
Adjacency matrix
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j
An array of lists is used. Size of the array is equal to the number of vertices. Let the array be array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This representation can also be used to represent a weighted graph. The weights of edges can be represented as lists of pairs. Following is adjacency list representation of the above graph.
Adjacency Matrix
Adjacency List
class Graph {
constructor() {
// implement this
}
addVertex(name) {
// implement this
}
addEdge(v1, v2) {
// implement this
}
removeEdge(v1, v2) {
// implement this
}
removeVertex(v) {
// implement this
}
DFS(from = '') {
// implement this
}
hasPath(from, to) {
// implement this
}
}
const g = new Graph();
g
.addVertex('A')
.addVertex('B')
.addVertex('C')
.addVertex('D')
.addVertex('F')
.addEdge('A', 'B')
.addEdge('A', 'C')
.addEdge('B', 'C')
.addEdge('C', 'D')
.addEdge('D', 'A')
.addEdge('D', 'F');
// g.removeVertex('C');
// console.log(g.DFS('A'));
// console.log(g.hasPath('A', 'C'));
// console.log(g.hasPath('A', 'F'));
let us recap previous topics
1
1. O(2^n)
2. O(n^2)
2
1. O(2^n)
2. O(n!)
3
4
5
6
7
9
10
function log(n) {
for (var i = 1; i <= Math.min(n, 100); i++) {
console.log(i);
}
}
11
function evenElementsArray(array) {
const newArray = Array(Math.ceil(array.length / 2));
for (var i = 0; i < array.length; i++) {
if (i % 2 === 0) {
newArray[i / 2] = array[i];
}
for (var j = 0; j < i; j++) { console.log(“hello”); }
}
return newArray;
}
12
Given an array of integers of size ‘n’. Our aim is to calculate the maximum sum of ‘k’ consecutive elements in the array.
Input : arr = [100, 200, 300, 400]
k = 2
Output : 700
13
1. O(n)
2. O(n log(n))
3. O(log(n))
14
1. Linear search
2. Binary search
15
1. Linear search
2. Binary search
16
1. O(n^2)
2. O(n log(n))
17
1. O(n^2)
2. O(n log(n))
18
19
20
20
21
22
1. O(n)
2. O(1)
3. O(log(n))
23
24
25
1. Stack
2. Queue
26
27
28
29
1. O(n)
2. O(1)
30
31
32
33
OK we are good