EEM 480 Algorithms and
Complexity
by
Assist. Prof. Dr. Emin Germen
What is this Course About
Of Course it is about :
Algorithms
Complexity
Any More?
Algorithms + Complexity = Data Structure
Data Structure : Concerns with the
representation and manipulation of data.
All the programs use and manipulate data
IS (Computer Programming == Data
Structure)
YES OR NO
What is Data Structure
Designing the program which modifies data
The Key Point
Algorithm
What is an Algorithm?
Algorithm is a procedure that consists of a finite
set of instructions which, given an input from
some set of possible inputs, enable to obtain an
output
Some Examples
Suppose you and I have the same amount of money.
How much should I give you so that you have $10
more than I do?
A dealer bought a book for $7, sold for $8, bought for
$9, sold for $10. How much profit did he make?
Solution provides an algorithm.
What is this course about
Learning and designing algorithms to
manipulate the data
Comparing criteria of different algorithms
Understand what a good program is
Learning effective utilizations of computer
sources
Syllabus
Review of C++
Platforms
Classes
Encapsulation, Overloading, Inheritance, Overriding
Pointers to objects
Templates
Algorithm Analysis
Necessary math review
Complexity definitions
Running time calculations
Arrays and Linked Lists
Linear lists
Formula based representation
Linked representations
Indirect addressing and pointers
Arrays and Matrices
Arrays, Matrices, Special matrices, Sparse matrices
Stacks
Queues
Trees
Binary trees
Tree traversals
B trees
Hash Tables
Priority Queues
Sorting algorithms
Basic Data Structure Implementations
Path compression
Union/Find Implementations
Reference and Evaluation
Data Structures, Algorithms and
Applications in C++ by Sartaj Sahni
Evaluation:
Mt 1
Mt 2
Projects
Final Exam
10%
20%
30%
40%
C++ Environment
Dot NET Platform
Microsoft Visual Studio 2005
Microsoft Visual Studio 2008
Microsoft Visual Studio 2010
Visual Studio 6
Eclipse, Netbeans IDE
etc., etc., ….
http://www.cplusplus.com/doc/tutorial/files/
C++ on Dot NET Platform
Code Window
Output
Solution Window
What is .NET Platform
MSIL is a CPU and
platform independent
instruction set that can
be executed in any
environment supporting
the .NET framework.
MSIL code is verified for
safety during runtime,
providing better security
and reliability than
natively compiled
binaries.
Before proceeding…
Basic Input/Output
The standard C++ library includes the header file
iostream, where the standard input and output stream
objects are declared.
Standard Output (cout)
cout << Hello world"; // prints Hello world on screen
cout << 17; // prints number 17 on screen
cout << my_var; // prints the content of my_var on screen
Standard Input (cin)
int my_age
cin >> my_age
Time for first codes
Too much errors. Why ?
Lets get rid of the errors
Magic is here
It works
Namespace
Namespaces allow to group entities like classes, objects and functions under a name. This
way the global scope can be divided in "sub-scopes", each one with its own name.
The format of namespaces is:
namespace identifier
{
entities
}
Where identifier is any valid identifier and entities is the set of classes, objects and functions
that are included within the namespace. For example:
namespace myNamespace
{
int a, b;
}
In this case, the variables a and b are normal variables declared within a namespace called
myNamespace. In order to access these variables from outside the myNamespace
namespace we have to use the scope operator ::. For example, to access the previous
variables from outside myNamespace we can write:
myNamespace::a
myNamespace::b
Namespace
The functionality of
namespaces is
especially useful in the
case that there is a
possibility that a global
object or function uses
the same identifier as
another one, causing
redefinition errors. For
example:
Output
5
3.1416
using
The keyword
using is used to
introduce a
name from a
namespace into
the current
declarative
region. For
example
Output
5
2.7183
10
3.1416
Object Oriented Programming
Object
Class
Encapsulation
Inheritence
Polymormhism
Object
An object is a bundle of variables and related
methods.
When an object is mapped into software
representation, it consists of 2 parts:
DATA SPECIFICATION
characteristics of data specification are referred to as
ATTRIBUTES
PROCESSES that may correctly change the data
structure. Processes are referred to as OPERATIONS
or METHODS
Class
A class is a blueprint for an object.
Objects with the same data specification (Attributes) and behavior
(Methods or Operations) are grouped together (called a class ).
Encapsulation
Encapsulation is the procedure of covering
up of data and functions into a single unit
Class
Data1
Data2
Procedure1(Data1)
Data2 = Procedure()
Encapsulation hides the implementation
away from the user
Encapsulation Example Try
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;
class C {
public:
C(char *s = "", int i = 0, double d = 1) {
strcpy(DataMember1,s);
DataMember2 = i;
DataMember3 = d;
}
void main(void)
{
C obj1("object1",100,200);
C obj2("object2"), object3;
obj1.MemberFunction1();
obj1.MemberFunction2(123, "object2");
}
void MemberFunction1() {
cout << DataMember1 << ' ' << DataMember2 << ' ‘
<< DataMember3 << endl;
}
void MemberFunction2(int i, char *s = "unknown") {
DataMember2 = i;
cout << i << " received from " << s << endl;
}
protected:
char DataMember1[20];
int DataMember2;
double DataMember3;
};
Inheritence
As objects do not exist by themselves but are instances
of a CLASS, a class can inherit the features of another
class and add its own modifications. (This could mean
restrictions or additions to its functionality). Inheritance
aids in the reuse of code.
Some New Features with OOP
Generic Class:
class or interface that declares one or more type
variables or formal type parameters and can be
parameterized. List<E> is a generic type.
Why is it necessary?
class intClass {
int storage[50];
................
};
class floatClass {
int storage[50];
................
};
template <class genType>
class genClass {
genType storage[50];
.................
};
genClass<int> intObject
genClass<float> floatObject
Generic Class Example
template <class genType>
void myswap(genType& e1, genType& e2) {
genType tmp;
tmp = e1;
e1 = e2;
e2 = tmp;
}
void main(void)
{
int
float
i1, i2;
f1, f2;
i1 = 10; i2 = 20;
f1 = 1.0; f2 = 2.0;
cout << "Before i1 = " << i1 << " i2 = " << i2 << endl;
myswap<int>(i1,i2);
cout << "After i1 = " << i1 << " i2 = " << i2 << endl;
}
Polymorphism
Polymorphism means the ability to request
that the same Operations be performed by
a wide range of different types of things.
So What???????
What are those things????
How can I use them???????
I have searched OOP in the Internet and
find ABSTRACTION. What does it
mean????
I can write good programs using structural
proggramming techniques? Why should I
use OOP?
Intoduction to OOP with C++
Object Oriented Programming
Class
The structure which keeps data and function
Interitance
Inheritance is also called derivation. The new class
inherits the functionality of an existing class. The
existing class is called the base class, and the new
class is called the derived class. A similar inheritance
can be derived for animals, mammals, and dogs.
Polymorphisim Overloading
Ability to have more than one function with the same name that
differ in their parameter lists.
Program 1
#include <stdio.h>
#include <iostream>
Header for the basic C++ procedures
void main(void)
{
cout << "This is not my first program";
cout << "\n";
}
Streaming functions
Program 2
//
//
//
//
Workspace: Triangle
Program name: Area.cpp
The area of a triangle is half its base times height
Area of triangle = (Base length of triangle * Height of
triangle)/2
#include <iostream.h> // Precompiled header
double base,height,area; // Declaring the variables
double Area(double,double); // Function
Prototype/declaration
int main()
{
cout << "\nEnter Height of Triangle: "; // Enter a
number
cin >> height; // Store the input in variable
cout << "\nEnter Base of Triangle: "; // Enter a
number
cin >> base; // Store the input in variable
area = Area(base,height);
// Store the result from
the Area function
// in the variable area
cout << "The Area of the Triangle is: "<< area << endl
;
return 0;
}
double Area (double base, double height) // Function
definition
{
area = (0.5*base*height);
return area;
}
The Output
Program 3 Polymorphism
if (choice == 2)
{
cout << "Enter radius of the
Circle: ";
cin >> radius;
Area_of_circle = Area(radius);
cout << "The area of the Circle
is: " << Area_of_circle<<endl;
}
if (choice != 1 && choice != 2)
{
cout << "Sorry! You must enter
either 1 or 2 \n";
}
# include <iostream.h>
double base,height,radius; // Global
variables
double Area_of_triangle,Area_of_circle; //
Global variables
int choice; // Global variable
double Area (double,double); // Function
prototype
double Area (double); // Function prototype
const double pi = 3.14; // Constant variable
}
void main() // main function
{
cout << "To find the area of a Triangle,
input 1 \n";
cout << "To find the area of a Circle,
input 2 \n";
cin >> choice;
if (choice == 1)
{
cout << "Enter the base of the
triangle: ";
cin >> base;
cout << "Enter the height of the
triangle: ";
cin >> height;
Area_of_triangle =
Area(base,height);
cout << "The Area of the Triangle
is: " << Area_of_triangle<<endl;
}
double Area (double base, double height)
{
return (0.5*base*height);
}
double Area(double radius)
{
return (pi*radius*radius);
}
The Output
Defining Class in C++
Almost same as defining Struct
Insert Functions into the Struct
Some important functions
Constructor
Destructor
Object Oriented C++ Class
#include <iostream.h>
class CCircle
{
public:
CCircle(int r);
void SetRadius(int r);
void DisplayArea(void);
~CCircle();
private:
float CalculateArea(void);
int m_radius;
int m_color;
};
CCircle::CCircle(int r)
{
m_radius = r;
}
CONSTRUCTOR
CCircle::~CCircle()
{}
void CCircle::DisplayArea(void)
{
float fArea;
fArea = CalculateArea();
cout << "The Arae of the circle : "
<< fArea << "\n";
}
float CCircle::CalculateArea(void)
{
DESTRUCTOR
float f;
f = (float) (3.14 * m_radius *
m_radius);
return f;
}
Dynamic Memory Allocation
new
int *y //pointer
y = new int; // creates place
*y = 10 // OK
Or
int *y = new int(10)
OOP in C++
Simple example of defining object and
class
Overloading
Programming Example
Inheritance
Programming Example
Programming Example
Templates
Programming Example
One Dimensional Array and
Exception Handling
float *x = new float [n]
Or better
float *x
try { x = new float [n];}
catch (xalloc) {
cerr << “Out of memory << endl;
exit(1); }
Delete
delete y; //free the memory allocated by *y
delete [] x // free the one dimensional array
© Copyright 2026 Paperzz