You are on page 1of 203

Pramod Chandra P.

Bhatt
Retired Prof. (IIT/D, IIIT-B)

Vidha Software Solutions


Data has some context:
- Personal data
- Data about a company / business
- Data about a region
- Data about an artifact: a table, computer
- Historical or logged data
- Data to lay out an integrated circuit
- Data about a civil structure like a column
- Data about water distribution network
- Data about a manufacturing plant
and so on.
The purpose: obtain insights from the data.

Personal data: specific personal information

Regional data: Economic prosperity

System performance from logged data


! "
Perform some operations on data.

Example: Find the


- average age of company employees.
- the maximum salary in a company
- count of types of components in a circuit
- probability of an earth quake in a quake prone area
# # $ %

Suppose we think about a switch. How do we


obtain a representation of a switch?
- draw a picture of a switch as it appears

- also shows a switch

- Integer values 0 / 1 also models a switch


$
Primitives:
- Binary data – like the switch as 0 / 1

- Bytes – characters in data transmission

- Word – to capture the integer values

- Partition a Word – capture exponent


and fractional part to describe real values
$

Mechanisms: Put primitives together to get more


complex structures.
- Example: A text string is a structure which is a
sequence of bytes
- Example: A two column table may have
employee_Id along with salary information
Structures capture relationships amongst the objects
(every form of data with a value is a data object).
Items to the left may
be smaller in value

Order relationship: bigger (smaller), next, is_a,


is_a_part_of, or simply a structured collection – like
name, age, education etc.
Typical computation requires use of data

Computations must use the permitted


operations.

The permitted operations are determined by


the context the data is modeling.
&
Model a switch: data may be binary to capture the
notion of “off” and “on”.
Model of a channel status: may also be binary to
capture if it is “busy” or “free”
For binary data the operations may be
- interrogate if it is 0 or 1 (“false” or “true”)
- complement the current value
Note there may be switch models with multiple values like on a
fan: “low”, “medium”, “high” to choose the speeds. then we may
have operations like “next”, “previous”, “last” etc. as well.
'
Consider integers. The operations may be:

- Special tests: = 0, <> 0, >= 0, <= 0, < 0

- Binary tests: =, <>, >=, <=, < a constant,


between two integers

- Binary operations: +, -, *, /, mod, ^

There are also some representational issues like


short_int, long_int etc.
(
Corresponding to every integer operation we may have
an operation for real numbers - except that a real is
raised to an integer exponent.
There may be type conversion or coercion possible.
Text or character data may have the following
operations:
string_length, string_copy, string_compare,
string_concatenate, next_characer value, detect
presence or absence of a substring etc.
)
What if we have structures?

Structures may be provided by built-in facility as


is the case by a sequence of data may be
defined by an array.

Structures may be user defined such as the


quadruple like (name, age, gender, title) or a
parts_list (for a washing machine).
* %
An array defines a collection of data-items in
sequence. An index defines the position in
sequence. The indices can have only integer values.

All data-items have the same structure in an array.


For example:
- an array of integers – all array elements can
only be integers.
- An array of structured data like (name, age,
gender, title) will have quadruples with the
same order.
* % &
Operations may be defined on indices, or array
elements.
All integer operations are possible on indices. Two
useful index operations are “increment_index” or
“decrement_index” by one or by an integer
expression.
Just as we can structure data by composing some
constituents we can define operations by combining
the basic primitive operations.
For instance, we can define a sort_by_name, or sort_by_age
on an array of triples with (name, age, education). (We need a
user defined comparator operator for our three field data from
primitive data operations)
+ * %
Suppose we have an array of integers. How is it
represented?
A short_integer occupies one word. So, a sequence
of memory addresses can store an integer array.
A long_integer may occupy two words. An increment
of 1 in index actually jumps two address ahead.
In general, an array of triples will store these triples
in sequence. The triples are separated by the
number of words a triple may require to store.
, # - +
Advantage:
- Accessing data at a certain index, takes a fixed
amount of time.
- To modify indexed information also requires a
fixed amount of time - retrieve, modify, rewrite.
Disadvantage:
- To insert (or delete) data at an index requires
redefining the indices of all affected data – this
may require all the n – data items in an array.
Later, we shall study where the sequential order (in
elements) need not be preserved.
. * %$
There are many situations when we need to
have representations of the form where each
element of an array is itself as array.
A two dimensional array is called a Matrix.
Such a matrix is useful to capture a set of
simultaneous algebraic equations.
Two dimensional arrays throw-up new
operators: like transpose, determinant, adjoint,
inverse etc.
. * %$

In general we may have more than two


dimensional arrays.

Additionally, there are representational issues


like storage of a sparse matrix.

The representation impinges on the efficacy of


the operation.
$ %
In most programming languages a text string is
stored as an array of characters.
For such text string additional operators that
are defined are:
- character_at_an_index,
- index_of_a_character,
- substring_starting _from_an index,
- index_of_first_matching_substring,
- substring_between_two_indices etc. etc.
%

Data always has a certain context.


A data is essentially a model.
Data needs representation and also data
operation definitions.
Operators are defined by the context of the data.
Data representation can be a major
consideration in determining the performance of
the operations.
/

Any Questions ???


*/ + #

An introduction to the problem solving process

Modeling some example scenario(s)

An introduction to Abstract Data Type (ADTs)

Some example ADTs and data structures


/ + ,

Data has context.

Data usually models some entity.

Data has to have a representation.

Data representation affects performance.


0 , ,
To solve a problem it is should be formalizeable.
Formalization is essentially a modeling process.
Once we have a model of an application scenario, we can explore
the scenario to find the desired solution.
Exploration requires
- representation of entities
- a sequence of operations – each step is an
identifiable elementary step.
The representation is called a data-structure and the sequence of
steps is called an algorithm.
Usually a program attempts the exploration:
- Program = algorithm + Data structure
* $
*

OUT IN

Services Sought :
- letters and stamp vending etc..
- registered letters, courier letters, parcels etc.
- money order, vikas patra, savings bank etc.
, %
What should be the number of counters?
Customer patience is limited: how to minimize the waiting times at
the counters?

Attempted Strategy:
1. For a given number of counters, find a
suitable combination of services that
minimizes average customer waiting time.
2. Increment / decrement the number of
counters and repeat step - 1.

Note we are already heading to model the scenario !


$
Entities of interest:
- the clerks pool – could be an array.
- the customer pool – technically infinite but could be
modeled as a large enough queue to simulate infinity with
customer reuse.

The response time of clerks: depends upon the nature of


service, interruptions like phone calls etc.

There could be “counter-queues”, one for each of the


counters under the assumption that the customers are well
behaved – they form a queue and seek FCFS and there are
no priorities. The waiting time for customers at counters.
0 ,
Input specification:
- arrival rates - service times
Output specification
- queue lengths
- average and maximum waiting times
- the number of customers serviced
Exploration: combine different services and
simulate for different number of clerks.
*
A case for visualization:
B

C A

Three roads meet and we need to capture typical


traffic movement with regulated traffic lights.
*
A case for visualization:
The table entries depict conflict of interest - B
can also be modeled as a graph. AB AC BC BA CA CB

AB
1 1
C A AC

Can also be captured as a graph !


BC
1 1
AB AC BA

BA
BC CA
1 1
CA CB CB
* *
Consider an arithmetic
expression like:
w
*
((A + B)*C + (D + E) * F) * G + G
It can also be written in a y v
parenthesis free form as
AB+C*DE+F*+G*
* *
C
x = A B + …… x
+ + u F
y = x C * …...
u = y D E + …. D E
v = y u F * …. A B
w=yv+G*
final result = w G *
$ %

The wagons coming from right may go straight


down to the left or may be shunted into the
yard. The symbols in the expression are to be
treated as wagons.
If it is a variable symbol :it goes straight
If is an operator then it is pushed in relation to
the symbol at the top of the stack.
$ %

The rules of operation for the stacked operators:


a. Opening parentheses are always pushed in.
b. Matching parentheses are cancelled – both
disbanded.
c. All operators till matching parentheses are
popped out .
d. An operator with a lower or equal precedence arrives, the one in the yard
is popped out and the incoming operator is pushed in to the yard.
e. An operator with higher precedence arrives is always pushed in.
f. Finally, all the operators are popped out.
-----|----( (A + B)*C + (D+E)* F)*G A---------|-----+B)*C + (D+E) *F) *G
(
(

AB-------|------)* C + (D+E)*F)*G
AB+-----|-----*C+(D+E)*F)*G +
( (
(

AB+C*---|----(D+E)*F)*G
AB+C-----|-----+(D+E)*F)*G
+
* (
(

Proceeding this way we finally get A B + C * D E + F * + G *


* %
In the examples shown we captured the following
types of entities:
Customer queue, clerk array, graph capturing conflicts,
stack that resulted in parentheses free expression.
The following types of operations were captured:
Enjoin a queue, behead a queue, push an operator, pop and
operator, color a node etc.
Questions to ponder:
- are we interested in the height of a customer
- is behead a queue a primitive operation
- does it matter if a graph is captured as a matrix
* % &
Answers:
- model what is needed
- synthesize the operations from primitive
operations
- abstraction does not need the details
of implementation only we need to
understand the behaviour.
Queue: Enjoin, behead, is_empty etc.
Stack: Push, pop, is_empty, is_full etc.
Graph: connect two vertices, remove an edge etc.
* % '
ADTs have the following characteristics:
Generalization: define synthesized behavior
Encapsulate: Hide implementation details

Typically following definitions are needed:


A constructor: to store
an access mechanism: to retrieve
a set of operations to work with variables.
The operations are specified with a pre-condition
and a post-condition.
%
Essentially a one dimensional array with the word-
size as the unit.
Note the address provides the direct access
An array is stored with an index for access
information which has an abstraction of being in a
sequence.
What if the indexed information is itself an address?
That is what a pointer in fact is – the content is an
address.
* %
Operations may be defined on indices, (or on array
elements). Let us consider operations on indices.
All integer operations are possible on indices. Two useful
index operations are “increment_index” or
“decrement_index” by one or by an integer expression.
Just as we can structure data by composing some
constituents we can define operations by combining the
basic primitive operations.
For instance, we can define a sort_by_name, or
sort_by_age on an array of triples with (name, age,
education). (We need a user defined comparator operator
which is composed from primitive data operations)
+ * %
Suppose we have an array of integers. How is it
represented?
A short_integer occupies one word. So, a sequence
of memory addresses can store an integer array.
A long_integer may occupy two words. An increment
of 1 in index actually jumps two address ahead.
In general, an array of triples will store these triples in
sequence. The triples separated by the number of
words a triple may require to store.
, # * %+
Advantage:
- Accessing data at a certain index, takes a fixed
amount of time.
- To modify indexed information also requires a
fixed amount of time - retrieve, modify, rewrite.
Disadvantage:
- To insert (or delete) data at an index requires
redefining the indices of all affected data – this
may require all the n – data items in an array.
1 ! 2
"
The answer lies in creating a template in which a part
of the data provides the information about the next
data item.

The data item Information about the next data item

This facilitates insertion as well as deletion without


having to move all of the data the around.
The second entry is essentially a pointer.
#

Why can not we have more than one pointer?

Of course we can – that gives us flexibility to


create arbitrarily structured data.
1 3
A brief recall of concepts

An example ADT: A Tree

Rationale for a good representation

An insertion sort using arrays

Sorting on a heap

The comparison
* 0$ #
Captures the behaviour

Abstraction and generalization

Encapsulation

Representation requires memory map


*0 4/ %0 5
Basic representation : a root, a left
subtree, a right subtree
Draw a figure of a binary tree using B
letters: BANGALORE
A N
Data structure: the node, left-
pointer, right-pointer G
operations: get_node(), A L O
put_node(), get_lst(), get_rst()
Also, there may be a traversal R E
operations defined on a binary tree
Traversals may be for search, or a
part of problem solving process
(like in optimization problems or AI
problems)
0 / %0
Pre-order traversal of the binary tree
(root, lst traversal, rst traversal)
results in traversing order B, A, G, R, B
E, A, N, L, O
In-order traversal of the binary tree A N
(lst traversal, root, rst traversal) G
results in the order R, G, E, A, A, B,
A L O
L, N, O
Post-order traversal of the binary tree R E
(lst, rst, not) results in the order R, E,
G, A, A, L, O, N, B
Clearly, the DS ought to support
these traversals by providing
appropriate pointer structure.
1 0 %
Definitions of the left and right sibling
required

Parent identification

Applications may require information - like


height of a node
* 6
/ %0 Symbol Code
Suppose we need to use a 000
symbols a, b, c, d, e for b 001
communication. c 010
d 011
e 100
We will need 3 bits to 0 1
encode each symbol 0 1
0 0
1 0 1
0
If all symbols occur with a b c d e
equal probability then this
is the best solution.
! " " #$ #%" $

Let the probability be as follows:


Symbol Probability
a 0.12
b 0.32
c 0.18
d 0.05
e 0.33

Can we now get a greater efficiency in


encoding?
! #
Sym Probability
Step-1: Sort the symbols in e 0.33
decreasing order of b 0.32
probability of occurrence
c 0.18
a 0.12
d 0.05
Step-2: Combine two least X (0.17)
probable codes
i) Replace this combination
by a new symbol say x
ii) Create a partial tree as
a(0.12) d(0.05)
shown
! &

Step-3: Inclusive Sym


e
Probability
0.33
of the new symbol b
c
0.32
0.18
create a new table x 0.17 (x is a or d)

X (0.17)

Step-4: repeat the


a(0.12) d(0.05)
steps 1 to 3.
! 0
The next set of codes are as follows:
Symbols | Probability
1
y | 0.35 (y is c or x)
e | 0.33 .65 0 1
.35
b | 0.32 0 1
0 1
.17
Symbols | Probability
e(.33) b(.32) c(.18) 0 1
z | 0.65 (z is e or b)
y | 0.35 (y is c or x)
a(.12) d(.05)
Giving us the final tree and code
e = 00, b = 01,c = 10,a = 110,d =111

Note we have only two symbols with code


having length = 3 !!
* * 7
77
7

0
1
Draw another encoding tree
0 1
e
e = 0, b = 10, c = 110, a = 1110, d 0 1
b
= 1111
0 1
c
Now which of the trees is optimal? a d
Compute the average code length
for both the cases Compute avg. code length
given the probability
It turns out that the Huffman
encoding is optimal.
& %' $" ( &" ) % * +

It is a prefix encoding

It yields unique resolution of encoded information

Note that every acceptable code (symbolic encoding)


should always unambiguously resolve - this is also a
requirement of parsing in compilers.
+ & #
&+ % ,$ $"- (
The algorithm should be correct
It should be economical on amount of work required
It should be economical on resources used
It should be clear and simple - no ambiguities
It should aim at optimal result whenever possible.
Algorithms are analyzed keeping the above in mind and
better ones are chosen.
Recall we had observed the representation can affect the
performance. We shall use two sort algorithms to show this.
&+ %.
Insertion sort algorithm - sorting an integer array

It sorts it in situ ie. in place

An example: Sort 6 3 4 7 8 2

The steps would be: 3 6 4 7 8 2

3 4 6 7 8 2

3 4 6 7 8 2

2 3 4 6 7 8
/ 0 ) 1
At every step scan the array and insert the
elements at their rightful place in the array sorted
up to that point in the array.

Note that we use a fixed amount of extra space.


2 & # &+ %
1. for j 2 to length[A]
2. do key A[J] /* Extra storage resource used "key" */
3. I j-1 /* Objective is to insert */
4. while i > 0 and A[i] > key
5. do A[i+1] A[i]
6. i i–1
7. done
8. A[i+1] key
) 3 2 &+ %1

Some assumptions and methodology


Associate a certain constant ei to denote
processing required for ith line.
Count the number of times each line gets
executed to determine cost of processing.
With every new definition (declaration) associate
a cost of space resource used.
& # .
Suppose the array was initially sorted
Each element is in its place, no element is moved.
Best case ....
line 1 c1: n times
line 2 c2: n-1 times
line 3 c3: n-1 times
line 4 c4: n-1 times
line 5 c5: 0 times
line 6 c6: 0 times
line 7 c7: n-1 times
c1*n + C2*(n-1) + C3(n-1) + C4*(n-1) + C7*(n-1)
In other words it would be linear in the size of the array.
& # .
Suppose the array was in inverse sorted order
Each element is moved by the maximum distance
Worst case ....

line 1 c1: n times


line 2 c2: n-1 times
line 3 c3: n-1 times
line 4 c4: j=2 to n(j) times = C4*((n+1)*n)/2 ---its n2 term
line 5 c5: j=2 to n(j) times = C5*((n+1)*n)/2 ---its n2 term
line 6 c6: j=2 to n(j) times = C6*((n+1)*n)/2 ---its n2 term
line 7 c7: n-1 times

In other words it would be square in the size of the array.


$( 4 %' 5 #
The space complexity
One additional space resource used for key
Two additional spaces used for control variables i and j

Overall space complexity is a constant.

The average case is more often with estimates like


worst case.
Processing complexity is square in the size of array.
2 % # 1
Consider a Heap: seen as a binary
tree - as full as it can be. First we
consider the indices only
So the indices can be computed 1
easily. 2 3
The left child of i has an index = 2*i
4
The right child of i has an index = 2*i
6 7
+1
Parent of index j is floor_of(j/2) 8 9 10 11
Note the leaf indices range from
A [floor_of (n/2) + 1] to n
2 ) '! ' #
Violates
In a heap the value at here
the parent node is >= 50
its child nodes.

55 29 50

A heap violating the Violates


heap property can be
39 43
heapified. May Satisfies
violate here
55

50 29
) ' #,6
Heapify(A, i)

!
" # $ %
7 0
Assured to heapify the first element

Given an array - not sorted we resort to


heapification of all the non-leaf elements

The final result is a heap


& *5 %' ,
Let us put an array of Violated 17
here
unsorted integers in a as 10 6
full a binary tree
structure. 19
1 2

10 3 12 5
Given array of integers is:
17, 10, 6, 19, 8, 1, 2, 10,
3, 12, 5
!
19
10

10 8
19 8

10 3
10 3
ok
violates
0 ) ' ) '
with n = 11 the number of leaf indices is (floor_of(n/2) + 1) to n ie. = 6
The leaf nodes are heaps by them selves
This means we need to heapify remaining
1 to floor_of(n/2) nodes to get a heap.

So to get a heap we need to execute


Build_heap() at non-heap nodes

Build_heap
1. heap_size[A] length(A)
2. for i floor_of(length(A) / 2) down_to 1
do heapify(A, i)
$ ) '
heapsort(A)
for i length(A) down_to 2
do exchange A[1] A[i]
heap_size(A) heap_size(A) - 1
heapify(A, 1)
) 3 2 &+ %1
heapify The number of exchanges depends upon
the height of the node
the height of nodes is bounded by log(n)
Build_heap Uses heapify about n/2 times
Heapsort Uses heapify n times
So overall costs = (n/2) log2(n) + log2(n)
<= 2n log2(n)
This expression is much smaller than n**2 for large
values of n
8
Data has context.
Data usually models some entity.
Data has to have a
representation.
Data representation affects
performance.
To solve a problem it is should be
formalizeable. So we do need a math
model.
Model of a point (x, y)
Model of a terrain (x, y, z)
Model of a switch (off, on); (0, 1)
Model of a person (name, age, gender,
marital-status)
* $
*

OUT IN

Services Sought :
- letters and stamp vending etc..
- registered letters, courier letters, parcels etc.
- money order, vikas patra, savings bank etc.
1 # *"
We have a data representation that is obtained
from some primitive data definitions:
Integers
Real
Binary
Character

And some mechanisms like struct to construct


data forms.
Entities of interest:
- point – a pair of real numbers
- polygon – a list of points
- the clerks pool – could be an array
- the customer pool – normally a queue
The operations of interest:
- projections on x or y axis, distance from origin
- a point is inside / outside a polygon
- insert / delete in an array, size of an array
- enqueue, dequeue, queue_empty, queue_full
Operations captured as functions described in C with an
appropriate memory map
* 0

An ADT is Data type with operations

Integer, queue, person or any other form of


data with operations on them would define
ADT.
0 * 09 :
Make an empty list List empty?
Get the first element Output list
Append two lists
Get the next element
Split a list into two
Insert an element
Delete an element

Abstraction is defined by the skeleton form used.


The data type determines the nature of operations.
The ADT (Abstract data type is defined by the abstraction
together with the data-type)
! 1 "
Each data artifact (user defined or otherwise)
should be seen to have state and operative
environment.
For instance, suppose we need a queue:
- We execute make_queue(), we get an
empty queue
Operation are defined with a pre-condition
(defined by state and operative environment)
and the operation leads to a post condition.
; $
, 7
make_queue(Q) A new empty queue Q
Queue_empty(Q) true / false
enque(Q, e) if Q not_full,
then Q Q+e else error
Queue_full(Q) true / false

dequeue(Q, e) If Q not_empty,
then Q Q - e else error

This is all it takes to capture the behavior of a queue.


* % $ %
ADTs have the following characteristics:
1. Generalization: define synthesized behavior

2. Encapsulate: Hide implementation details

Typically following definitions are needed:


1. A constructor: to store

2. an access mechanism: to retrieve

3. a set of operations to manipulate (or alter the state /


values ).
The manipulating operations are specified with a pre-
condition and a post-condition.
0 /
First define a data form – a basic skeleton.
Choose the primitive data type.
Define the rules of operations – specific to the
skeleton and the data type constituents.
- This defines the basic axiomatic structure for
the data in use.
This is what one gets from an ADT
Why move from ADTs to Object definitions?
Why C++?
1 % < "
What did we try to do – model real world objects.
So why not think in terms of Objects themselves?

Real-world has interacting objects in every scenario -


be it model of an organization or a CAD of system or
sciences or mathematics or social systems or window
object in a desk-top metaphor of operations…..
We basically have artifacts that can be recognized as
objects. So we need to think of data forms or data
artifacts that are objects – giving ourselves an
elevated level of abstraction.
1 = < "
An object clearly has a data-form. However, within this framework
it can also inherit. A human as an object inherits a lot from animal
kingdom
Lactating mammal
A manager inherits from definition of a person – name, age, gender.
Now we can work
with abstractions – generalizations and specializations and
create a hierarchy of object definitions to borrow behaviors
with instantiations – to get a specific object identity
In short an object has all the advantages of an ADT (in terms of
abstraction, encapsulation) to which inheritance is added.
Also an object has an identity
< # $# <
Hierarchy: Means of
transport
has a body
has chassis
has doors
a front door / rear door has a steering
has an engine mechanism
has a piston has a steering wheel
has length, diameter has diameter
has a cylinder
has a braking system
has length, diameter
has a brand name
<
My car is an instantiation of an object with a definite
identity from a class car. It has a brand name with an
engine with a certain number, and a chassis, a steering
and a braking mechanism.
So what’s a class:
Objects of same kind which would have a commonality in the mode of response.

In case we seek the brand name, steering diameter or


perform an operation – we perform it in exactly the same way
for each instance of car object.
Pure hierarchical inheritance leads to a tree.
Real-world objects inherit from multiple sources – that would lead
to a graph.
A student who is a part-time employee derives characteristics
from an object class student as well as employee.
A text-file derives its properties from the class file and text
object.
A window object derives its characteristics from “window” class
i.e. there is window manager which allows operations on
window.
Essentially, we can have a set of operations (specific to a class)
defined only once for a class and then use it for every object
instantiated from that class. Now that is code sharing.
<
Late 70s and early 80s : command based interpretation for interaction on
both main frames and time-shared system’s terminals.

Mouse as a pointing device: popular since 1989 (Perry and Volekar).

Xerox parc offered small-talk as a typeless OOP easy-to-use GUIs.

Apple’s computers “for the rest of us” – iconic interfaces for objects like
files. “drag and drop” for move and trash can for delete, menu bars with pull
down menus.

We now have dialog boxes, radio buttons, text selection etc. We design
objects that seem to communicate – messages or signals like in IPCs.

Object interfaces: a phenomenon of the 90s, with the advent of the


multimedia we have many more forms of object interfaces possible like
audio cues, visuals.
1 > % # "
A class has a definition.
A class may have private information – not accessible outside the
class. This may be shared amongst all object instances of that
class – a case of data-sharing. Data values define object states.
An interface for communication – available to other classes and
objects. A set of operations (often called methods).
A method invocation may require use of arguments. Invoking a
method requires:
A target object
The selector of the method
The arguments for the method (some times there may be a way to
use this as an implicit parameter.
0% 0 #
Constructors: usually new().
Destructors to delete objects
Modifiers to access the state of an object
Methods to update the state of an object.
We may have a subclass as a specialization of a
class and methods that over-ride the super-class
methods or may even have exclusive applicability to
the subclass.
The call paradigm usually employs dynamic binding
– which means that messages bind the method at
runtime
Overloading is primarily a mechanism to
derive more than one meaning from the same
symbol. For instance, + is often an
overloaded operator – used for integer / real
add operation. It may even be used for logical
OR or concatenation.
In OOP overloading can extend to objects
rather than being limited to base types.
, %
Essentially, polymorphism refers to assuming
different forms. Overloading operators is a kind of
polymorphism.
We earlier discussed list structure for integers or for
persons. By choosing an appropriate object form
(integer / person) we have the possibility of having a
list of integers or list of persons. The nature of the list
could depend upon the form of parameter. Such
polymorphism is called parametric polymorphism.
#
Early in the HLL development was a notion of
a sub-range type. This meant that values
could be constrained to be within some band.
OOPs may provide to maintain certain forms
of constraints. The idea is to incorporate a
run-time check . Also, this encourages “safe
programming” practice. Note that this makes
it easier to ensure pre and post conditions.
#
One can take a view that a class
is_a_kind_of_an_object.
With that view point we can have class generators !
In other words we can spawn classes with some
defined characteristics.
In smalltalk it is possible to
open a class browser,
select a class form
add this class to the classes available!
# ,!22

C++
Java
Smalltalk

For system design definitions:


Extensions of Petri-nets (Morphic- nets)
UML
? *
v2 A graph is a defined by
a pair of sets (V, E).
e1 e2 Edges
The pair (V, E) captures a
relationship.
v1 v3
If vertex vi (in V) is related to vj
(also in V), then there is an edge e
nodes e3
v4 (in E).
Vertices are shown as nodes and
e6 edges as lines ( that connect the
e4 vertex pair that have the defined
v6 relationship ).
v5

e5 An example graph with


6 nodes and 6 edges.
1 %6 ? "
Graphs model real life scenarios:
A computer communication NW / Electronic circuit
We can model rail or road network
Electrical power utility with its distribution
Graphs may be
Directed: edges define a specific vertex order like (v1, v2)
the edge connecting vertices v1 and v2 has
one way relationship (or meaning) in the
direction from v1 to v2.
Undirected: edges establish relationship both ways.
Weighted: The edges may have attached weights.
Simple: graphs having at most one edge between any
vertex pair.
$
Adjacent vertices: Suppose vertices v1 and v2 have an edge e
between them. The v1 and v2 are said to be adjacent. Also, v1
and v2 are said to be the two end vertices of the edge.
If e = (v1, v2) we shall refer to v1 as the first vertex and v2 as the
second vertex.
A Simple Path: A simple path defines a sequence of edges e1,
e2, ….ek with each edge identifying a distinct vertex (except
possibly the first and the last) such that for any two edges ei and
ei+1 in the sequence have the property that the second end vertex
of ei is same as the first end vertex of ei+1.
A path is closed if in the sequence above if the first and last
vertices are the same i.e. the second end vertex of ek is the first
end vertex of e1. Otherwise it is an open path.
Adjacency List Representation Edge Based Representation
v2
v1 v2 e1 ( v1 , v2 )
v2 v1 v3 e2 ( v2 , v3 )
e1 e2
v3 v2 e3 ( v5 , v7 )
v1 v3 v4 v5 v6 v7 e4 ( v4 , v5 )
e7 v5 v4 v7 v6 e5
v7 ( v5 , v6 )
v4
v6 v4 v5 e6 ( v4 , v6 )
e3 v7 v4 v5 e7
e6 ( v4 , v7 )
e4
v6 Adjacency Matrix
v5 v1 v2 v3 v4 v5 v6 v7
e5 v1 0 1 0 0 0 0 0
v2 1 0 1 0 0 0 0
v3 0 1 0 0 0 0 0
v4 0 0 0 0 1 1 1
v5 0 0 0 1 0 1 1
v6 0 0 0 1 1 0 0
v7 0 0 0 1 1 0 0
+ 1 ?
B
• Weights are number associated
6
A 5 C
with edges.

1
3 2 • The weight reflects cost
associated with the edge. It may,
D E
for instance, be the cost of
4 traversal along the edge.
6 1 6 0 %

First( ): returns the index of the first vertex


adjacent to . Returns null if there is no
vertex adjacent to .
Next( ): returns the index after index for
the vertices adjacent to . Returns null if is
the last index for vertices adjacent to .
Vertex( ) returns the vertex with index
amongst the vertices adjacent to .
0 , ,

Suppose we are given a directed, non-


negative edge weighted graph G.
Let us identify one vertex as a source vertex.
We may identify the destination node as well
The problem is to compute the minimum cost
path from the source to the destination
0 / *
Uses the principle of “Optimality”
The optimal path “p” from the source “s” to
the destination “d” via an intermediate node
“i” embeds the optimal path from source “s” to
d
node “i” as well.
s
Has long been the basis of i

“Dynamic Programming”
+
Essentially we can begin with path starting at
“s” and terminating at “s” with 0 cost
Now iteratively add one more node at a time
such that the path to the new node emanates
from the nodes selected so far.
d

s
i
0 6

We shall use the adjacency matrix as the


data structure for now
We shall use an array D[i] which will store the
cost of the path from the source to the node
“i”
For the example graph we initially
D[i] = [0,8, , ,35]
* ? 1 #
v1 v2 v3 v4 v5
v2

8 80 v1 0 8 35

v3 0 80 20
v1 v2
20
v3 0
35 45
v4 45 0 14

v5 v4 v5 8 0
14

8
<
= >* The basic idea is initially,
Procedure Dijkstra source vertex defines S
begin Initialize the distance vector
1. S = {1}; /* Initially we have source*/ D by locating edge weights
from C
2. for i = 2 to n do {
3. D[i] = C[1, i]; /* Initialize D */
Select the vertex adds the
4. for i = 1 to n - 1 do weights minimally to S and
begin add it to Set, adding one
5. choose a vertex w in V-S such that vertex at a time.
D(w) is minimum;
6. add w to S; With the added vertex
7. for each vertex x in V-S do
8. D[x] = min(D[x], D[w] + C[w, x])
modify the cost
end
end Till all vertices are covered.
}
* 1 =0
v2
v1
80
8
v3
35 20

45
v5 4
v4

8
No S V–S Which “w” Array D

0 {v1} {v1, v2, v4, v5} [v2, v3, v4, v5] [0, 8, , , 35]

2 3 4 5
1 {v1} {v2, v3, v4, v5} [0, 8, 88, 28, 35]
[8, , , 35]
3 4 5
2 {v1, v2} {v3, v4, v5} [0, 8, 88, 28, 32]
[88, 28, 35]
3 5
3 {v1, v2, v4} {v3, v5} [0, 8, 40, 28, 32]
[88, 32]
3
4 {v1, v2, v4, v5} {v4} [0, 8, 40, 28, 32]
[ 40 ]
v1 v1 v1 v1 v1
- - v2 v4 v5
- v2 v2 v2 v2
v1 v1 v1 v1 v1
1 % <
= >* 1 ="
This is a “greedy” algorithm – to that extent it looks
for local minimum. However, it does find the global
minimum. So we need to analyze why it works.
There are two cases we need to consider:

source

source

In this case x should have been in S in the first


place hence contradiction
0 * % <
= >*
Procedure Dijkstra
First we assume n vertices and e
begin
edges in the graph. If e << n then
1. S = {1}; /* Initially we have
use adjacency list representation
source*/
else use adjacency matrix rep. Let
us assume that for now.
2. for i = 2 to n do
Lines 1 to 3 take O(n) time
3. D[i] = C[1, i]; /* Initialize D */
Lines 4 and 5 also take O(n) time.
4. for i = 1 to n - 1 do Line 6 could take up to O(n.log(n))
begin time.
5. choose a vertex w in V-S such that Let us look at lines 7 and 8 first –
D(w) is minimum; it takes O(n) time and gets
6. add w to S; executed n-1 times – that gives us
7. for each vertex x in V-S do O(n2) complexity.
8. D[x] = min(D[x], D[w] + C[w, x]) We conclude that the algorithm
end takes O(n2) time.
end
0 ?

We had earlier seen traversals on trees –


pre-order, in-order and post-order.
The depth-first traversal in a graph is a
generalization of pre-order method of tree
traversal.
The DFS traversal is so called because the
idea is to go forwards deeper.
*
v2 v4 v1 v2 v3 v4

v1 v6 v5

v1 v3
v1 v6 v4

v6 v5 v3

v5 v1 v2

v6
Both these traversals are depth first traversals
(depends upon how the data was organized)
0 *
Assume the graph has 1..n nodes and the traversal begins at node 1

for i = 1 to n do procedure dfs(i: vertex)


mark[i] = unvisited; var w: vertex;
begin
1. mark(i) = visited;
for i = 1 to n do 2. for each vertex i on L(i) do
if mark[i] = unvisited 3. if mark[i] = unvisited
then dfs(i). 4. then dfs(i)
end
* ? 0
v2 v4

v1 v3

Suppose we begin at v6

v6 v4
v5
v6 v5 v1 v2 v3
v6
* % ,
procedure dfs(i: vertex) We will assume adjacency list
var w: vertex; representation.
begin Note that the way we mark the
1. mark(i) = visited; nodes we never revisit a node.
2. for each vertex i on L(i) do The total time spent in lines 2 – 3
going down the adjacency list at
3. if mark[i] = unvisited each node will sum up to the
4. then dfs(i) number of edges i.e O(e).
end Therefore the complexity of the
DFS graph traversal is O(e)
within a constant factor.
0 0

A graph is said to be a connected if is there


exist a path between any pair of nodes.
A tree is a connected sub-graph that has no
circuits .
A spanning tree T of a given graph G is a tree
that has all the vertices.
A DFS traversal of a graph in general yields a
spanning forest.
0 0 7
v2 v4

Note the darkened


v1 v3
edges form a
spanning tree.

v5
In general we may
get a spanning
v6
0 *

Recap: A graph, simple graph, weighted graph,


path, closed path, tree, spanning tree.

Our objective today is to find a minimum weight


spanning tree for a given weighted graph.
1 ?

e2 A e34
B 8 C e1 BE e6 FG
e9 D 6 e2 BA
e1 3 e10 5 e5
e7 EF
G
E e8
8
e3 CA e8 FD
3
e7 e6 7 e4 CE e9 BD
F
e4
8 e5 CG e10 CD
0
e2 A
B 8 C
e9 D 6
e10 5 e5
E G
e8
3 8
e7 e6 7
F
8
e4
* ,

Principle of Inclusion and Exclusion


Essentially partition the solution space into
two parts
One that includes the entity (has a certain prop.)
One that excludes the entity (not have a certain
prop.)
Problem
We have to generate combination of 4 from
the given 6 distinct objects.
Given Objects - A, B, C, D, E, F
Combination forms - A C E D (for eg)

Enumerate all possible combinations so formed


Partition the solution space – Enumerated set
of all the combinations of S into 2 parts

SA SA
This is the one which This is the one which
always includes A always excludes A
Eg: A.E.C.D Eg: B.D.C.E
$" (
5 5 6
SA = C SA = C S= C
3 4 4

SA SA’ =
(ie) these are mutually exclusive disjoint subsets. This is
important from program development viewpoint as it
eliminates redundant checks of duplicates.
The 2 problems of listing SA and SA’ are each more
simple than just listing S. This solves the given
problem.
The principle can be used recursively.
0
Construct a row to hold enumeration sets as
given below.

A B C
All those that All those that All those that All those that
include A exclude A exclude A, B exclude A, B,
and include B and include C C. This is a
null set.
4 " 8 "# % +

A NULL SET

B C D

All those that All those that All those that


include include include
A AND B A AND C A AND D
and exclude B and exclude B,C
The construction of the 2nd row gives us the basic
Data Structure which will be used to enumerate

With enumerations
as shown below
A B C
B C D ABCD
ABCE
C D E
3 ABCF
1 2
D E F
A B C With enumerations
as shown below

B C D
ABDE

C D 5
E ABDF
4

D E F
A B C With enumerations
as shown below

B C D
ABEF

C D E
6

D E F
A B C With enumerations
as shown below
ACDE
B C D
ACDF

C D E ACEF

D E F
A B C With enumerations
as shown below
ADEF
B C D

C D E

D E F
A B C With enumerations
as shown below
BCDE
B C D
BCDF

C D E BCEF
BDEF
D E F CDEF
We used a (n-m+1) x m matrix
Each iteration had one distinct output
Worst case is when m = n/2
The method can be modified for permutations as
well
We will use similar approaches to evaluate all
spanning trees of a connected simple graph
It is a special case of divide and conquer approach
of algorithm decision
They may lead to a recursive algorithm
* @# -

Arranging a league tournament


Will get a recursive solution
If we define the terminal case with 2 players
A and B… there can be only one league
match.
Test case with 4 players… consider as
groups of 2.
A C
B D

A B C D
B A D C

C D A B
D C B A
Eight Players
A B C D F G H
B A D C E H G
C D A B H E F
D C B A G F E
E F G H B C D
F E H G A D C
G H E F D A B
H G F E C B A
0
Recap: A graph, simple graph, connected
simple graph, closed path, tree, spanning
tree
Defn:
A cut set: Given a simple connected graph, a
cut set is a minimal set of edges whose
removal results in partitioning G into 2
connected graphs G1 and G2 with no
vertices in common.
Edges AC A
BD B C
D
EF
E G
CE

Partition the vertices in 2 groups


F
{ A, B, E }
{ C, D, F,G }

No Subset of these has this property as deleting any edge alone will leave G
connected
Set of edges {CG, FG} forms a cutset as,
vertex {G} and vertices {A,B,C,D,E,F} form
two partitions
A
B C
D

E G

F
Theorem:
Suppose we are given a simple connected graph G, and a cut
set C G, then at least one edge in C will always be a
member of every spanning tree T of G.
Proof:
Cutset partitions a graph G with vertices V into 2 sets of
vertices V1 and V2
Suppose we have any two vertices Vi V1 and Vj V2
Then every path in G that connects Vi and Vj must use at least
one edge from the cut set.
Therefore at least one edge of the cutset must be used in a
path between Vi and Vj in every spanning tree T or G
Theorem:
Suppose we are given a simple connected graph G, with
every vertex having a degree greater than or equal to 2.
Now suppose we delete an edge e in G to get graph G`.
G` has the following properties
G` is a simple connected graph A
Suppose we have a spanningB C
tree T` of G`, then T` is a D
G
spanning tree of G also. E
Follows from the fact that every
node has a degree greater F

than 2
Proof:
Followed from the fact that every node has a
degree greater than 2.
It’s a spanning tree of G because all the vertices
of G are retained in G`.
Additionally we notice that it’s a tree that
excludes edge e.
Theorem
Suppose we are given a simple connected graph
G, let e be an edge in G. Suppose on coalescing
e we get a new graph G``. Let T`` be a spanning
tree of G``. Then T`` U E is a spanning tree of
G.
Proof
T`` is a spanning tree and has the coalesced
vertex of e. So by splitting the coalesced vertex
and adding e we get the spanning tree of G.
A A
B C B C
D

E G E DF G

A A
B C B C
D
G E DF G
E

F
What we have learnt so far

At least one edge in any cutset is a member of


every spanning tree of G
By opening out an edge from a vertex of degree 2
or more we get a tree T` which is also a spanning
tree of G
By coalescing an edge e in G, we can get a tree
T`` such that T``U e is a spanning tree of G
The algorithm to enumerate all spanning
trees of a simple connected graph G.
Coalesce all edges in the pendant vertices
in G. A pendant vertex is a vertex of degree 1
Now we can enumerate trees of this
coalesced graph G` and all the trees can
be enumerated.
Partition the set of trees to be enumerated
as those that contain edges and those that
do not.
An Example A
Consider the given graph e1 e4
e1 = AB e4 = AD e6
e3
e2 = BC e5 = CD B D E
e3 = AC e6 = DE e2 e5
C

Note that the edge e6 will be A


always present in every e1 e4
spanning tree of G
e3
So basically we need to B D
enumerate the trees of graph e2 e5
with 4 nodes and 5 edges and C
add e6 to identify the trees of G
Step 2
Consider the cutset e1, e3, e4
Now we can partition all the A
spanning trees into the following e1 e4
partition e3
Always include e1 (AB) B D
e2 e5
Always exclude e1, but include e3(AC) C
Always exclude e1(AB) and exclude
e3(AC) but include e4(AD)
Also construct the row e1 e3 e4
Step 3
Trees that always include e1(AB)
Generate the graph by coalescing
e1(AB)
We get the graph as shown here.
Trees of this graph added with e1 will
always give the trees we are looking
for.
Step 4
Find a cutset in this graph
Suppose we select the cutset e1, e1 (AD,CD)
Trees into the subsets that always include
(e1 = AB) shall be
Always include e1=AB and e4 = AD
Always include e1=AB and exclude e4=AD but include
e5=CD
And the rows would be e1 e3 e4
e4 e5
On coalescing e4 = AD we get e2 e e5
3

And the cut set is, e2 e3 e5


BC AC CD
Adding the row we get
e1 e3 e4
e4 e5
e2 e3 e5
Given the trees
e1 e4 e2
A A A
e1 e4 e3 e1 e4 e1 e4 e1 e4
B D B e D B D
e1 e4 e5 e2
3 e5
C C C

As the first 3 trees that always


have e1 and e4
The next step is to find trees with
e1 = AB included
e4 = AD excluded

AB
Coalesce e5

e2 e Cutset e2,e3
3
e5
C D
Clearly the rows would yield the trees
(e1,e5,e2) and (e1,e5,e3)
A e1 e3 e4
e1 A
B
B e4 e5
D D
e2 C e2 e3
e5
C
&+ % + ! +%
An example of greedy heuristics

Vertex coloring problem:


Given an simple connected graph R
G = (V,E)
Obtain a coloring assignment
on the vertices such that adjacent
vertices have distinct colors.

An Example
We have used 4 colors
$' % +
Given a simple graph G, if we obtain a coloring assignment on the
vertices such that
The adjacent vertices have distinct colours
The number of colours used is minimum

Steps:
First we will design an algorithm using a heuristic.
We will observe that the heuristic uses a greedy strategy for
colouring
Also, we will show that the result is not optimal.
& *5 %' ,
1
6
Consider G, with 6 vertices.
Colour the vertices in sequence 2
i.e., color vertex 1,
then vertex 2, 3

and so on… till 6


5 4
The heuristics also states that
Reuse one of the colors used in coloring
Use a new color only if it is absolutely necessary
4 + +
6 1
Steps in coloring
2
V1 R
V2 must use new color G 3
V3 Re-use R
V4 Re-use G (cannot use R)
V5 must use a new color B
V6 must use new color Y 5 4
4 %' 5 # 3 #& + %1
n vertices coloured in sequence
n steps required.
At each stage we need to look up no more than either d+1 or n
colours, where, d maximum degree of a node in G.

2 %9 ' :
The optimal vertex coloring of a graph G requires no more than
(d+1) colors.
So the number of color comparison at each step is dir. prop. to n.
Therefore the complexity of the algorithm will be O(n2)
2 4 3 '
The no of colors used = 4. 1
This is not optimal. 6
Let us view this graph differently
in the context of the following 2
theorem
Even cycles can be colored
using 2 colors and this is 3
optimum coloring.
Odd cycles require 3 colors
minimally
5 4
A complete graph G, with n
vertices requires n colors.
& + + '
6 1
Vertices 1,4,5,6 from an even cycle.
So assign 1 4 5 6
R G R G 2
Assign V 2 B
Assign V 3 B 3

Time transition
We used 3 colors 5
There is an odd cycle in G 4
So this is the minimal coloring.
%' $" (
n!
2n
n2
There is no known algorithm to
color the vertices of a n
given simple graph optimally. n
log n
Algorithm complexity
Let us try to see the growth
rates for complexity
O(log(n)) < O( n ) < O(n) < O(2n) < O(n2) < O(n!)
; <&= 4$=$6
R Essentially if you can
B make non deterministic
1 choices at decision
points we could color the
2 graph in polynomial time.
When the choice at
3 decision points can be
made deterministically
R Then we get an algorithm
G with polynomial
complexity.
1 (B,G) R
6 1
6
2 2 (G,B)
3
3
5 4 5 G4
STEP 1
(COLORS IN SEQ R,G,B,Y)
(B,G) 6 R 1 (B,G) 6 R
1
2 (G,B)
2(G,B)

3 3G
R5 G4 R5 G4
6 1

5 4
6 1

5 4
6 1

5 4
6 1

5 4
R
B6 1

2 G

3 G

R5 G4
What makes a problem NP

NP stands for Non 1


6
deterministically polynomial i.e. if we
have non deterministic choices to
be made then there exists atleast 2
one choice (amongst many) which
will yield the result in polynomial
time. 3

For the Given Graph


(If we could make the correct
choice for vertices 2 and 6 we will 5 4
be able to color the graph G in
polynomial time.)
2 4 <!
Suppose we have a choice
tree as shown.

Suppose at every choice


point in the algorithm we have a
non deterministic choice to make
(in other words we have no
method of evaluating same function
which ensures an optimal choice).

In this situation we have only one method find an optimal solution…..


examine every possible combination of choices and after the enumeration of
all – we can find which is optimal.
However, the above also means that there exists an optimal choice at
every choice point. If we could make the choice by a polynomial time
evaluation then we have a polynomial time algorithm.
) > + 1
Suppose we know some ( but not all ) of the properties of
an optimal solution. In that case, we can use these properties
(whenever possible at choice point) to make the choice.
Now if we have no way of completely characterizing optimal
solution at every choice point, then we have a case of a
problem which is in class NP with no local or global optimal
solution strategy.
If we have some, but not a complete characterization of the
optimal solution and we use this to eliminate the choices, then
we get a locally optimal solution strategy. Usually greedy
algorithms use this strategy.
However this does not preclude the possibility of existence
of a set of nondeterministic choices that will give us the optimal
solution in polynomial time.
' " % <!
Greedy algorithms are designed by a partial knowledge or incomplete
knowledge or by using some properties (theorems) for some patterns
available in problem solving.

Example 1: We used the odd cycle property to find a lower bound on


the graph and if the recent matches the lower bound then we know we
have an optimal solution.
Example 2: Dijkstra’s Shortest Path Algorithm. Using the local property
but in the context of positive weighted graphs, the algorithm yields the
global shortest path.

If we do not have complete knowledge to make a choice at a choice


point and there exists a non deterministic points that yields a polynomial
time solution then that is an NP problem.
BLUE TOUR
1,5,4,3,2,6,7,9,8,10

1 2

10 5

3
4
8

6
9

7
RED TOUR
1,2,3,4,5,6,7,8,9,10

1 2

10 5

3
4
8

6
9

7
RED TOUR BLUE TOUR
1,2,3,4,5,6,7,8,9,10 1,5,4,3,2,6,7,9,8,10

1 2

10 5

3
4
8

6
9

7
Suppose we consider a 10 node graph as shown. Also suppose we
associate a cost >0 with every edge of the graph.

A TSP tour in a tour of the graph where in we find a closed path


(without repeating any vertex enroute except for the first and the last
vertex).

We have shown 2 tours -- red tour and blue tour. Clearly, there could
be many more tours.

The TSP problem is a classical problem where we have to make a


choice at every node --each node is a choice point to find the minimum
cost tour.
0 ? % %
Given a multiple connected/weighted graph G, we
proceed as follows :
For every nonadjacent vertex pair, associate an edge with infinite
weight (or weight which is > the largest weighted edge) now we
can solve the problem for a complete graph.
Select a vertex as the initial vertex.
Visit the vertex with the least edge weight from the last selected
vertex.
Repeat the above step till all vertices are visit without repeating a
previously visited vertex.
Now visit the first vertex to obtain the TSP tour. If there does not
exist an edge backtrack.
4 8 %
If there is a path from the last visited vertex to the visited vertex then the
algorithm works in polynomial time.
N visits to be made.
Each visit may require upto n comparisons
So O(n) complexity.

) '% 1
There are a total of n! TSP tours We can enumerate all of these and find
the minimum.
Suppose a 10 city solution could be found in 1 second .
How long will it take a 20 city solution.(from 20! Possibility)
It would be found in 20000 years.
1 % "
The usual is to use some iterative methods
Hill Climbing algorithm
Simulated annealing
Genetic algorithm
Neural computing
Any other soft computing method
7 '' %' +1
1. Find an initial solution
2. Design a mechanism to generate a move set
Move-Set - Basically the set of possible alternatives that
can be explored in the neighborhood of the initial
solution.
3. Select the best solution from the move set and
repeat from step 2 till no further improvements are
possible.
#
Usually a very simple (polynomial time)
algorithm is used to generate initial solution.
The ingenuity lies in generating Move-Set
Hill climbing pre-supposes a function whose
gradient can be exploited
Generic Algorithms use mutation etc.
The number of iterations is usually delimited
by the input size.
0 ,
The cost is proportional to the distance traveled

(0,0) (7,6)
6
(7,6) (6,3)
4 (7,6)
(6,3) (8,1)
(8,1) (1,5) (1,5)
5
(1,5) (3,2) 2 (6,3) 3
(3,2) (0,0) (3,2) (8,1)
1 (0,0)
Compute the cost of the tour 1
194
GENERATING A MOVE-SET FOR TSP

CONSIDER THAT THE COST IS DISTANCE


CITIES ARE LOCATED AT
(0,0); (1,5); (3,2); (6,3); (8,1) and (7,6)
(x,y) Co-ordinates
6
(7,6)
TOUR1 (1,5)
4
(0,0) (7,6) 5
(7,6) (6,3) (6,3)
(6,3) (8,1) 2
(8,1) (1,5) (3,2) (8,1)
(1,5) (3,2) 3
(3,2) (0,0)

COMPUTE THE COST OF TOUR 1 194


Choose 4 cities (1,5),(7,6),(0,0),(8,1)
In the present tour there are paths (1,5),
(8,1) and (0,0),(7,6)
From green connections switch to the blue
alternative leaving the rest of the path intact
New cost 146
Next Choose (1,5);(3,2);(0,0);(8,1)
2) * *?&@ !=* 4$<2 <A* B

(1,5); (3,2) (1,5)


(3,2)
(0,0); (8,1) (0,0) (8,1)

In this tour now choose the green connections (1,5);(0,0) and


(3,2);(8,1)
The new tour we get is
(0,0) (1,5)
COST
(1,5) (7,6) 120
(7,6) (6,3)
(6,3) (8,1)
(8,1) (3,2) (0,0)
(3,2) (0,0)
The cost is 120

Note that we choose the combinations of two edges


M
There is an C Selection possible to choose the better
3
combination of 3 edge exchange

Proportional to n3
But not optimal
@ A=&2* &<<*&= <3
LET X = INITIAL CONFIGURATION
1. LET E = EVAL(X) /* Some measure of goodness*/
2. LET i = Random move from the moveset
3. LET Ei = EVAL (MOVE(X,I)) /* what-if check*/
4. IF E<Ei THEN /* Move and update evaluation
X=MOVE(X,i) if the move is better */
E=Ei
ELSE
With some probability accept
/*HELPS THE MOVE EVEN IF THE MOVE IS WORSETO
KICK OUT OF LOCAL MINIMA AND MAXIMA */
X=MOVE(X,i)
E=Ei
6. REPEAT FROM STEP-3 TILL SATISFIED WITH THE SOLUTION
The notion of simulated annealing manifests
itself in the computation of the probability of
acceptance of a poorer alternative.

-( E-Ei ) / Tj )
e Tj IS THE TEMPERATURE

Cools as the iterations progress


Hence the probability comes close to zero
OTHER ANALOGY BASED ALGORITHMS
EVOLUTION / GENETIC PROGRAMMING / G.A.
Ants behaviour… ANN (Artificial Neural Network) …and so on.

A Brief On Genetic Algorithms


The basic strategy lies in arriving at a suitable string type
representation cross over results in a new population
giving an opportunity to explore new solutions. Only healthy
population carried forwards.

1101 1011 1001 0100


Initial Population
of Solns.
11 01 10 11 10 01 01 00
Break it into
Sections
11 11 01 10 10 01 01 00
New population
CROSS OVER RESULTS IN A NEW POPULATION

GIVING AN OPPORTUNITY TO EXPLORE NEW SOLUTIONS.

ONLY HEALTHY POPULATION CARRIED FORWARDS.

G.A ISSUES

• Bit stream representation is critical


• Evaluation function critical
• Generally population is ranked
• Bit selection may be randomized to achieve
larger explorations
THE POLYNOMIAL SPACE

One major advantage of operating in the polynomial space


is that it is a closed space.

1. Two algorithms A1 A2 in series will have a complexity


determined by the larger degree polynomial.

2. Two nested algorithms A1 A2 will result in a new polynomial


algorithm which has a complexity determined by the
product of the two complexities.

In other words we continue to have a polynomial complexity

MAPPING NP PROBLEMS
Suppose there are two NP complete problems P! and P2. It is
possible to show that P1 can be mapped into an equivalent
problem of type P2 in polynomial time.
; < <3 &== @ &? @ &= < *!*< *<2
*2 $; *62 4* 9@ &!! <3 ! ! :
Given a graph G we have a definition for all the edges (x,y) E
with (x V,y V) means x, y are adjacent vertices.
Now look upon x and y as boolean variables
Minterm x,y represents an edge.
If we now consider all edges in the graphs we get a function
= x,y x v, y v x,y v
A set of vertices are independent if they are non-adjacent
A maximal set cannot be fattened -i.e adding any other vertex
makes it lose independent set properly
Taking ’ gives maxterms
Each maxterm represents maximal independent set
–ab+bc+bd+ce+de+ef+eg+fg
Now computing ’ yields maximal independent sets
as
(acdg) (acdf) (bg) (bf) (ae)

c
f
b
g
e
a
d
!6$0=*@ < 4=& <!
Examples
Graph coloring
Traveling Sales Person problem
Bin packing problem
Channel routing problem
Placement problem
Satisfiability problem
Optimization is the key in the decision problem
Invariably we are seeking an answer as yes or no.
In optimization problems we seek to minimize (maximize) some
quantity
It is often possible to pose an optimization problem as a decision
problem and vice versa
Can a graph be colored does a tour exist < Some number
THE ‘ORDER OF ‘ NOTATION

Let f and g be function from N R


N: {0,1,2,…} R: Set of Real Nos.
Def: O(g) is the set of function f1 also from N R such that for some real c > 0
and some non negative integer no f(n) <= c g(n) for all n > = no
A function like 2n2 + 1.3 n is O(n2)
AN INTERESTING OBSERVATION
TIME SIZE OF LARGEST PROBLEM INSTANCE
COMPLEXITY SOLVABLE IN & HOUR C=COMPUTING
POWER
C 100 C 1000 C
n n1 100 n1 1000 n1

n2 n2 10n2 31.6 n2

2n n3 n3+6.64 n3+9.97
NOTE MAKING A TECHNOLOGY GROW BY A MULTIPLE DOES NOT HELP!!
FOR NP CLASS PROBLEM.
) &2 & <! ) &6 !6$0=*@

Can be transformed to a Known NP Complete Problem in


polynomial time.
The Implementation is that if every even problem in class NP
complete is solved in ptime
Then every problem in class NP shall have a polynomial time
solution.
In that case P=NP
A problem Q is NP Hard if every problem in NP is reducible to
Q-In Polynomial time
NP Hard In NP and at-least as hard as NP complete.
0 =%

You might also like