SlideShare ist ein Scribd-Unternehmen logo
1 von 132
Data Structures and Algorithms
“I Will, In Fact, Claim That The Difference Between A
Bad Programmer And A Good One Is Whether He
Considers His Code Or His Data Structures More
Important. Bad Programmers Worry About The
Code. Good Programmers Worry About Data
Structures And Their Relationships.” Linus Torvalds
 The choice of data structure and algorithm can make the difference
between a program running in a few seconds or many days.
Algorithm:
 a method or a process followed to solve a problem.
 An algorithm takes the input to a problem and transforms it to the output.
Algorithm Efficiency There are often many approaches (algorithms)to solve
a problem. How do we choose between them?!!
At the heart of computer program design are two goals:
1. To design an algorithm that is easy to understand, code and debug.(software )
2. To design an algorithm that makes efficient use of the computer’s resources.( algorithm
analysis)
Critical resources:
• Time • Space (disk, RAM) • Programmer’s effort
• Ease of use
Searching:
 When we maintain a collection of data, one of the operations we need is a search
routine to locate desired data quickly.
 To gauge the performance of searching algorithm :
• We will count the number of comparisons the algorithms make to analyze their
performance. (time it take to finish every time we search using algorithm)
Best, Worst and Average Cases
Not all inputs of a given size take the same time.
Example:
Sequential search for K in an array of n integers:
• Begin at first element in array and look at each element in turn until K is found.
Best Case: [Find at first position: 1 compare]
Worst Case: [Find at last position: n compares]
Average Case: [(n + 1)/2 compares]
Computation Complexity
Definition: Measure the efficiency of an algorithm
in terms of time or space required.
Time tends to be more important.
The efficiency of an algorithm is always stated as a
function of the input size
The “Big-O” notation: O(f(n))
Worst case scenario The maximum number of
computation steps taken on any input size n.
In an increasing order of complexity:
Constant time: O(1)
Logarithmic time: O(logn)
Linear time: O(n)
Polynomial time: O(n2)
Exponential time: O(2n)
Suppose each step takes 1 microseconds (10-6):
n O(1) O(logn) O(n) O(n^2) O(2^n)
10 1 1 10 100 1024
100 1 2 100 10000 40196936841331500 years
1000 1 3 1000 1 sec …
10000 1 4 10000 1.67 min …
Example of search results:
Unsuccessful Search: search(45)
Successful Search: search(12)
NOT_FOUND
4
number
sequential Search Performance:
• We count how many times the search value is compared against the array
elements.
• Successful Search
• We analyze the successful and unsuccessful searches separately.
– Best Case – 1 comparison
– Worst Case – N comparisons (N – array size)
• Unsuccessful Search
– Best Case = Worst Case – N comparisons
Notes:
The order of this algorithm is O(N) as N is the number of elements in
array.
Searching “self organized search”
• If we want to make sequential more fast we can make some priorities to some
entries that we can need to get to them 80% of time more than others we can
use to method to make it :
• First by use algorithm of “move to front”:
 we put our need entry at the top of the array so we can find it mush
easier when need it.
• Second by use algorithm of “ transpose”:
 every time we access an entry we make only one shift to the position of the
element so we can reach it next time mush easier.
Note : both of to algorithms don’t need any sorting for the array to use with it.
• /* C program to input N numbers and store them in an array.
• * Do a linear search for a given key and report success
• * or failure.
• */
• #include <stdio.h>
• void main()
• {
• int array[10];
• int i, num, keynum, found = 0;
• printf("Enter the value of num n");
• scanf("%d", &num);
• printf("Enter the elements one by one n");
• for (i = 0; i < num; i++)
• {scanf("%d", &array[i]);
• }
• printf("Input array is n");
• for (i = 0; i < num; i++)
• {
• printf("%dn", array[i]);
• }
• printf("Enter the element to be searched n");
• scanf("%d", &keynum);
• /* Linear search begins */
• for (i = 0; i < num ; i++)
• {
• if (keynum == array[i] )
• {
• found = 1;
• break;
• }
• }
• if (found == 1)
• printf("Element is present in the arrayn");
• else
• printf("Element is not present in the arrayn");
• }
Binary Search
• If the array is sorted, then we can apply the binary
search technique.
number
• The basic idea is straightforward. First search the value
in the middle position. If X is less than this value, then
search the middle of the left half next. If X is greater
than this value, then search the middle of the right half
next. Continue in this manner.
55 12 17 23 38 44 77
0 1 2 3 4 5 6 7 8
84 90
Sequence of Successful Search - 1
38 < 44 search( 44 )
Low=mid+1=5
#1
Sequence of Successful Search - 2
high = mid-1=5
44<77
#2
#3
Low=mid +1 so low > high
Finished
Binary Search Performance:
Successful Search:
Best Case – 1 comparison
Worst Case – log2N comparisons
Unsuccessful Search:
Best Case =Worst Case – log2N comparisons
Since the portion of an array to search is cut into half after every comparison, we compute
how many times the array can be divided into halves.
So we can note in the example we discussed we can see that we make 3 search in an array
of 8 elements .
So binary search can make number of search equal to log N for base=2.
After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1,
deriving K = log2N.
This * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed.
5. */6.#
include <stdio.h>
7.
8.void main()
9.{
10.int array[20];
11.int i, low, mid, high, key, size;
12.
13.printf("Enter the size of an arrayn");
14.scanf("%d", &size);
15.printf("Enter the array elementsn");
16.for (i = 0; i < size; i++)
17.{
18.scanf("%d", &array[i]);
19.}
20.printf("Enter the keyn");
21.scanf("%d", &key);
22./* search begins */
23.low = 0;
24.high = (size - 1);
25.while (low <= high)
26.{
27.mid = (low + high) / 2;
28.if (key == array[mid])
29.{
30.printf("SUCCESSFUL SEARCHn");
31.return;
32.}
33.if (key < array[mid])
34.high = mid - 1;
35.else
36.low = mid + 1;
37.}
38.printf("UNSUCCESSFUL SEARCHn");
39.}
Data Structure
 Data structures organize data⇒ more efficient programs.
 The choice of data structure and algorithm can make the difference between a
program running in a few seconds or many days.
Efficiency
• A solution is said to be efficient if it solves the problem within its resource
constraints.
• space [These are typical constraints for programs]
• time.
trees
Where I can find tree data structure?!
. PDF is a tree based format
Some very successful physics/games engines build trees to accurately
simulate human movement.
file system on a computer
We can insert/delete keys in moderate time (quicker than Arrays )
Trees & binary trees
Binary search tree (BST)
Algorithm is :
For BST we put values which is smaller than node value
in the left and bigger values in the right
As 30
have to
be in the
left
Difference between BT & BST
A binary tree is simply a tree in which each node can have at most two
children.
A binary search tree is a binary tree in which the nodes are assigned
values, with the following restrictions :
1. The left sub tree of a node can only have values less than the
node
2. The right sub tree of a node can only have values greater than the
node
3. The left sub tree of a node is a binary search tree.
4. The right sub tree of a node is a binary search tree.
BST Operations
BST Traversal
 Traversal mean how can BST rotate to visit every node in the tree
That can be use when we want to print every element in a tree or want
to get the max or min of tree elements .
Pre-order
In this way we first visit the root then left hand side then right hand side.
In-order
Visiting left sub tree then root then right sub tree
Post order
Visiting left sub tree then right then root at the end
Binary Tree Insertion
Insertion mean that you want to put new node to your BT
1 10 8 4 6 3 2 5
Binary Tree Insertion
1
1 10 8 4 6 3 2 5
1
1 10 8 4 6 3 2 5
1
1 10 8 4 6 3 2 5
1
1 10 8 4 6 3 2 5
1
10
1 10 8 4 6 3 2 5
1 10
1
10
8 4 6 3 2 5
1 10
1
10
8 4 6 3 2 5
1 10
1
10
8 4 6 3 2 5
1 10
1
10
8 4 6 3 2 5
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
6
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2
1 10
1
10
8 4 6 3 2 5
8
4
63
2 5
1 10
1
10
8 4 6 3 2 5
8
4
63
2 5
Binary Tree Deletion
1 10
1
10
8 4 6 3 2 5
8
4
63
2 5
1 10
1
10
8 4 6 3 2 5
8
4
63
2 5
1 10
1
10
8 4 6 3 2 5
8
4
63
5
1 10
1
10
8 4 6 3 2 5
8
4
63
5
We will delete
the node that
haven’t any
children and
make parent
pointe to NULL
1 10
1
10
8 4 6 3 5
8
4
63
5
Node 8 has only on child
1 10
1
10
8 4 6 3 5
8
4
63
5
1 10
1
10
8 4 6 3 5
4
63
5
1 10
1
10
8 4 6 3 5
4
63
5
1 10
1
10
8 4 6 3 5
4
63
5
We will delete the node and make upper parent point to the child
1 10
1
10
4 6 3 5
4
63
5
1 10
1
10
4 6 3 5
4
63
5
Node 4 contain two children
1 10
1
10
4 6 3 5
4
63
5
1 10
1
10
4 6 3 5
4
63
5
1 10
1
10
4 6 3 5
4
63
5
1 10
1
10
4 6 3 5
5
63
5
1 10
1
10
4 6 3 5
5
63
5
1 10
1
10
4 6 3 5
5
63
1 10
1
10
4 6 3 5
5
63
1 10
1
10
6 3 5
5
63
Here we make delete for a root that contain 2
children to solve it we will choose from our tree the
largest value in the right or the smallest value in the
left.
BST Search Performance:
Successful Search:
Best Case – 1 comparison
Worst Case – log2N comparisons
Unsuccessful Search:
Best Case =Worst Case – log2N comparisons
Applications of BST
• allows you to do range searches efficiently.
• Used in internet routers.
• Finding square root of given number
• In The language rules and are often used to build dictionaries.
• One of the best ways can be modified to make search of data but sometimes
having problem in insertion & deletion that can be complex when achieving
best searching using types of BSTs.
AVL (Adelson-Velskii and Landis) tree.
An AVL tree is identical to a BST except
height of the left and right subtrees can differ by at most 1.
height of an empty tree is defined to be (–1).
AVL Tree
5
8
2
4
3
1 7
0
1
2
3
Height
Height is the longest distance from
the root to the most far leaf
6
81
4
3
1
5
height
0
1
2
3
We have more than one node as difference between the left and right sub
trees that can make order of search and insert and delete become more than
log2n
So we need to make re-balance to the tree to make it AVL
Not an AVL tree
Note :
Balanced factor for every node :that is the
difference between the right and left sub trees to
make a decision if the difference more than 1 or
less than -1 it will need re-balance.
-1
01
0
0
0
0
-1
0
1
00 0
0 00 0
-1
01
0
0
0
0
-1
0
1
00 0
0 00 0U1 U2 U3 U4
U5 U6 U7 U8 U9 U10 U11 U12
B BB B
B B
Insertions and effect on balance
Tree becomes unbalanced only if the newly inserted node
Example:
Its balance now
1
0
A
B
T1
T3
T2
1
2
1
A
B
T1
T3
T2
1
2
When inset new node in the left hand side it become not balance and need to re-balance it
2
1
A
B
T1
T3
T2
1
2
0
0
A
B
T1
T3T2
new
new
We can choose node B and
make self-balance but it will
make more efforts in insertion
and deletion but make search
then order of log2n
AVL Tree Building Example
 Let us work through an example that inserts numbers in a balanced search tree.
We will check the balance after each insert and rebalance if necessary using rotations
Insert(1)
1
Insert(2)
1
2
1
2
3
-2
Insert(3) single left rotation
1
2
3
-2
Insert(3) single left rotation
1
2
3
More examples :
100
120
Balance factor =-1
0
So parent balance factor still -1its balance
140
120 -1
-2
0
insert 140 Go back to
every parent
to see it’s
balance
factor
It’s over weight in
the right .
It’s first case right-
right unbalance
140
120
Null To replace we
make change for
nodes pointers
make 120 to link
for 100 &100 to
link for what 120
link for in the left
hand side of it
0
0
0
Insert 80:
140
120
0
1
0
80
2-1=1 balance
Insert 60:
140
120
0
2
80
60
It’s note balance and
this time we can let it
second case as this
time it’s left-left
weight
Re-balance:
140
120
0
0
80
60
0
1
We will change two links 100 link
to what 80 link to in the right
(null)and 80 will link to what 100
was link to so 80 link to 100
Insert 90:
140
120
3
80
60
0
2
90
unbalance
1
0
0
Case 3 : when
we have left-
right weight
140
120
0
80
60
0
90
We will choose 100
as it between 80
and 120 and make
it the root
0
1
Null
We make the right side of 100 to be left
side of 120 and left side of 100 right
side of 80 as 80<100<120
0
140
120
0
80
60
0
90
0
2
Null
1
130
Insert 130:
Case 4:
When find right-
left weight
To re-balance:
140120
0
80
60
0
90
0
2
1
130
Null
We make the right side of
130 to be left side of 140
and left side of 130 right
side of 120 as
120<130<140
• When we maintain a collection of data, many applications call
for rearranging the data in certain order, e.g. arranging Person
information in ascending order of age.
• Here’s the problem statement:
Given an array of N integer values, arrange the values into ascending
order.
• We will count the number of comparisons the algorithms make
to analyze their performance.
– The ideal sorting algorithm will make the least possible number of
comparisons to arrange data in a designated order.
• We will compare different sorting algorithms by analyzing their
worst-case performance.
Sorting
1. Find the smallest
element in the list.(by
putting element by
element as min and
compare with all of the
array)
2. Exchange the element in
the first position and the
smallest element. Now
the smallest element is
in the first position.
3. Repeat Step 1 and 2
with the list having one
less element till make it
sort.
exchange
first
First min
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted unsorted
Selection Sort
0 1 2 3 4 5 6 7 8
5 17 23 90 12 44 38 84 77
sorted
Pass #
1
2
3
7
5 12 23 90 17 44 38 84 77
5 12 17 90 23 44 38 84 77
5 12 17 23 38 44 77 84 90
5 12 17 23 38 44 77 84 90
8
We can
note that
this
method
make a lot
of rotation
we don’t
need it as
we can
see the no
difference
between
as 7&8
1./*
2. * C program to accept N numbers and arrange them in an ascending order
3. */
4.#include <stdio.h>
5.
6.void main()
7.{
8.int i, j, a, n, number[30];
9.
10.printf("Enter the value of N n");
11.scanf("%d", &n);
12.printf("Enter the numbers n");
13.for (i = 0; i < n; ++i)
14.scanf("%d", &number[i]);
15.for (i = 0; i < n; ++i)
16.{
17.for (j = i + 1; j < n; ++j)
18.{
19.if (number[i] > number[j])
20.{
21.a = number[i];
22.number[i] = number[j];
23.number[j] = a;
24.}
25.}
26.}
27.printf("The numbers arranged in ascending order are given below n");
28.for (i = 0; i < n; ++i)
29.printf("%dn", number[i]);
30.}
Selection sort C-code
Selection Sort Performance
• We derive the total number of comparisons by
counting the number of times the inner loop is
executed.
• For each execution of the outer loop, the inner
loop is executed length – start times
• Complexity O(n^2) as:
• We loop n+n-1+n-2+n-3……..+1 nearly we have
order of n*n.
• We can use this method if I need to get first one
or two or three elements for getting first
element in its right place this method will be
convenient but if need a very large array to be
sort it will take a lot of time !!
• If n=1000 we need to loop 1000000 time its to
mush !!
• Adv:But we can see its very simple to
implement(for software).
• Adv. :Very good for storage as it needn’t to
waste space in memory it sort in place.
• Disadv:It’s Blind sort as it will loop O(N^2) even
having a sorted array.
Bubble Sort
• With the selection sort, we make one exchange at the end of
one pass.
• The bubble sort improves the performance by making more than
one exchange during its pass.
• By making multiple exchanges, we will be able to move more
elements toward their correct positions using the same number
of comparisons as the selection sort makes.
• The key idea of the bubble sort is to make pairwise comparisons
and exchange the positions of the pair if they are out of order.
0 1 2 3 4 5 6 7 8
23 17 5 90 12 44 38 84 77
One Pass of Bubble Sort
exchange
17 23 5 90 12 44 38 84 77
exchange
17 5 23 90 12 44 38 84 77
exchange
Ok
17 5 23 12 90 44 38 84 77
exchange
17 5 23 12 44 90 38 84 77 17 5 23 12 44 38 90 84 77
exchange
17 5 23 12 44 38 84 90 77 17 5 23 12 44 38 84 77 90
The largest value 90 is at the end of
the list.
1) 2)
3)
4)
5)
6)
7) 8)
1./* Bubble sorting begins */
2.for (i = 0; i < num; i++)
3.{
4.for (j = 0; j < (num - i - 1); j++)
5.{
6.if (array[j] > array[j + 1])
7.{
8.temp = array[j];
9.array[j] = array[j + 1];
10.array[j + 1] = temp;
11.}
12.}
13.}
14.printf("Sorted array is...n");
15.for (i = 0; i < num; i++)
16.{
17.printf("%dn", array[i]);
18.}
19.}
Bubble Sort Performance
• In the worst case, the outer while loop is executed N-1 times for
carrying out N-1 passes.
• For each execution of the outer loop, the inner loop is executed
bottom+1 times. The number of comparisons in each successive
pass is N-1, N-2, … , 1. Summing these will result in the total
number of comparisons.
• So the performances of the bubble sort and the selection sort
are approximately equivalent.
However, on the average, the bubble sort performs much better
than the selection sort because it can finish the sorting without
doing all N-1 passes .
 Example : if we have sorted array it will loop only one
time not as selection sort o(n^2)
 Its not Blind sort.
Thank you 

Weitere ähnliche Inhalte

Was ist angesagt?

9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
irdginfo
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
eShikshak
 

Was ist angesagt? (20)

Data structures
Data structuresData structures
Data structures
 
PYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMSPYTHON FULL TUTORIAL WITH PROGRAMMS
PYTHON FULL TUTORIAL WITH PROGRAMMS
 
Cryptography Using Laplace Transform
Cryptography Using Laplace TransformCryptography Using Laplace Transform
Cryptography Using Laplace Transform
 
Roty_Shift: A Proposed Method for Generating Secret Keys
Roty_Shift: A Proposed Method for Generating Secret KeysRoty_Shift: A Proposed Method for Generating Secret Keys
Roty_Shift: A Proposed Method for Generating Secret Keys
 
9 big o-notation
9 big o-notation9 big o-notation
9 big o-notation
 
Searching Algorithms
Searching AlgorithmsSearching Algorithms
Searching Algorithms
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
 
529 199-206
529 199-206529 199-206
529 199-206
 
BANKER'S ALGORITHM
BANKER'S ALGORITHMBANKER'S ALGORITHM
BANKER'S ALGORITHM
 
15 unionfind
15 unionfind15 unionfind
15 unionfind
 
Os
OsOs
Os
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
 
Data clustering using map reduce
Data clustering using map reduceData clustering using map reduce
Data clustering using map reduce
 
Data Streaming Algorithms
Data Streaming AlgorithmsData Streaming Algorithms
Data Streaming Algorithms
 
Quick and Heap Sort with examples
Quick and Heap Sort with examplesQuick and Heap Sort with examples
Quick and Heap Sort with examples
 
Union find
Union findUnion find
Union find
 
Eryk_Kulikowski_a3
Eryk_Kulikowski_a3Eryk_Kulikowski_a3
Eryk_Kulikowski_a3
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 

Andere mochten auch

Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
sotlsoc
 
Chapter 3.1
Chapter 3.1Chapter 3.1
Chapter 3.1
sotlsoc
 
Program logic formulation
Program logic formulationProgram logic formulation
Program logic formulation
Sara Corpuz
 

Andere mochten auch (14)

Webpage Visual Design and Online Prototype
Webpage Visual Design and Online PrototypeWebpage Visual Design and Online Prototype
Webpage Visual Design and Online Prototype
 
Chapter 7.4
Chapter 7.4Chapter 7.4
Chapter 7.4
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
Chapter 3.1
Chapter 3.1Chapter 3.1
Chapter 3.1
 
Logic Formulation 2
Logic Formulation 2Logic Formulation 2
Logic Formulation 2
 
Chap 2(const var-datatype)
Chap 2(const var-datatype)Chap 2(const var-datatype)
Chap 2(const var-datatype)
 
2. electric field calculation
2. electric field calculation2. electric field calculation
2. electric field calculation
 
Digital Logic
Digital LogicDigital Logic
Digital Logic
 
Problem Solving with Algorithms and Data Structure - Lists
Problem Solving with Algorithms and Data Structure - ListsProblem Solving with Algorithms and Data Structure - Lists
Problem Solving with Algorithms and Data Structure - Lists
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Komunikasyon
KomunikasyonKomunikasyon
Komunikasyon
 
Data Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer ScienceData Structure & Algorithms | Computer Science
Data Structure & Algorithms | Computer Science
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Program logic formulation
Program logic formulationProgram logic formulation
Program logic formulation
 

Ähnlich wie Basics in algorithms and data structure

CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 
DataMiningReport
DataMiningReportDataMiningReport
DataMiningReport
?? ?
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 
Data Structure
Data StructureData Structure
Data Structure
sheraz1
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
Qundeel
 

Ähnlich wie Basics in algorithms and data structure (20)

Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Algorithms.
Algorithms. Algorithms.
Algorithms.
 
Merge sort analysis and its real time applications
Merge sort analysis and its real time applicationsMerge sort analysis and its real time applications
Merge sort analysis and its real time applications
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Basic terminologies & asymptotic notations
Basic terminologies & asymptotic notationsBasic terminologies & asymptotic notations
Basic terminologies & asymptotic notations
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
DataMiningReport
DataMiningReportDataMiningReport
DataMiningReport
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structure
Data StructureData Structure
Data Structure
 
Lec 1 Ds
Lec 1 DsLec 1 Ds
Lec 1 Ds
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Searching.pptx
Searching.pptxSearching.pptx
Searching.pptx
 
Lecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptLecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).ppt
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
RAJAT PROJECT.pptx
RAJAT PROJECT.pptxRAJAT PROJECT.pptx
RAJAT PROJECT.pptx
 

Basics in algorithms and data structure

  • 1. Data Structures and Algorithms “I Will, In Fact, Claim That The Difference Between A Bad Programmer And A Good One Is Whether He Considers His Code Or His Data Structures More Important. Bad Programmers Worry About The Code. Good Programmers Worry About Data Structures And Their Relationships.” Linus Torvalds  The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.
  • 2. Algorithm:  a method or a process followed to solve a problem.  An algorithm takes the input to a problem and transforms it to the output. Algorithm Efficiency There are often many approaches (algorithms)to solve a problem. How do we choose between them?!! At the heart of computer program design are two goals: 1. To design an algorithm that is easy to understand, code and debug.(software ) 2. To design an algorithm that makes efficient use of the computer’s resources.( algorithm analysis) Critical resources: • Time • Space (disk, RAM) • Programmer’s effort • Ease of use
  • 3. Searching:  When we maintain a collection of data, one of the operations we need is a search routine to locate desired data quickly.  To gauge the performance of searching algorithm : • We will count the number of comparisons the algorithms make to analyze their performance. (time it take to finish every time we search using algorithm) Best, Worst and Average Cases Not all inputs of a given size take the same time. Example: Sequential search for K in an array of n integers: • Begin at first element in array and look at each element in turn until K is found. Best Case: [Find at first position: 1 compare] Worst Case: [Find at last position: n compares] Average Case: [(n + 1)/2 compares]
  • 4. Computation Complexity Definition: Measure the efficiency of an algorithm in terms of time or space required. Time tends to be more important. The efficiency of an algorithm is always stated as a function of the input size The “Big-O” notation: O(f(n)) Worst case scenario The maximum number of computation steps taken on any input size n.
  • 5. In an increasing order of complexity: Constant time: O(1) Logarithmic time: O(logn) Linear time: O(n) Polynomial time: O(n2) Exponential time: O(2n) Suppose each step takes 1 microseconds (10-6): n O(1) O(logn) O(n) O(n^2) O(2^n) 10 1 1 10 100 1024 100 1 2 100 10000 40196936841331500 years 1000 1 3 1000 1 sec … 10000 1 4 10000 1.67 min …
  • 6. Example of search results: Unsuccessful Search: search(45) Successful Search: search(12) NOT_FOUND 4 number
  • 7. sequential Search Performance: • We count how many times the search value is compared against the array elements. • Successful Search • We analyze the successful and unsuccessful searches separately. – Best Case – 1 comparison – Worst Case – N comparisons (N – array size) • Unsuccessful Search – Best Case = Worst Case – N comparisons Notes: The order of this algorithm is O(N) as N is the number of elements in array.
  • 8. Searching “self organized search” • If we want to make sequential more fast we can make some priorities to some entries that we can need to get to them 80% of time more than others we can use to method to make it : • First by use algorithm of “move to front”:  we put our need entry at the top of the array so we can find it mush easier when need it. • Second by use algorithm of “ transpose”:  every time we access an entry we make only one shift to the position of the element so we can reach it next time mush easier. Note : both of to algorithms don’t need any sorting for the array to use with it.
  • 9. • /* C program to input N numbers and store them in an array. • * Do a linear search for a given key and report success • * or failure. • */ • #include <stdio.h> • void main() • { • int array[10]; • int i, num, keynum, found = 0; • printf("Enter the value of num n"); • scanf("%d", &num); • printf("Enter the elements one by one n"); • for (i = 0; i < num; i++) • {scanf("%d", &array[i]); • } • printf("Input array is n"); • for (i = 0; i < num; i++) • { • printf("%dn", array[i]); • } • printf("Enter the element to be searched n"); • scanf("%d", &keynum); • /* Linear search begins */ • for (i = 0; i < num ; i++) • { • if (keynum == array[i] ) • { • found = 1; • break; • } • } • if (found == 1) • printf("Element is present in the arrayn"); • else • printf("Element is not present in the arrayn"); • }
  • 10. Binary Search • If the array is sorted, then we can apply the binary search technique. number • The basic idea is straightforward. First search the value in the middle position. If X is less than this value, then search the middle of the left half next. If X is greater than this value, then search the middle of the right half next. Continue in this manner. 55 12 17 23 38 44 77 0 1 2 3 4 5 6 7 8 84 90
  • 11. Sequence of Successful Search - 1 38 < 44 search( 44 ) Low=mid+1=5 #1
  • 12. Sequence of Successful Search - 2 high = mid-1=5 44<77 #2 #3 Low=mid +1 so low > high Finished
  • 13. Binary Search Performance: Successful Search: Best Case – 1 comparison Worst Case – log2N comparisons Unsuccessful Search: Best Case =Worst Case – log2N comparisons Since the portion of an array to search is cut into half after every comparison, we compute how many times the array can be divided into halves. So we can note in the example we discussed we can see that we make 3 search in an array of 8 elements . So binary search can make number of search equal to log N for base=2. After K comparisons, there will be N/2K elements in the list. We solve for K when N/2K = 1, deriving K = log2N.
  • 14. This * Otherwise, a message "UNSUCCESSFUL SEARCH" is displayed. 5. */6.# include <stdio.h> 7. 8.void main() 9.{ 10.int array[20]; 11.int i, low, mid, high, key, size; 12. 13.printf("Enter the size of an arrayn"); 14.scanf("%d", &size); 15.printf("Enter the array elementsn"); 16.for (i = 0; i < size; i++) 17.{ 18.scanf("%d", &array[i]); 19.} 20.printf("Enter the keyn"); 21.scanf("%d", &key); 22./* search begins */ 23.low = 0; 24.high = (size - 1); 25.while (low <= high) 26.{ 27.mid = (low + high) / 2; 28.if (key == array[mid]) 29.{ 30.printf("SUCCESSFUL SEARCHn"); 31.return; 32.} 33.if (key < array[mid]) 34.high = mid - 1; 35.else 36.low = mid + 1; 37.} 38.printf("UNSUCCESSFUL SEARCHn"); 39.}
  • 15.
  • 16. Data Structure  Data structures organize data⇒ more efficient programs.  The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days. Efficiency • A solution is said to be efficient if it solves the problem within its resource constraints. • space [These are typical constraints for programs] • time.
  • 17. trees
  • 18. Where I can find tree data structure?! . PDF is a tree based format Some very successful physics/games engines build trees to accurately simulate human movement. file system on a computer We can insert/delete keys in moderate time (quicker than Arrays )
  • 19. Trees & binary trees
  • 20. Binary search tree (BST) Algorithm is :
  • 21. For BST we put values which is smaller than node value in the left and bigger values in the right As 30 have to be in the left
  • 22. Difference between BT & BST A binary tree is simply a tree in which each node can have at most two children. A binary search tree is a binary tree in which the nodes are assigned values, with the following restrictions : 1. The left sub tree of a node can only have values less than the node 2. The right sub tree of a node can only have values greater than the node 3. The left sub tree of a node is a binary search tree. 4. The right sub tree of a node is a binary search tree.
  • 24. BST Traversal  Traversal mean how can BST rotate to visit every node in the tree That can be use when we want to print every element in a tree or want to get the max or min of tree elements .
  • 25. Pre-order In this way we first visit the root then left hand side then right hand side.
  • 26. In-order Visiting left sub tree then root then right sub tree
  • 27. Post order Visiting left sub tree then right then root at the end
  • 28. Binary Tree Insertion Insertion mean that you want to put new node to your BT
  • 29. 1 10 8 4 6 3 2 5 Binary Tree Insertion
  • 30. 1 1 10 8 4 6 3 2 5
  • 31. 1 1 10 8 4 6 3 2 5
  • 32. 1 1 10 8 4 6 3 2 5
  • 33. 1 1 10 8 4 6 3 2 5
  • 34. 1 10 1 10 8 4 6 3 2 5
  • 35. 1 10 1 10 8 4 6 3 2 5
  • 36. 1 10 1 10 8 4 6 3 2 5
  • 37. 1 10 1 10 8 4 6 3 2 5
  • 38. 1 10 1 10 8 4 6 3 2 5
  • 39. 1 10 1 10 8 4 6 3 2 5 8
  • 40. 1 10 1 10 8 4 6 3 2 5 8
  • 41. 1 10 1 10 8 4 6 3 2 5 8
  • 42. 1 10 1 10 8 4 6 3 2 5 8
  • 43. 1 10 1 10 8 4 6 3 2 5 8
  • 44. 1 10 1 10 8 4 6 3 2 5 8
  • 45. 1 10 1 10 8 4 6 3 2 5 8 4
  • 46. 1 10 1 10 8 4 6 3 2 5 8 4
  • 47. 1 10 1 10 8 4 6 3 2 5 8 4
  • 48. 1 10 1 10 8 4 6 3 2 5 8 4
  • 49. 1 10 1 10 8 4 6 3 2 5 8 4
  • 50. 1 10 1 10 8 4 6 3 2 5 8 4
  • 51. 1 10 1 10 8 4 6 3 2 5 8 4
  • 52. 1 10 1 10 8 4 6 3 2 5 8 4
  • 53. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 54. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 55. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 56. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 57. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 58. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 59. 1 10 1 10 8 4 6 3 2 5 8 4 6
  • 60. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 61. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 62. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 63. 10 1 10 8 4 6 3 2 5 8 4 63
  • 64. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 65. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 66. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 67. 1 10 1 10 8 4 6 3 2 5 8 4 63
  • 68. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 69. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 70. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 71. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 72. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 73. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 74. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 75. 1 10 1 10 8 4 6 3 2 5 8 4 63 2
  • 76. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  • 77. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  • 79. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  • 80. 1 10 1 10 8 4 6 3 2 5 8 4 63 2 5
  • 81. 1 10 1 10 8 4 6 3 2 5 8 4 63 5
  • 82. 1 10 1 10 8 4 6 3 2 5 8 4 63 5 We will delete the node that haven’t any children and make parent pointe to NULL
  • 83. 1 10 1 10 8 4 6 3 5 8 4 63 5 Node 8 has only on child
  • 84. 1 10 1 10 8 4 6 3 5 8 4 63 5
  • 85. 1 10 1 10 8 4 6 3 5 4 63 5
  • 86. 1 10 1 10 8 4 6 3 5 4 63 5
  • 87. 1 10 1 10 8 4 6 3 5 4 63 5 We will delete the node and make upper parent point to the child
  • 88. 1 10 1 10 4 6 3 5 4 63 5
  • 89. 1 10 1 10 4 6 3 5 4 63 5 Node 4 contain two children
  • 90. 1 10 1 10 4 6 3 5 4 63 5
  • 91. 1 10 1 10 4 6 3 5 4 63 5
  • 92. 1 10 1 10 4 6 3 5 4 63 5
  • 93. 1 10 1 10 4 6 3 5 5 63 5
  • 94. 1 10 1 10 4 6 3 5 5 63 5
  • 95. 1 10 1 10 4 6 3 5 5 63
  • 96. 1 10 1 10 4 6 3 5 5 63
  • 97. 1 10 1 10 6 3 5 5 63 Here we make delete for a root that contain 2 children to solve it we will choose from our tree the largest value in the right or the smallest value in the left.
  • 98. BST Search Performance: Successful Search: Best Case – 1 comparison Worst Case – log2N comparisons Unsuccessful Search: Best Case =Worst Case – log2N comparisons
  • 99. Applications of BST • allows you to do range searches efficiently. • Used in internet routers. • Finding square root of given number • In The language rules and are often used to build dictionaries. • One of the best ways can be modified to make search of data but sometimes having problem in insertion & deletion that can be complex when achieving best searching using types of BSTs.
  • 100. AVL (Adelson-Velskii and Landis) tree. An AVL tree is identical to a BST except height of the left and right subtrees can differ by at most 1. height of an empty tree is defined to be (–1). AVL Tree 5 8 2 4 3 1 7 0 1 2 3 Height Height is the longest distance from the root to the most far leaf
  • 101. 6 81 4 3 1 5 height 0 1 2 3 We have more than one node as difference between the left and right sub trees that can make order of search and insert and delete become more than log2n So we need to make re-balance to the tree to make it AVL Not an AVL tree
  • 102. Note : Balanced factor for every node :that is the difference between the right and left sub trees to make a decision if the difference more than 1 or less than -1 it will need re-balance.
  • 104. -1 01 0 0 0 0 -1 0 1 00 0 0 00 0U1 U2 U3 U4 U5 U6 U7 U8 U9 U10 U11 U12 B BB B B B Insertions and effect on balance
  • 105. Tree becomes unbalanced only if the newly inserted node Example: Its balance now 1 0 A B T1 T3 T2 1
  • 106. 2 1 A B T1 T3 T2 1 2 When inset new node in the left hand side it become not balance and need to re-balance it
  • 107. 2 1 A B T1 T3 T2 1 2 0 0 A B T1 T3T2 new new We can choose node B and make self-balance but it will make more efforts in insertion and deletion but make search then order of log2n
  • 108. AVL Tree Building Example  Let us work through an example that inserts numbers in a balanced search tree. We will check the balance after each insert and rebalance if necessary using rotations Insert(1) 1
  • 112. 1 2 3
  • 113. More examples : 100 120 Balance factor =-1 0 So parent balance factor still -1its balance
  • 114. 140 120 -1 -2 0 insert 140 Go back to every parent to see it’s balance factor It’s over weight in the right . It’s first case right- right unbalance
  • 115. 140 120 Null To replace we make change for nodes pointers make 120 to link for 100 &100 to link for what 120 link for in the left hand side of it 0 0 0
  • 117. Insert 60: 140 120 0 2 80 60 It’s note balance and this time we can let it second case as this time it’s left-left weight
  • 118. Re-balance: 140 120 0 0 80 60 0 1 We will change two links 100 link to what 80 link to in the right (null)and 80 will link to what 100 was link to so 80 link to 100
  • 119. Insert 90: 140 120 3 80 60 0 2 90 unbalance 1 0 0 Case 3 : when we have left- right weight
  • 120. 140 120 0 80 60 0 90 We will choose 100 as it between 80 and 120 and make it the root 0 1 Null We make the right side of 100 to be left side of 120 and left side of 100 right side of 80 as 80<100<120 0
  • 122. To re-balance: 140120 0 80 60 0 90 0 2 1 130 Null We make the right side of 130 to be left side of 140 and left side of 130 right side of 120 as 120<130<140
  • 123. • When we maintain a collection of data, many applications call for rearranging the data in certain order, e.g. arranging Person information in ascending order of age. • Here’s the problem statement: Given an array of N integer values, arrange the values into ascending order. • We will count the number of comparisons the algorithms make to analyze their performance. – The ideal sorting algorithm will make the least possible number of comparisons to arrange data in a designated order. • We will compare different sorting algorithms by analyzing their worst-case performance. Sorting
  • 124. 1. Find the smallest element in the list.(by putting element by element as min and compare with all of the array) 2. Exchange the element in the first position and the smallest element. Now the smallest element is in the first position. 3. Repeat Step 1 and 2 with the list having one less element till make it sort. exchange first First min 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted unsorted Selection Sort
  • 125. 0 1 2 3 4 5 6 7 8 5 17 23 90 12 44 38 84 77 sorted Pass # 1 2 3 7 5 12 23 90 17 44 38 84 77 5 12 17 90 23 44 38 84 77 5 12 17 23 38 44 77 84 90 5 12 17 23 38 44 77 84 90 8 We can note that this method make a lot of rotation we don’t need it as we can see the no difference between as 7&8
  • 126. 1./* 2. * C program to accept N numbers and arrange them in an ascending order 3. */ 4.#include <stdio.h> 5. 6.void main() 7.{ 8.int i, j, a, n, number[30]; 9. 10.printf("Enter the value of N n"); 11.scanf("%d", &n); 12.printf("Enter the numbers n"); 13.for (i = 0; i < n; ++i) 14.scanf("%d", &number[i]); 15.for (i = 0; i < n; ++i) 16.{ 17.for (j = i + 1; j < n; ++j) 18.{ 19.if (number[i] > number[j]) 20.{ 21.a = number[i]; 22.number[i] = number[j]; 23.number[j] = a; 24.} 25.} 26.} 27.printf("The numbers arranged in ascending order are given below n"); 28.for (i = 0; i < n; ++i) 29.printf("%dn", number[i]); 30.} Selection sort C-code
  • 127. Selection Sort Performance • We derive the total number of comparisons by counting the number of times the inner loop is executed. • For each execution of the outer loop, the inner loop is executed length – start times • Complexity O(n^2) as: • We loop n+n-1+n-2+n-3……..+1 nearly we have order of n*n. • We can use this method if I need to get first one or two or three elements for getting first element in its right place this method will be convenient but if need a very large array to be sort it will take a lot of time !! • If n=1000 we need to loop 1000000 time its to mush !! • Adv:But we can see its very simple to implement(for software). • Adv. :Very good for storage as it needn’t to waste space in memory it sort in place. • Disadv:It’s Blind sort as it will loop O(N^2) even having a sorted array.
  • 128. Bubble Sort • With the selection sort, we make one exchange at the end of one pass. • The bubble sort improves the performance by making more than one exchange during its pass. • By making multiple exchanges, we will be able to move more elements toward their correct positions using the same number of comparisons as the selection sort makes. • The key idea of the bubble sort is to make pairwise comparisons and exchange the positions of the pair if they are out of order.
  • 129. 0 1 2 3 4 5 6 7 8 23 17 5 90 12 44 38 84 77 One Pass of Bubble Sort exchange 17 23 5 90 12 44 38 84 77 exchange 17 5 23 90 12 44 38 84 77 exchange Ok 17 5 23 12 90 44 38 84 77 exchange 17 5 23 12 44 90 38 84 77 17 5 23 12 44 38 90 84 77 exchange 17 5 23 12 44 38 84 90 77 17 5 23 12 44 38 84 77 90 The largest value 90 is at the end of the list. 1) 2) 3) 4) 5) 6) 7) 8)
  • 130. 1./* Bubble sorting begins */ 2.for (i = 0; i < num; i++) 3.{ 4.for (j = 0; j < (num - i - 1); j++) 5.{ 6.if (array[j] > array[j + 1]) 7.{ 8.temp = array[j]; 9.array[j] = array[j + 1]; 10.array[j + 1] = temp; 11.} 12.} 13.} 14.printf("Sorted array is...n"); 15.for (i = 0; i < num; i++) 16.{ 17.printf("%dn", array[i]); 18.} 19.}
  • 131. Bubble Sort Performance • In the worst case, the outer while loop is executed N-1 times for carrying out N-1 passes. • For each execution of the outer loop, the inner loop is executed bottom+1 times. The number of comparisons in each successive pass is N-1, N-2, … , 1. Summing these will result in the total number of comparisons. • So the performances of the bubble sort and the selection sort are approximately equivalent. However, on the average, the bubble sort performs much better than the selection sort because it can finish the sorting without doing all N-1 passes .  Example : if we have sorted array it will loop only one time not as selection sort o(n^2)  Its not Blind sort.