L1 Preliminary
L1 Preliminary
ALGORITHMS ANALYSIS
Lecturer & Tutor: Savio TSE
E-mail: [email protected]
Tutorials: LDK 2
T1+T2: Monday, 1pm-2am
T5+T6: Monday, 2pm-3pm
T7+T8: Wednesday, 2pm-3pm
T4+T9: Wednesday, 3am-4am
T10+T11: Thursday, 12pm-1pm
T3+T12: Thursday, 1pm-2pm
1-11
The most important
question:
1-21
What is algorithm?
AnALGORITHM can be considered as a
tool for solving a computational
problem.
1-22
What is algorithm?
Algorithm + Data Structure
=
Spirit of a program
1-23
What is algorithm?
1. terminates in a finite number of steps
2. rigorously & unambiguously specified
3. input & output domains are clearly
defined
4. Correctness can be proven
5. steps are sufficiently simple and
primitive
1-24
Why Theoretical Computer Science?
Theoretical Computer Science
Discrete Math, Algorithm, Data structure, …
System
Operating Systems, Networking, Language, …
Software Engineering
A subject for developing large system.
Applications
Without applications, computer is useless!
All these branches together form the core of
Computer Science.
You can decide which one is core to yourself.1-25
What is algorithm analysis? (1)
Algorithm Analysis
We want to analyze
the execution of the
algorithm
1-29
What is algorithm analysis? (2)
Algorithm Analysis
We want to analyze
the execution of the
algorithm
1-30
What is algorithm analysis? (3)
Algorithm Analysis
We want to analyze
the execution of the
algorithm
1-31
What is algorithm analysis? (4)
Algorithm Analysis
We want to analyze
the execution of the
algorithm
1-32
Time vs Space Trade-off
Two algorithms designed for solving the same problem won’t have such
a big difference in memory requirements. (normally)
SPACE complexity plays a more theoretical role. Here we can make the
hierarchy of problem classes finer.
1-33
Basic Issues Related to Algorithms
1-35
Fundamental data structures
• array
• linked list
• tree
• I think, in the next 30 years,
there will only be these three
fundamental data structures. 1-36
Other common data structures
list
stack
queue
priority queue
Binary tree
1-37
Some Well-known Computational Problems
Sorting
Searching
Shortest paths in a graph
Minimum spanning tree
Primality testing
Traveling salesman problem (NP hard)
Knapsack problem (NP hard)
Chess
Towers of Hanoi
1-38
Fundamental Data Structures
Array
Linked-list
Tree
Array Revisit – linear search – O(n) time
Head pointer
Null
Head pointer
Null
Linked-List Revisit
HeadNode L;
L.Next = NULL;
// L is the header node.
Linked-List Revisit
Head pointer
Null Node* L = NULL;
// L is the head pointer,
// initialized to NULL
Linked-List Revisit
Head pointer
Null
#define N 1000
struct node {
int element;
node* next[N];
};
struct node {
int element;
node* child; // red
node* sibling; // black, horizontal
};
Tree – Revisit
Physical tree
root root
rootptr
treeheadnode
NULL NULL
Binary tree Revisit
Traversal algorithms:
Preorder traversal
Inorder traversal
Postorder traversal
Binary tree Revisit
Preorder traversal
Postorder traversal
Preorder traversal
Inorder traversal
Inorder traversal
Postorder traversal
x x