Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week8 #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Week-8/allPathFromSourceToTarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
let v;

let adjList;

// A directed graph using
// adjacency list representation
const Graph = (vertices) => {
// initialise vertex count
v = vertices;

// initialise adjacency list
initAdjList();
}

// utility method to initialise
// adjacency list
const initAdjList = () => {
adjList = new Array(v);

for (let i = 0; i < v; i++) {
adjList[i] = [];
}
}

// add edge from u to v
const addEdge = (u, v) => {
// Add v to u's list.
adjList[u].push(v);
}

// Prints all paths from
// 's' to 'd'
const printAllPaths = (s, d) => {
let isVisited = new Array(v);
for (let i = 0; i < v; i++)
isVisited[i] = false;
let pathList = [];

// add source to path[]
pathList.push(s);

// Call recursive utility
printAllPathsUtil(s, d, isVisited, pathList);
}

// A recursive const to print
// all paths from 'u' to 'd'.
// isVisited[] keeps track of
// vertices in current path.
// localPathList<> stores actual
// vertices in the current path
const printAllPathsUtil = (u, d, isVisited, localPathList) => {
if (u == (d)) {
console.log(localPathList + "\n");
// if match found then no need to
// traverse more till depth
return;
}

// Mark the current node
isVisited[u] = true;

// Recur for all the vertices
// adjacent to current vertex
for (let i = 0; i < adjList[u].length; i++) {
if (!isVisited[adjList[u][i]]) {
// store current node
// in path[]
localPathList.push(adjList[u][i]);
printAllPathsUtil(adjList[u][i], d,
isVisited, localPathList);

// remove current node
// in path[]
localPathList.splice(localPathList.indexOf
(adjList[u][i]), 1);
}
}

// Mark the current node
isVisited[u] = false;
}

// Driver program
// Create a sample graph
Graph(4);
addEdge(0, 1);
addEdge(0, 2);
addEdge(2, 3);
addEdge(1, 3);

// arbitrary source
let source = 0;

// arbitrary destination
let destination = 3;

console.log(
"Following are all different paths from "
+ source + " to " + destination + "\n");
printAllPaths(source, destination);

// Time complexity O(V^V)
// Space complexity O(V^V)
78 changes: 78 additions & 0 deletions Week-8/findIfPathExistsInGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// This class represents an bi-directional graph
// using adjacency list representation
let V; // No. of vertices

// Pointer to an array containing adjacency lists
let adj;
V = 3;
adj = new Array();
for (let i = 0; i < V; i++)
adj.push(new Array());


// const to add an edge to graph
const addEdge = (v, w) => {
adj[v].push(w);
adj[w].push(v);
}

// A BFS based const to check whether d is reachable from s.
const isReachable = (s, d) => {
// Base case
if (s == d)
return true;

// Mark all the vertices as not visited
let visited = new Array(V).fill(false);


// Create a queue for BFS
let queue = new Array();

// Mark the current node as visited and enqueue it
visited[s] = true;
queue.push(s);

while (queue.length != 0) {

// Dequeue a vertex from queue and print it
s = queue.pop();

// Get all adjacent vertices of the dequeued vertex s
// If a adjacent has not been visited, then mark it
// visited and enqueue it
for (let i = 0; i < adj[s].length; i++) {

// If this adjacent node is the destination node,
// then return true
if (adj[s][i] == d)
return true;

// Else, continue to do BFS
if (!visited[adj[s][i]]) {
visited[adj[s][i]] = true;
queue.push(adj[s][i]);
}
}
}

// If BFS is complete without visiting d
return false;
}

// Driver program

// Create a graph given in the above diagram
addEdge(0, 1);
addEdge(0, 2);
addEdge(1, 2);
addEdge(2, 0);

let source = 0, destination = 2;
if (isReachable(source, destination))
console.log("\n There is a path from " + source + " to " + destination);
else
console.log("\n There is no path from " + source + " to " + destination);

// Time complexity O(V+E)
// Space complexity O(V)
43 changes: 43 additions & 0 deletions Week-8/findTheTownJudge.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const findJudge = (N, trust) => {
// keep track of how many likes the element gives
let likesCountList = {}
//keep track of how many likes the element receives
let beingLikedCountList = {}

//hash the key from 1 to N
for (let i = 1; i <= N; i++) {
likesCountList[i] = 0
beingLikedCountList[i] = 0
}

//loop through trust to hash value to hashes
for (let ele of trust) {
likesCountList[ele[0]]++
beingLikedCountList[ele[1]]++
}

//variable to store potential judge

let judge = 0
//if the element doesn't give out any likes
//it is the potantial judge
for (key in likesCountList) {
if (likesCountList[key] === 0) judge = key
}

//if the potential judge receives likes from everybody other than itself
//it means it is the judge
//otherwise judge doesn't exist

if (beingLikedCountList[judge] === N - 1) return judge
else return -1
};

// Driver Code
console.log(findJudge(2, [[1, 2]]))
console.log(findJudge(3, [[1, 3], [2, 3]]))



// Time complexity O(N)
// Space complexity O(N)
14 changes: 14 additions & 0 deletions Week-8/levelOrderTraversal_BT.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
let LevelOrder = function(root){
if(!root) return[];
const queue=[root];
const result=[];
while (queue.length>0){
let len = queue.length;
result.push(queue.map(node=>node.val))
while(len--){
let node = queue.shift();
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
}
} return result;
}
9 changes: 9 additions & 0 deletions Week-8/maxDepth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var maxDepth = function(root) {
if(!root){
return 0;
}
return Math.max(maxDepth(root.left),maxDepth(root.right)) + 1;
};

// Time complexity - O(N)
// Space complexity - O(N)
6 changes: 6 additions & 0 deletions Week-8/validateBinaryTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var isValidBST = function(root, min=null, max=null) {
if (!root) return true;
if (min && root.val <= min.val) return false;
if (max && root.val >= max.val) return false;
return isValidBST(root.left, min, root) && isValidBST(root.right, root, max);
};