You are on page 1of 25

C.K.

Pithawala college of
Engineering and technology

Enrollment No Name
Subject : Analysis and Design
140090107005 Harshil Devani
of Algorithms
140090107006 Dhameliya Smit
(2150703)
140090107008 Gajera Kishan
140090107026 Panseriya Vikalp
Topic : Graphs: Shortest paths,
140090107053 Shroff Vishv
Job Scheduling Problem,
Huffman code

Guided by:- Prof. Yogesh Kapuriya & Prof.


Single-Source Shortest Path Problem

Shortest The problem of finding shortest


paths from a source vertex v to all other
vertices in the graph.
Paths
Dijkstra's algorithm is a solution to the
single-source shortest path problem in graph
theory.

Shortest

Works on both directed and undirected graphs.
However, all edges must have nonnegative

Paths weights.

Approach: Greedy

Input: Weighted graph G={E,V} and source


vertex v V, such that all edge weights are
Dijkstras nonnegative
Algorithm
Output: Lengths of shortest paths (or the shortest
paths themselves) from a given source vertex
v V to all other vertices
Pseudocode
dist[s] 0 //distance to source vertex is zero
for all v V{s}

Shortest do dist[v]
infinity
//set all other distances to

S //the set of visited vertices is initially empty


Paths QV
while Q
//the queue initially contains all vertices
//while the queue is not empty
do u MinDistance(Q,dist)
SS{u}
for all v neighbors[u]
do if dist[v] > dist[u] + w(u, v) //if new shortest
Dijkstras path found
Algorithm then d[v] d[u] + w(u, v)
return dist
Algorithm
Algorithm Single_Short_Path (s , weight , Dist , n) {
// weight Weight of each edges as a Matrix.

Shortest // Dist Distance (Shortest path) from source vertex s to


any other .
// S - it stores all the visited vertices of graph. It's Boolean

Paths type array.


// n Total number of vertices in graph.
S[s] 1 ; //set vertex s to true in array S and put it in S
Dist[p] 0;
for i 2 to n do {
S[i] 0 ;
Dijkstras Dist weight[s,i] ;
Algorithm }
Algorithm
for val 2 to n-2 do {
/*Choose q from the vertices that are not visited and

Shortest with minimum distance.*/


Dist[q] = min { Dist[i] } ;
S[q] 1; //select vertex for shortest path
Paths /*Update Distance value of the other nodes*/
for(all node r adjacent to q with S[r] = 0) do
if( Dist[r] > Dist[q] + weight[p,q] ) then
Dist[r] Dist[q] + Dist[p,q] ;
}
Dijkstras }
Algorithm
An Example for Directed Graph
(non-negative weighted)

3+2
OUT
3
? =5

8
OU 5
?

8
T 2
a c

3 8 13 >=
8+5
0 13
?

8
OU s 7 t
6 1
T
4 9+4 =
4 13
4
?
8 5
b f 1?9

8
0
4OUT
< 3+7
10
OUT>
3+6 =10
4+5
Time Analysis:

1.Each vertex can be connected to (V-1) vertices, hence the

Shortest number of adjacent edges to each vertex is V-1.


Let us say E represents V-1 edges connected to each vertex.

Paths 2.Finding & Updating each adjacent vertex's weight in min heap
is O(log(V)) + O(1) or O(log(V)).

3.Hence, the time complexity for updating all adjacent vertices


of a vertex is E*(logV) or E*logV.

4.Hence time complexity for all V vertices is V * (E*logV)


i.e O(VElogV).
Negative Weights

Graphs can have negative weights

Shortest Shortest positive-weight path is a net gain


Path may include individual losses

Paths Problem:
Negative weight cycles
Allow arbitrarily-low path costs
Dijkstras
Algorithm Solution:
Detect presence of negative-weight cycles
Complication of Negative
Weights

Shortest 2 24

Paths s
12
2 2
12

t
Dijkstras
5 1
Algorithm
15 -10 11
0

-4 26
Negative cycle
Directed cycle whose sum of edge weights is
negative.

Shortest -6
-4

Paths Observations
7

If negative cycle C on path from s to t, then shortest


path can be made arbitrarily negative by spinning
around cycle
There exists a shortest s-t path that is simple.

s t

C
cost(C) < 0
Job scheduling is anoptimization
problemin which ideal jobs are assigned to
resources at particular times.
Job Given an array of jobs where every job has a
deadline and associated profit if the job is

Scheduling
finished before the deadline.
every job takes single unit of time.
If job starts before or at deadline, profit is
obtained, otherwise no profit.
Consider all possible schedule and compute
the minimum total time in the system.
the minimum possible deadline for any job is
1.

To maximize total profit.


Pseudocode
for i 1 to n do
k min( dmax, DEADLINE(i) )
/* where DEADLINE(i) denotes deadline of ith job

Job while k >= 1 do


if timeslot[k] is EMPTY then

Scheduling timeslot[k] job(i)


break
kk-1

Free Free

Pseudocode
x x x x x x x= used

k k k k k
All in Same set
Algorithm Job_Seq ( D , J , n )
{
// D[i] denotes i th deadline where , 1 i n
// J[i] denotes i th Job
//Initially
D[0] 0;
Job J[0] 0;
J[1] 1;

Scheduling
count 1;
for i 2 to n do
t count;
while ( ( D[J[t]] > D[i] ) AND ( D[J[t]]!=t ) ) do
t t 1;
if( ( D[J[t]] D[i] ) AND ( D[J[t]]!=t ) ) then
{
//insertion of i th feasible sequence into J array
Algorithm for s count to ( t+1 ) step 1 do
J[ s+1 ] J[ s ] ;
J[ t+1 ] I;
count count +1 ;
}
return count;
}
An Example

JOB P1 P2 P3 P4 P5
DEADLIN 1 4 3 3 1

Job E
PROFIT 3 5 18 20 38

Scheduling
Solution :

Step 1 : Arrange profits in descending order.

JOB P5 P4 P3 P2 P1
DEADLIN 1 3 3 4 1
E
PROFIT 38 20 18 5 3
Step 2 : Create an array J[ ] which stores the job , Initially
J[ ] will be.
1 2 3 4 5

0 0 0 0 0

J[5]

Step 3 : Add i th job in array J[ ] at index denoted by its deadline Di .


First job is P5 in array J[ ] at 1st index , because its deadline is 1
1 2 3 4 5

P5

Step 4 : Next job is P4 . Insert it at index 3.

1 2 3 4 5

P5 P4
Step 5 : Next job is P3, but its deadline is 3 and it is not
possible to insert P3 at 3. Hence just discard it.
1 2 3 4 5

P5 P4

Step 6 : Insert P2 at 4th index as deadline of P2 is 4.

1 2 3 4 5

P5 P4 P2

Step 7 : Just discard P1 whose deadline is 1.

Step 8 : Thus we now obtain the job sequence as 5-4-2 with


profit of 63.
P5( 38 ) + P4( 20 ) + P2( 5 ) = 63
So , Max profit we can get from given data
is 63 (MAX) .
This algorithm is basically a coding technique
for encoding data. Such an encoded data is
used in data compression techniques.
Each Here data is inputted as a sequence of
Huffman characters.
Table of frequency of occurrence of each character

code in the data is built.


After from it Huffmans tree is constructed.
it is further used for encoding each character , so
that binary encoding is obtained for given data.
It is specific method of representing each symbol .
This method produces a code in such manner that
no code word is prefix of some other code word.
Such code are called Prefix code or Prefix Free
codes.
Algorithm Huffman ( c )
{
n |c| //Numbers of leaves in tree
Q c //Priority queue used to construct binary heap

Huffman for i 1 to n 1 do
{
temp get_node()
code left[temp] Get_min(Q)
Right[temp] Get_min(Q)
a left[temp]
b right[temp]
F[temp] f[a] + f[b]
insert(Q,temp)
}
Algorithm return Get_min(Q)
}

The Huffmans algorithm requires O(log n) time .


Huffmans fixed length
encoding
Step 1 :

B: A:
F:3 E:7 C:9
10 15

Step 2 :
1 1
0 9
0 1

B: A:
F:3 E:7 C:9
10 15
Step 3 :
2
0 9
1
1 1
0 9
0 1 0 1
B: A:
F:3 E:7 C:9
10 15

4
0 4 1 Symbol Code
2 1 word
9 1 5 A 111
0 1
B 011
1 1 1
0 9 5 C 010
0 0 1 1
1 E 001
B: A: F 000
F:3 E:7 C:9
10 15
Huffmans Variable length
encoding
Step 1 :

B: A:
F:3 E:7 C:9
10 15

Step 2 :
1
0
0 1

B: A:
F:3 E:7 C:9
10 15
Step 3 : 1
9
0 1

C:9 1
0
0 1

B: A:
F:3 E:7
10 15
Step 4 :
2
9
0 1

B: 1
10 9
0 1

C:9 1
0
0 1

F:3 E:7 A:
15
Step 5 : 4
4
1 Symbol Code
0
word
A: 2 A 0
15 9
0 1 B 10
C 110
B: 1
E 1110
10 9
0 1 F 1111

C:9 1
0
0 1

F:3 E:7
Final Analysis
:-
Total bits required in fixed length encoding
= ( 15 x 3 ) + ( 10 x 3 ) + ( 9 x 3 ) + ( 7 x 3 ) +
(3x3)
Huffman A B C E F

code = 132

Total bits required in variable length encoding


= ( 15 x 1 ) + ( 10 x 2 ) + ( 9 x 3 ) + ( 7 x 4 ) +
(3x4)
A B C E F

= 102
Total bits required in fixed Total bits required in
length encoding variable length encoding

You might also like