Professional Documents
Culture Documents
Bhatt
Retired Prof. (IIT/D, IIIT-B)
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.
C A
AB
1 1
C A AC
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 *
$ %
AB-------|------)* C + (D+E)*F)*G
AB+-----|-----*C+(D+E)*F)*G +
( (
(
AB+C*---|----(D+E)*F)*G
AB+C-----|-----+(D+E)*F)*G
+
* (
(
Sorting on a heap
The comparison
* 0$ #
Captures the behaviour
Encapsulation
Parent identification
X (0.17)
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
An example: Sort 6 3 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.
55 29 50
50 29
) ' #,6
Heapify(A, i)
!
" # $ %
7 0
Assured to heapify the first element
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.
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
dequeue(Q, e) If Q not_empty,
then Q Q - e else error
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.
C++
Java
Smalltalk
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 %
“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
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
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
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
v5
In general we may
get a spanning
v6
0 *
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
* ,
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
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
* @# -
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
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
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
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
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
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.
We have shown 2 tours -- red tour and blue tour. Clearly, there could
be many more tours.
) '% 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
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
G.A ISSUES
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
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=*@