r/AskComputerScience • u/Dornith • May 27 '21
Seemingly arbitrary tasks that are actually really hard.
I'm sure we're all familiar with this XKCD. In the spirit of it, I wanted to pose the question:
What's something that seems, to anyone not familiar with CS, like it should be simple, but is actually really hard or complicated. Fun stories relating to this are also appreciated.
18
May 27 '21 edited May 27 '21
Consider a polynomial with all integer coefficients and finitely many indeterminates (like 3xy - z + 10). Can you write an algorithm to determine if there are any integer values for the coefficients that cause the polynomial to equal zero? No math beyond what you learn in middle school is needed to understand. Many examples are trivial to solve.
There is no general solution at all.
1
u/Poddster May 27 '21
Why would the general public think this is simple to solve?
3
u/Dornith May 28 '21 edited May 28 '21
I think it depends on how you present the problem. If you use technical language like they did here, it sounds difficult.
But if you just say, "7th grade algebra equations", or something a lay person would understand, they would assume it would be easy.
3
1
May 27 '21
Isthat because its like SAT nut with integers?
17
u/teraflop May 27 '21
It's worse than that. NP-hard problems like SAT are solvable, in principle, if you're willing to spend enough time. Worst case, you can just enumerate all possible combinations of the input variables, and either you find a valid solution or you don't. This might take a very long time, but it's easy to define the algorithm and show that it always gives the correct answer.
Diophantine equations are sort of analogous to Boolean formulas, except with integer variables instead of Booleans. You can't just brute-force them, because there are infinitely many possible values you could substitute into each variable. And since deciding whether such an equation has solutions is equivalent to deciding whether an arbitrary Turing machine halts, there can be no general algorithm that always gives the correct answer, no matter how sophisticated.
2
5
u/east_lisp_junk May 27 '21
Resemblance to SAT doesn't really get you very far. Not only is SAT decidable, it has a very straightforward (but slow) algorithm.
28
u/claytonkb May 27 '21
General-purpose text-editing is surprisingly devilish. "It's just a bunch of bytes in memory, how hard could it be?" But then you have Unicode. And then you have to keep track of newlines. But there isn't just one kind of newline, there are various newlines depending on what systems you support. Then there are the extended-ASCII Latin character-sets that were used in Europe pre-Unicode (not sure if phased out yet). Then there are random standards like various pre-Unicode wide-character formats. Then there is endianness in case you are not just going to work one platform.
Then there are algorithmic problems involved in quickly traversing a document -- it's fairly easy to get into unintended quadratics. For example, naive backspacing with flat-memory layout of strings is quadratic because you have to keep re-copying the trailing portion of the document after the deleted character. N backspaces -> N * length of trailing portion of document copied bytes. There are lots of good solutions to these problems, they're just surprisingly knotty given the conceptual simplicity of what you're trying to do.
And then there's Undo/Redo. Simplistic editors maintain an undo history which is nice. But trying to store an Undo/Redo history in a sane fashion is, once again, surprisingly tricky. You can have a literal "replay" of the user's edits but this is not very efficient. You can store snapshots at various intervals (like "key frames" in video encoding) but this has problems. A more general solution is to implement piece tables and formulate Undo/Redo as edit-lists. This, in turn, will make you want to rethink how you store the entire document and you're probably going to start looking into storing it as some kind of tree structure. If you implement any kind of marks/tags/anchors, you'll need a hash-table to store those so they can be quickly indexed, and you need to take care to maintain global coherency of the document when these are added/removed. We could keep going but, before long, we will end up describing the specification of Vim or Emacs and understanding a little of what's going on under the hood gives you a new appreciation of just how much you don't really care about all those implementation details...
"Let's write a text-editor. It should make a nice weekend project..." famous last words...
4
u/gokstudio May 27 '21
I'd really love to learn what's the best ways to do these tasks are. Any resources to learn the implementation details of these features that you recommend?
Just discussions of the data structures and algorithms should be fine
5
u/claytonkb May 27 '21
I think if you read any in-depth article that delves into the guts of Vim or Emacs, you're going to learn about these topics. You can start with Piece table Wiki and chase references. A brief web-search turned up this project but I've not reviewed it so I can't give any recommendation. Based on a brief glance, it looks like a great place to start if you're familiar with C -- you'll get a feel for the low-level problems involved with the naive solution which will give you an increased appreciation for more sophisticated solutions. Hope that helps!
2
1
u/beeskness420 May 28 '21
Not that I know anything about text-editors, but I’ve heard of ropes used in that context.
22
u/cattime742 May 27 '21
I mean, the Halting Problem immediately springs to mind.
2
u/phistoh May 28 '21
Or the Post correspondence problem which seems like a simple brainteaser (at least if it is not written down the super formal way) but is nonetheless undecidable.
17
u/ChrisC1234 BSCS, MSCS, CS Pro (20+) May 27 '21
Time
Things like time zones, daylight savings time, people’s expectations about time, and so on. It’s ridiculously complicated. Even billion dollar corporations can screw it up (iPhones had a problem with daylight savings time one time and a ton of people’s alarms didn’t go off.)
5
u/Rein215 May 28 '21
At the same time I feel like Unix time is more simple than people expect right?
Like
"So how does a computer store time?"
"You just have a number consisting of the seconds which have passed since January 1th 1970"
"Cool"
3
9
u/roman_fyseek May 27 '21
Security Classification Roll-up.
In general, Top Secret supersedes Secret which supersedes Confidential which supersedes Unclassified.
Two Unclassified statements combined with one Secret statement makes a document Secret.
Which is also to say that two database columns with Unclassified data combined with one database column makes the result set Secret.
However, there is also the case where the query itself contains Secret data. So, now, you have two Unclassified columns that were the result of a Secret select statement. Is the data Secret? Is it only Secret when the Unclassified columns are combined with the Secret select statement? Or, is the very fact of the existence of the data Secret because it's the result of a Secret select?
Additionally, the vast majority of classified data is the result of an aggregation of completely unclassified data. It's the way that it's aggregated that causes the classification.
Is your Unclassified result set actually Top Secret because of the way it's assembled or who produced it?
And, that's why I'm happy that I'm a contractor and never have to answer these questions. I can simply say, "Sure, I can get you that data. Just send me an email in which you take responsibility for the spill," and the problems just go away by themselves.
4
u/midwayfair May 28 '21
Anything where the average person would say "just look at it."
Usually this is something visual. I think people nowadays probably understand that computers have a hard time with "vision" because they're used to seeing a computer get it wrong, like tagging the wrong thing as a face or whatever, but even computer scientists thought that this was going to be an easy problem when they started it. The idea of even being able to distinguish an "object in the foreground" from "stuff in the background" was exceptionally convoluted. But we're so naturally good at it that it doesn't seem like it should be that hard.
You can generalize this to any task that requires context: Summarizing text, autonomous driving, etc. These are tasks where we can provide a bunch of unconscious information simply because we have creativity -- humans can tell themselves stories about events that never occurred, and we can do it quickly, which means that we can strategize in the moment about situations that we really have no direct experience with. This makes a lot of tasks seem simpler than they are.
6
u/beeskness420 May 27 '21 edited May 27 '21
If I give you a collection of matrices does there exist any product of them equal to the zero matrix?
This problem is undecidable for even some pretty simple collections.
4
u/ggchappell May 27 '21
Interesting. I take it we're allowing repetitions of a single matrix, and thus arbitrarily long products.
So, how do we prove it? I messed around a bit with reduction from the Halting Problem, but I'm having trouble getting it to work.
3
u/beeskness420 May 27 '21 edited May 27 '21
I actually don’t know the proof, and I don’t think it’s obvious.
It’s known as the mortal matrix problem, there is a link in this wiki to a paper on proving it for sets of 6 or more 3x3 matrices or a set of 2 15x15 matrices.
Looks like 2x2 matrices are only NP-Hard.
3x3 were shown to be undecidable in 1970 by Paterson
3
6
3
u/ghjm MSCS, CS Pro (20+) May 28 '21
We've all seen the great advances in deep learning in recent years. The example from the xkcd, recognizing if a picture is of a bird, is now quite solvable, although it still takes a lot of effort to train a model.
What's very counterintuitive to non-experts is that even if we can train a model that reliably recognizes birds, we can't tell you why or how it recognizes birds - we have no way to look inside the model and figure out what characteristics it considers significant. If something marginally bird-like shows up, like a bat or flying squirrel, will it identify it a bird? Literally the only way to know is to try it and see.
2
u/ookyou May 28 '21 edited May 28 '21
Subset sum: Given an integer n and a list of numbers, tell me if any subset of numbers in the list sum up to n.
Example1: N = 5, List = [2, -2, 3, 0] = Yes (2 + 3)
Example2: N = 5, List = [2, -2, 6, 0] = No
2
2
u/phistoh May 28 '21
Many video games are hard. Not all are as "hard" (unsolvable) as other problems mentioned here, but still "hard" enough.
Some examples:
Super Mario Bros is (depending on the assumptions) NP-hard or even PSPACE-hard.
Sokoban is PSPACE-hard
Minesweeper is NP-hard (and an infinite version even undecidable)
2048 is NP-hard
Pokémon is NP-hard
Lemmings is PSPACE-hard
Sure these proofs all analyze "generalized" versions of the game, so it is not directly a "A computer has a hard time playing this game." and rather a "You can build levels which follow the general rules of the game, with which a computer has a hard time to solve them." But I think it is still an interesting fact.
0
May 27 '21
[deleted]
3
u/Nulaccur May 27 '21
Can't you just compare both strings.toLowerCase() ?
15
u/racei May 27 '21
I assume they're getting at the fact that different cultures and languages have different casing rules.
3
0
May 27 '21
[deleted]
2
u/Rein215 May 28 '21
See in particular the section on the Turkish I.
I will respectfully decline. I wish to remain sane. You lost me at "beyond ASCII".
9
u/spaztheannoyingkitty May 28 '21
Anything with natural languages.
Time. OMG fucking time. Did you know there's a 3/4 timezone?!
Anything requiring a computer to understand and remember things about the world around it and make decisions. Autonomous cars fit squarely in this category.
Computers that need to physically interact with humans. There's a reason robotics tend to have a giant "disconnect power immediately" button.
There may be some aspects of computer security that could be mentioned too.
In the broadest sense, anything that animals have been perfecting for millions of years. Limbed locomotion, visual interpretation of the world, etc.