Graphs: Shortest Paths, Job Scheduling Problem, Huffman Code
Graphs: Shortest Paths, Job Scheduling Problem, Huffman Code
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
Shortest
Works on both directed and undirected graphs.
However, all edges must have nonnegative
Paths weights.
Approach: Greedy
Shortest do dist[v]
infinity
//set all other distances to
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:
Paths 2.Finding & Updating each adjacent vertex's weight in min heap
is O(log(V)) + O(1) or O(log(V)).
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
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.
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 :
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]
P5
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
1 2 3 4 5
P5 P4 P2
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)
}
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
= 102
Total bits required in fixed Total bits required in
length encoding variable length encoding