You are on page 1of 6

ICS 2105 – DSA 7. Aggregation (summing up, creating 3.

Be easy to read and understand (comments,


Introduction summaries, obtaining totals) proper naming and naming conventions and
Data is raw fact(s). Data can be processed to 8. Update (keeping track of changes to ensure indentation).
information. This information can be used for that data is in the correct current state) 4. Be easy to debug by subdividing the program
information making. Data is essential in taking action 9. Retrieval into manageable chunks (blocks) and avoid
or an informed decision. There are many sources of 10. Protection against loss, unauthorized access, the use of unplanned pointers and global
information and a piece of information may be theft variables.
generated from multiple resources. 5. The program should also be easy to modify
It is important that there be flexibility in the ways that Objectives of Data Management systems such as the creation of documentation,
data items can be aggregated and summarized so that - To make data resources resilient (stable, modularization (ease in replacing problematic
meaningful information can be produced to support unchanged, can withstand damage) modules).
decision making. This is the essence of data structures - It must be flexible and adaptable to support
and algorithms. an organizations decision making processes Programming Strategies
It is necessary to have formal ways of constructing a
Data elements in an organization may have resulted There are 4 useful guidelines for a Data program so that it can be built efficiently and reliably.
from actions taken by forces or bodies external to the Management System This can be done by decomposing a program into
organizations. The data resource must support several 1. Data must be represented (place in a specific small modules, that can themselves be written and
types of decision-making. form) and stored so that they can be tested before being incorporated into larger modules
accessed later. which are in turn constructed and tested.
There are three main decision types 2. Data must be organized so that they can be
1. Operational – day to day running of the selectively and efficiently accessed. Advantages of Modularization
organization 3. Data must be processed and presented so 1. Addresses problem complexities, ease of
2. Tactical/control – the mission of the that they support the user environment understanding.
organization effectively. 2. Ease of reading and understanding
3. Strategic/planning – long term plan 4. Data must be protected and managed so that 3. Testing is easy
they retain their values. 4. Maintenance is cheap and fast
Data Management
Data is expensive and important. They must be Data Structures We need a formal basis for partitioning our large tasks
managed in such a way that they are correct and A data structure is an organized collection of data plus into smaller ones. The notion of abstraction is very
available to produce the needed information. all its allowed operations. useful. Abstractions are high-level views of objects or
Data type – a possible set of data and its allowed functions which enable us to forget about the low level
Aspects of Data Handling operations. details and concentrate on the problem at hand.
The aspects of data handling include the following
1. Measurement Programming Structures There are two important forms of abstraction:
2. Collection 1. Functional abstraction
3. Transcription (translate to a form that can be Good Program must have the following properties: 2. Structural abstraction
understood) 1. Runs correctly, in accordance with
4. Validation specifications. Functional Abstraction
5. Organization (putting related data together) 2. Runs efficiently making good use of memory In this form we specify functions for our modules e.g.
6. Storage (hold the data for future reference) and runs fast. this module will sort the items in its input stream into
ascending order based on an ordering rules for the Pre and Post Conditions In order to learn more about algorithms we analyze it
items and place them on its output stream. At this point Pre-conditions define the state of the program which i.e. we study the specification of the algorithm and to
we are not concerned with how the sort is performed the client guarantees will be true before calling any draw conclusion about how the implementation of that
but simply that output is sorted according to the methods. These are conditions that must be satisfied algorithm (program) will perform in general.
ordering rule. before a procedure (method) is executed.
A post-condition defines the state of the program that We analyze the following:
Structural Abstraction the object methods will guarantee to create for you 1. Run time of a program as a function of its
This is better known as object orientation. We when it returns. input.
construct software models from the behavior of real 2. Determine the total/maximum memory space
world objects. Template Functions needed for program data.
These are special constructs that allow the user to 3. Determine the total size of the program code.
Terms and concepts in OOP create generic data types. 4. Determine whether the program correctly
Abstract Data Types computes the desired results.
These are data types whose properties (Domain and Error Handling 5. Determine the complexity of the program.
operations) are specified independently of any other 6. Determine the robustness of the program.
implementation. No program or program fragment can be considered
complete until appropriate error handling has been The primary concern of algorithm analysis is the
Composite Data Types added to avoid unexpected program failures and running time. There are many factors that affect the
These are ADT’s that contain component elements disasters. running time of any program. They include
(logical arrangement of data elements and a set of If errors aren’t processed correctly the program cannot 1. Hardware – processor used, memory
operations needed to access the data elements). guarantee the expected result. In case of an error that available
program cannot sensibly continue. The program must 2. Programming language in which the algorithm
Primitive Data Types thus be able to die gracefully. This is done by informing is written
*** the user of the error and saving the state of the 3. The language compiler/interpretor used.
program at the point prior to the error. 4. The computer’s operating system.
Constructors
This is a member function of a class that does the Algorithms Symbols used: -
following: These symbols have mathematically precise symbols.
1. To allocate memory for the object This is a finite collection of precise instructions to They are used to evaluate and concisely describe the
2. To initialize non - static members of a class perform a certain task, in a finite amount of time and performance of an algorithm in time and space.
with a finite amount of resources.
Destructors 1. Big O. This is the upper bound of a function.
This is a member function that reallocates memory An algorithm can be given in many ways e.g. It could Incase of algorithm analysis, we use this to
space, which is done automatically. be written down in English or any other natural bind the worst case running time or the
language. longest running time possible for any inputs of
Methods We are interested in algorithms that have been size n. The maximum runtime of the algorithm
These are clear sequences or steps of instructions precisely specified using mathematical formalism. E.g. is in the order of Big O.
taken to accomplish a particular task. Programming languages. 2. Omega Ω (Upper Case). This is also the
An algorithm is the description of the work to be done. order of growth but is the opposite of Big O. It
is the lower bound face of a function. The
minimum runtime of an algorithm is in the 1. Rough binding. Using this method, we bind Write a program to output C++Program
order of Omega. each time by something simple to make the
3. Beta β. This is the rate of growth, compound summation manageable. The size of an array must be known before program
of Big O and Omega. 2. Exact computation. In this method we have execution.
4. Little o to compute the exact numerical value for the The main advantage of an array data structure is
5. Omega ω (Lower Case) summation. the ease of access. They have direct
3. Induction. This can be used to prove some correspondence with memory systems in all
Complexity Classes statements. Induction with respect to a, computer systems.
parameter n. We assume that the statement
1. Class I – Constant time. This is where the is true up to but not including n. Thus we can There exists 2 or more dimensional arrays.
running time of the algorithm does not depend verify the statement for n.
on the value of n. 4. Telescoping series. Telescoping is a series e.g.
2. Log*n where each term can be expressed as a int store[4][3];
3. Logn n – Logarithmic time. A common difference in such a way that most of the
application of this is used binary search. terms cancel each other out. The disadvantage of arrays is the fact that the size
Every time one goes through the recursions 5. Comparison with an integral. must be known.
the problem is reduced in half.
4. n – Linear time. The running time of the Elementary Data Structures Linked List
algorithm a is the proportional value of n. Arrays
5. n*Log(n). These are a fixed number of data items that are stored diagram
6. n2 – Quadratic time. An increase in the input contiguously and are accessible by an index. We refer
directly affects the input, causing it to double to the first element of an array a by a[i]. It is the The main advantage of linked list over an array is
or even triplicate. responsibility of a program to store something that the link list can grow and shrink in size during
7. na – Polynomial meaningful in an array position a [ i ] by referring to it. their lifetime. They provide flexibility in allowing the
8. 2n, 10 n, a n – Exponential time items to be rearranged.
9. n! – Factorial time e.g.
#include <iostream> A linked list is a set of items arranged or organized
Summations #define n 10 sequentially, just like in arrays. In an array, the
Summations are used as a mathematical tool to help int main () sequential organization is provided by the position of
analyze the running time of algorithms. In most cases, { the array.
algorithms are completed through the repetition of int marks [n];
simple actions or by incursion. for(int i=0; i<n; i++) In a linked list we use explicit arrangements in which
{ each item is a part of a node that contains a link to the
In order to anayse the time taken by looping programs cout<<”Enter Element”<<i++; next node.
we have to deal with summations as one must sum the cin>>marks[ i ];
time taken in various iterations. } In the above diagram, it shows a linked list with items
return 0; represented by letters. The nodes are represented by
There are a number of ways to deal with summations: } circles and the arrow represents the link. The head
points to the first node of the list. The main purpose of
Exercise the dummy nodes is to allow for certain operations to
be executed, especially those involving the first and - To the item after 1. Enqueue – places an element value onto the
last node. queue (at the back of the queue).
The cost of providing this extra capability is doubling 2. Dequeue – removes an element from the
The explicit representation of the ordering allows the number of links on manipulation of operations. queue (from the front of the queue).
certain operations to be performed much more
efficiently than would be possible for arrays. Some Stacks Enqueue consists of the following actions:
operations are made possible e.g. moving ‘s’ to the A stack is an abstract data type in which elements are - Identification of the array element
beginning of the array, which would involve moving all added and removed from only one end, a last in or first that is at the back of the queue.
the elements in the array. out structure (LIFO). - Placing an element onto the queue.

The disadvantage of linked list is the access time At logical level a stack is an ordered group of Disadvantage of Queues and Stacks
taken. homogeneous items or elements, occurring in a Their sizes must be known prior to implementation of
particular sequence, organized according to how long\ actions.
e.g. they have been on the stack.
struct node
{ Because elements are only added and removed from
int key; the top of the stack, the last element to be added to it
struct node*next; is the first to be removed.
};
struct node* head*E; Stack operations
head = new node; 1. Push – this is an operation hat adds an
Z = new node; element to the top of the stack.
Head ->next = Z; 2. Pop – this is an operation that removes an
element from the top of the stack.
Inserting an element in a linked list 3. Top – this is an operation that returns a copy
- Create the node of the top element, without removing it from
- Make it point to the next/previous item the stack.
- Destroy the existing link 4. Makeempty – this operation sets the stack to
empty, and when one needs to use a stack it
Exercise must be empty.
Create a list with 5 elements 5. Isempty – this is an operations that is used to
tell whether a stack contains any elements
Types of linked lists before we pop it.
1. Circular linked lists. These are sometimes 6. Isfull – this is an operation that is used to test
used as an alternative for having damaged whether a stack is full before pushing.
nodes.
2. Double linked lists. It makes it possible to find Queues
the item before *** by using double linked list These are sequential organizations of data. Data is
in which we maintain 2 links on each node: - accessible using first in first out order (FIFO).
- To the item before
Trees Each node has exactly one node above it. The
A tree is an empty collection of vertices and edges that nodes directly below a node are called child/children.
satisfy certain requirements. Nodes without children are sometimes called leaves
or terminal nodes.

We refer to unterminal nodes e.g. D as iternal nodes.


Any node is the root of a sub tree consisting of it and
nodes below it. A set of trees is called a forest.

An ordered tree
Sometimes the way in which the children of each node
are ordered is significant. An ordered tree is one in
which the order of the children at any node is A binary tree could be empty, consisting of no internal
specific. nodes and external nodes. A full binary tree is one in
A vertex is a simple object that has a name and which internal nodes completely fill every level except
carries associated information. An edge is a Levels probably the last.
connection between two vertices. The level of a node is the number of nodes on the
path, from the nodes of the roots, not including itself. Applications of trees
A path in a tree is a list of distinct vertices in which Tree traversals – visiting elements of a tree.
successive vertices are connected by edges in the Height Traversal tree
tree. The height of the tree is the maximum level among all To traverse a tree is to systematically visit every node.
the nodes in the tree. There are a number of methods for traversing a tree.
One node in the tree is designated as the root. The
defining property of a tree is that there is exactly one Path length 1. Pre-order (ABC)
path between the route and each of the other nodes in This is the sum of all the levels.
the tree. If there is more than one route between the
route and some node then we have a graph. Multi-way
If each node must have a specific number of children
appearing in a specific order, then we have a multi-way 2. Post-order
tree. (BCA)
A multi-way tree is appropriate to define special 3. In order
external nodes which have no children. They are called (BAC)
dummy nodes.
The simplest type of the multi-way tree is the binary
tree. A binary tree is an ordered tree consisting of 2
types of nodes;
1. External nodes with no children
2. Internal nodes with exactly two children
Example 2 of Pre-order traversing

Algorithms
1. Pre-order transversal
This order is defined by the recursive rule.
Step1: Visit the root, then visit the left sub-tree
Step2: Then visit the right sub-tree

Pre-order of binary tree


PMSAALERTEE

2. In order transversal
Step1: Visit the left sub-tree
Step2: Visit the root
Step3: Visit the right sub-tree

In-order binary tree


ASAMPLETREE

3. Post-order transversal
Step1: Visit the left sub-tree
Step2: Visit the right sub-tree
Step3: Visit the root

Post-order for binary tree


AASQMTEERELP

Construct a perse tree


(A+B)*C+(D+E)

You might also like