Computing Part III

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Computing

for M.Sc. Programme


By:
Dr. Musbah Mohammed Elahresh
Associative Professor in Fault
Tolerant Computing
( Part III )
Computing

Part III
Data Organization and Utilization
Just To Remember !

Process
Input ( Computing ) Output

real- world real- world


Signal Data Program Data Signal
conversion structures modules structures conversion

event digital data digital data action


data acquisition
Variable and Data
• A variable is a name of data item in a program by which that
data item is referenced and accessed; like :
int x=10 ; x is a name of the integer data value 10.
• By this name we can access and perform operations on that
data value ; e.g: x=x*2 ; printf(“x=%d”, x) ; x=0;
• The data name is then translated to a memory address in the
main memory space by the compiler.
• The data name is interpreted by the user/programmer
according to its meaning in the real world application.
• A data item may hold a single data value from a specific type
or multiple data values of similar or different types.
Variable and Data Cont.
• This leads to the following classifications:
>> simple (Basic) data type holds Data
only one data value that can not be
split into multiple values ‘Scalable’. Compound Simple
and usually built-in the language. float results[7] int value;
>> compound data type holds more struct { int real; char mask;
than one data value of different types/ float img;
meaning or similar types/meaning } complex
they are usually abstract except arrays ‘Structured’
float, int, charare built-in data types (Basic).
struct { …… } is Abstract Data Type ADT.
‫أ لتجريد ‪Abstraction‬‬
‫هو بناء و تعريف نوع من البيانات غير متضمن في لغة البرمجة المستخدمة و ذلك‬
‫بتجميع عناصره (مكوناته) في تريكبة بيانية واحدة و إخفاء تفاصيلها عن المستخدم‬
‫و ذلك بإحتواءها في إطار لغوي و إعطاءها إسم يمثل هذا النوع الجديد للتعامل معها‬
‫ككتلة واحدة و تعريف متغيرات من هذا النوع و إمكانية الوصول الى عناصرها و‬
‫التعامل معها حسب نوعها مع ضوروة تحديد العمليات عليها ‪.‬‬

‫إذا التجريد يشمل نمدجة بيانات العالم الحقيقي في تركيبة بيانية واحدة و إخفاء‬
‫التفاصيل عن المستخدم ‪ .‬هذا يعني إنه إستحداث للنوع‪:‬‬
‫‪ -‬إستحداث نوع لهذه التركيبة البيانية‬
‫‪ -‬تعريف و تحديد عناصرها‬
‫‪ -‬تحديد العمليات عليها‬
‫‪ -‬راجع األمثلة اآلتية بلغة السي‪:‬‬
Abstraction ‫ أ لتجريد‬cont.
To build a type for complex numbers: X + iY
struct ComNum{ float x ; real imaginary
float y; data structure
};
typedef struct ComNum Complex; new type

To declare variables of type Complex:


Complex n1, n2 ;
elements are:
variable n1.x n1.y
type n2.x n2.y
?? Now ! What is the difference from: int n1, n2 ; ?
Abstraction ‫ أ لتجريد‬cont.
To build a type to handle, manipulate and process data about a course
subject. Each subject has: name, code, credit, level. In C struct is used to
encapsulate data elements.
typedef struct { char name[15];
char code[6];
int ctedit;
char level; /* D,B,M */
} Subject ;
To declare four subjects for a master level:
Subject sbj1, sbj2, sbj3, sbj4 ;
OR: Subject sbj[4] ; !!!!!!!!!! as an array !!!!!!!
To define the values of a subject ; say sbj4 :
sbj4.name=“Computing”; sbj4.code=“EE604”;
sbj4.credit=3; sbj4.level=‘M’;
Data Structures
• The ability to create compound data types using the capabilities
provided by most of programming languages leads to different
data organizations at the level of the program logic and data
abstraction.
• Each data organization forms a compound data unit called Data
Structure.
• Why data structures in Computing ?
>> Since computing deals with data from real-world.
>> Data structure deals with best modeling of the real world data.
>> Data structures enhance processing speed !
>> When data is well organized this eases its manipulation and
handling and also processing.
Data Structures Cont.
>> By organizing data in data structures at the logical level this results
in memory space utilization.
>> Data structures give an acceptable meaning of data as it appears in
the real world.
• Since data structures are not built-in types but are to be defined and
constructed by the programmer, they are called Abstract Data Types
ADT.
• Since any data structure holds many data items, they are treated as
compound data types ! and hence each data structure in the program
has a name.
• To utilize the data of any type and any organization (simple, compound,
abstract) a set of operations have to be defined as algorithms to
perform the operations on that data and produce the required results.
Data Utilization
• Data structures form a Gate Way of data flow between the
program components themselves and the outside world.
• Data under handling, manipulation and processing should be in
the main memory.

Operations
(algorithms)

communication Data structures Files/database

I/O System

Outside world
Data Utilization Cont.
• A program= set of algorithms on a set of data structures
written in a programming language.
• An algorithm = operation on data structure
• An algorithm is a set of steps define the problem solution.
• A problem is solvable algorithmically (computable) if a
computer program can be written and will produce correct
output for any input run for a sufficient time and allocated
enough memory storage.
• From algorithm to program to process.
• The program during execution (process) should be in the main
memory.
• A program at its static state is on a storage media.
Problem Solving and Algorithm Efficiency
• Time and storage space are important factors. They are
the measures of Computational Complexity.
• Algorithms criteria:
1. Correctness 2. Amount of work done 3. Amount of space
used 4. Simplicity and clarity 5. Optimality
• Well defined and selected Data structures :
- Arrays and Matrices - Records - Queues (simple queue,
circular queue, buffers) - Stacks
- Lists (simple linked lists, bidirectional linked lists, circular linked
lists, multi-ways linked lists) - Trees (binary trees, general Trees,
Binary Search trees, Binary sort trees ) - Graphs ( undirected
graphs, directed graphs, un weighted graphs, weighted graphs. -
hash tables
• Algorithms to investigate: searching , sorting for the selected DS.
C Features
• It is a modular, imperative, general purpose programming
language.
• Developed for systems programming by Ritchie and Karinghan
1970  UNIX OS.
• Known as ANSI C / R&K C.
• Basic data types  int char float double
• Compound data types  arrays, struct, union
• Data type abstraction using typedef and struct.
• Program modules  functions with clear interface
• Program Entry point and also exit point  main function
• I/O operations are performed using standard library functions
such as scanf, printf, getchar, ….
• Any name followed by ( ) is considered as a function.
C functions
• To work with functions in C you must consider:
(1) function prototype (declaration or header file)
(2) function definition ( code module)
(4) function call ( in the calling module)
• In these three considerations:
- the function name should be the same
- the returned type should be the same
- the parameters should be the same for type, number and order.
• The function code module structure is as shown:
head / interface
Returned type name ( parameters)
Body {
variables type
code statements
returned value
return value
}
C function example
• The following function computes the sum of two complex numbers.
It implements one of the operations on complex numbers. Assume
that we have already built an abstract data type Complex :

Complex addx ( Complex p1, Complex p2)


{
Complex R ; /* local variable to hold the Result */
R.x = p1.x + p2.x ; /* add real parts together */
R.y = p1.y + p2.y ; /* add imaginary parts together */
return R ; /* return the sum to the calling module */
}
• Exercise 1: code the Subtraction operation .
• Exercise 2: code a function to compare two complex numbers.
Arrays and Matrices
• An array is a set of data items of the same type, have the same meaning and
operations and kept in memory in contiguous locations under one name.
• Each element is accessed individually by that name and index represents its position
in the array.
• Examples:
- A set of students names : each item is a string
- A set of temperature measures : each value is a real number.
- A set of rooms numbers : each number is an integer.
• If the array name is rooms then rooms[3] gives room number 214 and the seventh
room number is rooms[6] which is 422.
index
0 1 2 3 4 5 6
rooms: 115 120 201 214 117 110 422

name data value


Arrays and Matrices cont.
• Two dimensional array form a matrix that has rows and
columns.
• Two indexes are needed one for the rows and one for the
columns.
• Example a matrix or a table of 4 rows and 5 columns that
contains integer values that represent image pixels:
Columns pixels [0][4]
Rows 0 1 2 3 4
0 22 22 22 25 25
pixels:
1 54 45 54 77 77
2 12 88 82 81 80
pixels [2][3]
3 27 42 55 40 44
Arrays and Matrices cont.
• The selection of array dimensions depends on the application and how
data to be organized.
• For example if data represents exam results of 100 students and it is
required to process this data together then it is convenient to use one
dimensional array of type real. In C we can write this as follows: float
results[100];
• And if the application is to process points in X-Y plain that represent
gray level image pixels of size 250 x 250 then a two dimensional array
of type integer is used as follows:
int pixels[250][250];
• Elements access: pixels[40][80] = 120;
printfI(“Level=%d”, pixels[40][80]);
• There are matrices of multi-dimensions: see references !
Arrays and Matrices cont.
• There are many operations on arrays and matrices, but
here the focus is on reading data into an array/matrix,
printing the contents of an array/matrix, Searching and
Sorting an array operations.
• Array sort: there are many sorting algorithms, but here
Quick Sort is considered.
• Array search: there are many searching algorithms, but
here the Binary search is investigated.
• Note: operations on the data items are performed on the
basis of the data type and what to do with that data !
Arrays and Matrices cont.
• Reading and printing arrays:
int values[10] ; /* define an array ‘values’ of 10 elements */
int i, item; /* i for loop , item for a data item */
for(i=0;i<10;i++) {
scanf(“%d”, &item); /* substitute by reading function*/
values[i]=item; }
. . . . . . . . . . . Any process
for(i=0;i<10;i++)
printf(“%d ”, values[i]); /* substitute by output function*/
Arrays and Matrices cont.
• Reading and printing matrices:
int pixels[100][100] ; /* define a matrix 0f 100x100 elements */
int i,j, item;
for(i=0;i<100;i++)
for(j=0;j<100;j++) { /* reading elements of matrix */
scanf(“%d”, &item); /* substitute by reading function*/
pixels[i][j]=item; }
. . . . . . . . . . . Any process
for(i=0;i<100;i++) /* writing elements of matrix */
for (j=0;j<100;j++)
printf(“%d ”, pixels[i][j]); /* substitute by output function*/
Arrays and Matrices cont.
• Quick Sort:
- known also as partition exchange sort.
- selects a reference data item to start with a .
- elements in the data set are partitioned into two groups so
that a is placed at position j and elements from position 0 to j-
1 are < a and elements from position j+1 to n-1 are >= a.
- a becomes the jth smallest element and it remains in this
position until the data set is sorted.
- If the process is repeated recursively for the subsets of the
obtained portions x[0] …x[j-1] and x[j+1]….x[n-1] and then to
the next subsets an so on then we obtain a sorted set of data.
Arrays and Matrices cont.
When x is in its proper position it is ignored in the next divide.

First last

first split Point last

<x xx >x

sort recursively by quicksort sort recursively by quicksort


Arrays and Matrices cont.
• Example: 25 57 48 37 12 92 86 33
The element 25 is selected as a reference, we get :
12 25 57 48 37 92 86 33
So we get subsets: ( 12) 25 ( 57 48 37 92 86 33)
In the less sub set no further partitioning is needed, for
greater subset 57 is selected as a reference ,then we get :
12 25 ( 48 37 33) 57 (92 86 )
then: 48 selected we get
12 25 ( 37 33) 48 57 (92 86 )
then: 37 selected
12 25 ( 33) 37 48 57 (92 86 )
next subset: 92 selected:
12 25 33 37 48 57 (86) 92
No further partition is possible so the data set is get sorted.
Arrays and Matrices cont.
•Programming algorithm:
Quicksort(x,lb,ub) /* lb , ub are upper boundary and lower boundary */
If lb>= ub return /* sort complete */
Partition (x, lb, ub, j) /* partition the data set x between the lower and upper
boundaries and return j as the position of the selected
reference element so that x[i]<x[j] for lb<=i<j and
x[i]>=x[j] for j<i<=ub. x[j] is now in its final position */
quicksort(x,lb,j-1) /* lower sub set: recursion process call with new limits*/
/* returns new lb, j-1 (ub) for its inner recursion call*/
quicksort(x,j+1,ub); /* uper sub set: recursion process call with new limits*/
/* returns new lb(j+1),ub for its inner recursion call*/
/* end of sorting */

•Other Sorting Algorithms:


- Bubble Sort - Straight Selection Sort - Binary tree Sort

- INSERTION SORTS - MERGE AND RADIX SORTS


See references for further study !
Arrays and Matrices cont.
• Binary search:
The most efficient method of searching. Data set had to be
sorted. The argument is compared with the middle value. If
less then lower part is searched again using binary search. If
greater then upper part is searched. The following algorithm
shows that:
low=0; hi=n-1 ;
while(low<=hi) {
mid=(low+hi)/2;
if(key==key(mid) return mid ;
if(key<k(mid) hi=mid-1;
else low=mid+1;
} /* end while *
return -1;
Arrays and Matrices cont.
• Other Searching Algorithms:
- Sequential Search
-Indexed Sequential Search
- Interpolation Search
- Tree Search
- Multi-way Search Trees
-Digital search Trees
- HASHING
Queues
simple queue, circular queue
• It is an ordered collection of data items from which items are
deleted at one end (called front of queue) and to which items
are inserted at the other end (called rear of queue).
• It is a dynamic data structure where the actual length
(number of elements) stretches and shrinks at run time.
• Implementation: 9 8 7 6 5 4 3 2 1 0 -1
rear 5 2 6 7 12 8 25 15 72 11
#define MAXQUEUE 10
struct queue { q Front
int items[MAXQUEUE] ;
int front ;
int rear ;
}q;
q.front=0; q.rear=-1; // initially queue is empty
Queues
simple queue, circular queue
• The operations can be described as follows:
Insertion: q.items[++q.rear] = x ;
Remove: if(!empty(q) x= q.items[0] ;
And the shift function is :
for(i=0; i<q.rear;i++)
q.items[i] = q.items[i+1] ;
q.rear -- ;
The empty function can be as follows:
return ( q.rear == -1 ) ;
Queues
simple queue, circular queue
• For circular queue : see references
• It is also called bounded buffer
• Useful for producer – consumer processes
• Queue is empty if rear=front pointers.
• Queue becomes full when rear reaches front
pointer-1.
Trees
Binary Search trees, Binary sort trees
• Trees are dynamic data structures.
• Each element is called a node.
• Nodes are organized as a tree structure that has a root and leaves called child (or son) and
the node called parent (or father) node.
• In this structure each node forms a sub-tree as shown in the figure:

Struct nodetype { root A


int data ;
struct nodeyupe *left;
struct nodetype *right; sonB
struct nodeytype *father;
C
};

typedef struct nodetype *NODPTR; D lE


eaves F G
Trees cont.
Binary Search trees, Binary sort trees
• Traversing a binary tree in preorder involves three operations:
- Visit the root.
- Traverse the left sub tree in preorder
- Traverse the right subtree in preorder
Gives: A B D E C F G
• Traversing a binary tree in inorder involves three operations:
- Traverse the left subtree in inorder.
- Visit the root
- Traverse the right subtree in inorder.
 Gives: D B E A F C G
• Traversing a binary tree in postorder involves three operations:
- Traverse the left subtree in postorder.
- Traverse the right subtree in postorder
- Visit the root
Gives: D E B F G C A
Trees cont.
Binary Search trees, Binary sort trees
Example: sort the following set using binary tree
22 17 25 22 15 44 15 10 25 18
First: build a binary sort tree as shown below.
Second: traverse the tree using inorder algorithm.
Third: the obtained output is a sorted set.
22
< >=
17
25
15 18 22 44

10 15 10 15 15 17 18 22 22 25 25 44
25
Trees cont.
Binary Search trees, Binary sort trees
• Nodes implementation:
NODPTR maketree(int x) {
NODPTR p;
P=(NODPTR) malloc(sizeof(struct nodetype));
p->data = x;
p->left= NULL;
p->right=NULL;
p-> father= NULL;
return p;
}
void setleft( NODPTR p, int x) {
if(p==NULL) printf(“No insertion”);
else if(p->left !=NULL) printf(“Invalid insertion”);
else {
p->left= maketree(x)
(p->left)->father= p; }
}
void setright( ) { // complete it }
Trees cont.
Binary Search trees, Binary sort trees
• Traversal Algorithms Implementations:
Void pretrav(NODPTR tree) {
If(tree!=NULL) {
Printf(“%d\n”,tree->data); // visit the root
Pretrav(tree->left); // traverse left subtree
Pretrav(tree->right); // traverse right subtree
} /* end if */
} /* end pretrav */
Void intrav(NODPTR tree) {
If(tree!=NULL) {
intrav(tree->left); // traverse left subtree
Printf(“%d\n”,tree->data); // visit the root
intrav(tree->right); // traverse right subtree
} /* end if */
} /* end intrav */
Void posttrav(NODPTR tree) {
If(tree!=NULL) {
Posttrav(tree->left); // traverse left subtree
Posttrav(tree->right); // traverse right subtree
Printf(“%d\n”,tree->data); // visit the root
} /* end if */
Trees
Binary Search trees, Binary sort trees
• Binary Tree Searching : Binary trees are used to organize data in a
sorted form if it is retrieved using inorder traversal algorithm. Binary
trees are also used for binary search as shown by the following
algorithm:

P=tree ; /* pointer to the root */


While(p!=null && key!=key(p) ) /* go through the tree
until item is found or end*/
P=(key <key(p) ? left(p): right(p);
return p;
• Exercise: refer to the previous binary tree; search for the values: 22
25 10 45
Trees cont.
Binary Search trees, Binary sort trees
• Binary tree Sort implementation:
/* establish the first element as a root */
tree=maketree(x[0]);
/* repeat for each successive element the followings */
for(i=1; i<n, i++) {
y=x[i];
p=tree ;
/* traverse down the tree until leaf is reached */
while(p!=null) {
q=p;
if(y<data(p)) p=left(p) ;
else p=right(p);
} /* end while */
if(y<data(q)) setleft(q,y);
else setright(q,y);
} /* end for */
/* tree is built. Traverse it using inorder algorithm */
intrav(tree);
Graphs
directed graphs, weighted graphs
• A graph is a set of nodes (vertices) and a set of arcs (edges).
• Each arc is specified by a pair of nodes
• nodes= { A,B,C,D,E,F,G,H} , arcs={(A,B), (A,D), (A,C), (C,D),
(C,F), (E,G), (A,A) }
• A graph is called directed graph if the pairs of nodes are
ordered pairs; undirected otherwise
B
E H
A
D
G
C
F
Graphs
directed graphs, weighted graphs
• A relation R on a set A is a set of ordered pairs of
elements of A that associated with some relation.
Example if A={3,5,6,8,10,17} then the set R={<3,10>,
<5,6>, <5,8>, <6,17>, <8,17>, <10,17>} is a relation set.
This relation can be described as follows (<x,y>): x is
related to y if x<y AND y%x is ODD.
• A relation is represented by a graph in which nodes are
the underlying set and arcs represent the ordered pairs of
the relation: 3 10
Each arch is weighted by the 5
8
relations R: y%x is odd and x<y 17
6
Graphs
directed graphs, weighted graphs
•Operations on Graphs:
The followings are some common operations on graphs:
join(a,b): add an arc from node a to node b if not exist.
joinwt(a,b,x): add an arc from node a to node b with weight x.
remv(a,b) and remvwt(a,b,x): remove an arc from node a to node b.
Add node to a graph, create a node .
Delete node from a graph
Adjacent(a,b): returns true if a is adjacent to b an d false otherwise.

A B C D E A B C D E
A 0 0 1 1 0 A 0 0 0 1 1
B 0 0 1 0 0 B 0 0 0 1 1
C 0 0 0 1 1 C 0 0 0 1 1
D 0 0 0 0 1 D 0 0 0 1 0
E 0 0 0 1 0 E 0 0 0 0 1
Adjacent matrix with length=1 adjacent matrix with length 2
( 1 indicates an edge between two nodes)

Path length= number of nodes from node a to node b.


Graphs
directed graphs, weighted graphs
• Graph creation:
#define MAXNODES 50
struct node {
/* information associated with each node */
} ;
struct arc {
int adj;
/* information associated with each arc */
};
Struct graph {
struct node nodes[MAXNODES];
struct arc arcs[MAXNODES][MAXNODES];
};
Struct graph g ;
Q & A

END OF P A R T III

You might also like