I\'ve been assigned an exercise in my university. I took it home and tried to pr
ID: 649293 • Letter: I
Question
I've been assigned an exercise in my university. I took it home and tried to program an algorithm to solve it, it was something related to graphs, finding connected components, I guess.
Then I made the most trivial thing that came into my mind and then showed to my lecturer. After a brief observation, he perceived that the runtime complexity of my solution was inviable and shown something more efficient. And there is a tradition of programmers who have no idea on what is computational complexity (I was one of those), so is it a problem if a programmer has no idea on what is computational complexity?
Explanation / Answer
Yes, I would say knowing something about computational complexity is a must for any serious programmer. So long as you are not dealing with huge data sets you will be fine not knowing complexity, but if you want to write a program that tackles serious problems you need it.
In your specific case, your example of finding connected components might have worked for graphs of up to say 100 nodes. However, if you tried a graph with 100.000 nodes then your lecturer's algorithm would probably have managed that in 1 second, while your algorithm would have (depending on how bad the complexity was) taken 1 hour, 1 day, or maybe even 1 eternity.
A somewhat common mistake students make in our algorithms course is to iterate over an array like this:
while array not empty
examine first element of array
remove first element from array
This might not be the most beautiful code but in a complicated program something like this might show up without the programmer being aware of it. Now, what is the problem with this program?
Suppose we run it on a data set of 100.000 elements. Compared to the following program, the former program will run 50.000 slower.
while array not empty
examine last element of array
remove last element from array
I hope you agree that having the knowledge to make your program run 50.000 times faster is probably an important thing for a programmer. Understanding the difference between the two programs requires some basic knowledge about complexity theory and some knowledge about the particulars of the language you are programming in.
In my pseudocode language, "removing an element from an array" shifts all the elements to the right of the element being removed one position from the left. This makes removing the last element an O(1) operation since in order to do that we only need to interact with 1 element. Removing the first element is O(n) since in order to remove the first element we need to shift all the other n?1 elements one position to the left as well.
A very basic exercise in complexity is to prove that the first program will do 12n2 operations while the second program uses only n operations. If you plug in n=100.000 you will see one program is drastically more efficient than the other.
This is just a toy example but it already requires a basic understanding of complexity to tell the difference between the two programs, and if you are actually trying to debug/optimize a more complicated program that has this mistake it takes an even greater understanding to find out where the bug is. Because a mistake like removing an element from an array in this fashion can be hidden very well by abstractions in the code.
Having a good understanding of complexity also helps when comparing two approaches to solve a problem. Suppose you had come up with two different approaches to solving the connected components problem on your own: in order to decide between them it would be very useful if you could (quickly) estimate their complexity and pick the better one.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.