r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

31

u/[deleted] Apr 05 '22

Advanced algorithm analysis and probabilistic algorithms (Las Vegas and Monte Carlo algorithms). First one because i find the subject boring and the second, because i struggle with probability theory and stochastic processes.

66

u/procrastinatingcoder Apr 05 '22

Pohl Ira has a great way of vulgarizing the Monte Carlo algorithm, or well, the general idea behind it. So I'll just be re-using his explanation.

Let's say you were playing a game of chess. You want a computer to do "optimal" moves. So you try to calculate every possible game. This whole thing goes out of control very fast. It's trivial for the first few moves, there's not that many options, but it multiplies very very fast.

Two little concepts:

  • The question changes to "What's a good move". We'll define a "good move" as a move that gives you more chances of winning than losing.

  • If two people of identical skill level play, you'd expect that over a thousand or ten thousand games, the victories would be split 50/50 roughly.

Now, identical skill level can mean both masters or... both complete and utter idiots. So let's say the computer plays both sides of the game, but instead of calculating anything, it plays completely randomly . So both sides play completely randomly, which is an "identical skill level". Where over a thousand games, you'd have roughly 500 wins and 500 loss for each player.

How does this help us? Well, let's say that instead of playing completely randomly, the computer plays completely randomly EXCEPT the first move. Which is always the same for one of the players. If that player "wins" more than a rough 500/500, then you could assume that move gave some sort of advantage to that player. And the more skewed it is, the "better" the move.

And while calculating every possibility is incredibly demanding, playing randomly is very easy for a computer.

So the computer just does exactly that. It picks a "first move", does a thousand games, sees if it wins or loses more. Then it tries another "first move", and does that for a bunch of them, then it takes the one that seems the most "advantageous".

So nothing is set in stone, everything is random, you have no idea why it works, but your algorithm gives you a "good move".

In general though, Monte Carlo just talks about trying and experiment multiple times, and using those results to make a decision of some kind. In a trivial way, you could roll a 6-sided dice 1 million times. And then if you were to bet on the next number, just pick the one that had the highest count when you did your experiment.

Note that the dice might seem not to make sense, all sides are equals. But think on it for a second, are all dices perfect what if it was slightly worn, and that it had a very very very small imbalance that gave a bit bigger odds that it would land on a particular side. Testing it with a million rolls would give you a good idea, which is also easier than trying to do a perfect physics simulation of the die.

As for heuristic algorithms in general, I think the primality test is a fairly easy one to understand, but I've already written a wall of text here.

6

u/Third_Party_Opinion Apr 05 '22

This was cool to read, thank you for the effort.