Coding, Computer Science, Crafty, Geography, Murderous Maths, Notes on everyday life, Simon's sketch book

Pathfinding algorithms: Dijkstra’s and Breadth-first search

The photos below show Simon playing with Breadth-first search and Dijkstra’s algorithms to find the most efficient path from S to E on a set of graphs. The two more complex graphs are weighed and undirected. To make it more fun, I suggest we pretend we travel from, say, Stockholm to Eindhoven and name all the intermediate stops as well, depending on their first letters. And the weights become ticket prices. Just to make it clear, it was I who needed to add this fun bit with the pretend play, Simon was perfectly happy with the abstract graphs (although he did enjoy my company doing this and my cranking up a joke every now and then regarding taking a detour to Eindhoven via South Africa).

this was an example of how an algorithm can send you the wrong way if it has data of the “right” way being weighted more (due to traffic jams, for example)
Coding, Community Projects, Computer Science, Contributing, JavaScript, Murderous Maths, Physics, Simon's Own Code, Simon's sketch book

Simon’s Community Contribution: Variation of 2D Casting Coding Challenge in p5.js

This is Simon’s version of Daniel Shiffman’s 2D Casting code, made on Wednesday last week right after the live session. Link to the live session including the coding challenge.

Code and interactive animation are online at: https://editor.p5js.org/simontiger/sketches/ugHX4yKQC
Play with the animation online at:
https://editor.p5js.org/simontiger/present/ugHX4yKQC
https://editor.p5js.org/simontiger/full/ugHX4yKQC

Simon’s suggestions during a patron-only live session yesterday
a screenshot of Simon’s community contribution published on the Coding Train website

Simon has also made one more, optimized version of this project (with fewer rays, runs faster): https://editor.p5js.org/simontiger/present/F6TCHAZs_
https://editor.p5js.org/simontiger/sketches/F6TCHAZs_

Both of Simon’s versions have been added to the community contributions on the Coding Train website: https://thecodingtrain.com/CodingChallenges/145-2d-ray-casting.html

screenshot of the optimized version
Coding, JavaScript, Machine Learning, Milestones, Murderous Maths, neural networks, Notes on everyday life, Set the beautiful mind free, Simon teaching, Simon's Own Code, Simon's sketch book

What Simon did instead of taking the SAT on Saturday

On Saturday morning, Simon didn’t go to the SAT examination location, although we had registered him to try taking the SAT (with great difficulties, because he is so young). In the course of the past few weeks, after trying a couple of practice SAT tests on the Khan Academy website, we have discovered that the test doesn’t reveal the depth of Simon’s mathematical talent (the tasks don’t touch the fields he is mostly busy with, like trigonometry, topology or calculus and require that instead, he solves much more primitive problems in a strictly timed fashion, while Simon prefers taking time to explore more complex projects). This is what happens with most standardized tests: Simon does have the knowledge but not the speed (because he hasn’t been training these narrow skills for hours on end as his older peers at school have). Nor does he have the desire to play the game (get that grade, guess the answers he deosn’t know), he doesn’t see the point. What did he do instead on his Saturday? He had a good night sleep (instead of having to show up at the remote SAT location at 8 a.m.) and then he…

built an A.I. applying a genetic algorithm, a neural network controlling cars moving on a highway! The cars use rays to avoid the walls of the highway. Implementing neuroevolution. What better illustration does one need to juxtapose true achievement and what today’s school system often wants us to view as achievemnt – getting a high grade on a test? The former is a beautiful page from Simon’s portfolio, showing what he really genuinely can do, a real life skill, something he is passionately motivated to explore deeper, without seeking a reward, his altruist contribution to the world, if you will. The latter says no more than how well one has been trained to apply certain strategies, in a competitive setting.

Simon’s code is online: https://repl.it/@simontiger/Raytracing-AI

Simon has put this version on GitHub: https://github.com/simon-tiger/Raycasting-A.I.

He has also created an improved version with an improved fitness function. “In the improved version, there’s a feature that only shows the best car (and you can toggle that feature on and off). And most importantly, I am now casting relative to where it’s going (so the linearity is gone, but it jiggles a lot, so I might linear interpolate it)”, – Simon explains. You can play with the improved version here: https://repl.it/@simontiger/Raycasting-AI-Improved

Finally, Simon is currently working on a version that combines all the three versions: the original, the improved and the version with relative directions (work in progress): https://repl.it/@simontiger/Raytracing-AI-Full

“I am eventually going to make a version of this using TensorFlow.js because with the toy library I’m using now it’s surprisingly linear. I’m going to put more hidden layers in the network”.

The raytracing part of the code largely comes from Daniel Shiffman.

Simon’s two other videos about this project, that was fully completed in one day:

Part 1
Part 2


Coding, Computer Science, Contributing, Group

Example of Simon contributing an issue on GitHub

Below is Simon’s issue/ topic suggestion he contributed to the Coding Train GitHub yesterday, addressed to Daniel Shiffman:

Function Overloading

Nah.

Operator Overloading

OK, first, we have to understand the valueOf() function. valueOf() is a function, that typically performs on a string, that converts it into a “primitive” data type.

A number is a primitive data type, because it’s not an object. And, you can see that by opening a Terminal window, running node and type:

> 2 + 2 // and get
4

Because JS doesn’t have any “real” overloading, you can’t put + in between two objects, in between two objects, and get an answer!

Well, with one slight exception, which is the valueOf() function. It will tell JS how to interpret an instance of an object as a primitive data type, whenever you want to add them together or something. If you put a valueOf() function into the prototype (or class, if you will) of an object, and it converts it into a number, or some other primitive data type, you can suddenly do arithmetic on these objects!

This does have a limitation, though. Say you write a class:

class Vector {
  constructor(x=0, y=0) {
    this.x = x;
    this.y = y;
  }

  // Maybe you want a valueOf() function to add them together
  valueOf() {
    // TL;DR What goes here???
  }
}

Maybe you want to add the individual components of a vector to add two vectors together.
It turns out, the answer is, nothing can go in the place of the question mark!

Here’s why: There’s no real primitive data type, with further components. That’s because that’s what a primitive data type is! It doesn’t have data, it is it’s own data! A Vector does have further components, however, and so we arrive at a contradiction. Because a vector has further components, and valueOf() must return something without further components, this situation is impossible!

String Overloading

Well, it’s really the same as Operator Overloading, except you need to use the toString() function instead of the valueOf() function. It converts it into a string. I don’t really know why you’d want to do this, maybe you would affect how you print it. I actually don’t even know if toString() can do this.

[TL;DR] Overloading

Look at this messy video about Matrix Math that you made: https://www.youtube.com/watch?v=NgZAIkDcPkI, at 23:15.

Guess what, there’s a way to fix that!

I don’t know how this one is called. When you say this, it’s an object. But, because objects are associative arrays, you can actually treat this as an associative array! Something like this:

class Matrix {
  constructor(rows, cols) {
    this.rows = rows;
    this.cols = cols;
    // this.data = []

    for (let i = 0; i < this.rows; i++) {
      this[i] = []; // OMG
      for (let j = 0; j < this.cols; j++) {
        this[i][j] = 0; // OMG
      }
    }
  }
  // .
  // .
  // .
}

It’s as easy as that!
If you want to, you could even add a this.length variable, and suddenly, you would even be able to iterate over it, with a for..of loop!

Coding, Community Projects, Computer Science, Contributing, Geometry Joys, Group, Milestones, Murderous Maths, Notes on everyday life

More examples of Simon’s chat contributions on math and coding

Simon is always extremely active in the discussions about the current projects made by/ lectures given by NYU’s Asdociate Professor Daniel Shiffman during his live sessions on the Coding Train channel. He also enjoys “initiating discussions” among the channel’s patrons (grown-up programmers) and Daniel. “Mom, the discussion I initiated is still going on!” I couldn’t possibly post all the coding and math comments/ suggestions that Simon makes in the chats on YouTube, Slack and GitHub (and I don’t believe I should either), but every now and then, I like collecting samples of Simon’s contributing to the discussion:

Simon contributing to a discussion prior to a live session on ray tracing
Simon contributing to Daniel Shiffman’s tutorial on the computational geometry “minimum spanning tree” problem

The small font above says:

Correction: The MST problem does not allow any loops (like A->B, B->C, C->D, D->A again.) So the solution at 2:30 is wrong! In fact, _no wonder it does that_, because Prim’s Algorithm will never find a loop. Here’s why:

Let’s suppose that it could find a loop (let’s say, a loop of 4, so A->B, B->C, C->D, D->A again, but this argument would work the same each way.) Ok, so it will start from A, and mark it as reached. It will check A against B, C and D, find B, and mark B as reached. Then, it will check A against C and D, and B against C and D. and it will find that it should connect B and C, and mark C as reached. Then, it will check A, B and C all against D, and find that it should connect C and D, and mark D as reached. But now, we reach a problem. It will not connect D and A, because both are already reached!

Why was it designed like that? Because that’s what the problem says! It’s a Minimum Spanning _Tree_, so it can’t have any loops.

So there you go, that’s why Prim’s algorithm will not find a loop.

Coding, Computer Science, Contributing, Group, Milestones

Simon’s Computer Science Algorithm Suggestions

Simon’s suggestion for the Coding Train on GitHub:

Because I like computer science these days, here are some computer science algorithm suggestions:

  • Data Structures
    • Array
    • Linked List
    • Hash Table
    • Stack
    • Queue
    • Priority Queue (Binary heap)
    • Suffix Array
    • Graph Theory
      • Graph (general)
      • Tree
      • Binary Tree
        • Full vs. Complete
        • BST
        • Binary Heap
        • AVL Tree
        • Red-Black Tree
        • Segment Tree
        • DFA
      • Biparite
      • UFDS
      • Fenwick Tree
      • Min Spanning Tree
      • Suffix Tree
    • Computational Geometry
      • Polygon, etc.
  • Algorithms
    • Shuffling
      • Fisher-Yates Algorithm
    • Sorting
      • Bubble Sort
      • Selection Sort
      • Insertion Sort
      • Merge Sort
      • Quicksort
      • Counting Sort
      • Radix Sort
      • More?
      • Even More? (Scroll down to see a GIANT table)
    • Traversal / Pathfinding / SSSP
      • Basic
        • Pre-order
        • In-order
        • Post-order
        • Depth-first
        • Breadth-first
        • More?
      • Shortest Pathfinding
        • Dijkstra’s Shortest Path
        • A*
      • More?
      • Even More?
    • Substring Finding
    • Min Spanning Tree
      • Brute-force
      • Kruskal’s Algorithm
      • Prim’s Algorithm(s)
    • Max-flow
    • Graph Matching
    • Cycle Finding
    • Convex Hull
      • Gift Wrapping
      • Graham Scan
      • Quickhull
      • “The Ultimate Planar Convex Hull Algorithm”
      • More?
    • Min Vertex Cover
      • Brute-force
      • MVC
      • Approximation
    • Traveling Salesman
      • Brute-force
      • Dynamic Programming
      • Approximation
    • Steiner Tree

If you’re brave, Do you want more even after all of this?
One last note: you’re not going to do all of them (probably!).

Coding, Computer Science, Experiments, Geometry Joys, Milestones, Murderous Maths, Simon's Own Code

Experimenting with random walks in Wolfram Mathematica

Simon’s code is published online at:
https://www.wolframcloud.com/objects/monajune0/Published/Random_walk_distribution.nb
https://www.wolframcloud.com/objects/monajune0/Published/Random_walk_distribution2D.nb

Experimenting with random walks in Wolfram Mathematica

“If I take many random walks and see what the endpoints of those random walks are, what I’ll find is a Gaussian distribution!” Simon says. In the video, he programs 1D and 2D random walks and 2D and 3D histograms to show the distribution of the endpoints​ in Wolfram Mathematica.

Coding, Computer Science, Crafty, Geography, Geometry Joys, Murderous Maths, Simon's sketch book

Dijkstra’s pathfinding algorithm

“I have first built a maze, then I turned it into a graph and applied Dijkstra’s pathfinding algorithm!”

a maze to which Dijkstra’s pathfinding algorithm is applied

Simon learned this from the Computerphile channel. He later also attempted to solve the same maze using another pathfinding algorithm (A-Star).

Coding, Computer Science, Milestones, Murderous Maths, Python, RaspberryPi, Simon teaching, Simon's Own Code, Simon's sketch book

More Sorting Algorithms!

An update to Simon’s new project: a series of video tutorials on sorting algorithms! See the full playlist here.

Part 7: Heapsort

Finally, parts 6 and 7 of Simon’s exciting series of video tutorials about sorting algorithms are done! In the videos, Simon codes on his RaspberryPi, but here is the link to the Python code (parts 6 – 7) available on his GitHub page:
https://gist.github.com/simon-tiger/be3864b36f6d89fecd06f150063a6321

Part 6: Shellsort

The code of the sorting algorithms discussed in the previous videos (parts 1 – 5) is available here: https://gist.github.com/simon-tiger/5be70247a066f69c2578be5bb8e41e59

Simon wrote the Shellsort code himself. He tried to run his own code for Heapsort as well, but didn’t get the list fully sorted, so in the end he implemented the heapsort code that he learned from Brilliant.

“Then, with VERY much relief, I MASSIVELY condensed the code (to just 3 lines!), using Zax Rosenberg’s blog“, Simon adds.