Unit - 1
Unit- 1
Design and Analysis of Algorithm
Design and Analysis of Algorithm
INTRODUCTION
INTRODUCTION
Dr. K. RANJITH SINGH
Dr. K. RANJITH SINGH
Assistant Professor
Assistant Professor
Department of Computer Science
Department of Computer Science
Karpagam Academy of Higher Education
Karpagam Academy of Higher Education
(Deemed to be University)
(Deemed to be University)
Coimbatore
Coimbatore
2.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-2
What is an algorithm?
What is an algorithm?
An
An algorithm
algorithm is a sequence of unambiguous instructions
is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
for solving a problem, i.e., for obtaining a required
output for any
output for any legitimate
legitimate input in a finite amount of
input in a finite amount of
time.
time.
“computer”
problem
algorithm
input output
3.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-3
Algorithm
Algorithm
An
An algorithm
algorithm is a sequence of unambiguous
is a sequence of unambiguous
instructions for solving a problem, i.e., for
instructions for solving a problem, i.e., for
obtaining a required output for any legitimate
obtaining a required output for any legitimate
input in a finite amount of time.
input in a finite amount of time.
• Can be represented various forms
• Unambiguity/clearness
• Effectiveness
• Finiteness/termination
• Correctness
4.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-4
Historical Perspective
Historical Perspective
Euclid’s algorithm for finding the greatest common divisor
Euclid’s algorithm for finding the greatest common divisor
Muhammad ibn Musa al-Khwarizmi – 9
Muhammad ibn Musa al-Khwarizmi – 9th
th
century
century
mathematician
mathematician
www.lib.virginia.edu/science/parshall/khwariz.html
www.lib.virginia.edu/science/parshall/khwariz.html
5.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-5
Notion of algorithm and problem
Notion of algorithm and problem
“computer”
algorithmic solution
(different from a conventional solution)
problem
algorithm
input
(or instance)
output
6.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-6
Example of computational problem: sorting
Example of computational problem: sorting
Statement of problem:
Statement of problem:
• Input:
Input: A sequence of
A sequence of n
n numbers <a
numbers <a1
1,
, a
a2
2, …, a
, …, an
n>
>
• Output:
Output: A reordering of the input sequence <a
A reordering of the input sequence <a´
´
1
1,
, a
a´
´
2
2, …, a
, …, a´
´
n
n> so that
> so that
a
a´
´
i
i ≤
≤ a
a´
´
j
j whenever
whenever i
i <
< j
j
Instance: The sequence <5, 3, 2, 8, 3>
Instance: The sequence <5, 3, 2, 8, 3>
Algorithms:
Algorithms:
• Selection sort
Selection sort
• Insertion sort
Insertion sort
• Merge sort
Merge sort
• (many others)
(many others)
7.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-7
Selection Sort
Selection Sort
Input: array
Input: array a[1],…,a[n]
a[1],…,a[n]
Output: array
Output: array a
a sorted in non-decreasing order
sorted in non-decreasing order
Algorithm:
Algorithm:
for i=1 to n
swap a[i] with smallest of a[i],…,a[n]
• Is this unambiguous? Effective?
• See also pseudocode, section 3.1
8.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-8
Some Well-known Computational Problems
Some Well-known Computational Problems
Sorting
Sorting
Searching
Searching
Shortest paths in a graph
Shortest paths in a graph
Minimum spanning tree
Minimum spanning tree
Primality testing
Primality testing
Traveling salesman problem
Traveling salesman problem
Knapsack problem
Knapsack problem
Chess
Chess
Towers of Hanoi
Towers of Hanoi
Program termination
Program termination
Some of these problems don’t have efficient algorithms,
or algorithms at all!
9.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-9
Basic Issues Related to Algorithms
Basic Issues Related to Algorithms
How to design algorithms
How to design algorithms
How to express algorithms
How to express algorithms
Proving correctness
Proving correctness
Efficiency (or complexity) analysis
Efficiency (or complexity) analysis
• Theoretical analysis
Theoretical analysis
• Empirical analysis
Empirical analysis
Optimality
Optimality
10.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-10
Algorithm design strategies
Algorithm design strategies
Brute force
Brute force
Divide and conquer
Divide and conquer
Decrease and conquer
Decrease and conquer
Transform and conquer
Transform and conquer
Greedy approach
Greedy approach
Dynamic programming
Dynamic programming
Backtracking and branch-and-bound
Backtracking and branch-and-bound
Space and time tradeoffs
Space and time tradeoffs
11.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-11
Analysis of Algorithms
Analysis of Algorithms
How good is the algorithm?
How good is the algorithm?
• Correctness
Correctness
• Time efficiency
Time efficiency
• Space efficiency
Space efficiency
Does there exist a better algorithm?
Does there exist a better algorithm?
• Lower bounds
Lower bounds
• Optimality
Optimality
12.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-12
What is an algorithm?
What is an algorithm?
Recipe, process, method, technique, procedure, routine,…
Recipe, process, method, technique, procedure, routine,…
with the following requirements:
with the following requirements:
1.
1. Finiteness
Finiteness
terminates after a finite number of steps
terminates after a finite number of steps
2.
2. Definiteness
Definiteness
rigorously and unambiguously specified
rigorously and unambiguously specified
3.
3. Clearly specified input
Clearly specified input
valid inputs are clearly specified
valid inputs are clearly specified
4.
4. Clearly specified/expected output
Clearly specified/expected output
can be proved to produce the correct output given a valid input
can be proved to produce the correct output given a valid input
5.
5. Effectiveness
Effectiveness
steps are sufficiently simple and basic
steps are sufficiently simple and basic
13.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-13
Why study algorithms?
Why study algorithms?
Theoretical importance
Theoretical importance
• the core of computer science
the core of computer science
Practical importance
Practical importance
• A practitioner’s toolkit of known algorithms
A practitioner’s toolkit of known algorithms
• Framework for designing and analyzing algorithms for new
Framework for designing and analyzing algorithms for new
problems
problems
Example: Google’s PageRank Technology
14.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-14
Euclid’s Algorithm
Euclid’s Algorithm
Problem: Find gcd(
Problem: Find gcd(m,n
m,n), the greatest common divisor of two
), the greatest common divisor of two
nonnegative, not both zero integers
nonnegative, not both zero integers m
m and
and n
n
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Examples: gcd(60,24) = 12, gcd(60,0) = 60, gcd(0,0) = ?
Euclid’s algorithm is based on repeated application of equality
Euclid’s algorithm is based on repeated application of equality
gcd(
gcd(m,n
m,n) = gcd(
) = gcd(n, m
n, m mod
mod n
n)
)
until the second number becomes 0, which makes the problem
until the second number becomes 0, which makes the problem
trivial.
trivial.
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
Example: gcd(60,24) = gcd(24,12) = gcd(12,0) = 12
15.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-15
Two descriptions of Euclid’s algorithm
Two descriptions of Euclid’s algorithm
Step 1 If
Step 1 If n
n = 0, return
= 0, return m
m and stop; otherwise go to Step 2
and stop; otherwise go to Step 2
Step 2
Step 2 Divide
Divide m
m by
by n
n and assign the value of the remainder to
and assign the value of the remainder to r
r
Step 3 Assign the value of
Step 3 Assign the value of n
n to
to m
m and the value of
and the value of r
r to
to n.
n. Go to
Go to
Step 1.
Step 1.
while
while n
n ≠ 0
≠ 0 do
do
r ← m
r ← m mod
mod n
n
m← n
m← n
n ← r
n ← r
return
return m
m
16.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-16
Other methods for computing gcd(
Other methods for computing gcd(m,n
m,n)
)
Consecutive integer checking algorithm
Consecutive integer checking algorithm
Step 1 Assign the value of min{
Step 1 Assign the value of min{m,n
m,n} to
} to t
t
Step 2
Step 2 Divide
Divide m
m by
by t.
t. If the remainder is 0, go to Step 3;
If the remainder is 0, go to Step 3;
otherwise, go to Step 4
otherwise, go to Step 4
Step 3
Step 3 Divide
Divide n
n by
by t.
t. If the remainder is 0, return
If the remainder is 0, return t
t and stop;
and stop;
otherwise, go to Step 4
otherwise, go to Step 4
Step 4 Decrease
Step 4 Decrease t
t by 1 and go to Step 2
by 1 and go to Step 2
Is this slower than Euclid’s algorithm?
How much slower?
O(n), if n <= m , vs O(log n)
17.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-17
Other methods for gcd(
Other methods for gcd(m,n
m,n) [cont.]
) [cont.]
Middle-school procedure
Middle-school procedure
Step 1 Find the prime factorization of
Step 1 Find the prime factorization of m
m
Step 2 Find the prime factorization of
Step 2 Find the prime factorization of n
n
Step 3 Find all the common prime factors
Step 3 Find all the common prime factors
Step 4 Compute the product of all the common prime factors
Step 4 Compute the product of all the common prime factors
and return it as gcd
and return it as gcd(m,n
(m,n)
)
Is this an algorithm?
Is this an algorithm?
How efficient is it?
How efficient is it?
Time complexity: O(sqrt(n))
18.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-18
Sieve of Eratosthenes
Sieve of Eratosthenes
Input:
Input: Integer
Integer n
n ≥
≥ 2
2
Output: List of primes less than or equal to
Output: List of primes less than or equal to n
n
for
for p
p ← 2
← 2 to
to n
n do
do A
A[
[p
p] ←
] ← p
p
for
for p
p ← 2
← 2 to
to n
n do
do
if
if A
A[
[p
p]
]
0 //
0 //p
p hasn’t been previously eliminated from the list
hasn’t been previously eliminated from the list
j
j ←
← p
p*
* p
p
while
while j
j ≤
≤ n
n do
do
A
A[
[j
j]
] ← 0
← 0 //mark element as eliminated
//mark element as eliminated
j
j ←
← j
j + p
+ p
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Example: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Time complexity: O(n)
19.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-19
Two main issues related to algorithms
Two main issues related to algorithms
How to design algorithms
How to design algorithms
How to analyze algorithm efficiency
How to analyze algorithm efficiency
20.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-20
Algorithm design techniques/strategies
Algorithm design techniques/strategies
Brute force
Brute force
Divide and conquer
Divide and conquer
Decrease and conquer
Decrease and conquer
Transform and conquer
Transform and conquer
Space and time tradeoffs
Space and time tradeoffs
Greedy approach
Greedy approach
Dynamic programming
Dynamic programming
Iterative improvement
Iterative improvement
Backtracking
Backtracking
Branch and bound
Branch and bound
21.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-21
Analysis of algorithms
Analysis of algorithms
How good is the algorithm?
How good is the algorithm?
• time efficiency
time efficiency
• space efficiency
space efficiency
• correctness ignored in this course
correctness ignored in this course
Does there exist a better algorithm?
Does there exist a better algorithm?
• lower bounds
lower bounds
• optimality
optimality
22.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-22
Important problem types
Important problem types
sorting
sorting
searching
searching
string processing
string processing
graph problems
graph problems
combinatorial problems
combinatorial problems
geometric problems
geometric problems
numerical problems
numerical problems
23.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-23
Sorting (I)
Sorting (I)
Rearrange the items of a given list in ascending order.
Rearrange the items of a given list in ascending order.
• Input: A sequence of n numbers <a
Input: A sequence of n numbers <a1
1,
, a
a2
2, …, a
, …, an
n>
>
• Output: A reordering <a
Output: A reordering <a´
´
1
1,
, a
a´
´
2
2, …, a
, …, a´
´
n
n> of the input sequence such that a
> of the input sequence such that a
´
´
1
1≤
≤ a
a´
´
2
2 ≤
≤ …
… ≤
≤ a
a´
´
n.
n.
Why sorting?
Why sorting?
• Help searching
Help searching
• Algorithms often use sorting as a key subroutine.
Algorithms often use sorting as a key subroutine.
Sorting key
Sorting key
• A specially chosen piece of information used to guide sorting. E.g., sort
A specially chosen piece of information used to guide sorting. E.g., sort
student records by names.
student records by names.
24.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-24
Sorting (II)
Sorting (II)
Examples of sorting algorithms
Examples of sorting algorithms
• Selection sort
Selection sort
• Bubble sort
Bubble sort
• Insertion sort
Insertion sort
• Merge sort
Merge sort
• Heap sort …
Heap sort …
Evaluate sorting algorithm complexity: the number of key comparisons.
Evaluate sorting algorithm complexity: the number of key comparisons.
Two properties
Two properties
• Stability
Stability: A sorting algorithm is called stable if it preserves the relative order of
: A sorting algorithm is called stable if it preserves the relative order of
any two equal elements in its input.
any two equal elements in its input.
• In place
In place : A sorting algorithm is in place if it does not require extra memory,
: A sorting algorithm is in place if it does not require extra memory,
except, possibly for a few memory units.
except, possibly for a few memory units.
25.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-25
Selection Sort
Selection Sort
Algorithm
Algorithm SelectionSort(A[0..n-1])
SelectionSort(A[0..n-1])
//The algorithm sorts a given array by selection sort
//The algorithm sorts a given array by selection sort
//Input: An array A[0..n-1] of orderable elements
//Input: An array A[0..n-1] of orderable elements
//Output: Array A[0..n-1] sorted in ascending order
//Output: Array A[0..n-1] sorted in ascending order
for i
for i
0 to n – 2 do
0 to n – 2 do
min
min
i
i
for j
for j
i + 1 to n – 1 do
i + 1 to n – 1 do
if A[j] < A[min]
if A[j] < A[min]
min
min
j
j
swap A[i] and A[min]
swap A[i] and A[min]
26.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-26
Searching
Searching
Find a given value, called a
Find a given value, called a search key
search key, in a given set.
, in a given set.
Examples of searching algorithms
Examples of searching algorithms
• Sequential search
Sequential search
• Binary search …
Binary search …
Input: sorted array a_i < … < a_j and key x;
m (i+j)/2;
while i < j and x != a_m do
if x < a_m then j m-1
else i m+1;
if x = a_m then output a_m;
Time: O(log n)
27.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-27
String Processing
String Processing
A string is a sequence of characters from an alphabet.
A string is a sequence of characters from an alphabet.
Text strings: letters, numbers, and special characters.
Text strings: letters, numbers, and special characters.
String matching: searching for a given word/pattern in a
String matching: searching for a given word/pattern in a
text.
text.
Examples:
(i) searching for a word or phrase on WWW or in a
Word document
(ii) searching for a short read in the reference genomic
sequence
28.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-28
Graph Problems
Graph Problems
Informal definition
Informal definition
• A graph is a collection of points called
A graph is a collection of points called vertices
vertices, some of
, some of
which are connected by line segments called
which are connected by line segments called edges
edges.
.
Modeling real-life problems
Modeling real-life problems
• Modeling WWW
Modeling WWW
• Communication networks
Communication networks
• Project scheduling …
Project scheduling …
Examples of graph algorithms
Examples of graph algorithms
• Graph traversal algorithms
Graph traversal algorithms
• Shortest-path algorithms
Shortest-path algorithms
• Topological sorting
Topological sorting
29.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-29
Fundamental data structures
Fundamental data structures
list
list
• array
array
• linked list
linked list
• string
string
stack
stack
queue
queue
priority queue/heap
priority queue/heap
graph
graph
tree and binary tree
tree and binary tree
set and dictionary
set and dictionary
30.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-30
Linear Data Structures
Linear Data Structures
Arrays
Arrays
• A sequence of n items of the same
A sequence of n items of the same
data type that are stored
data type that are stored
contiguously in computer memory
contiguously in computer memory
and made accessible by specifying a
and made accessible by specifying a
value of the array’s index.
value of the array’s index.
Linked List
Linked List
• A sequence of zero or more nodes
A sequence of zero or more nodes
each containing two kinds of
each containing two kinds of
information: some data and one or
information: some data and one or
more links called pointers to other
more links called pointers to other
nodes of the linked list.
nodes of the linked list.
• Singly linked list (next pointer)
Singly linked list (next pointer)
• Doubly linked list (next + previous
Doubly linked list (next + previous
pointers)
pointers)
Arrays
fixed length (need preliminary
reservation of memory)
contiguous memory locations
direct access
Insert/delete
Linked Lists
dynamic length
arbitrary memory locations
access by following links
Insert/delete
…
a1 an
a2 .
31.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-31
Stacks and Queues
Stacks and Queues
Stacks
Stacks
• A stack of plates
A stack of plates
– insertion/deletion can be done only at the top.
insertion/deletion can be done only at the top.
– LIFO
LIFO
• Two operations (push and pop)
Two operations (push and pop)
Queues
Queues
• A queue of customers waiting for services
A queue of customers waiting for services
– Insertion/enqueue from the rear and deletion/dequeue from
Insertion/enqueue from the rear and deletion/dequeue from
the front.
the front.
– FIFO
FIFO
• Two operations (enqueue and dequeue)
Two operations (enqueue and dequeue)
32.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-32
Priority Queue and Heap
Priority Queue and Heap
Priority queues (implemented using heaps)
A data structure for maintaining a set of
elements, each associated with a key/priority,
with the following operations
Finding the element with the highest priority
Deleting the element with the highest priority
Inserting a new element
Scheduling jobs on a shared computer
9
6 8
5 2 3
9 6 5
8 2 3
33.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-33
Graphs
Graphs
Formal definition
Formal definition
• A graph
A graph G = <V, E>
G = <V, E> is defined by a pair of two sets: a
is defined by a pair of two sets: a
finite set V of items called
finite set V of items called vertices
vertices and a set E of vertex
and a set E of vertex
pairs called
pairs called edges
edges.
.
Undirected
Undirected and
and directed
directed graphs (
graphs (digraphs
digraphs).
).
What’s the maximum number of edges in an undirected
What’s the maximum number of edges in an undirected
graph with |V| vertices?
graph with |V| vertices?
Complete, dense,
Complete, dense, and
and sparse
sparse graphs
graphs
• A graph with every pair of its vertices connected by an
A graph with every pair of its vertices connected by an
edge is called complete, K
edge is called complete, K|V|
|V|
1 2
3 4
34.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-34
Graph Representation
Graph Representation
Adjacency matrix
Adjacency matrix
• n x n boolean matrix if |V| is n.
n x n boolean matrix if |V| is n.
• The element on the ith row and jth column is 1 if there’s an
The element on the ith row and jth column is 1 if there’s an
edge from ith vertex to the jth vertex; otherwise 0.
edge from ith vertex to the jth vertex; otherwise 0.
• The adjacency matrix of an undirected graph is symmetric.
The adjacency matrix of an undirected graph is symmetric.
Adjacency linked lists
Adjacency linked lists
• A collection of linked lists, one for each vertex, that contain all
A collection of linked lists, one for each vertex, that contain all
the vertices adjacent to the list’s vertex.
the vertices adjacent to the list’s vertex.
Which data structure would you use if the graph is a 100-node
Which data structure would you use if the graph is a 100-node
star shape?
star shape?
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
2 3 4
4
4
35.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-35
Weighted Graphs
Weighted Graphs
Weighted graphs
Weighted graphs
• Graphs or digraphs with numbers assigned to the edges.
Graphs or digraphs with numbers assigned to the edges.
1 2
3 4
6
8
5
7
9
36.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-36
Graph Properties -- Paths and Connectivity
Graph Properties -- Paths and Connectivity
Paths
Paths
• A path from vertex u to v of a graph G is defined as a sequence of
A path from vertex u to v of a graph G is defined as a sequence of
adjacent (connected by an edge) vertices that starts with u and ends
adjacent (connected by an edge) vertices that starts with u and ends
with v.
with v.
• Simple paths
Simple paths: All edges of a path are distinct.
: All edges of a path are distinct.
• Path lengths: the number of edges, or the number of vertices – 1.
Path lengths: the number of edges, or the number of vertices – 1.
Connected graphs
Connected graphs
• A graph is said to be connected if for every pair of its vertices u and
A graph is said to be connected if for every pair of its vertices u and
v there is a path from u to v.
v there is a path from u to v.
Connected component
Connected component
• The maximum connected subgraph of a given graph.
The maximum connected subgraph of a given graph.
37.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-37
Graph Properties -- Acyclicity
Graph Properties -- Acyclicity
Cycle
Cycle
• A simple path of a positive length that starts and
A simple path of a positive length that starts and
ends a the same vertex.
ends a the same vertex.
Acyclic graph
Acyclic graph
• A graph without cycles
A graph without cycles
• DAG
DAG (Directed Acyclic Graph)
(Directed Acyclic Graph)
1 2
3 4
38.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-38
Trees
Trees
Trees
Trees
• A tree (or
A tree (or free tree
free tree) is a connected acyclic graph.
) is a connected acyclic graph.
• Forest: a graph that has no cycles but is not necessarily connected.
Forest: a graph that has no cycles but is not necessarily connected.
Properties of trees
Properties of trees
• For every two vertices in a tree there always exists exactly one
For every two vertices in a tree there always exists exactly one
simple path from one of these vertices to the other.
simple path from one of these vertices to the other. Why?
Why?
– Rooted trees
Rooted trees:
: The above property makes it possible to select an
The above property makes it possible to select an
arbitrary vertex in a free tree and consider it as the root of the
arbitrary vertex in a free tree and consider it as the root of the
so called rooted tree.
so called rooted tree.
– Levels in a rooted tree.
Levels in a rooted tree.
|E| = |V| - 1 1 3
2 4
5
1
3
2
4 5
rooted
39.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-39
Rooted Trees (I)
Rooted Trees (I)
Ancestors
Ancestors
• For any vertex
For any vertex v
v in a tree
in a tree T
T, all the vertices on the simple path
, all the vertices on the simple path
from the root to that vertex are called ancestors.
from the root to that vertex are called ancestors.
Descendants
Descendants
• All the vertices for which a vertex
All the vertices for which a vertex v
v is an ancestor are said to
is an ancestor are said to
be descendants of
be descendants of v
v.
.
Parent, child
Parent, child and
and siblings
siblings
• If
If (u, v)
(u, v) is the last edge of the simple path from the root to
is the last edge of the simple path from the root to
vertex
vertex v
v,
, u
u is said to be the parent of
is said to be the parent of v
v and
and v
v is called a child
is called a child
of
of u
u.
.
• Vertices that have the same parent are called siblings.
Vertices that have the same parent are called siblings.
Leaves
Leaves
• A vertex without children is called a leaf.
A vertex without children is called a leaf.
Subtree
Subtree
• A vertex
A vertex v
v with all its descendants is called the subtree of
with all its descendants is called the subtree of T
T
rooted at
rooted at v
v.
.
40.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-40
Rooted Trees (II)
Rooted Trees (II)
Depth
Depth of a vertex
of a vertex
• The length of the simple path from the root to the vertex.
The length of the simple path from the root to the vertex.
Height
Height of a tree
of a tree
• The length of the longest simple path from the root to a leaf.
The length of the longest simple path from the root to a leaf.
1
3
2
4 5
h = 2
41.
Karpagam Academy ofHigher Education Design & Analysis of Algorithms, 1-41
Ordered Trees
Ordered Trees
Ordered trees
Ordered trees
• An ordered tree is a rooted tree in which all the children of each
An ordered tree is a rooted tree in which all the children of each
vertex are ordered.
vertex are ordered.
Binary trees
Binary trees
• A binary tree is an ordered tree in which every vertex has no more
A binary tree is an ordered tree in which every vertex has no more
than two children and each children is designated s either a left child
than two children and each children is designated s either a left child
or a right child of its parent.
or a right child of its parent.
Binary search trees
Binary search trees
• Each vertex is assigned a number.
Each vertex is assigned a number.
• A number assigned to each parental vertex is larger than all the
A number assigned to each parental vertex is larger than all the
numbers in its left subtree and smaller than all the numbers in its
numbers in its left subtree and smaller than all the numbers in its
right subtree.
right subtree.
log
log2
2n
n
h
h
n – 1
n – 1, where h is the height of a binary tree and n the size.
, where h is the height of a binary tree and n the size.
9
6 8
5 2 3
6
3 9
2 5 8
Editor's Notes
#4 Euclid’s algorithm is good for introducing the notion of an algorithm because it
makes a clear separation from a program that implements the algorithm.
It is also one that is familiar to most students.
Al Khowarizmi (many spellings possible...) – “algorism” (originally) and then
later “algorithm” come from his name.
#7 The algorithm is given *very* informally here. Show students the pseudocode in
section 3.1.
This is a good opportunity to discuss pseudocode conventions.
#8 1-4 have well known efficient (polynomial-time) solutions
5: primality testing has recently been found to have an efficient solution
This is a great problem to discuss because it has recently been in the news
(see mathworld news at: http://mathworld.wolfram.com/news/2002-08-07_primetest/
or original article: http://www.cse.iitk.ac.in/primality.pdf)
6(TSP)-9(chess) are all problems for which no efficient solution has been found
it is possible to informally discuss the “try all possibilities” approach that is required
to get exact solutions to such problems
10: Towers of Hanoi is a problem that has only exponential-time solutions (simply
because the output required is so large)
11: Program termination is undecidable
#12 The formalization of the notion of an algorithm led to great breakthroughs in the
foundations of mathematics in the 1930s.
#14 Euclid’s algorithm is good for introducing the notion of an algorithm because it
makes a clear separation from a program that implements the algorithm.
It is also one that is familiar to most students.
Al Khowarizmi (many spellings possible...) – “algorism” (originally) and then
later “algorithm” come from his name.
#36 Examples of a simple path and a not simple path.
Connected graphs: starting from any vertex, we can always find a path to reach all the other vertices. (Ball-String example.)
From NIST:
Connected graphs:
Definition: An undirected graph that has a path between every pair of vertices.
Strongly connected graphs:
Definition: A directed graph that has a path from each vertex to every other vertex.
Connected component: …
Strongly connected component:
a strongly connected component of a digraph G is a maximal strongly connected subgraph of G.