Algorithm analysis completely from scratch

Status
Not open for further replies.
Background: I’m a 28 year old 2nd year Computer Science student. I quit my job doing back end web development a year ago to attend university for the first time. My previous education record is nothing to be proud of and I’m trying really hard to fix the mistakes I made when I was younger. I barely graduated “high school” (quotes as I’m in the UK), once I left school I began working in 1st level technical support over the course of the next ten years I went to 2nd level support to junior sys admin and finally into development where I have been for about the last 3 years. I enjoy development and while I have obvious gaps in my knowledge due to learning on the job I’ve made reasonable progress in the industry.

I’ve been lurking in this forum for a long time reading many many threads that I don’t fully understand, however I continue to lurk as I understand the wealth of knowledge and experience here does eventually rub off on me to some degree.

I’ve realised that as I have decided that I wish to make development my career, to fully realise my potential I needed to attend university, for myself and future employers. So while my on the job experience has helped greatly in nearly all of my classes, I’m a fish out of water in my Data Structures and Algorithms class.

The main problem I’m having is not the concepts per se. Its that all academic books that I’ve been reading along with my professor are making assumptions on math knowledge (not ability) that I frankly don’t have. To give an example, we are doing big O notation at the moment. The information provided to explain the concept was

DEFINITION: f(n) is in O(g(n)) if and only if c and N>0
exist such that for all n > N, f(n) ≤ c• g(n)

Which means absolutely nothing to me, as it appears to make assumptions of previous knowledge that I don’t have. I’m in danger of rambling for pages so I’ll try to wrap this up to a concise question.

How would you advise someone in my situation with essentially no previous math/comp sci education to get to grip with such material. Are there resources for plain English descriptions of comp sci subjects? Once I have them explained in plain English I find the concepts easy to grasp it’s this academic shorthand which I’m finding very intimidating and I feel is making things more difficult than they actually are.

I’d be extremely grateful for any advice, thanks for reading this far :)
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
That's a pretty tough one... Computer Science relies heavily on math as there is lots of theory involved. Mathematical definitions are usually the most concise and precise way to 'say' things. The other thing that CS classes give students is a common vocabulary. If I walked up to someone else and said "looks like an B+ tree would solve that, right?" they will know what I mean. Similarly with the math. Even if you found plain English versions of the subjects, you'd still need to be able to 'speak' the language eventually.

Are there any foundation math classes that you can take like at night or something? Data Structures and Algorithms (Algorithms in particular) are fairly advanced classes in the curriculum (like 2nd year and later... after the foundation classes in math and such). It seems that you've somehow got into an accelerated program or have been advised to skip some classes that you shouldn't have skipped.
 
fitten":1n2o4woi said:
That's a pretty tough one... Computer Science relies heavily on math as there is lots of theory involved. Mathematical definitions are usually the most concise and precise way to 'say' things. The other thing that CS classes give students is a common vocabulary. If I walked up to someone else and said "looks like an B+ tree would solve that, right?" they will know what I mean. Similarly with the math. Even if you found plain English versions of the subjects, you'd still need to be able to 'speak' the language eventually.

Are there any foundation math classes that you can take like at night or something? Data Structures and Algorithms (Algorithms in particular) are fairly advanced classes in the curriculum (like 2nd year and later... after the foundation classes in math and such). It seems that you've somehow got into an accelerated program or have been advised to skip some classes that you shouldn't have skipped.

My entry to the university was based completely on my professional experience (due to my age), we did a discrete math class in first year but while I see the obvious relevance to comp sci it didnt fill in all the gaps. I realise I may be asking for the impossible (Algorithms for dummies so to speak). I just find it so incredibly frustrating that when its explained in english I go "oh ok thats pretty simple and straight forward" and think in my mind "why the fuck don't the notes just say that in the first place".

I don't think that I've been misplaced into an accelerated class, I fear its more to the fact that its been ten years since I opened a math textbook (excluding last year obviously) so I don't even have the fundamental basics. One example being I didn't know (or at least couldn't remember) what a logarithm was, I had to spend a few minutes on khan academy just to get the concept.

I'm not afraid of the work but I need to know where to put it. What areas of math directly apply to algorithms? I fear I may be asking how long is a piece of string.
 

fitten

Ars Legatus Legionis
55,634
Subscriptor++
MistaMagoo":316rkhh1 said:
My entry to the university was based completely on my professional experience (due to my age), we did a discrete math class in first year but while I see the obvious relevance to comp sci it didnt fill in all the gaps. I realise I may be asking for the impossible (Algorithms for dummies so to speak). I just find it so incredibly frustrating that when its explained in english I go "oh ok thats pretty simple and straight forward" and think in my mind "why the fuck don't the notes just say that in the first place".

Well... that *is* what the notes say in the first place... you just aren't familiar with the language. You might as well ask a translator who has translated something from another language for you why didn't the original speaker just say that in English in the first place ;)

What areas of math directly apply to algorithms? I fear I may be asking how long is a piece of string.

The example above was algebra. However, the concepts are better explained in calculus, IMO. Basically, getting some familiarity with limits (the limit of f(x) as x approaches infinity is...) may help because that's kind of what BigO is similar to, sorta. As n goes to infinity and ignore the constant because you can always find one big enough, what does f(n) trend like?

f(n) = 1000 * n^^2 + 20 * n + 123434234

BigO says that trends like n^^2 (n to the 2nd power, n-squared). g(n) is n^^2

So basically you can analyze a function like that... every piece of data has to be compared to every other piece of data, so n^^2 (n pieces of data multiplied by n comparisons) is the BigO. It doesn't matter exactly how much time each comparison takes or some of the other 'fluff' that goes on or the amount of work done when the match is found (done once when the comparison is made or something).

Anyway, hard to get off into a discussion without going into examples. But basically for the quickest to get familiar, maybe go over some limits and take the time to slow down and read (in English) what the squiggles are saying and then figure out what that means (maybe you can practice by writing what it says out in English... each little bit, then figure out what it's trying to say as a whole).

In the above, you may see the shortcut is finding the term of the highest order and that's basically the answer (something multiplied by n^^2 is the highest order term in the above example... so n^^2).
 
Well, that definition involves functions, sets, inequalities, first order logic, and basic arithmetic.

I'd say when you ask someone what a definition means you should also ask about how the definition is saying it, keep notes and study the notation so you eventually learn what you need.

For example, I assume someone explained that definition of big-O notation to you. Did you learn enough that you can now read the definition?

Can you read the mathematical definitions for big-omega and big-theta (http://en.wikipedia.org/wiki/Big_O_notation) now and understand how they're related?
 
Faramir":157xmf3d said:
That's a formal definition of big O notation. Do you understand the basic idea of big O notation and just need to work backward to the formal definition, or are you trying to understand the idea directly from the definition?

My understanding of big O is essentially this. Its a form of notation to describe how an algorithm scales relative to its input size increasing. I understand that O(1) means it takes the same amount of time regardless of input size. I think that O(n) means that it scales at most, proportional to the input.

When trying to identify what the corresponding notation is for a supplied algorithm, I'm giving myself rules such as if it has a nested loop its n squared etc. I've been told in the lecture we are not to do that... doh :( However I'm not sure of the best way of doing other than trying to identify such "rules". Most of that knowledge was picked up from reading answers to stack overflow questions such as "Describe big O notation to a ten year old" and similar topics.

I'm getting the feeling I should have a look at some high school math text books and just start all over again, however much of a horrific thought that is. I think its all about identifying what aspects of math I need to learn and in what order, I think this is essentially the question I'm asking in this thread..

I really want to do well in this and I know I'm smart enough, its all those days skipping school messing around with girls and getting high are really coming back to haunt me.
 
fitten":2et83qre said:
Well... that *is* what the notes say in the first place... you just aren't familiar with the language. You might as well ask a translator who has translated something from another language for you why didn't the original speaker just say that in English in the first place ;)

Thats a fair point, its a emotional response I'm having I'll admit. It just feels like its being obfuscated and there would be benefit to include another way of communicating the concept.
 
DEFINITION: f(n) is in O(g(n)) if and only if c and N>0
exist such that for all n > N, f(n) ≤ c• g(n)
A function f(n) is in complexity class g(n) if and only if there are values c and N such that for all values of n that are greater than N, f(n) is smaller than or equal to c * g(n).

That is to say, once the problem is "big enough" (where "big enough" is determined by N), its runtime behaviour (the behaviour we usually care about is "time to run", and we quite often care about "memory") is described by a simple inequality.

So, for example, a particular sorting algorithm might be described as having runtime in O(n log n). This means that, once you're sorting a sufficient number of items (i.e. it might not be true if you're only sorting a few), the time to sort will be less than or equal to the number of items, multiplied by the base 2 logarithm of the number of items, multiplied by some constant.

The big-O notation, and especially the sloppy way in which people tend to use it, is a bit odd.

Edit; wrong logarithm, durrrrrr.
 
DrPizza":1rqsd9ed said:
DEFINITION: f(n) is in O(g(n)) if and only if c and N>0
exist such that for all n > N, f(n) ≤ c• g(n)
A function f(n) is in complexity class g(n) if and only if there are values c and N such that for all values of n that are greater than N, f(n) is smaller than or equal to c * g(n).

That is to say, once the problem is "big enough" (where "big enough" is determined by N), its runtime behaviour (the behaviour we usually care about is "time to run", and we quite often care about "memory") is described by a simple inequality.

So, for example, a particular sorting algorithm might be described as having runtime in O(n log n). This means that, once you're sorting a sufficient number of items (i.e. it might not be true if you're only sorting a few), the time to sort will be less than or equal to the number of items, multiplied by the natural logarithm of the number of items, multiplied by some constant.

The big-O notation, and especially the sloppy way in which people tend to use it, is a bit odd.

Thanks for your response, I understand your explanation to a point. Some of the questions that immediately jump at me is what does the "g" represent? The n log n part isint clear to me what it is describing.

DrPizza did you go through the UK education system? Is there a part of the gcse/A level syllabus that directly relates to the math I'm encountering now and will in the future on this course?

Considering the amount of ground I need to make up I'm considering trying to find a suitable tutor in London, it shouldn't be too difficult, I hope.

Oh and are you available for hire for maths tutoring? ;) :D
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
DrPizza did you go through the UK education system? Is there a part of the gcse/A level syllabus that directly relates to the math I'm encountering now and will in the future on this course?

I did Maths and Further Maths at A Level (Pure and Applied, no stats). I found very little of it useful directly in CS apart from proof by induction (not even sure that's on the curriculum these days). I foud that the Maths I had to do at uni covered a great deal of it.

The key thing conceptually for me was proofs really. At uni in Maths you pretty much immediately stop doing anything "useful" and start working from the basics up (then later you start realizing how much of the basics you took for granted). This rigorous splitting up of something into precisely defined component parts (just look at how DrPizza wrote the english language translation, things like "if and only if" you approach things rigorously).

I'm not sure a secondary maths class is going to help with that. If you're at uni they may well be able to pop you on an additional maths intro course that will establish much of the short hand of functional descriptions (though your uni really should have covered that already, perhaps you can get your lecturer to go over it for you)
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
Oh, another tip. You get better at this sort of thing through practice. Much like a spoken or written language I found.

Try 'translating' some examples of this into precise formal English. Get your tutor to review your work and tell you how and where you went wrong. Keep practicing. Never think you can "skip" something from a lecture. Instead if you lost the thread revisit the notes (I hope they provide them rather than forcing you to copy as you go - easy to have transcription errors there) and work through it bit by bit till you do.
 
ShuggyCoUk":1yin7ugu said:
DrPizza did you go through the UK education system? Is there a part of the gcse/A level syllabus that directly relates to the math I'm encountering now and will in the future on this course?

I did Maths and Further Maths at A Level (Pure and Applied, no stats). I found very little of it useful directly in CS apart from proof by induction (not even sure that's on the curriculum these days). I foud that the Maths I had to do at uni covered a great deal of it.

The key thing conceptually for me was proofs really. At uni in Maths you pretty much immediately stop doing anything "useful" and start working from the basics up (then later you start realizing how much of the basics you took for granted). This rigorous splitting up of something into precisely defined component parts (just look at how DrPizza wrote the english language translation, things like "if and only if" you approach things rigorously).

I'm not sure a secondary maths class is going to help with that. If you're at uni they may well be able to pop you on an additional maths intro course that will establish much of the short hand of functional descriptions (though your uni really should have covered that already, perhaps you can get your lecturer to go over it for you)

Thats an extremely helpful post, admittedly I sort of coasted through the first year math course. Honestly I was surprised how little work I could do to get a half decent grade! They were too soft in retrospect, I get the feeling I should have failed it now.

So to move on a little bit, does anyone have good suggestions for a intro discrete maths textbook (keeping in mind where I'm coming from, something in the same idea of Oreilly's head first series would be good)?
 
Faramir":pdavn98u said:
From a few posts back it sounds like you don't have a strong grasp of functions e.g. g(x). If that's the case you really need to brush up on pre-calculus.

Yes you are correct, however its 1:30am and I really should be in bed. I'm going to go over what was covered in the first year class and post the different topics for folks here to suggest if anything additional should be included.

Thanks to everyone so far, I'm freaking out a little less now.
 
Thanks for your response, I understand your explanation to a point. Some of the questions that immediately jump at me is what does the "g" represent? The n log n part isint clear to me what it is describing.
g(n) is just any function of n. Common functions of n are:

  • 1
  • n
  • n^2
  • n log n
  • n!
  • e^n

That is to say, most of the functions you will typically study will fall into one of those categories.

DrPizza did you go through the UK education system? Is there a part of the gcse/A level syllabus that directly relates to the math I'm encountering now and will in the future on this course?
Yes, I did. I did GCSE Maths (1996), A-level Maths (1998), A-level Further Maths (1999), and S-level Maths (1998); all were final paper, non-modular courses. All exams should be, IMO.

I also started, but have (temporarily?) abandoned, an Open University maths degree.

The A-levels gave me a solid foundation in algebra, calculus, and so on and so forth, but we did not cover computational complexity. It is covered in some degree-level courses.

There's no substitute for doing the A-levels. Unlike any single degree-level course, the A-levels are broad, and will cover a wide range of topics. It's a little different in these days of modular exams, which displeases me no end, because you can do lots of narrow exams instead of one broad one.

Considering the amount of ground I need to make up I'm considering trying to find a suitable tutor in London, it shouldn't be too difficult, I hope.
I would imagine that your university or the OU would be able to point you at a suitable remedial mathematics course, although I'm a little surprised that you are in your second year already.

Oh and are you available for hire for maths tutoring? ;) :D
I don't think that would be good for either of us.
 
MistaMagoo":89k1mfz4 said:
Thanks for your response, I understand your explanation to a point. Some of the questions that immediately jump at me is what does the "g" represent? The n log n part isint clear to me what it is describing.
'g' is just a function. O(g(n)) is a set of functions and the definition is telling you which functions are in that set. For example, if you're given f(n) = 2*n + 3 and g(n) = 2^n you can tell whether or not 2*n + 3 is in O(2^n) by figuring out if there are values for c and N that make the statement "for all n > N, f(n) ≤ c*g(n)" true.

n log n is just another function, 'n multiplied by the natural logarithm of n'.
 
DrPizza":drlrwigx said:
although I'm a little surprised that you are in your second year already.

Yeah I am way behind in the maths, my only redeeming feature is that all my other subjects have not really caused me any difficulty due to my work experience. So I can afford to spend the majority of my time on the algorithms class. Shuggy's point that A levels would cover a lot of topics that are not relevant is giving me some confidence that with some focused revision I can catch up. I can appreciate that comment may sound bad, but to be blunt I'm working towards being a developer not a computer scientist

I'm very happy that I've managed to find a Head first series book dealing with algebra (http://www.headfirstlabs.com/books/hfalg/), I'm hoping that going through that book to start will help refresh some of the fundamentals before going on to revise the first year maths class.
 
A-levels are the most relevant thing you can study; if you can get an A at A-level, particularly in proper, final exam, non-modular A-levels, you have proven yourself to be able to familiar with, and able to manipulate, the foundational subjects to handle all mathematics.

It's much the same as the "calculus" thread a few posts down; programmers may not often use calculus on a day-to-day basis, but the underlying principle of rule application and algorithmic processing--the skills that enable them to perform calculus--are fundamental to software development.

Unless you are intimately familiar with the bread-and-butter subjects, you will always be at a disadvantage, relative to your peers, when working on those bits of mathematics that are more useful to computer science.

Case in point: your broad unfamiliarity with functions and algebra. A good grade at A-level would make these things second nature.
 
DrPizza":7g2yi50q said:
A-levels are the most relevant thing you can study; if you can get an A at A-level, particularly in proper, final exam, non-modular A-levels, you have proven yourself to be able to familiar with, and able to manipulate, the foundational subjects to handle all mathematics.

It's much the same as the "calculus" thread a few posts down; programmers may not often use calculus on a day-to-day basis, but the underlying principle of rule application and algorithmic processing--the skills that enable them to perform calculus--are fundamental to software development.

Unless you are intimately familiar with the bread-and-butter subjects, you will always be at a disadvantage, relative to your peers, when working on those bits of mathematics that are more useful to computer science.

Case in point: your broad unfamiliarity with functions and algebra. A good grade at A-level would make these things second nature.

I'm more focused on what's required for me to perform well for this particular course, right now. I'd be very concerned that taking A levels would increase my work load too much and have a negative knock on effect elsewhere. I feel I need to focus on whats being demanded, when the time comes after I've graduated I'll look to cast my net a bit wider.

In addition, the reality is none of the products that I've worked on have used anything above basic arithmetic. From my professional experience so far it appears that the vast majority of software development is essentially CRUD applications moving bits of data around with the odd bit of writing a file or something similar. I'm aware that there are plenty of posters here involved in very advanced development it shows with comments made relating to java shops and the like. As boring as it sounds, for the immediate future I'm ok with just writing such CRUD applications, its low stress but well paid work.
 

Tuborg

Ars Tribunus Militum
2,911
MistaMagoo":14f8ybuk said:
DrPizza":14f8ybuk said:
DEFINITION: f(n) is in O(g(n)) if and only if c and N>0
exist such that for all n > N, f(n) ≤ c• g(n)
A function f(n) is in complexity class g(n) if and only if there are values c and N such that for all values of n that are greater than N, f(n) is smaller than or equal to c * g(n).

That is to say, once the problem is "big enough" (where "big enough" is determined by N), its runtime behaviour (the behaviour we usually care about is "time to run", and we quite often care about "memory") is described by a simple inequality.

So, for example, a particular sorting algorithm might be described as having runtime in O(n log n). This means that, once you're sorting a sufficient number of items (i.e. it might not be true if you're only sorting a few), the time to sort will be less than or equal to the number of items, multiplied by the natural logarithm of the number of items, multiplied by some constant.

The big-O notation, and especially the sloppy way in which people tend to use it, is a bit odd.

Thanks for your response, I understand your explanation to a point. Some of the questions that immediately jump at me is what does the "g" represent? The n log n part isint clear to me what it is describing.

The function "g" represents the time taken for the algorithm to run given input of size n. For anything but the most trivial of algorithms, this function is impossible to write down; there are just too many cases. So instead, we can approximate the runtime in some useful fashion. And big-O notation provides one way of doing that. You just try to measure the runtime of the most intensive part of the algorithm, because for inputs sufficiently large this will dominate the length of time taken for the algorithm to run. Sometimes instead of the most intensive part of the algorithm, you try to measure the average case (and big-O notation is still used).

The best way to learn a mathematical definition is to use it. I would compare the big-O runtime of some sorting algorithms which you're probably already familiar with. I think it's pretty easy to see why bubble sort is O(n^2): you have to compare (in the worse case) every pair of elements in the list. How many pairs are there? There are n*(n-1)/2... or roughly n^2.

Quick sort is an example of where the average case is analyzed.
 

sid0

Ars Tribunus Militum
2,364
The function "g" represents the time taken for the algorithm to run given input of size n.
No, f(n) represents the time taken for the algorithm to run given input of size n. From my experience with TAing it is very important to get what seem like minor details to us right so that someone new to all this isn't confused even more.

I think the point where you can start noticing and correcting errors in textbooks is the point where you've gained competence in the area.

You just try to measure the runtime of the most intensive part of the algorithm
Again, no, that is not what worst case means. Worst case for n means the maximum amount of time/space an algorithm takes given any input of size n. So if for every n, (2^n) - 1 of the inputs finish in time \Theta(n^2) but one input finishes in time \Theta(2^n) then the worst case time complexity is \Theta(2^n). And I didn't just dream this up: for a lot of NP-complete problems, "commonly-seen" or "real-world" examples are easy to solve. I'm referring to cases like SAT here.

Of course, average case complexity drags in its own problems, such as what random distribution to work with. Quick sort is average case O(n log n) assuming a uniform random distribution. Depending on the pivot selection method you can generally find a distribution that'll fuck you over. For instance if the pivot's always chosen to be the first element then for a distribution where elements are "almost sorted" your average case is pretty bad.
 
Status
Not open for further replies.