Adsa-Ii-1 (R20) Unit 4.
Adsa-Ii-1 (R20) Unit 4.
DIVIDE-AND-CONQUER
GENERAL METHOD:
Given a function to compute on n inputs the divide-and-conquer
strategy suggests,
Divide : Splitting the inputs into k distinct subsets, 1< k ≤n ,
yielding k
sub problems.
Conquer : These Sub-problems must be solved, and then
Combine : a method must be found to combine sub-solutions into a
solution of the whole.
Note : If the sub-problems are still relatively large, then the
divide-and-conquer strategy can possibly be reapplied.
1 Algorithm DAndC(P)
2 {
3 If small(P) then return S(P);
4 else
5 {
6 divide P into smaller instances P 1,P2,………,Pk, k>=1;
7 Apply DAndC to each of these subproblems;
8 return Combine(DAndC(P1),DAndC(P2),…… DAndC(Pk));
9 }
10 }
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
T(n)
{ = T(1)
aT(n/b)+f(n) n>1
n=1
Example:
Consider the case in which a = 2 and b = 2. Let T(1) = 2 and f(n)
= n.
We have
1
T(n) = 2T(n/2) + n
= 2[2T(n/4) + n/2] + n
= 4T(n/4) + 2n 2
= 4[2T(n/8) + n/4] + 2n
= 8T(n/8) + 3n 3
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
= 2n+ n log2n
* as in the rule of Big-Oh, if f(n) is a polynomial of degree k, then f(n) is
O(nk), ie., drop lower-order terms and constant factors.
= 2 n + n log2n
= O(n logn).
Analysis of QuickSort
Best Case: Each (recursive) Partition call will split the array in halves. We
would get the familiar recurrence for the running time:
T(n) = 2T(n/2) + O(n) = O(n log n):
Worst Case: Partition always causes an unbalanced split (all elements going to
one of the subarrays). The recurrence we get in this case is
T(n) = T(n -1) + O(n);
which solves to T(n) = O(n2).
BINARY SEARCH :
A binary search algorithm is a technique for finding a particular value in a
sorted list. The binary search consists of the following steps:
Let ai, 1 < i < n be a list of elements that are sorted in non decreasing
order.
Search a sorted array by repeatedly dividing the search interval in half.
Begin with an interval covering the whole array.
If the value of the search key is less than the item in the middle of the
interval, narrow the interval to the lower half. Otherwise narrow it to the
upper half.
Repeatedly check until the value is found or the interval is empty.
The most straight farward implementation is recursion.
Algorithm :
Algorithm BSearch(a,l,u,x)
{
if(l = u) then
{
if(x = a[u]) then return u;
else return 0;
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
else
{ // reduce P into a smaller subproblems.
mid := (l+u)/2;
if(x < a[mid]) then
return BSearch(a,l,mid-1,x);
else if (x>a[mid]) then
return BSearch(a,mid+1,u,x);
else return mid;
}
}
Example :
Given a list of elements 1, 4, 5, 9, 13, 35, 37 41. now we need to
search for an element x = 35 in the given list. To do that,
1 2 3 4 5 6 7 8
1 4 5 9 13 35 37 41
L mid U
Find the middle element by the step
mid = (l+u)/2 = (1+8)/2 = 4. now the value with index 4 is the middle
element.
x > a[mid] [:. ( 35 > 9 ) ]
1 2 3 4 5 6 7 8
l = mid + 1;
1 4 5 9 13 35 37 41
L mid U
Check the relation between the search element x = 35 and the middle element
which we calculated above. The search element is greater than the middle
element. So, the search element may exist in the right sub-list to the middle
element.
Now the lower bound of the sub-list is 5 which will be pointed with mid+1 and
the upper bound is u.
Then solve the right sub-list as in the above recursively.
In this sub-list the middle element is
mid = (l + u) / 2 = (5 + 8) / 2 = 6.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
X = a[mid] [:. (35 = 35)]
1 2 3 4 5 6 7 8
1 4 5 9 13 35 37 41
L mid U
The element located in the mid position is 35. now check the search element
x=35 with the middle element. In this case both are equal, means the element
found at the position mid, and return the position 6. So, the element found at
index position 6 in the given list.
MERGE SORT:
we assume throughout that the elements are to be sorted in
non-decreasing order. Given a sequence of n elements (also called keys) a[1],
…,a[n].
Divide the list into two sets a[1],…,a[n/2] and a[n/2] +1],….,a[n].
Each set is recursively sorted, and then,
Sorted sequences are merged to produce a single sorted sequence of n
elements (final result).
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
12 MergeSort(low,mid);
13 MergeSort(mid + 1, high);
14 // Combine the solutions.
15 Merge(low, mid, high );
16 }
17 }
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
In many optimization problems, there are two types of solutions: feasible
solutions, and optimal solutions.
The greedy method suggests that one can devise an algorithm that works
in stages, considering one input at a time. At each stage, a decision is made
regarding whether a particular input is in an optimal solution.
Greedy algorithms are easy to invent, easy to implement and most of the
time quite efficient. However, there are many problems that cannot be solved
correctly by the greedy approach.
Advantage :
The principle advantage of greedy algorithm is that they are usually
straightforward, easy to understand and easy to code.
Disadvantage :
Their main disadvantage is that for many problems there is no greedy
algorithm.
“The one with maximum benefit from multiple choices is selected” is the
basic idea of greedy method. A greedy method arrives at a solution by making a
sequence of choices, each of which simply looks the best at the moment.
Control abstraction :
The function Select selects an input from a[] and removes it. The
selected inputs value is assigned to x.
Feasible is a Boolean-valued function that determines whether x can be
included into the solution vector.
The function Union combines x with the solution and updates the
objective function.
The function Greedy describes the essential way that a greedy algorithm
will look, once a particular problem is chosen and the functions Select,
Feasible, and Union are properly implemented.
Algorithm :
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Algorithm Greedy(a,n)
{
solution := 0; // initialize the solution
for i:=1 to n do
{
x := Select(a);
if Feasible(solution, x) then
solution := Union(solution, x);
}
return solution;
}
KNAPSACK PROBLEM :
We now try to apply the greedy method to solve the knapsack problem.
We are given n objects and a knapsack or bag.
Object i has a weight wi and
The knapsack has a capacity m.
If a function xi, 0< xi < 1, of object i is placed into the knapsack, then
a profit pixi is earned.
Maximize ∑ p ix i
1
1<i<n
Subjected to ∑ w ix i < m 2
1<i<n
And 0 < xi < 1, 1 < i < n
3
The profits and weights are positive numbers.
A feasible solution is any set (x1,……,xn) satisfying the above eq. 2
& eq.3.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
An optimal solution is a feasible solution for which eq. 1 is
maximized.
Example :
Consider the following instance of the knapsack problem: n = 3, m =
20, (p1,p2,p3) = (25,24,15), and (w1,w2,w3) = (18,15,10).
Four feasible solutions are :
(x1,x2,x3) ∑ w ix i ∑ p ix i
(1/2,1/3,1/4
1 16.5 24.25
)
2 (1,2/15,0) 20 28.2
3 (0,2/3,1) 20 31
4 (0,1,1/2) 20 31.5
Of these four feasible solutions, solution 4 yields the maximum profit. This
solution is optimal for the given problem instance.
Algorithm :
Algorithm greedyKnapsack(m,n)
{ // p[1…n] and w[1….n] contain the profits and weights
respectively of the n objects ordered such that p[i]/w[i] >
p[i+1]/w[i+1]
for i :=1 to n do x[i] := 0.0; // initialize x.
U := m;
for i := 1 to n do
{
if(w[i] > U) then break;
x[i] := 1.0; U := U-w[i];
}
if(i < n) then x[i] := U / w[i];
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
JOB SEQUENCING WITH DEADLINES :
We are given a set of n jobs.
Associated with job i is an integer deadline di > 0 and a profit pi > 0.
For any job i the profit pi is earned iff the job is completed by its
deadline.
To complete a job, one has to process the job on a machine for one
unit of time.
Only one machine is available for processing jobs.
A feasible solution for this problem is a subset Jof jobs such that
each job in this subset can be completed by its deadline.
The value of a feasible solution J is “a sum of the profits of the jobs
in J, or ∑ pi (iєj)”.
An optimal solution is “a feasible solution with maximum value”.
Example :
Let n = 4, (p1,p2,p3,p4) = (100,10,15,27) and (d1,d2,d3,d4) =
(2,1,2,1). The feasible solutions and their values are :
Processin
Feasible
g value
solution
sequence
1 (1,2) 2,1 110
2 (1,3) 1,3 or 3,1 115
3 (1,4) 4,1 127
4 (2,3) 2,3 25
5 (3,4) 4,3 42
6 (1) 1 100
7 (2) 2 10
8 (3) 3 15
9 (4) 4 27
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
by job 1. Thus the processing of job 4 begins at time zero and that of job 1 is
completed at time 2.
Step 2 : add next ith job to the solution set list if ith job can be
completed by its deadline, di. Assign ith job to the kth vacant time slot in
the list. The list[k] = 0, then kth time slot is empty. If kth time slot is not
vacant, then search the preceding slot,(k-1), and so on, meeting its
deadline constraint.
The greedy method described above always obtains an optimal solution to the
job sequencing problem.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Job( Pi D Profi Operation List[1..4] initialized
i) i t to 0
1 10 2 100 Assign job 1 to 0 1 0 0
0 slot 2
4 27 1 127 Assign job 4 to
4 1 0 0
slot 1
3 15 2 127 Reject job 3,
because it 4 1 0 0
violates its
deadline
2 10 1 127 Reject job 2,
because it 4 1 0 0
violates its
deadline
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Minimum Spanning Trees
Spanning trees
A spanning tree of a graph is just a subgraph that contains all the vertices and
is a tree. A graph may have many spanning trees; for instance the complete
graph on four vertices
This problem can be solved by many different algorithms. Two of them include :
“Prims” Algorithm
“Kruskal’s” Algorithm.
PRIM’S ALGORITHM
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At any stage, we can make out that we have a set of nodes that have
already been included in the tree, the remainder of the nodes have not.
The Prim’s algorithm then finds a new node to be included in the tree by
choosing the edge ( vi , vj ) has the minimum weight (cost) among all
edges, where vi is the in the tree and vj is yet to be added to the tree.
The algorithm starts by selecting a node arbitrarily, and then in each stage, we
include an edge (by adding an associated node) in the tree.
We can represent a weighted graph by an adjacency matrix to store the
set of edges.
An entry(i,j) in an adjacency matrix contains information on the edge that
goes from the vertex i to the vertex j.
Each matrix entry constrains the weight of the corresponding edge.
A specially chosen weight value is used to indicate edges that the missing
from the graph.
A large value “INF” is used to denoted an edge that is missing from the
graph(“INF” may be a constant equal to 99999). The adjacency matrix for
the weighted undirected graph is shown as follows:
1
28 1 2 3 4 5 6 7
10
(a) 1 (b) 1 ∞
1 28 ∞ (c)∞ ∞ 1 10 ∞
2
14 2 28 ∞ 16 ∞ ∞ ∞ 14
16
10 6 7 10
3 ∞ 16 ∞ 12 10 ∞ ∞ ∞
2
24 3
4 ∞ ∞2 12 ∞ 22 ∞ 2
18
25 18 5 ∞ ∞ ∞ 22 ∞ 25 24
6 7 12 6
6
7
10 ∞ ∞ ∞
6
25
7
∞ ∞
5 3
22 4 25
7 ∞ 14 3 ∞ 18
25 24 ∞ 3
∞
5 5 5
4 4 22 4
10 10 10
2 2 2
16 14 16
6 7 6 7 6 7
3 3 3
SRET 25 12
25
12
25
12
5 5 5
22 4 22 4 22 4
Advanced Data structures and Algorithms (ADSA) Unit - III
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Algorithm Prims( a[][] , int n)
{
size := n;
for i := 0 to size step 1 do
for j := 0 to size step 1 do
adjMatrix[i][j] := a[i][j];
selected[0] := 1;
nEdges := 1;
while(nEdges < size)
{
min := 99999;
for i:=0 to size step 1 do
if(selected[i] = 1 )
for j=0 to size step 1 do
if(selected[j] = 0)
if(min>adjMatrix[i][j])
{
min := adjMatrix[i][j];
row := i; col := j;
}
selected[col] := 1;
nEdges ++;
sum := sum + adjMatrix[row][col];
//selected edge is row - col
}
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Kruskal's algorithm
Kruskals algorithm constructs the minimum spanning tree of a graph
by adding edges to the spanning tree one-by-one.
At all points during its execution, the set of edges selected by prim’s
algorithm forms exactly one tree.
On the other hand, The set of edges selected by kruskal’s algorithm
forms a forest of trees.
Kruskals algorithm is conceptually quite simple.
Kruskal's algorithm:
sort the edges of G in increasing order by length
keep a subgraph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S
Note that, whenever you add an edge (u,v), it's always the smallest
connecting the part of S reachable from u with the rest of G, so by the
lemma it must be part of the MST.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Consider the given graph G: 9
0
1
12
5
4
5 2
6
8 6
8 4
4
7 3
0 0
1 1
4
5 2 5 2
6 6
4 4
3 3
(c) Edge 2-3 is added (d) Edge 1-2 is added
0 0
1 1
4 4 5
5 2 5 2
6 6
4 4
4 4
If we include 3edge 3-6 in the tree T, then a cycle
3 (1-2-3-6-1) will be
formed. So, reject the edge 3-6. Next edge 3-4 with weight 7 is added.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
(e) Edge 3-4 is added (f) Edge 4-5 is added
0 0
1 1
4 5 4 5
5 2 5 2
6 6
4 8 4
4 4
7 3 7 3
Addition of edge 4-6 also creates a cycle. Finally, edge 0-1 is added to
obtain the minimum spanning tree.
0 9
1
5
4
5 2
6
8 4
4
7 3
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
SINGLE SOURCE SHORTEST PATH:
A minimum spanning tree gives no indication about the shortest path between two vertices. In real-
life, we are required to find the shortest path between two cities.
For example, one would be interested in finding most economical route between any two
cities in a given railway network.
We are given a directed graph G in which every edge has a weight, and our problem is to find a path
from one vertex v to another vertex w such that the sum of the weights on the path is as minimal as
possible. We shall call such a path a shortest path, even though the weights may represent costs,
time, or some other quantity other than distance.
10
1 6
2 20
9
2 6 5
10 2 12
3 4
4
It turns out that it is just easy to solve the more general problem of starting at one node called the
source, and finding the shortest path to every other node, instead of to just one destination node. For
simplicity, we take the source to be node 1, and our problem then consists of finding the shortest
path from node 1 to every other node in the graph.
The solution we will show for the shortest-path problem is called Dijkstra’s Algorithm.
Dijkstra’s Algorithm
This algorithm works by maintaining a set S of vertices whose shortest distance from the source is
already known.
Initially, S contains only the source vertex.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At each step, we added to S a remaining vertex v whose distance from the source is as short
as possible.
Then we can find a shortest path from the source to v that passes only through vertices in S.
at each step of the algorithm, we use an array dist to record the length of the shortest path to
each vertex.
Once S includes all vertices, dist will hold the shortest distance from the source to each
vertex.
Algorithm ShortestPaths(v,cost,dist,n)
{
for i:=1 to n do
{ // Initialize S.
S[i] := false; dist[i] := cost[v,i];
}
S[v] := true; dist[v] := 0.0;
for num = 2 to n-1 do
{
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u] := true; // put u in S
for (each w adjacent to u with S[w] = false) do
//update distances
if(dist[w]>dist[u]+cost(u,w)) then
dist[w] := dist[u] + cost[u,w];
}
}
4
From the above adjacency matrix of a given graph Initially, S = {1},d[2] = 2, d[3] = ∞, d[4] = 6, d[5]
= 20, d[6] = 10.
10
1 6 d=10
2 20
iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010
2 6 5 d=20
d=2
Initial S = {1}
3 4 d=6
d=∞
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010
2 6 5 d=20
d=2
10
S = {1,2}
3 4 d=6
d=12
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810
2 6 5 d=18
d=2
10 12
S = {1,2,4}
3 4 d=6
4
d=10
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3}
3 4 d=6
4
d=10
10
1 6 d=10
2 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
9 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6}
3 4 d=6
4
d=10
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
10 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
1 6 d=10
2 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210(5){1,2,4,3,6,5}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6,5}
3 4 d=6
4
d=10
At last the d[i] contains the shortest path from source vertex 1 to the vertex i.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
REFERENCES
http://www.codeguru.com/cpp/cpp/algorithms/sort/article.php/c5109
high do a[k]:= b[k];
31 }
Example :
1 2 3 4 5 6 7 8 9
75 40 10 90 50 95 55 15 65
1 2 3 4 5 6 7 8 9
75 40 10 90 50 95 55 15 65
6 7 8 9
1 2 3 4 5
95 55 15 65
90 75 40 10 50
1 2 3 4 5 6 7 8 9
75 40 10 90 50 95 55 15 65
1 2 4 5
6 7 8 9
75 40 50 90
55 95 15 65
1 2
40 75
6 7 8 9
SRET 15 55 65 95
Advanced Data structures and Algorithms (ADSA) Unit - III
1 2 3
10 40 75
1 2 3 4 5
10 40 50 75 90
1 2 3 4 5 6 7 8 9
10 15 40 50 55 65 75 90 95
{
a n=1, a is a constant
T(n)=
2T(n/2)+cn n>1, c is a constant
:. T(n) =O(nlogn).
QUICKSORT:
In quick sort, the division into two sub arrays is made so that
the sorted subarrays do not need to be merged later. This is accomplished by
rearranging the elements in a[1 : n] such that a[i] ≤ a[j] for all i between 1 and
m and all j between m + 1 and n for some m, 1≤m≤n. Thus , the elements in
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
a[1 : m] and a[m+1 : n] can be independently sorted. No merge is needed.
Algorithm interchange(a,i,j)
{ // Exchange a[i] with a[j].
temp:= a[i];
a[i] := a[j]; a[j] :=temp;
}
10 7 15 12 4 9 13 3
Quick Sort :
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
10 7 15 12 4 9 13 3
pivot p q
Move the pointer p towards right until it reaches the element which is
bigger than the pivot element and q towards left until it reaches the element
which is smaller than the pivot element.
10 7 15 12 4 9 13 3
pivot p q
10 7 3 12 4 9 13 15
pivot p q
Again exchange the p and q values. Then the list as follows:
10 7 3 9 4 12 13 15
pivot q p
Here we find that q crosses p. “when ever p/q crosses q/p” or
“even if the process stops at the element which is pointed by both p & q”
then perform the exchange operation of q and pivot element.
Now the pivot element is in its original position. That means, after
performing the exchange operation the elements appearing to the left of
pivot element are smaller than the pivot element and the elements
appearing to the right of pivot element are greater than the pivot element
.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
4 7 3 9 10 12 13 15
Based on the position of pivot element we divide the list into two
halves as above.
Apply the same Quick sort technique to each of the sub-lists
recursively. Finally we get all the elements in the sorted order.
The average case analysis (albeit difficult to evaluate) also comes out to be O(Nlog2N).
C(i,j) = ∑ A(i,k)B(k,j)
1≤k≤n
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
for all i and j between 1 and n. To compute C(i,j) using this formula, we need n
multiplications. As the matrix C has n 2 elements, the time for the resulting
matrix multiplication algorithm, which we refer to as the conventional is O(n 3).
[ ][ ][ ]
C11 C12 A11 A12 B11 B12
= X
C21 C22 A21 A22 B21 B22
If the matrices A and B are not of type 2 n x 2n , we fill the missing rows and
columns with zeros.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
algorithm is given by the recurrence
T(n) { b
= 8T(n/2) + cn 2
n≤2
n>2 Where b and c
are constants.
C11 = P+S-T+V
C12 = R+T
C21 = Q+S
C22 = P+R-Q+U
The resulting recurrence relation for T(n) is
b n≤2
Where a and b
{ T(n) = 7T(n/2) + an2
are constants.
n>2
[ ][ ]
A11 B11
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
A12 B12
2 4 2 7
AB = A21 A22 B21 B22
5 6 8 2
The given matrices are small enough to perform the matrix multiplication.
So, we can directly compute the multiplication using Strassen’s multiplication
algorithm as follows:
P = (2+6) X(2+2) = 32
Q = (5+6) X 2 = 22
R = 2X(7–2) = 10
S = 6X(8–2) = 36
T = (2+4)X2 = 12
U = (5–2)X(2+7) = 27
V = (4–6)X(8+2) = -20
[ ][ ] [ ]
5
Another Example,
6 8 2
=
58 47
[ ][ ]
1 2 3 4 1 4 2 7
0 6 0 3 3 1 3 5
AB = 4 1 1 2 2 0 1 3
0 3 5 0 1 4 5 1
[ ][ ][ ][ ]
1
0
2
6
3
0
4
3
1
3
4
1
2
3
7
[ ][ ][ ][ ]
4
0
1
3
1
5
2
0
2
1
0
4
1
5
3
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Substitute all the A’s and B’s in the strassen’s Matrix multiplication algorithm as in the previous
example.
The above algorithm gives the strassen’s matrix multiplication after dividing matrices into
sub-matrices and recursively multiply sub-matrices.
We perform seven n/2 and n/2 matrix multiplications and eighteen n/2 and n/2 matrix
additions. The matrix additions take O(n 2) time. If the matrix multiplications are done
recursively, then the running time satisfies
T(n) = 7T(n/2) + O(n2)
The solution of this recursion is T(n) = O(nlog27) = O(n2.81)
REFERENCES
http://www.codeguru.com/cpp/cpp/algorithms/sort/article.php/c5109
GREEDY METHOD:
Advantage :
The principle advantage of greedy algorithm is that they are usually
straightforward, easy to understand and easy to code.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Disadvantage :
Their main disadvantage is that for many problems there is no greedy
algorithm.
“The one with maximum benefit from multiple choices is selected” is the
basic idea of greedy method. A greedy method arrives at a solution by making a
sequence of choices, each of which simply looks the best at the moment.
Control abstraction :
The function Select selects an input from a[] and removes it. The
selected inputs value is assigned to x.
Feasible is a Boolean-valued function that determines whether x can be
included into the solution vector.
The function Union combines x with the solution and updates the
objective function.
The function Greedy describes the essential way that a greedy algorithm
will look, once a particular problem is chosen and the functions Select,
Feasible, and Union are properly implemented.
Algorithm :
Algorithm Greedy(a,n)
{
solution := 0; // initialize the solution
for i:=1 to n do
{
x := Select(a);
if Feasible(solution, x) then
solution := Union(solution, x);
}
return solution;
}
KNAPSACK PROBLEM :
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
We now try to apply the greedy method to solve the knapsack problem.
We are given n objects and a knapsack or bag.
Object i has a weight wi and
The knapsack has a capacity m.
If a function xi, 0< xi < 1, of object i is placed into the knapsack, then
a profit pixi is earned.
Maximize ∑ p ix i
1
1<i<n
Subjected to ∑ w ix i < m 2
1<i<n
And 0 < xi < 1, 1 < i < n
3
The profits and weights are positive numbers.
A feasible solution is any set (x1,……,xn) satisfying the above eq. 2
& eq.3.
An optimal solution is a feasible solution for which eq. 1 is
maximized.
Example :
Consider the following instance of the knapsack problem: n = 3, m =
20, (p1,p2,p3) = (25,24,15), and (w1,w2,w3) = (18,15,10).
Four feasible solutions are :
(x1,x2,x3) ∑ w ix i ∑ p ix i
(1/2,1/3,1/4
1 16.5 24.25
)
2 (1,2/15,0) 20 28.2
3 (0,2/3,1) 20 31
4 (0,1,1/2) 20 31.5
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Of these four feasible solutions, solution 4 yields the maximum profit. This
solution is optimal for the given problem instance.
Algorithm :
Algorithm greedyKnapsack(m,n)
{ // p[1…n] and w[1….n] contain the profits and weights
respectively of the n objects ordered such that p[i]/w[i] >
p[i+1]/w[i+1]
for i :=1 to n do x[i] := 0.0; // initialize x.
U := m;
for i := 1 to n do
{
if(w[i] > U) then break;
x[i] := 1.0; U := U-w[i];
}
if(i < n) then x[i] := U / w[i];
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
JOB SEQUENCING WITH DEADLINES :
We are given a set of n jobs.
Associated with job i is an integer deadline di > 0 and a profit pi > 0.
For any job i the profit pi is earned iff the job is completed by its
deadline.
To complete a job, one has to process the job on a machine for one
unit of time.
Only one machine is available for processing jobs.
A feasible solution for this problem is a subset Jof jobs such that
each job in this subset can be completed by its deadline.
The value of a feasible solution J is “a sum of the profits of the jobs
in J, or ∑ pi (iєj)”.
An optimal solution is “a feasible solution with maximum value”.
Example :
Let n = 4, (p1,p2,p3,p4) = (100,10,15,27) and (d1,d2,d3,d4) =
(2,1,2,1). The feasible solutions and their values are :
Processin
Feasible
g value
solution
sequence
1 (1,2) 2,1 110
2 (1,3) 1,3 or 3,1 115
3 (1,4) 4,1 127
4 (2,3) 2,3 25
5 (3,4) 4,3 42
6 (1) 1 100
7 (2) 2 10
8 (3) 3 15
9 (4) 4 27
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
by job 1. Thus the processing of job 4 begins at time zero and that of job 1 is
completed at time 2.
Step 2 : add next ith job to the solution set list if ith job can be
completed by its deadline, di. Assign ith job to the kth vacant time slot in
the list. The list[k] = 0, then kth time slot is empty. If kth time slot is not
vacant, then search the preceding slot,(k-1), and so on, meeting its
deadline constraint.
The greedy method described above always obtains an optimal solution to the
job sequencing problem.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Job( Pi D Profi Operation List[1..4] initialized
i) i t to 0
1 10 2 100 Assign job 1 to 0 1 0 0
0 slot 2
4 27 1 127 Assign job 4 to
4 1 0 0
slot 1
3 15 2 127 Reject job 3,
because it 4 1 0 0
violates its
deadline
2 10 1 127 Reject job 2,
because it 4 1 0 0
violates its
deadline
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Minimum Spanning Trees
Spanning trees
A spanning tree of a graph is just a subgraph that contains all the vertices and
is a tree. A graph may have many spanning trees; for instance the complete
graph on four vertices
This problem can be solved by many different algorithms. Two of them include :
“Prims” Algorithm
“Kruskal’s” Algorithm.
PRIM’S ALGORITHM
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At any stage, we can make out that we have a set of nodes that have
already been included in the tree, the remainder of the nodes have not.
The Prim’s algorithm then finds a new node to be included in the tree by
choosing the edge ( vi , vj ) has the minimum weight (cost) among all
edges, where vi is the in the tree and vj is yet to be added to the tree.
The algorithm starts by selecting a node arbitrarily, and then in each stage, we
include an edge (by adding an associated node) in the tree.
We can represent a weighted graph by an adjacency matrix to store the
set of edges.
An entry(i,j) in an adjacency matrix contains information on the edge that
goes from the vertex i to the vertex j.
Each matrix entry constrains the weight of the corresponding edge.
A specially chosen weight value is used to indicate edges that the missing
from the graph.
A large value “INF” is used to denoted an edge that is missing from the
graph(“INF” may be a constant equal to 99999). The adjacency matrix for
the weighted undirected graph is shown as follows:
1
28 1 2 3 4 5 6 7
10
(a) 1 (b) 1 ∞
1 28 ∞ (c)∞ ∞ 1 10 ∞
2
14 2 28 ∞ 16 ∞ ∞ ∞ 14
16
10 6 7 10
3 ∞ 16 ∞ 12 10 ∞ ∞ ∞
2
24 3
4 ∞ ∞2 12 ∞ 22 ∞ 2
18
25 18 5 ∞ ∞ ∞ 22 ∞ 25 24
6 7 12 6
6
7
10 ∞ ∞ ∞
6
25
7
∞ ∞
5 3
22 4 25
7 ∞ 14 3 ∞ 18
25 24 ∞ 3
∞
5 5 5
4 4 22 4
10 10 10
2 2 2
16 14 16
6 7 6 7 6 7
3 3 3
SRET 25 12
25
12
25
12
5 5 5
22 4 22 4 22 4
Advanced Data structures and Algorithms (ADSA) Unit - III
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Algorithm Prims( a[][] , int n)
{
size := n;
for i := 0 to size step 1 do
for j := 0 to size step 1 do
adjMatrix[i][j] := a[i][j];
selected[0] := 1;
nEdges := 1;
while(nEdges < size)
{
min := 99999;
for i:=0 to size step 1 do
if(selected[i] = 1 )
for j=0 to size step 1 do
if(selected[j] = 0)
if(min>adjMatrix[i][j])
{
min := adjMatrix[i][j];
row := i; col := j;
}
selected[col] := 1;
nEdges ++;
sum := sum + adjMatrix[row][col];
//selected edge is row - col
}
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Kruskal's algorithm
Kruskals algorithm constructs the minimum spanning tree of a graph
by adding edges to the spanning tree one-by-one.
At all points during its execution, the set of edges selected by prim’s
algorithm forms exactly one tree.
On the other hand, The set of edges selected by kruskal’s algorithm
forms a forest of trees.
Kruskals algorithm is conceptually quite simple.
Kruskal's algorithm:
sort the edges of G in increasing order by length
keep a subgraph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S
Note that, whenever you add an edge (u,v), it's always the smallest
connecting the part of S reachable from u with the rest of G, so by the
lemma it must be part of the MST.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Consider the given graph G: 9
0
1
12
5
4
5 2
6
8 6
8 4
4
7 3
0 0
1 1
4
5 2 5 2
6 6
4 4
3 3
(c) Edge 2-3 is added (d) Edge 1-2 is added
0 0
1 1
4 4 5
5 2 5 2
6 6
4 4
4 4
If we include 3edge 3-6 in the tree T, then a cycle
3 (1-2-3-6-1) will be
formed. So, reject the edge 3-6. Next edge 3-4 with weight 7 is added.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
(e) Edge 3-4 is added (f) Edge 4-5 is added
0 0
1 1
4 5 4 5
5 2 5 2
6 6
4 8 4
4 4
7 3 7 3
Addition of edge 4-6 also creates a cycle. Finally, edge 0-1 is added to
obtain the minimum spanning tree.
0 9
1
5
4
5 2
6
8 4
4
7 3
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
SINGLE SOURCE SHORTEST PATH:
A minimum spanning tree gives no indication about the shortest path between two vertices. In real-
life, we are required to find the shortest path between two cities.
For example, one would be interested in finding most economical route between any two
cities in a given railway network.
We are given a directed graph G in which every edge has a weight, and our problem is to find a path
from one vertex v to another vertex w such that the sum of the weights on the path is as minimal as
possible. We shall call such a path a shortest path, even though the weights may represent costs,
time, or some other quantity other than distance.
10
1 6
2 20
9
2 6 5
10 2 12
3 4
4
It turns out that it is just easy to solve the more general problem of starting at one node called the
source, and finding the shortest path to every other node, instead of to just one destination node. For
simplicity, we take the source to be node 1, and our problem then consists of finding the shortest
path from node 1 to every other node in the graph.
The solution we will show for the shortest-path problem is called Dijkstra’s Algorithm.
Dijkstra’s Algorithm
This algorithm works by maintaining a set S of vertices whose shortest distance from the source is
already known.
Initially, S contains only the source vertex.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At each step, we added to S a remaining vertex v whose distance from the source is as short
as possible.
Then we can find a shortest path from the source to v that passes only through vertices in S.
at each step of the algorithm, we use an array dist to record the length of the shortest path to
each vertex.
Once S includes all vertices, dist will hold the shortest distance from the source to each
vertex.
Algorithm ShortestPaths(v,cost,dist,n)
{
for i:=1 to n do
{ // Initialize S.
S[i] := false; dist[i] := cost[v,i];
}
S[v] := true; dist[v] := 0.0;
for num = 2 to n-1 do
{
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u] := true; // put u in S
for (each w adjacent to u with S[w] = false) do
//update distances
if(dist[w]>dist[u]+cost(u,w)) then
dist[w] := dist[u] + cost[u,w];
}
}
4
From the above adjacency matrix of a given graph Initially, S = {1},d[2] = 2, d[3] = ∞, d[4] = 6, d[5]
= 20, d[6] = 10.
10
1 6 d=10
2 20
iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010
2 6 5 d=20
d=2
Initial S = {1}
3 4 d=6
d=∞
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010
2 6 5 d=20
d=2
10
S = {1,2}
3 4 d=6
d=12
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810
2 6 5 d=18
d=2
10 12
S = {1,2,4}
3 4 d=6
4
d=10
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3}
3 4 d=6
4
d=10
10
1 6 d=10
2 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
9 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6}
3 4 d=6
4
d=10
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
10 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
1 6 d=10
2 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210(5){1,2,4,3,6,5}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6,5}
3 4 d=6
4
d=10
At last the d[i] contains the shortest path from source vertex 1 to the vertex i.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
REFERENCES
http://www.codeguru.com/cpp/cpp/algorithms/sort/article.php/c5109
GREEDY METHOD:
Advantage :
The principle advantage of greedy algorithm is that they are usually
straightforward, easy to understand and easy to code.
Disadvantage :
Their main disadvantage is that for many problems there is no greedy
algorithm.
“The one with maximum benefit from multiple choices is selected” is the
basic idea of greedy method. A greedy method arrives at a solution by making a
sequence of choices, each of which simply looks the best at the moment.
Control abstraction :
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
The function Select selects an input from a[] and removes it. The
selected inputs value is assigned to x.
Feasible is a Boolean-valued function that determines whether x can be
included into the solution vector.
The function Union combines x with the solution and updates the
objective function.
The function Greedy describes the essential way that a greedy algorithm
will look, once a particular problem is chosen and the functions Select,
Feasible, and Union are properly implemented.
Algorithm :
Algorithm Greedy(a,n)
{
solution := 0; // initialize the solution
for i:=1 to n do
{
x := Select(a);
if Feasible(solution, x) then
solution := Union(solution, x);
}
return solution;
}
KNAPSACK PROBLEM :
We now try to apply the greedy method to solve the knapsack problem.
We are given n objects and a knapsack or bag.
Object i has a weight wi and
The knapsack has a capacity m.
If a function xi, 0< xi < 1, of object i is placed into the knapsack, then
a profit pixi is earned.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Objective : The objective is to obtain a filling of the knapsack that
“maximizes the total profit earned”. Since the knapsack capacity is m,
“we require the total weight of all chosen objects to be at most m”.
Formally the problem can be stated as
Maximize ∑ p ix i
1
1<i<n
Subjected to ∑ w ix i < m 2
1<i<n
And 0 < xi < 1, 1 < i < n
3
The profits and weights are positive numbers.
A feasible solution is any set (x1,……,xn) satisfying the above eq. 2
& eq.3.
An optimal solution is a feasible solution for which eq. 1 is
maximized.
Example :
Consider the following instance of the knapsack problem: n = 3, m =
20, (p1,p2,p3) = (25,24,15), and (w1,w2,w3) = (18,15,10).
Four feasible solutions are :
(x1,x2,x3) ∑ w ix i ∑ p ix i
(1/2,1/3,1/4
1 16.5 24.25
)
2 (1,2/15,0) 20 28.2
3 (0,2/3,1) 20 31
4 (0,1,1/2) 20 31.5
Of these four feasible solutions, solution 4 yields the maximum profit. This
solution is optimal for the given problem instance.
Algorithm :
Algorithm greedyKnapsack(m,n)
{ // p[1…n] and w[1….n] contain the profits and weights
respectively of the n objects ordered such that p[i]/w[i] >
p[i+1]/w[i+1]
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
for i :=1 to n do x[i] := 0.0; // initialize x.
U := m;
for i := 1 to n do
{
if(w[i] > U) then break;
x[i] := 1.0; U := U-w[i];
}
if(i < n) then x[i] := U / w[i];
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
JOB SEQUENCING WITH DEADLINES :
We are given a set of n jobs.
Associated with job i is an integer deadline di > 0 and a profit pi > 0.
For any job i the profit pi is earned iff the job is completed by its
deadline.
To complete a job, one has to process the job on a machine for one
unit of time.
Only one machine is available for processing jobs.
A feasible solution for this problem is a subset Jof jobs such that
each job in this subset can be completed by its deadline.
The value of a feasible solution J is “a sum of the profits of the jobs
in J, or ∑ pi (iєj)”.
An optimal solution is “a feasible solution with maximum value”.
Example :
Let n = 4, (p1,p2,p3,p4) = (100,10,15,27) and (d1,d2,d3,d4) =
(2,1,2,1). The feasible solutions and their values are :
Processin
Feasible
g value
solution
sequence
1 (1,2) 2,1 110
2 (1,3) 1,3 or 3,1 115
3 (1,4) 4,1 127
4 (2,3) 2,3 25
5 (3,4) 4,3 42
6 (1) 1 100
7 (2) 2 10
8 (3) 3 15
9 (4) 4 27
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
by job 1. Thus the processing of job 4 begins at time zero and that of job 1 is
completed at time 2.
Step 2 : add next ith job to the solution set list if ith job can be
completed by its deadline, di. Assign ith job to the kth vacant time slot in
the list. The list[k] = 0, then kth time slot is empty. If kth time slot is not
vacant, then search the preceding slot,(k-1), and so on, meeting its
deadline constraint.
The greedy method described above always obtains an optimal solution to the
job sequencing problem.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Job( Pi D Profi Operation List[1..4] initialized
i) i t to 0
1 10 2 100 Assign job 1 to 0 1 0 0
0 slot 2
4 27 1 127 Assign job 4 to
4 1 0 0
slot 1
3 15 2 127 Reject job 3,
because it 4 1 0 0
violates its
deadline
2 10 1 127 Reject job 2,
because it 4 1 0 0
violates its
deadline
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Minimum Spanning Trees
Spanning trees
A spanning tree of a graph is just a subgraph that contains all the vertices and
is a tree. A graph may have many spanning trees; for instance the complete
graph on four vertices
This problem can be solved by many different algorithms. Two of them include :
“Prims” Algorithm
“Kruskal’s” Algorithm.
PRIM’S ALGORITHM
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At any stage, we can make out that we have a set of nodes that have
already been included in the tree, the remainder of the nodes have not.
The Prim’s algorithm then finds a new node to be included in the tree by
choosing the edge ( vi , vj ) has the minimum weight (cost) among all
edges, where vi is the in the tree and vj is yet to be added to the tree.
The algorithm starts by selecting a node arbitrarily, and then in each stage, we
include an edge (by adding an associated node) in the tree.
We can represent a weighted graph by an adjacency matrix to store the
set of edges.
An entry(i,j) in an adjacency matrix contains information on the edge that
goes from the vertex i to the vertex j.
Each matrix entry constrains the weight of the corresponding edge.
A specially chosen weight value is used to indicate edges that the missing
from the graph.
A large value “INF” is used to denoted an edge that is missing from the
graph(“INF” may be a constant equal to 99999). The adjacency matrix for
the weighted undirected graph is shown as follows:
1
28 1 2 3 4 5 6 7
10
(a) 1 (b) 1 ∞
1 28 ∞ (c)∞ ∞ 1 10 ∞
2
14 2 28 ∞ 16 ∞ ∞ ∞ 14
16
10 6 7 10
3 ∞ 16 ∞ 12 10 ∞ ∞ ∞
2
24 3
4 ∞ ∞2 12 ∞ 22 ∞ 2
18
25 18 5 ∞ ∞ ∞ 22 ∞ 25 24
6 7 12 6
6
7
10 ∞ ∞ ∞
6
25
7
∞ ∞
5 3
22 4 25
7 ∞ 14 3 ∞ 18
25 24 ∞ 3
∞
5 5 5
4 4 22 4
10 10 10
2 2 2
16 14 16
6 7 6 7 6 7
3 3 3
SRET 25 12
25
12
25
12
5 5 5
22 4 22 4 22 4
Advanced Data structures and Algorithms (ADSA) Unit - III
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Algorithm Prims( a[][] , int n)
{
size := n;
for i := 0 to size step 1 do
for j := 0 to size step 1 do
adjMatrix[i][j] := a[i][j];
selected[0] := 1;
nEdges := 1;
while(nEdges < size)
{
min := 99999;
for i:=0 to size step 1 do
if(selected[i] = 1 )
for j=0 to size step 1 do
if(selected[j] = 0)
if(min>adjMatrix[i][j])
{
min := adjMatrix[i][j];
row := i; col := j;
}
selected[col] := 1;
nEdges ++;
sum := sum + adjMatrix[row][col];
//selected edge is row - col
}
}
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Kruskal's algorithm
Kruskals algorithm constructs the minimum spanning tree of a graph
by adding edges to the spanning tree one-by-one.
At all points during its execution, the set of edges selected by prim’s
algorithm forms exactly one tree.
On the other hand, The set of edges selected by kruskal’s algorithm
forms a forest of trees.
Kruskals algorithm is conceptually quite simple.
Kruskal's algorithm:
sort the edges of G in increasing order by length
keep a subgraph S of G, initially empty
for each edge e in sorted order
if the endpoints of e are disconnected in S
add e to S
return S
Note that, whenever you add an edge (u,v), it's always the smallest
connecting the part of S reachable from u with the rest of G, so by the
lemma it must be part of the MST.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
Consider the given graph G: 9
0
1
12
5
4
5 2
6
8 6
8 4
4
7 3
0 0
1 1
4
5 2 5 2
6 6
4 4
3 3
(c) Edge 2-3 is added (d) Edge 1-2 is added
0 0
1 1
4 4 5
5 2 5 2
6 6
4 4
4 4
If we include 3edge 3-6 in the tree T, then a cycle
3 (1-2-3-6-1) will be
formed. So, reject the edge 3-6. Next edge 3-4 with weight 7 is added.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
(e) Edge 3-4 is added (f) Edge 4-5 is added
0 0
1 1
4 5 4 5
5 2 5 2
6 6
4 8 4
4 4
7 3 7 3
Addition of edge 4-6 also creates a cycle. Finally, edge 0-1 is added to
obtain the minimum spanning tree.
0 9
1
5
4
5 2
6
8 4
4
7 3
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
SINGLE SOURCE SHORTEST PATH:
A minimum spanning tree gives no indication about the shortest path between two vertices. In real-
life, we are required to find the shortest path between two cities.
For example, one would be interested in finding most economical route between any two
cities in a given railway network.
We are given a directed graph G in which every edge has a weight, and our problem is to find a path
from one vertex v to another vertex w such that the sum of the weights on the path is as minimal as
possible. We shall call such a path a shortest path, even though the weights may represent costs,
time, or some other quantity other than distance.
10
1 6
2 20
9
2 6 5
10 2 12
3 4
4
It turns out that it is just easy to solve the more general problem of starting at one node called the
source, and finding the shortest path to every other node, instead of to just one destination node. For
simplicity, we take the source to be node 1, and our problem then consists of finding the shortest
path from node 1 to every other node in the graph.
The solution we will show for the shortest-path problem is called Dijkstra’s Algorithm.
Dijkstra’s Algorithm
This algorithm works by maintaining a set S of vertices whose shortest distance from the source is
already known.
Initially, S contains only the source vertex.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
At each step, we added to S a remaining vertex v whose distance from the source is as short
as possible.
Then we can find a shortest path from the source to v that passes only through vertices in S.
at each step of the algorithm, we use an array dist to record the length of the shortest path to
each vertex.
Once S includes all vertices, dist will hold the shortest distance from the source to each
vertex.
Algorithm ShortestPaths(v,cost,dist,n)
{
for i:=1 to n do
{ // Initialize S.
S[i] := false; dist[i] := cost[v,i];
}
S[v] := true; dist[v] := 0.0;
for num = 2 to n-1 do
{
Choose u from among those vertices not in S such that dist[u] is minimum;
S[u] := true; // put u in S
for (each w adjacent to u with S[w] = false) do
//update distances
if(dist[w]>dist[u]+cost(u,w)) then
dist[w] := dist[u] + cost[u,w];
}
}
4
From the above adjacency matrix of a given graph Initially, S = {1},d[2] = 2, d[3] = ∞, d[4] = 6, d[5]
= 20, d[6] = 10.
10
1 6 d=10
2 20
iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010
2 6 5 d=20
d=2
Initial S = {1}
3 4 d=6
d=∞
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010
2 6 5 d=20
d=2
10
S = {1,2}
3 4 d=6
d=12
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810
2 6 5 d=18
d=2
10 12
S = {1,2,4}
3 4 d=6
4
d=10
10
1 6 d=10
2 20 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
{1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3}
3 4 d=6
4
d=10
10
1 6 d=10
2 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
9 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6}
3 4 d=6
4
d=10
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
10 iterationSd[2]d[3]d[4]d[5]d[6](initial){1}2∞62010(1)
1 6 d=10
2 {1,2}21262010(2){1,2,4}21061810(3){1,2,4,3}21061210(4)
{1,2,4,3,6}21061210(5){1,2,4,3,6,5}21061210
2 6 5 d=12
d=2
2 12
S = {1,2,4,3,6,5}
3 4 d=6
4
d=10
At last the d[i] contains the shortest path from source vertex 1 to the vertex i.
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III
REFERENCES
http://www.codeguru.com/cpp/cpp/algorithms/sort/article.php/c5109
SRET