Intro to Algorithms: Crash Course Computer Science #13 — Transcript
Full transcript
- 0:03Hi, I’m Carrie Anne, and welcome to CrashCourse Computer Science!
- 0:05Over the past two episodes, we got our first taste of programming in a high-level language,
- 0:10like Python or Java.
- 0:11We talked about different types of programming language statements – like assignments,
- 0:14ifs, and loops – as well as putting statements into functions that perform a computation,
- 0:18like calculating an exponent.
- 0:20Importantly, the function we wrote to calculate exponents is only one possible solution.
- 0:24There are other ways to write this function – using different statements in different
- 0:27orders – that achieve exactly the same numerical result.
- 0:30The difference between them is the algorithm, that is the specific steps used to complete
- 0:34the computation.
- 0:35Some algorithms are better than others even if they produce equal results.
- 0:38Generally, the fewer steps it takes to compute, the better it is, though sometimes we care
- 0:42about other factors, like how much memory it uses.
- 0:45The term algorithm comes from Persian polymath Muḥammad ibn Mūsā al-Khwārizmī who was
- 0:49one of the fathers of algebra more than a millennium ago.
- 0:52The crafting of efficient algorithms – a problem that existed long before modern computers
- 0:56– led to a whole science surrounding computation, which evolved into the modern discipline of…
- 1:00you guessed it!
- 1:01Computer Science!
- 1:02INTRO
- 1:11One of the most storied algorithmic problems in all of computer science is sorting… as
- 1:15in sorting names or sorting numbers.
- 1:18Computers sort all the time.
- 1:19Looking for the cheapest airfare, arranging your email by most recently sent, or scrolling
- 1:23your contacts by last name -- those all require sorting.
- 1:27You might think “sorting isn’t so tough… how many algorithms can there possibly be?”
- 1:30The answer is: a lot.
- 1:32Computer Scientists have spent decades inventing algorithms for sorting, with cool names like
- 1:36Bubble Sort and Spaghetti Sort.
- 1:38Let’s try sorting!
- 1:39Imagine we have a set of airfare prices to Indianapolis.
- 1:42We’ll talk about how data like this is represented in memory next week, but for now, a series
- 1:47of items like this is called an array.
- 1:49Let’s take a look at these numbers to help see how we might sort this programmatically.
- 1:52We’ll start with a simple algorithm.
- 1:55First, let’s scan down the array to find the smallest number.
- 1:58Starting at the top with 307.
- 1:59It’s the only number we’ve seen, so it’s also the smallest.
- 2:03The next is 239, that’s smaller than 307, so it becomes our new smallest number.
- 2:09Next is 214, our new smallest number.
- 2:11250 is not, neither is 384, 299, 223 or 312.
- 2:19So we’ve finished scanning all numbers, and 214 is the smallest.
- 2:22To put this into ascending order, we swap 214 with the number in the top location.
- 2:27Great.
- 2:28We sorted one number!
- 2:29Now we repeat the same procedure, but instead of starting at the top, we can start one spot
- 2:33below.
- 2:34First we see 239, which we save as our new smallest number.
- 2:38Scanning the rest of the array, we find 223 is the next smallest, so we swap this with
- 2:43the number in the second spot.
- 2:44Now we repeat again, starting from the third number down.
- 2:47This time, we swap 239 with 307.
- 2:51This process continues until we get to the very last number, and voila, the array is
- 2:55sorted and you’re ready to book that flight to Indianapolis!
- 2:58The process we just walked through is one way – or one algorithm – for sorting an
- 3:02array.
- 3:03It’s called Selection Sort -- and it’s pretty basic.
- 3:05Here’s the pseudo-code.
- 3:06This function can be used to sort 8, 80, or 80 million numbers - and once you've written
- 3:11the function, you can use it over and over again.
- 3:14With this sort algorithm, we loop through each position in the array, from top to bottom,
- 3:17and then for each of those positions, we have to loop through the array to find the smallest
- 3:21number to swap.
- 3:22You can see this in the code, where one FOR loop is nested inside of another FOR loop.
- 3:26This means, very roughly, that if we want to sort N items, we have to loop N times,
- 3:30inside of which, we loop N times, for a grand total of roughly N times N loops...
- 3:35Or N squared.
- 3:36This relationship of input size to the number of steps the algorithm takes to run characterizes
- 3:41the complexity of the Selection Sort algorithm.
- 3:44It gives you an approximation of how fast, or slow, an algorithm is going to be.
- 3:48Computer Scientists write this order of growth in something known as – no joke – “big
- 3:52O notation”.
- 3:53N squared is not particularly efficient.
- 3:56Our example array had n = 8 items, and 8 squared is 64.
- 4:00If we increase the size of our array from 8 items to 80, the running time is now 80
- 4:05squared, which is 6,400.
- 4:07So although our array only grew by 10 times - from 8 to 80 – the running time increased
- 4:11by 100 times – from 64 to 6,400!
- 4:16This effect magnifies as the array gets larger.
- 4:18That’s a big problem for a company like Google, which has to sort arrays with millions
- 4:22or billions of entries.
- 4:23So, you might ask, as a burgeoning computer scientist, is there a more efficient sorting algorithm?
- 4:28Let’s go back to our old, unsorted array and try a different algorithm, merge sort.
- 4:33The first thing merge sort does is check if the size of the array is greater than 1.
- 4:36If it is, it splits the array into two halves.
- 4:39Since our array is size 8, it gets split into two arrays of size 4.
- 4:43These are still bigger than size 1, so they get split again, into arrays of size 2, and
- 4:47finally they split into 8 arrays with 1 item in each.
- 4:50Now we are ready to merge, which is how “merge sort” gets its name.
- 4:53Starting with the first two arrays, we read the first – and only – value in them,
- 4:57in this case, 307 and 239.
- 5:00239 is smaller, so we take that value first.
- 5:03The only number left is 307, so we put that value second.
- 5:07We’ve successfully merged two arrays.
- 5:09We now repeat this process for the remaining pairs, putting them each in sorted order.
- 5:13Then the merge process repeats.
- 5:15Again, we take the first two arrays, and we compare the first numbers in them.
- 5:19This time its 239 and 214.
- 5:22214 is lowest, so we take that number first.
- 5:25Now we look again at the first two numbers in both arrays: 239 and 250.
- 5:31239 is lower, so we take that number next.
- 5:34Now we look at the next two numbers: 307 and 250.
- 5:37250 is lower, so we take that.
- 5:40Finally, we’re left with just 307, so that gets added last.
- 5:44In every case, we start with two arrays, each individually sorted, and merge them into a
- 5:48larger sorted array.
- 5:50We repeat the exact same merging process for the two remaining arrays of size two.
- 5:54Now we have two sorted arrays of size 4.
- 5:56Just as before, we merge, comparing the first two numbers in each array, and taking the
- 6:00lowest.
- 6:01We repeat this until all the numbers are merged, and then our array is fully sorted again!
- 6:05The bad news is: no matter how many times we sort these, you’re still going to have
- 6:08to pay $214 to get to Indianapolis.
- 6:11Anyway, the “Big O” computational complexity of merge sort is N times the Log of N.
- 6:16The N comes from the number of times we need to compare and merge items, which is directly
- 6:20proportional to the number of items in the array.
- 6:22The Log N comes from the number of merge steps.
- 6:25In our example, we broke our array of 8 items into 4, then 2, and finally 1.
- 6:29That’s 3 splits.
- 6:31Splitting in half repeatedly like this has a logarithmic relationship with the number
- 6:34of items - trust me!
- 6:36Log base 2 of 8 equals 3 splits.
- 6:38If we double the size of our array to 16 – that's twice as many items to sort – it only increases
- 6:42the number of split steps by 1 since log base 2 of 16 equals 4.
- 6:46Even if we increase the size of the array more than a thousand times, from 8 items to
- 6:508000 items, the number of split steps stays pretty low.
- 6:54Log base 2 of 8000 is roughly 13.
- 6:56That's more, but not much more than 3 -- about four times larger – and yet we’re sorting
- 7:01a lot more numbers.
- 7:02For this reason, merge sort is much more efficient than selection sort.
- 7:06And now I can put my ceramic cat collection in name order MUCH faster!
- 7:10There are literally dozens of sorting algorithms we could review, but instead, I want to move
- 7:14on to my other favorite category of classic algorithmic problems: graph search!
- 7:18A graph is a network of nodes connected by lines.
- 7:20You can think of it like a map, with cities and roads connecting them.
- 7:23Routes between these cities take different amounts of time.
- 7:26We can label each line with what is called a cost or weight.
- 7:29In this case, it’s weeks of travel.
- 7:31Now let’s say we want to find the fastest route for an army at Highgarden to reach the
- 7:35castle at Winterfell.
- 7:36The simplest approach would just be to try every single path exhaustively and calculate
- 7:41the total cost of each.
- 7:42That’s a brute force approach.
- 7:43We could have used a brute force approach in sorting, by systematically trying every
- 7:48permutation of the array to check if it’s sorted.
- 7:50This would have an N factorial complexity - that is the number of nodes, times one less,
- 7:56times one less than that, and so on until 1.
- 7:58Which is way worse than even N squared.
- 8:00But, we can be way more clever!
- 8:03The classic algorithmic solution to this graph problem was invented by one of the greatest
- 8:06minds in computer science practice and theory, Edsger Dijkstra, so it’s appropriately named
- 8:11Dijkstra's algorithm.
- 8:12We start in Highgarden with a cost of 0, which we mark inside the node.
- 8:16For now, we mark all other cities with question marks - we don’t know the cost of getting
- 8:20to them yet.
- 8:21Dijkstra's algorithm always starts with the node with lowest cost.
- 8:24In this case, it only knows about one node, Highgarden, so it starts there.
- 8:28It follows all paths from that node to all connecting nodes that are one step away, and
- 8:32records the cost to get to each of them.
- 8:34That completes one round of the algorithm.
- 8:36We haven’t encountered Winterfell yet, so we loop and run Dijkstra's algorithm again.
- 8:40With Highgarden already checked, the next lowest cost node is King's Landing.
- 8:44Just as before, we follow every unvisited line to any connecting cities.
- 8:47The line to The Trident has a cost of 5.
- 8:50However, we want to keep a running cost from Highgarden, so the total cost of getting to
- 8:54The Trident is 8 plus 5, which is 13 weeks.
- 8:57Now we follow the offroad path to Riverrun, which has a high cost of 25, for a total of 33.
- 9:02But we can see inside of Riverrun that we’ve already found a path with a lower cost of
- 9:06just 10.
- 9:07So we disregard our new path, and stick with the previous, better path.
- 9:10We’ve now explored every line from King's Landing and didn’t find Winterfell, so we
- 9:14move on.
- 9:15The next lowest cost node is Riverrun, at 10 weeks.
- 9:18First we check the path to The Trident, which has a total cost of 10 plus 2, or 12.
- 9:23That’s slightly better than the previous path we found, which had a cost of 13, so
- 9:27we update the path and cost to The Trident.
- 9:29There is also a line from Riverrun to Pyke with a cost of 3.
- 9:3110 plus 3 is 13, which beats the previous cost of 14, and so we update Pyke's path and
- 9:36cost as well.
- 9:37That’s all paths from Riverrun checked... so... you guessed it, Dijkstra's algorithm
- 9:41loops again.
- 9:42The node with the next lowest cost is The Trident and the only line from The Trident
- 9:46that we haven’t checked is a path to Winterfell!
- 9:49It has a cost of 10, plus we need to add in the cost of 12 it takes to get to The Trident,
- 9:53for a grand total cost of 22.
- 9:55We check our last path, from Pyke to Winterfell, which sums to 31.
- 9:59Now we know the lowest total cost, and also the fastest route for the army to get there,
- 10:03which avoids King’s Landing!
- 10:05Dijkstra's original algorithm, conceived in 1956, had a complexity of the number of nodes
- 10:09in the graph squared.
- 10:10And squared, as we already discussed, is never great, because it means the algorithm can’t
- 10:14scale to big problems - like the entire road map of the United States.
- 10:18Fortunately, Dijkstra's algorithm was improved a few years later to take the number of nodes
- 10:22in the graph, times the log of the number of nodes, PLUS the number of lines.
- 10:26Although this looks more complicated, it’s actually quite a bit faster.
- 10:30Plugging in our example graph, with 6 cities and 9 lines, proves it.
- 10:33Our algorithm drops from 36 loops to around 14.
- 10:36As with sorting, there are innumerable graph search algorithms, with different pros and cons.
- 10:41Every time you use a service like Google Maps to find directions, an algorithm much like
- 10:45Dijkstra's is running on servers to figure out the best route for you.
- 10:48Algorithms are everywhere and the modern world would not be possible without them.
- 10:52We touched only the very tip of the algorithmic iceberg in this episode, but a central part
- 10:57of being a computer scientist is leveraging existing algorithms and writing new ones when
- 11:01needed, and I hope this little taste has intrigued you to SEARCH further.
- 11:05I’ll see you next week.
About this transcript
This page contains the full transcript of Intro to Algorithms: Crash Course Computer Science #13 by CrashCourse, generated from the public captions YouTube serves with the video. The transcript has 2,311 words across 206 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.