Algorithm and Process Algorithm Process
Algorithm and Process Algorithm Process
FIT1045
Algorithm and Process
Algorithm Process
A finite sequence of steps for A sequence of steps for problem
Definition
problem solving solving
Finiteness Infiniteness (not in syllabus)
Difference Þ The code must terminate after Þ The code does not have to
finite steps terminate
1. Definiteness
Þ Each step must be precise and unambiguous
2. Input
Þ Zero or more
Similarities
3. Output
Þ One or more
4. Effectiveness
Each step must be simple and can be done in a finite time
Definitions
A path that passes through every vertex exactly once
Hamiltonian Cycle
and returns back to the start
O: A subset of vertices such that every edge in the graph
has at least one vertex
Vertex Cover
S: A subset of vertices of a graph such that every edge is
connected to at least one vertex.
O: A subgraph such that there exists an edge between
every two vertices
Clique
S: Each vertex is connected to all other vertices with an
edge
P Class Decision problem that can be solved in polynomial time
Decision problems whose solutions can be verified in
NP Class polynomial time
**Solution (like certificate) is NP**
O: Original; S: Simplified
**NP is not equivalent to Non-Polynomial**
NP Certificates
Problem Certificate
1. Hamiltonian cycle
Ordered set of vertices / cities
2. TSP
1. Vertex cover
2. Clique Set of vertices
3. Independent set
lOMoARcPSD|7981319
Terminologies
Tree Terms Height Terms
Highest: Level 4
Heap Conditions
• for max-heap, the children values must be less than or equal to the parent’s value
• for min-heap, the children values must be more than or equal to the parent’s value
• fill up children’s position from left to right
Binary Tree Conditions
• every node can have at most two children
• every subtree is a binary tree
• every empty tree is a binary tree
Balanced Binary Tree
• For every node, the | height of the left subtree – height of right subtree | ≤ 1
Stack and Queues
Stack Queue
Last In First Out (LIFO) First In First Out (FIFO)
• .pop() • .pop(0)
• .append() • .append()
• len • len
Invariance
Sort Invariant
In the kth iteration of the loop, the first k items are sorted and
Selection
have the smallest elements in the list
In the kth iteration of the loop, the first k items are sorted, but
Insertion
they are not the smallest elements in the list
List Operations
Operation Description
a.count(x) Return the number of times item x
a.index(x) Returns the first position
a.insert(k, x) Inserts item x at index k
a.reverse() Reverses all the items
lOMoARcPSD|7981319
Graph Representations
Type Description
0 1 2 3
0 0 1 0 0
1 1 0 1 1
Adjacency Matrix 2 0 1 0 1
3 0 1 1 0
For weighted: replace the 0 and 1 with the weight
0 1
1 0, 2, 3
Adjacency List
2 1, 3
3 2, 1
Edge List [(0,1), (1,2), (1,3), (2,3)]
Divide and Conquer
Divide Conquer
Conquer by solving the small instances and
Divide the instance of a problem into 2 or
General case combine solutions to obtain solution to
more smaller instances
bigger instances
Binary search Dividing the list into half It searches the target in the mid
Repeatedly merging the sub-lists to
Merge sort Divide the list into sub-lists of length 1 become a new sorted sub-list until there is
only one sorted list
Choosing one element in the list as pivot.
Partition them into a left list (for < pivot) Combine the small sub-lists until it
Quick sort
and a right list (for > pivot) until the list is becomes only one sorted list.
trivial to sort (empty or one item)
General Backtracking Algorithm
def backtracking(partialSolution, Parameters(eg: N):
possibleSolution = getPossible(partialSolution)
if possibleSolution == []:
print(partialSolution)
else:
for item in possibleSolution:
partialSolution.append(item)
backtracking(partialSolution, Parameters)
partialSolution.pop()