1. Home
  2. Explore
  3. Computer Science
  4. Introduction to Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Introduction to Algorithms

Speaker 1Speaker 2

Introduction to Algorithms

18:44
0:00-18:44
  1. Hope

    Welcome to our conversation about algorithms: what they are, how we measure them, and why some problems become extraordinarily difficult as they grow. The word sounds modern and technical, but its history reaches back to a mathematician who lived more than a thousand years ago.

  2. Anna

    That mathematician was Muhammad al-Khwarizmi. The exact details of his origins and religious identity are disputed, and different places have claimed him, but he worked in Baghdad at the House of Wisdom. His name was Latinized as “Algoritmi,” and that is where the word “algorithm” came from.

  3. Hope

    Al-Khwarizmi also wrote about calculation and algebra, and helped introduce what was called Arabic numbering in Europe. That system drew on Indian numbering, so even the familiar label has a more complicated history than it first appears. There’s a useful connection here: methods for working with numbers and equations helped inspire the broader idea of a method for carrying out a task.

  4. Anna

    So what do we mean by an algorithm today? In the basic sense, it’s a procedure: a set of rules for doing something. But for a procedure to count as an effective method, it needs more than a vague instruction like “work it out.”

  5. Hope

    A classic definition says an effective method has a finite number of precise instructions. Applied to a problem in the method’s class, it always stops after a finite number of steps and produces the correct answer. In principle, a person should be able to follow it rigorously, using only writing materials.

  6. Anna

    That’s why school procedures such as long division or solving a quadratic equation are good examples. You don’t have to invent a new approach every time; you follow a defined method. This also connects algorithms to proof, because a formal procedure can show how to derive an answer from stated rules.

  7. Hope

    One key feature is that an algorithm must be unambiguous: its instructions need to be clear. But unambiguous doesn’t necessarily mean deterministic. A route-planning method, for instance, might choose different roads on different days because traffic or other conditions have changed.

  8. Anna

    Even if the exact route varies, the method can still have the same structure: start at one place, respond to the conditions, and arrive at the destination. So variation in the inputs or circumstances doesn’t automatically disqualify something from being an algorithm.

  9. Hope

    And algorithms weren’t originally designed only for machines. For much of the history of computing, a “computer” could be a person doing calculations. Teams of human mathematicians handled work for expensive, high-profile projects, including the early NASA program.

  10. Anna

    That’s the history behind the human computers portrayed in the film Hidden Figures, including mathematician Katherine Johnson. They helped develop and apply numerical methods. The film makes algorithm design look wonderfully dramatic, but many real problems can be handled with established methods rather than a fresh moment of genius.

  11. Hope

    Exactly. Computer science has a body of standard algorithms, and knowing one can save you from starting from scratch. They aren’t as neatly interchangeable as standard electronic components, though: software often still needs customization, and that can create opportunities for mistakes.

  12. Anna

    Sorting is a good place to see why the choice of algorithm matters. President Obama was once asked about sorting a million 32-bit integers, and he joked that bubble sort would be the wrong choice. Computer scientists agree: bubble sort is easy to understand, but very inefficient for large lists.

  13. Hope

    Here’s the basic idea. Look at neighboring values in a list; if they’re in the wrong order, swap them. Keep sweeping through the list, comparing pairs, until the values are ordered from smallest to largest.

  14. Anna

    Imagine people holding number cards and comparing with the person beside them. When a pair is out of order, they switch places. Small values gradually move toward one end and large values toward the other—that’s why a dance can make bubble sort surprisingly easy to visualize.

  15. Hope

    But a visual explanation doesn’t tell us how well the method scales. We can time one run, but the measured time depends on the computer. Instead, we can ask how the number of steps grows as the list size, which we’ll call N, gets bigger.

  16. Anna

    That growth is called time complexity—or, more precisely, step complexity. Space complexity is a separate question: how much storage the algorithm requires. When people say “complexity” without qualifying it, they usually mean time complexity.

  17. Hope

    Bubble sort also shows why it can help to distinguish best and worst cases. If the list is already ordered, a sweep finds no pairs to swap; in the lecture’s example, that takes about N work. If the list is reversed, values have a long way to move, and repeated sweeps can require work proportional to N squared.

  18. Anna

    That N-squared growth is written as O(N²), using Big-O notation. It describes how the work scales, not an exact stopwatch reading. A test program that repeatedly sorts lists of increasing sizes can show the same general N-squared pattern.

  19. Hope

    Big-O helps us compare methods without turning the discussion into a contest over whose computer is faster. It does have limitations: it focuses on scaling and leaves out constants and details of the machine. A method with an enormous constant factor can be worse in practice than its asymptotic label suggests.

  20. Anna

    For sorting, a widely used alternative is merge sort. It divides the data into smaller lists, sorts those smaller lists, and merges them back together. That divide-and-merge approach avoids making a badly placed value ripple through the entire list in the way bubble sort can.

  21. Hope

    Another example is Timsort, associated with Tim Peters and used in several programming languages and systems. It has had bugs discovered and fixed, which is a useful reminder that a standard, widely used algorithm still needs careful implementation and testing.

  22. Anna

    There’s a rough hierarchy of growth rates. Being able to inspect the data once, with work proportional to N, is wonderful, but many algorithms take longer. Logarithmic growth and N log N are often manageable; polynomial growth can still become costly; and exponential growth can become impossible surprisingly quickly.

  23. Hope

    That idea is often associated with the Cobham–Edmonds view of tractability: polynomial-time problems are the “nice” ones, grouped into P, while problems outside that group may be much harder to handle. It’s a rule of thumb about scaling, not a promise that every polynomial-time algorithm is fast on real hardware.

  24. Anna

    And the opposite warning matters too: not being in P doesn’t mean a problem is useless or that we should give up. Many practical problems don’t fit neatly into that supposedly easy category, so computer scientists use exact methods for small cases and approximations or other strategies for larger ones.

  25. Hope

    The traveling salesperson problem, or TSP, makes the challenge concrete. Given a collection of places, visit every place exactly once, return to where you started, and make the total route as short as possible. It’s a simple problem to state, but the number of possible tours grows extremely fast.

  26. Anna

    For an illustration, imagine walking among thirteen surviving Wren churches in the City of London. Represent the churches as points in a graph, with the distance between each pair as the cost of traveling between them. In the lecture, those distances used Google Maps walking routes, averaged in both directions because the routes to and from a place can differ.

  27. Hope

    A brute-force search would start at one church, try every possible next church, then every possible one after that, and keep going. For thirteen churches, the lecture counted roughly 500 million possibilities if clockwise and anticlockwise tours are treated separately. That’s a lot, but a computer can still manage a problem of that size.

  28. Anna

    Now increase the problem to 52 churches. The number of possible arrangements grows factorially: each additional church multiplies the possibilities by another factor. If a thirteen-church search took a millisecond, the same brute-force approach for 52 was estimated at about five times ten to the 48 years on the same computer.

  29. Hope

    That jump is the point. A problem can be manageable at a small size and hopeless at a modestly larger one, even if you imagine a much faster machine. This is why we care about scaling rather than just asking how long one small example takes.

  30. Anna

    One practical response is an approximation. With the nearest-neighbor algorithm, choose the closest unvisited place, move there, and repeat until every place has been visited. It’s quick and intuitive, but it doesn’t guarantee the shortest route.

  31. Hope

    In the church example, nearest-neighbor routes were around five kilometers, with different starting points producing different results. The lecture compared those with a route of about 4.4 kilometers. The difference gives us a way to talk about how an approximate result relates to an optimal one.

  32. Anna

    That’s where bounds are useful. An upper bound tells us a limit on how bad a route we’ve found could be; a lower bound tells us something the optimum cannot go below. Bounds don’t necessarily reveal the exact answer, but they can tell us how far our result might be from it.

  33. Hope

    For TSP, there’s also the exact Held–Karp algorithm, which has exponential-style scaling—described in the lecture as proportional to N squared times two to the N. It can solve small or moderate toy problems exactly, but its cost becomes a problem as the number of locations grows.

  34. Anna

    A familiar approximate approach is the Christofides algorithm, also called Christofides–Serdyukov. Its guarantee is that the route it produces is no more than one and a half times the optimum, while its scaling makes it useful for larger problems. The lecture also mentioned a newer improvement whose guarantee is only minutely better—by a term on the order of ten to the minus 36—but whose new approach attracted interest.

  35. Hope

    So there isn’t one universally best answer to a difficult optimization problem. We may choose an exact algorithm when the instance is small, a fast heuristic when we need an answer quickly, or an approximation with a known guarantee when we want to understand how far from optimal the result could be.

  36. Anna

    A couple of common algorithm structures help explain how procedures are built. Iteration means repeating steps. Recursion means defining a procedure in terms of a smaller version of itself—for example, factorial N can be defined as N times factorial of N minus one.

  37. Hope

    Algorithms have also been represented in different ways. Flowcharts use standardized symbols to show steps and decisions. Frank and Lillian Gilbreth developed early process charts for studying human activity, and one of the earliest examples resembling a modern flowchart appears in computer pioneer Grace Hopper’s papers, attributed to programmer Margery K. League.

  38. Anna

    Flowcharts later went out of fashion, perhaps partly because drawing them on early computers was awkward. Programmers now often use pseudocode, a written outline of the steps. The trouble is that pseudocode isn’t consistently standardized, so people can mean slightly different things by it.

  39. Hope

    For a more formal foundation, the lecture turns to Alan Turing and Alonzo Church. Church developed lambda calculus, an algebra for describing functions. Turing’s 1936 work was aimed at a mathematical question called the Entscheidungsproblem: given a formula and a system of axioms, can we determine whether the formula follows from those axioms?

  40. Anna

    Turing introduced a simple theoretical model of a computer: a Turing machine. It reads and changes symbols on a tape according to rules. A universal Turing machine can also represent a program on its tape, letting us reason about a general-purpose machine even though Turing machines aren’t practical computers.

  41. Hope

    The Church–Turing thesis captures the idea that any algorithm can be simulated by a Turing machine, and that a Turing machine’s behavior can be described algorithmically. It gives theoretical computer science a way to talk precisely about what can be computed, without requiring us to build a machine for every possible calculation.

  42. Anna

    That formal viewpoint also helps define classes of problems. A decision problem is one whose answer is yes or no. For example, instead of asking for the shortest TSP route, we could ask, “Is there a route shorter than this specified distance?”

  43. Hope

    P is the class of decision problems solvable by a deterministic Turing machine in polynomial time. Many important tasks fall into P, including sorting, linear programming, and the discrete Fourier transform. The transform matters in applications such as mobile phones and high-definition television, where calculations need to be carried out efficiently and repeatedly.

  44. Anna

    NP is defined differently: it’s the class of problems where a proposed solution can be verified in polynomial time. Think of Sudoku. Once someone gives you a completed grid, you can check its rows, columns, and subgrids for the required digits without searching through every possible completed puzzle.

  45. Hope

    Finding a solution can be much harder than checking one. For Sudoku, checking a filled grid is straightforward, but filling the grid can demand a great deal of work. That contrast—easy to verify, potentially difficult to find—is the heart of the distinction between P and NP.

  46. Anna

    Every problem in P is also in NP, because if you can solve a problem efficiently, you can certainly verify the answer efficiently. But there are problems known to be in NP for which no polynomial-time solution is known. The famous question is whether P equals NP.

  47. Hope

    Many theorists suspect that P does not equal NP, but no one has proved it either way. The Clay Mathematics Institute has offered a million-dollar prize for a proof. Some problems called NP-complete are especially central because, in an appropriate technical sense, a solution to one can be transformed into a solution to others.

  48. Anna

    The lecture used examples such as Sudoku, routing, circuit design, and job scheduling to suggest the range of these challenges. It also mentioned protein folding and public-key cryptography when discussing hard problems, while noting that the status of particular problems can change: an algorithm for finding primes, for instance, was shown to be in P in 2006.

  49. Hope

    The P-versus-NP question matters partly because of cryptography. Some public-key systems rely on an asymmetry: it should be easy to check or use a key, but difficult for an outsider to solve the underlying problem. If P turned out to equal NP, some cryptographic systems could be in danger, though the consequences would depend on the details of the proof.

  50. Anna

    And the landscape is bigger than P and NP. The lecture’s closing explainer mentioned NP-hard problems, co-NP, PSPACE, probabilistic polynomial time, or BPP, and its quantum counterpart BQP. There are many complexity classes—one researcher’s count was 416—and some problems may not be solvable by any computer at all.

  51. Hope

    That last point leads to an important question: are there fundamentally unsolvable problems? Turing studied the halting problem: given a machine and a program, can we always determine whether the program will stop? The answer is no. It’s a surprising example of a problem that is not merely difficult, but impossible to solve in full generality.

  52. Anna

    There are also real-world questions about how algorithms behave. An algorithm can produce discriminatory outcomes, even if the software itself has no intentions. For example, a fraud-detection system used credit scoring, which was correlated with where people lived; location was also correlated with ethnicity, so the system could produce racially biased decisions.

  53. Hope

    That’s why the design of an algorithm and the data it uses matter. A modern challenge is to identify and reduce bias, including in training data, and to check whether decisions meet fairness standards. Calling a decision “algorithmic” doesn’t remove the responsibility to examine its effects.

  54. Anna

    Another issue is that software can change after people first assess it. A router or computer may receive updated algorithms, and a system that learns from its own experience may no longer behave exactly as it did when it was specified. Updates can be useful, but they raise questions about testing, verification, and whether the system is still doing what people intended.

  55. Hope

    The cancelled A-level exams in the UK offered a stark example of public trust and fairness. Exam authorities tested different algorithms for awarding grades, and the approach was expected to disadvantage some students. The problem was that the consequences for students—many of whom take several A-levels and care deeply about fairness—were not adequately anticipated.

  56. Anna

    So the lesson wasn’t simply that algorithms are bad. The failure also involved the people and institutions choosing how to use them, and their failure to understand the experience of young adults affected by the decisions. Algorithms can help, but their goals, data, limitations, and human consequences all need scrutiny.

  57. Hope

    That brings us back to the central idea: an algorithm is a clear, finite method for solving a class of problems, but methods can differ enormously in efficiency and in the quality of their results. Some problems have fast exact solutions; others demand approximations, careful bounds, or a recognition that no general solution exists.

  58. Anna

    And understanding algorithms means more than memorizing procedures. It means asking what problem the procedure solves, how its work grows as the problem grows, whether its answer can be checked, and what happens when people rely on it. Those questions prepare us to look next at data structures, programs, computers, networks, and security.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent