Skip to content

Commit c95e65a

Browse files
committed
updates
1 parent 77703de commit c95e65a

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

graphs/bfsScript.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Breadth First Search
2+
3+
As the name implies, breadth first search is a graph search algorithm that starts at one node, explores as widely as it can first, before going further down adjacent nodes.
4+
5+
We'll add a breadthFirstSearch method to our graph object. We want to accept two arguments, a startingNodeKey to find which node in our graph to start from, and a visitFunction to call when we "visit" each node.
6+
7+
We'll start by getting our starting node using the this.getNode method.
8+
9+
Next, we need to keep track of which nodes we have visited and which ones we haven't. There are several ways to do this, but I'm going to do it through an object. I'm going to reduce our nodes down to an object where each key is the current node's key, and the value is set to false.
10+
11+
Next, we need to keep track, in order, which nodes we need to visit. We'll use a createQueue function I've imported from another lesson. The first node we need to visit is our startingNode.
12+
13+
Now, we want to start at our startingNode, so we will dequeue it from the queue and label it our current node. If we haven't visited this node before, we want to run the visiting function on it, and mark it as visited in our visited hash. Since this is our first node, we know that we'll hit this condition.
14+
15+
Next, if our currentNode has any neighbors, we want to add them to our queue. While the queue is not empty, we want to continue dequeueing items and performing this algorithm, so we'll use a while loop. The while loop stops when we've visited every node in our graph.
16+
17+
Now that we've implemented breadth first search, let's try it out on a graph.
18+
19+
I've premade a graph for us to use that looks like this that I'l copy paste into here. I want to use breadthFirstSearch to print out the node keys as we arrive at each one into the console.
20+
21+
If I bring the graph back onto the screen and compare it to the result in the terminal, we can see how we branched out from node A and visited all of its neighbors before proceeding down node B's neighbors.

graphs/dfsScript.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Depth First Search
2+
3+
As the name implies, depth first search is a graph search algorithm that explores as far as it can down a particular path before climbing back up working down another one.
4+
5+
we'll add a depthFirstSearch method to our graph object. This method will receive two arguments, a startingNodeKey to find which node in the graph to start the search from, and a visiting function to be called as we visit each node for the first time.
6+
7+
First, we need to get the starting node by using the getNode method
8+
9+
Next, we need to keep track of which nodes we have visited and which ones we havent. There are several ways to do this, but I'm going to do it by keeping a visited object, where each key corresponds with a node's key, and the value starts as false.
10+
11+
Now, depth first search involves a recursive algorithm, essentially if there's another level to go, explore that one, until we reach a dead end.
12+
13+
I'm going to create an explore function that we will call on nodes. If we've visited this node, then return from the function immediately. Otherwise, call the visiting function on the node, mark it as visited, and then for each of its neighbors we need to explore deeper. Because we have our guard statement at the start of our function, we can safely call it on each node, knowing that if we've visited it, nothing will be returned.
14+
15+
We then start our algorithm by calling explore on our starting node.
16+
17+
Now that we've created our depth first search algorithm, let's use it.
18+
19+
I've premade a graph that I will copy paste into the file that looks like this.
20+
21+
I want to call depthFirstSearch on our graph, starting at node 'a' and logging out each key as we go. If I bring the graph back onto the screen and we call our searchin the terminal, we can see that went as far as we could down the path of A before coming back up and following another path.

graphs/script.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Graphs
2+
3+
A graph is a collection made up of nodes, also known as vertices, that may or may not be connected to other nodes. These connections are called edges.
4+
5+
To build our graph, we're going to start by creating our node factory function.
6+
7+
When we create a node, we need to give it a value that we can use to identify it, we'll call it a key. We'll describe adjacent nodes in our graph as neighbors, as there is no hierarchy implied by the data structure. Lastly, we need a way to add neighbors to our node, so we'll add an addNeighbor method that pushes a node into our neighbors array.
8+
9+
Now, we can create our graph factory function. createGraph receives on argument, directed. A directed graph's edges point in one direction from a node to another. In order for two nodes to have symmetric edges, they must both point to each other.
10+
11+
In an undirected graph, we assume this symmetry of edges, that when one node points to the other, then the opposite is true as well.
12+
13+
We'll set directed to false by default, and we'll return in it the object created by our factory.
14+
15+
Now, a graph is a collection of nodes and a collection of edges, so we'll create arrays for both in closure. We'll pass these references to our returned object as well.
16+
17+
Now we'll start adding methods to our graph. The first will be the addNode method. Add node receives a key as an argument and uses our createNode function to add a node to the nodes array.
18+
19+
We also want to be able to get nodes from our graph, so we'll add a getNode method. getNode will search for a matching key and return the first one, so we'll use array's find method.
20+
21+
Next, we need to be able to add edges between nodes, so we'll create an addEdge method. addEdge receives two arguments, node1Key, and node2Key. We use our getNode function to get these two nodes. We then will add node2 as a neighbor to node1. This happens regardless of whether the graph is directed or undirected. The same goes for pushing our edge into the edges array. We'll simply pass a string of our two keys, adding a delimiter between them.
22+
23+
Now, If the graph is undirected, we also want to add node1 as a neighbor of node2. We don't worry about adding a second edge because we don't want the number of edges in our graph to be misrepresented. If we were to add an edge here, we'd have to implement a way of resolving both edges as one were we ever to count them and provide a edgesLength property.
24+
25+
Now that we can add nodes and edges, we'd probably like to be able to visualize our graph. We'll create a print method that will print out our nodes and neighbors in the console.
26+
27+
We want to return a string derived from our nodes. So I'm going to map over our nodes, gather some data about them and return a result.
28+
29+
I'll destructure the key and neighbors from each node. The string we'll return will begin with the key. Then if there are any neighbors, we want to concatenate that to our current result. We'll map over each neighbor and add its key. Lastly, we'll return the result and call a join with a newline on our array of strings.
30+
31+
Now, we can create a graph and try it out.
32+
33+
I'm going to create a directed graph of the Shevlin family, me my wife and our two cats Krios and Tali. This graph we'll describe who loves whom in this household.
34+
35+
Now, my wife and I love each other, so we can add an edge between us going in both directions.
36+
37+
And we love our cats, her a tad more than me, I'm a dog person, but we have no way of weighting our edges. So we'll add edges from me to both our cats and from Anna to both our cats.
38+
39+
Now, our cats have very little love for each other. They tolerate one another, but they do fight from time to time, no edges there, BUT Krios loves Anna, and Tali displays some affection towards me.
40+
41+
Now, if we call print on our graph, we'll get a read out of our relationships to each other.

quickSort/script.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Quick Sort
2+
3+
Quick Sort is a recursive sorting algorithm. We take our original array, breaking it down into smaller arrays to sort. In particular, quick sort uses the concept of a "pivot". We pick one item, it could be at the head or the tail of the array, so long as it's consistent. We then compare each item to that pivot, if it's less than the pivot, we push it to a left array, if it's more we push it to the right array. We then return the array that is the quick sort called on the left, the pivot, and quick sort called on the right.
4+
5+
We'll make a function to create our algorithm. Our function takes an array as an argument and will return an array. In this case, we'll return a new array of sorted items, so for now we'll return an empty array.
6+
7+
Next, since we know we will call quickSort recursively, we need to establish our base case to prevent a stack overflow. If the array length is less than two, we want to return that array.
8+
9+
Now, we want to establish our pivot, we'll use the last item in the array as our pivot. Since I'll need the pivot index later in the algorithm, I'm going to store that as a variable and derive the pivot from that.
10+
11+
Now, I'll create empty arrays for what will be our left and right arrays.
12+
13+
Now, we want to loop through every item in the array up to the pivot, hence why we stored the pivotIndex. We'll store the current item in a variable. Now, if the currentItem is less than the pivot, push it into the left array, otherwise into the right array.
14+
15+
When our loop is done, we now want to call quick sort recursively on our left and right arrays, placing our pivot in the middle.

stacks/yakstack.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Yak Shaving Stack
2+
3+
const { createStack } = require('./index')
4+
5+
const yakStack = createStack()
6+
7+
yakStack.push('small bug')
8+
yakStack.push('fix models')
9+
yakStack.push(
10+
'fork framework, fix, make pull request'
11+
)
12+
yakStack.push(
13+
'fundamental problem in your programming language'
14+
)
15+
yakStack.pop()
16+
yakStack.pop()
17+
yakStack.pop()
18+
19+
console.log(yakStack.peek())

0 commit comments

Comments
 (0)