This course introduces basic elements of the design and analysis of computer algorithms. Topics include asymptotic notations and analysis, divide and conquer strategy, greedy methods, dynamic programming, basic graph algorithms, NP-completeness, and approximation algorithms. For each topic, beside in-depth coverage, one or more representative problems and their algorithms shall be discussed.
CSC325Semester V3 credit hours8 units72 solved questions947 study cardsFull study modes
This course introduces basic elements of the design and analysis of computer algorithms. Topics include asymptotic notations and analysis, divide and conquer strategy, greedy methods, dynamic programming, basic graph algorithms, NP-completeness, and approximation algorithms. For each topic, beside in-depth coverage, one or more representative problems and their algorithms shall be discussed.
The free sample is open to everyone. Sign in for 2 free weeks of every answer, flashcards and progress.
This unit sets up the vocabulary and machinery the rest of the course depends on: what an algorithm is, the RAM model used to count its steps, the time and space measures built on that count, aggregate analysis for a sequence of operations, the asymptotic notations that describe growth, and the recurrence relations that recursive algorithms produce together with the three standard techniques for solving them.
An algorithm is a finite set of unambiguous instructions that takes some value(s) as input, performs a well-defined computation, and produces value(s) as output in a finite amount of time. A procedure only qualifies as an algorithm if it satisfies the following properties: input/output (it accepts zero or more inputs and produces at least one output), correctness (it produces the output required by the problem statement for every valid input), finiteness (it terminates after a finite number of steps, never looping forever), feasibility (every instruction is basic enough to be carried out, in principle by hand, in finite time), flexibility (it can be modified or extended without a full redesign), and efficiency (it uses as little running time and memory as the problem allows). An algorithm is distinct from a program: the algorithm is the language-independent idea, the program is its expression in a particular programming language.
The Random Access Machine (RAM) model is the abstract machine used to count the number of steps an algorithm performs, independent of any real processor's clock speed or instruction set. Under the RAM model, each simple arithmetic operation ($+$, $-$, $\times$, $/$), comparison, assignment, and memory reference (reading or writing a variable or array cell) costs exactly one step, and every word is assumed large enough to hold any value the algorithm manipulates, so a single word operation is $O(1)$ regardless of the magnitude of the number stored. Control constructs such as loops and function calls are not themselves basic operations; their cost is the sum of the costs of the statements they execute, counted once per execution. This uniform-cost assumption is what allows time complexity to be expressed as a function of the input size $n$ alone, rather than of the underlying hardware.
The time complexity of an algorithm is the number of elementary steps, as counted under the RAM model, that it performs as a function of the input size $n$. The space complexity is the amount of memory, again counted as a function of $n$, that the algorithm needs to hold its input, output, and working variables. Because the exact step count depends on the particular input and not only on its size, complexity is reported for three cases: the best case is the minimum running time over all inputs of size $n$, a lower bound on how fast the algorithm can possibly run at that size; the worst case is the maximum running time over all inputs of size $n$, and is the figure normally quoted as "the" complexity because no input of that size can force the algorithm to run any slower; the average case is the expected running time over all inputs of size $n$, usually under a uniform probability assumption on the inputs.
Two rules of thumb keep the analysis of a piece of code manageable. First, statements that run in constant time regardless of $n$, assignments, printf, scanf, return, contribute only an additive constant and are neglected once a loop dominates the count. Second, the cost of loops is what matters:
for (i = 1; i <= n; i *= c), runs $O(\log_c n) = O(\log n)$ times, since the index takes only that many multiplications to reach $n$.Applying this to a complete factorial program:
int main(void) {
int i, n, fact = 1;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fact = fact * i;
printf("Factorial = %d\n", fact);
return 0;
}
counting under the RAM model: the prompt and the read are $1$ step each; the loop initialization i = 1 is $1$ step; the test i <= n runs once per iteration including the final failing test, so $n$ times; the increment i++ and the body fact = fact * i each run once per successful iteration, so $(n-1)$ times each; the final print and the return are $1$ step each. Summing,
$$ \begin{aligned} T(n) &= \underbrace{2}{\text{prompt, scanf}} + \underbrace{1}{i=1} + \underbrace{n}{i \le n} + \underbrace{(n-1)}{i++} + \underbrace{(n-1)}{\text{fact}=\text{fact}\times i} + \underbrace{2}{\text{print, return}} \ &= 3n + 3. \end{aligned} $$
So $T(n) = 3n + 3 = \Theta(n)$: linear in $n$, exactly as the informal "one loop, one pass" rule predicts. The space used is three words, for i, n, and fact, none of which grow with $n$, so the space complexity is $O(1)$.
Design and Analysis of Algorithms for TU BSc CSIT is covered here in full, from syllabus notes to solved past questions. This subject has 8 chapters mapped to the official syllabus, and 72 past exam questions with written model answers. The opening of every note and answer is free, and the most-searched questions are free in full. A free account gets 2 free weeks of the whole program plus flashcards and progress tracking so weak topics come back before the exam.
This TU BSc CSIT Design and Analysis of Algorithms guide covers Foundation of Algorithm Analysis, Iterative Algorithms, Divide and Conquer Algorithms, Greedy Algorithms, Dynamic Programming, Backtracking, Number Theoretic Algorithms, NP Completeness, across 8 chapters. It includes model answers to 72 past exam questions, plus flashcards for quick revision.
Design and Analysis of Algorithms has 8 chapters mapped to the official syllabus.
Yes. Each of the 72 past questions comes with a written model answer; the most-searched ones are free in full, the rest open with a free account's 2 free weeks or a pass.
Free to start. The opening of every note and answer is free, the most-searched questions are free in full, and a free account gets 2 free weeks of the whole program plus flashcards and progress tracking. After that a program pass keeps everything open.
Based on the official syllabus.
This unit sets up the vocabulary and machinery the rest of the course depends on: what an algorithm is, the RAM model used to count its steps, the time and space measures built on that count, aggregate analysis for a sequence of operations, the asymptotic notations that describe growth, and the recurrence relations that recursive algorithms produce together with the three standard techniques for solving them.
An algorithm is a finite set of unambiguous instructions that takes some value(s) as input, performs a well-defined computation, and produces value(s) as output in a finite amount of time. A procedure only qualifies as an algorithm if it satisfies the following properties: input/output (it accepts zero or more inputs and produces at least one output), correctness (it produces the output required by the problem statement for every valid input), finiteness (it terminates after a finite number of steps, never looping forever), feasibility (every instruction is basic enough to be carried out, in principle by hand, in finite time), flexibility (it can be modified or extended without a full redesign), and efficiency (it uses as little running time and memory as the problem allows). An algorithm is distinct from a program: the algorithm is the language-independent idea, the program is its expression in a particular programming language.
The Random Access Machine (RAM) model is the abstract machine used to count the number of steps an algorithm performs, independent of any real processor's clock speed or instruction set. Under the RAM model, each simple arithmetic operation (, , , ), comparison, assignment, and memory reference (reading or writing a variable or array cell) costs exactly one step, and every word is assumed large enough to hold any value the algorithm manipulates, so a single word operation is regardless of the magnitude of the number stored. Control constructs such as loops and function calls are not themselves basic operations; their cost is the sum of the costs of the statements they execute, counted once per execution. This uniform-cost assumption is what allows time complexity to be expressed as a function of the input size alone, rather than of the underlying hardware.
The time complexity of an algorithm is the number of elementary steps, as counted under the RAM model, that it performs as a function of the input size . The space complexity is the amount of memory, again counted as a function of , that the algorithm needs to hold its input, output, and working variables. Because the exact step count depends on the particular input and not only on its size, complexity is reported for three cases: the best case is the minimum running time over all inputs of size , a lower bound on how fast the algorithm can possibly run at that size; the worst case is the maximum running time over all inputs of size , and is the figure normally quoted as "the" complexity because no input of that size can force the algorithm to run any slower; the average case is the expected running time over all inputs of size , usually under a uniform probability assumption on the inputs.
Two rules of thumb keep the analysis of a piece of code manageable. First, statements that run in constant time regardless of , assignments, printf, scanf, return, contribute only an additive constant and are neglected once a loop dominates the count. Second, the cost of loops is what matters:
for (i = 1; i <= n; i *= c), runs times, since the index takes only that many multiplications to reach .Applying this to a complete factorial program:
int main(void) {
int i, n, fact = 1;
printf("Enter n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
fact = fact * i;
printf("Factorial = %d\n", fact);
return 0;
}
counting under the RAM model: the prompt and the read are step each; the loop initialization i = 1 is step; the test i <= n runs once per iteration including the final failing test, so times; the increment i++ and the body fact = fact * i each run once per successful iteration, so times each; the final print and the return are step each. Summing,
So : linear in , exactly as the informal "one loop, one pass" rule predicts. The space used is three words, for i, n, and fact, none of which grow with , so the space complexity is .