0% found this document useful (0 votes)
17 views68 pages

Adsa-Ii-1 (R20) Unit 4.

The document discusses the divide-and-conquer strategy in algorithms, detailing its general method of dividing problems into sub-problems, conquering them recursively, and combining their solutions. It also covers specific algorithms such as binary search and merge sort, along with their implementations and complexities, and introduces the greedy method for optimization problems, exemplified by the knapsack problem. The document emphasizes the advantages and disadvantages of greedy algorithms and provides control abstractions for implementing these strategies.

Uploaded by

koushi8485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views68 pages

Adsa-Ii-1 (R20) Unit 4.

The document discusses the divide-and-conquer strategy in algorithms, detailing its general method of dividing problems into sub-problems, conquering them recursively, and combining their solutions. It also covers specific algorithms such as binary search and merge sort, along with their implementations and complexities, and introduces the greedy method for optimization problems, exemplified by the knapsack problem. The document emphasizes the advantages and disadvantages of greedy algorithms and provides control abstractions for implementing these strategies.

Uploaded by

koushi8485
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 68

Advanced Data structures and Algorithms (ADSA) Unit - III

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.

We can write a control abstraction that mirrors the way an


algorithm based on divide-and-conquer will look.

DAndC(Algorithm)is initially invoked as DAndC(P), where

 P is the problem to be solved.


 Small(P) is a Boolean-valued function that determines whether the
input size is small enough that the answer can be computed without
splitting.
 If this is so, the function S is invoked. Otherwise the problem P is
divided into smaller sub-problems.
 These sub-problems P1,P2,………,Pk are solved by recursive applications
of DAndC.
 Combine is a function that determines the solution to P using the
solutions to the k sub-problems.
 If the size of P is n and the sizes of the k sub-problems are n 1,n2,….,nk,
respectively, then the computing time of DAndC is described by the
recurrence relation.

Algorithm: control abstraction for divide-and conquer

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 }

The complexity of many divide-and-conquer algorithms is given by recurrences


of the form(General method)

SRET
Advanced Data structures and Algorithms (ADSA) Unit - III

T(n)
{ = T(1)
aT(n/b)+f(n) n>1
n=1

n : size of original problem.


T(i) : time to solve problem of size i.
a : number of subproblems.
b : size of each subproblem.
f(n) : time to divide and combine
subproblems.
where a and b are known constants. We assume that T(1) is known and n is a
power of b
(i.e., n=bk).

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

Is in the form 23T(n/23)+3n


= 2kT(n/2k)+kn k
As we know that n is a power of b that is,
n=bk. [:.where b = 2;]
apply log on both sides, then,
n=bk. [:.where b = 2;]
=> log n = log2k
=> k = log2 n. [substitute the k value in the
above k-th
term.]
T(n) = 2log2nT(n/ 2log2n) + nlog2 n, corresponding to the choice of k=
log2 n.
= nT(n/n)+n log2n
= nT(1)+ n log2n

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].

The general idea is,

 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).

Divide array into two halves FIRST PART SECOND PART

Recursively sort FIRST PART SECOND PART

Algorithm : Merge sort Merge

1 Algorithm MergeSort(low, high) Sorted Array


2 //a[low : high] is a global array to be sorted.
3 //Small(P) is true if there is only one element
4 //to sort. In this case the list is already sorted.
5 {
6 If(low<high) then // if there are more than one element.
7 {
8 // Divide P into subproblems.
9 // Find where to split the set.
10 Mid:=(low + high)/2;
11 // Solve the subproblems.

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 }

1 Algorithm Merge(low, mid, high)


2 // a[low : high] is a global array containing two sorted
3 // subsets in a[low : mid] and in a[mid + 1: high] The goal
4 // is to merge these two sets into a single set residing
5 // in a[low : high]. B[ ] is an auxiliary global array.
6 {
7 h:= low; i:= low; j:= mid + 1;
8 While ((h ≤ mid) and (j ≤ high)) do
9 {
10 If(a[h] ≤ a[j]) then
11 {
12 b[i]:= a[h];h:= h + 1;
13 }
14 else
15 {
16 b[i]:= a[j]; j:= j + 1;
17 }
18 i:= i + 1;
19 }
20 If(h>mid) then
21 for k:= j to high do
22 {
23 b[i]:= a[k]; i:= i + 1;
24 }
25 else
26 for k:= h to mid do
27 {
28 b[i]:= a[k]; i:= i + 1;
29 }
30 for k:= low to
GREEDY METHOD:

Many of the problems that computer algorithms are designed to solve


have involve optimization or finding a best solution. That is some operation can
be performed in a number of different ways, all meeting some basic constraint,
and the problem is to find the way that is optimal. It means that it maximizes or
minimizes the value of some desirable quantity.

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.

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.

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

Solution 3 is optimal. In this solution only jobs 1 and 4 are processed


and the value is 127. These jobs must be processed in the order job 4 followed

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.

To formulate a greedy algorithm to obtain an optimal solution , we


must formulate an optimization measure to determine how the next job is
chosen.
 We have to maximize the objective function ∑ pi (that is total profit is
the optimization measure ). Using this measure, the next job to
include is that increases total profit the most, subject to a resulting
feasible solution, list. This requires us to consider jobs in non-increasing
order of the pi’s.
Ordering the jobs in decreasing order of profit:
Profits = (100,27,15,10)
Jobs = (1,4,3,2)
Deadlines = (2,1,2,1)

The formal algorithm is as follows :


Step 1 : Sort Pi into decreasing order. After sorting p1 > p2 > p3
… pi

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.

Step 3 : stop if all jobs are examined. Otherwise, go to step2.

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

Optimal job sequences is 4 -> 1 with maximum profit of 127.


Algorithm : jobSequence()
Initialize list with zero. // make all n time slots
Profit = 0; // profit = total profit.
For i=1 to n do
K = d[i]; // set ith jobs deadline to k.
While (k>0)
If(list[k] = 0) // if kth slot is vacant,
List[k] = job[i]; // assign ith job to kth slot.
Add p[i] to profit; // accumulate total profit.
Go to step 3; // exit from while-loop
(break)
End if
Decrement k by 1; // otherwise get preceding
slot.
End while
End for
Print list.

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

has sixteen spanning trees:

Minimum spanning trees


Now suppose the edges of the graph have weights or lengths. The weight of a
tree is just the sum of weights of its edges. Obviously, different trees have
different lengths. The problem: how to find the minimum length spanning tree?

Applications of Minimum-Cost Spanning Trees


Minimum-cost spanning trees have many applications. Some are:
• Building cable networks that join n locations with minimum cost.
• Building a road network that joins n cities with minimum cost.
• Obtaining an independent set of circuit equations for an electrical
network.
• In pattern recognition minimal spanning trees can be used to find noisy
pixels.

This problem can be solved by many different algorithms. Two of them include :
 “Prims” Algorithm
 “Kruskal’s” Algorithm.
PRIM’S ALGORITHM

Prim and Dijkstra discovered independently for creating a minimum-cost


spanning tree for a weighted graph.
The minimum spanning tree grows in successive stages.

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

(d) 1 (e) 1 (f) 1

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

 The Prim’s algorithm is implemented using the adjacency matrix of a


graph.
 This matrix is denoted by adjMatrix [i,j] where I and j operate from 0 to
n-1 for n-node weighted undirected graph.
 We assume that the [i,j] element of the adjacency matrix is infinity
(INF), if there is no edge between the nodes i and j.
 we can use a large value for infinity, which is not nearer to the weights
of the given graph.
 The selected is an integer array.
 The elements of selected contain either 0’s or 1’s. (0 represents not
included in the minimum spanning tree and 1 included). The variables
used in this algorithm are as follows:

Size = Number of nodes in the graph.


INF denoted a constant of having a large positive value of 99999.
nEdges = Current number of edges included in the MST.
Min = Minimum weight (cost) of edge.
Row, col indicate the row and column of the edge included in the
MST.
Sum = Total minimum weight (cost) of the edges included in the
MST.

Algorithm: Minimum cost spanning tree using Prim’s algorithm

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.

This algorithm is known as a greedy algorithm, because it chooses at each


step the cheapest edge to add to S.
The kruskals algorithm by considering the graph G shown in figure 16.14(a).
 The edges of the graph are arranged in increasing order of weights
 initially the spanning tree, T is empty.
 We select the edge with smallest weight and include it in T.
 If the selected edge creates a cycle, then it will be removed from
T.
 Repeat these two steps until the tree T contains n-1 edges(where n
is the number of vertices in the graph G).
 If the tree T contains less than n-1 edges and the edge list is empty,
then no spanning tree T is possible for the graph G. these steps are
illustrated as follows:

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

The edges of the graph are arranged in increasing order of weights


Edge 1-6 2-3 1-2 3-6 3-4 4-5 4-6 0-1 0-5
Weigh
4 4 5 6 7 8 8 9 12
t
Includ
yes yes yes no yes Yes no yes no
e
(a) Initial forest of n trees (b) Edge 1-6 is added

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 is based on the adjacency matrix representation of a graph.


 It finds not only the shortest path from one specified vertex to another, but the shortest paths
from the specified vertex to all the other vertices.
 Dijkstra’s Algorithm is called as greedy technique.

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.

Greedy Algorithm to generate Shortest paths:

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];
}
}

Let us apply Dijkstra to the given digraph


10 1 2 3 4 5 6
1 6
2 20 1 ∞ 2 ∞ 6 20 10
9
2 ∞ ∞ 10 ∞ ∞ ∞
2 6 5 3 ∞ ∞ ∞ ∞ 2 ∞
4 ∞ ∞ 4 ∞ 12 ∞
10 2 12 5 ∞ ∞ ∞ ∞ ∞ ∞
3 4 6 ∞ ∞ ∞ ∞ 9 ∞
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III

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 :

The operations of two way merge sort algorithm is illustrated as follows.


We assume to sort the given array a[n] into ascending order. We split it into two
sub arrays: a[1],…a[n/2] and a[[n/2]+1],…a[n]. each sub array is individually
sorted, and the resulting sorted sub arrays are merged to produce a single
sorted array of n elements.
Consider an array of 9 elements : { 75, 40, 10, 90, 50, 95, 55, 15, 65 }.
The merge sort algorithm divides the array into sub arrays and merges them
into sorted sub arrays by the above Merge() algorithm.

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

Note:- Dashed-lines indicate the process of splitting and the


regular arrows the merging process.

If the time for the merging operation is proportional to n, then the


computing time for merge sort is described by the recurrence relation

{
a n=1, a is a constant
T(n)=
2T(n/2)+cn n>1, c is a constant

When n is a power of 2, n = 2 k, we can solve this equation by successive


substitutions:

T(n) = 2(2T(n/4) + cn/2) + cn


= 4T(n/4) + 2cn
= 4(2T(n/8) + cn/4) + 2cn
.
.
.
.
= 2kT(1) + kcn
= an + cnlogn

It is easy to see that if 2k < n ≤ 2k+1, then T(n) ≤ T(2k+1).

:. 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: partition the array a[m:p-1] about a[m]


Algorithm
Partition(a,l,u) // Within a[m],a[m+1],..,a[p-1] the elements are
{ // rearranged in such a manner that if initially t =
repeat a[m],
{ // then after completion a[q] = t for some q
repeat berween m
p:= p + 1; // and p – 1, a[k] ≤ t for m ≤ k < q,and a[k] ≥ t
until (a[p] // for q<k<p. q is returned.
≥ pivot element);
repeat
q:= q –1;
until (a[q] ≤ pivot element);
If(p < q) then interchange(a,p,q);
} until (p ≥ q);
Interchange the a[q] and pivot element; return q;
}

Algorithm interchange(a,i,j)
{ // Exchange a[i] with a[j].
temp:= a[i];
a[i] := a[j]; a[j] :=temp;
}

Algorithm: Sorting by partitioning.


Algorithm QuickSort(p,q)
// sorts the elements a[p],…a[q] which reside in the global
// array a[1:n] into ascending order; a[n+1] is considered to
// be defined and must be >= all the elements in a[1:n].
{
If(p<q) then //if there are more than one element
{ // divide P into two subproblems.
j:= partition(a,p,q+1); // j is the position of the
partitioning element.
// solve the subproblems.
QuickSort(p,j-1);
QuickSort(j+1,q);
// There is no need for combining solutions.
}
}
Example : Given list of elements

10 7 15 12 4 9 13 3

Quick Sort :

SRET
Advanced Data structures and Algorithms (ADSA) Unit - III

Select the first element/object as a pivot element/object, which plays the


main role in the division of given problem into sub-problems. Mark p and q
pointers that points the first & last elements in the remaining list.

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

Now exchange the p and q values. Then the list as follows:

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.

Time Complexity Analysis


The analysis of the procedure QUICK_SORT is given by
T(N) = P(N) + T(J-LB) + T(UB - J)

The worst case time analysis, assuming J = LB, then becomes


Tw = P(N) + Tw(0) + Tw(N-1)
= c*N + Tw(N-1)
= c*N + c*(N-1) + Tw(N-2)
= c*N + c*(N-1) + c*(N-2) + Tw(N-3)
.
.
.
= c * {(N+1)(N)}/2 = O(N2)
The best case analysis occurs when the table is always partitioned in half, that
is, J = [(LB+UB)/2]. The analysis becomes:
Tb = P(N) + 2Tb(N/2)
= c*N + 2Tb(N/2)
= c*N + 2c(N/2) + 4Tb(N/4)
= c*N + 2c(N/2) + 4c(N/4) + 8Tb(N/8)
.
.
.
= (log2N)*c*N + 2log2N*Tb(1)
= O(Nlog2N)

The average case analysis (albeit difficult to evaluate) also comes out to be O(Nlog2N).

STRASSEN’S MATRIX MULTIPLICATION:

Let A and B be two n x n matrices. The product matrix C = AB is also an n x n


matrix whose i, jth element is formed by taking the elements in the ith row of A
and the jth column of B and multiplying them to get

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).

Strassen’s matrix multiplication algorithm is able to perform this


calculation in time O(n2.81). Thus one must be able to speed up the code by
using Strassen’s algorithm instead of the traditional algorithm.

Given nxn matrices A and B we wish to calculate C=AB based on


recursive Divide and Conquer scheme. To do this, first we divide the matrices as
follows(decomposing C=AB into four blocks).

[ ][ ][ ]
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.

The elements of C are given by:

C11 = A11 B11 + A12 B21


C12 = A11 B12 + A12 B22
C21 = A21 B11 + A22 B21
C22 = A21 B12 + A22 B22
This algorithm will continue applying itself to smaller-sized
submatrices until n becomes suitably small (n=2) so that the product is
computed directly.

To compute AB using the traditional method, we need to


perform eight multiplications of n/2 x n/2 matrices and four additions of n/2 x
n/2 matrices. Since two n/2 x n/2 matrices can be added in time cn 2 for some
constant c, the overall computing time T(n) of the resulting divide-and-conquer

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.

This recurrence can be solved in the same way as earlier


recurrences to obtain T(n) = O(n 3). Hence no improvement over the
conventional method has been made.

Since matrix multiplications are more expensive than matrix additions


(O(n3) versus O(n2)), we can attempt to reformulate the equations for C ij so as to
have fewer multiplications and possibly more additions.
Volker Strassen has discovered a way to compute the C ij’s of using only 7
multiplications and 18 additions or substractions. His method involves first
computing the seven n/2 x n/2 matrices P,Q,R,S,T,U and V as in. Then the C ij’s
are computed using the formulas. As can be seen, P,Q,R,S,T,U and V can be
computed using 7 matrix multiplications and 10 matrix addition or subtractions.
The Cij’s require an additional 8 additions or subtractions.
P = (A11 + A22) (B11 + B22)
Q = (A21 + A22) B11
R = A11(B12 - B22)
S = A22(B21 - B11)
T = (A11 + A12) B22
U = (A21 - A11) (B11 + B12)
V = (A12 - A22) (B21 + B22)

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

Example, to perform the multiplication of A and B

[ ][ ]
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

C11 = P+S-T+V = 32 + 36 – 12 + (-20) = 36


C12 = R+T = 10 + 12 = 22
C21 = Q+S = 22 + 36 = 58
C22 = P+R-Q+U = 32 + 10 – 22 + 27 = 47
2 4 2 7 36 22

[ ][ ] [ ]
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

We define the following eight n/2 by n/2 matrices.

A11 A12 B11 B12

[ ][ ][ ][ ]
1

0
2

6
3

0
4

3
1

3
4

1
2

3
7

A21 A22 B21 B22

[ ][ ][ ][ ]
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:

Many of the problems that computer algorithms are designed to solve


have involve optimization or finding a best solution. That is some operation can
be performed in a number of different ways, all meeting some basic constraint,
and the problem is to find the way that is optimal. It means that it maximizes or
minimizes the value of some desirable quantity.
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.

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.

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

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

Solution 3 is optimal. In this solution only jobs 1 and 4 are processed


and the value is 127. These jobs must be processed in the order job 4 followed

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.

To formulate a greedy algorithm to obtain an optimal solution , we


must formulate an optimization measure to determine how the next job is
chosen.
 We have to maximize the objective function ∑ pi (that is total profit is
the optimization measure ). Using this measure, the next job to
include is that increases total profit the most, subject to a resulting
feasible solution, list. This requires us to consider jobs in non-increasing
order of the pi’s.
Ordering the jobs in decreasing order of profit:
Profits = (100,27,15,10)
Jobs = (1,4,3,2)
Deadlines = (2,1,2,1)

The formal algorithm is as follows :


Step 1 : Sort Pi into decreasing order. After sorting p1 > p2 > p3
… pi

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.

Step 3 : stop if all jobs are examined. Otherwise, go to step2.

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

Optimal job sequences is 4 -> 1 with maximum profit of 127.


Algorithm : jobSequence()
Initialize list with zero. // make all n time slots
Profit = 0; // profit = total profit.
For i=1 to n do
K = d[i]; // set ith jobs deadline to k.
While (k>0)
If(list[k] = 0) // if kth slot is vacant,
List[k] = job[i]; // assign ith job to kth slot.
Add p[i] to profit; // accumulate total profit.
Go to step 3; // exit from while-loop
(break)
End if
Decrement k by 1; // otherwise get preceding
slot.
End while
End for
Print list.

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

has sixteen spanning trees:

Minimum spanning trees


Now suppose the edges of the graph have weights or lengths. The weight of a
tree is just the sum of weights of its edges. Obviously, different trees have
different lengths. The problem: how to find the minimum length spanning tree?

Applications of Minimum-Cost Spanning Trees


Minimum-cost spanning trees have many applications. Some are:
• Building cable networks that join n locations with minimum cost.
• Building a road network that joins n cities with minimum cost.
• Obtaining an independent set of circuit equations for an electrical
network.
• In pattern recognition minimal spanning trees can be used to find noisy
pixels.

This problem can be solved by many different algorithms. Two of them include :
 “Prims” Algorithm
 “Kruskal’s” Algorithm.
PRIM’S ALGORITHM

Prim and Dijkstra discovered independently for creating a minimum-cost


spanning tree for a weighted graph.
The minimum spanning tree grows in successive stages.

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

(d) 1 (e) 1 (f) 1

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

 The Prim’s algorithm is implemented using the adjacency matrix of a


graph.
 This matrix is denoted by adjMatrix [i,j] where I and j operate from 0 to
n-1 for n-node weighted undirected graph.
 We assume that the [i,j] element of the adjacency matrix is infinity
(INF), if there is no edge between the nodes i and j.
 we can use a large value for infinity, which is not nearer to the weights
of the given graph.
 The selected is an integer array.
 The elements of selected contain either 0’s or 1’s. (0 represents not
included in the minimum spanning tree and 1 included). The variables
used in this algorithm are as follows:

Size = Number of nodes in the graph.


INF denoted a constant of having a large positive value of 99999.
nEdges = Current number of edges included in the MST.
Min = Minimum weight (cost) of edge.
Row, col indicate the row and column of the edge included in the
MST.
Sum = Total minimum weight (cost) of the edges included in the
MST.

Algorithm: Minimum cost spanning tree using Prim’s algorithm

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.

This algorithm is known as a greedy algorithm, because it chooses at each


step the cheapest edge to add to S.
The kruskals algorithm by considering the graph G shown in figure 16.14(a).
 The edges of the graph are arranged in increasing order of weights
 initially the spanning tree, T is empty.
 We select the edge with smallest weight and include it in T.
 If the selected edge creates a cycle, then it will be removed from
T.
 Repeat these two steps until the tree T contains n-1 edges(where n
is the number of vertices in the graph G).
 If the tree T contains less than n-1 edges and the edge list is empty,
then no spanning tree T is possible for the graph G. these steps are
illustrated as follows:

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

The edges of the graph are arranged in increasing order of weights


Edge 1-6 2-3 1-2 3-6 3-4 4-5 4-6 0-1 0-5
Weigh
4 4 5 6 7 8 8 9 12
t
Includ
yes yes yes no yes yes no yes no
e
(b)Initial forest of n trees (b) Edge 1-6 is added

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 is based on the adjacency matrix representation of a graph.


 It finds not only the shortest path from one specified vertex to another, but the shortest paths
from the specified vertex to all the other vertices.
 Dijkstra’s Algorithm is called as greedy technique.

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.

Greedy Algorithm to generate Shortest paths:

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];
}
}

Let us apply Dijkstra to the given digraph


10 1 2 3 4 5 6
1 6
2 20 1 ∞ 2 ∞ 6 20 10
9
2 ∞ ∞ 10 ∞ ∞ ∞
2 6 5 3 ∞ ∞ ∞ ∞ 2 ∞
4 ∞ ∞ 4 ∞ 12 ∞
10 2 12 5 ∞ ∞ ∞ ∞ ∞ ∞
3 4 6 ∞ ∞ ∞ ∞ 9 ∞
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III

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:

Many of the problems that computer algorithms are designed to solve


have involve optimization or finding a best solution. That is some operation can
be performed in a number of different ways, all meeting some basic constraint,
and the problem is to find the way that is optimal. It means that it maximizes or
minimizes the value of some desirable quantity.
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 :

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

Solution 3 is optimal. In this solution only jobs 1 and 4 are processed


and the value is 127. These jobs must be processed in the order job 4 followed

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.

To formulate a greedy algorithm to obtain an optimal solution , we


must formulate an optimization measure to determine how the next job is
chosen.
 We have to maximize the objective function ∑ pi (that is total profit is
the optimization measure ). Using this measure, the next job to
include is that increases total profit the most, subject to a resulting
feasible solution, list. This requires us to consider jobs in non-increasing
order of the pi’s.
Ordering the jobs in decreasing order of profit:
Profits = (100,27,15,10)
Jobs = (1,4,3,2)
Deadlines = (2,1,2,1)

The formal algorithm is as follows :


Step 1 : Sort Pi into decreasing order. After sorting p1 > p2 > p3
… pi

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.

Step 3 : stop if all jobs are examined. Otherwise, go to step2.

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

Optimal job sequences is 4 -> 1 with maximum profit of 127.


Algorithm : jobSequence()
Initialize list with zero. // make all n time slots
Profit = 0; // profit = total profit.
For i=1 to n do
K = d[i]; // set ith jobs deadline to k.
While (k>0)
If(list[k] = 0) // if kth slot is vacant,
List[k] = job[i]; // assign ith job to kth slot.
Add p[i] to profit; // accumulate total profit.
Go to step 3; // exit from while-loop
(break)
End if
Decrement k by 1; // otherwise get preceding
slot.
End while
End for
Print list.

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

has sixteen spanning trees:

Minimum spanning trees


Now suppose the edges of the graph have weights or lengths. The weight of a
tree is just the sum of weights of its edges. Obviously, different trees have
different lengths. The problem: how to find the minimum length spanning tree?

Applications of Minimum-Cost Spanning Trees


Minimum-cost spanning trees have many applications. Some are:
• Building cable networks that join n locations with minimum cost.
• Building a road network that joins n cities with minimum cost.
• Obtaining an independent set of circuit equations for an electrical
network.
• In pattern recognition minimal spanning trees can be used to find noisy
pixels.

This problem can be solved by many different algorithms. Two of them include :
 “Prims” Algorithm
 “Kruskal’s” Algorithm.
PRIM’S ALGORITHM

Prim and Dijkstra discovered independently for creating a minimum-cost


spanning tree for a weighted graph.
The minimum spanning tree grows in successive stages.

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

(d) 1 (e) 1 (f) 1

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

 The Prim’s algorithm is implemented using the adjacency matrix of a


graph.
 This matrix is denoted by adjMatrix [i,j] where I and j operate from 0 to
n-1 for n-node weighted undirected graph.
 We assume that the [i,j] element of the adjacency matrix is infinity
(INF), if there is no edge between the nodes i and j.
 we can use a large value for infinity, which is not nearer to the weights
of the given graph.
 The selected is an integer array.
 The elements of selected contain either 0’s or 1’s. (0 represents not
included in the minimum spanning tree and 1 included). The variables
used in this algorithm are as follows:

Size = Number of nodes in the graph.


INF denoted a constant of having a large positive value of 99999.
nEdges = Current number of edges included in the MST.
Min = Minimum weight (cost) of edge.
Row, col indicate the row and column of the edge included in the
MST.
Sum = Total minimum weight (cost) of the edges included in the
MST.

Algorithm: Minimum cost spanning tree using Prim’s algorithm

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.

This algorithm is known as a greedy algorithm, because it chooses at each


step the cheapest edge to add to S.
The kruskals algorithm by considering the graph G shown in figure 16.14(a).
 The edges of the graph are arranged in increasing order of weights
 initially the spanning tree, T is empty.
 We select the edge with smallest weight and include it in T.
 If the selected edge creates a cycle, then it will be removed from
T.
 Repeat these two steps until the tree T contains n-1 edges(where n
is the number of vertices in the graph G).
 If the tree T contains less than n-1 edges and the edge list is empty,
then no spanning tree T is possible for the graph G. these steps are
illustrated as follows:

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

The edges of the graph are arranged in increasing order of weights


Edge 1-6 2-3 1-2 3-6 3-4 4-5 4-6 0-1 0-5
Weigh
4 4 5 6 7 8 8 9 12
t
Includ
yes yes yes no yes yes no yes no
e
(c) Initial forest of n trees (b) Edge 1-6 is added

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 is based on the adjacency matrix representation of a graph.


 It finds not only the shortest path from one specified vertex to another, but the shortest paths
from the specified vertex to all the other vertices.
 Dijkstra’s Algorithm is called as greedy technique.

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.

Greedy Algorithm to generate Shortest paths:

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];
}
}

Let us apply Dijkstra to the given digraph


10 1 2 3 4 5 6
1 6
2 20 1 ∞ 2 ∞ 6 20 10
9
2 ∞ ∞ 10 ∞ ∞ ∞
2 6 5 3 ∞ ∞ ∞ ∞ 2 ∞
4 ∞ ∞ 4 ∞ 12 ∞
10 2 12 5 ∞ ∞ ∞ ∞ ∞ ∞
3 4 6 ∞ ∞ ∞ ∞ 9 ∞
SRET
Advanced Data structures and Algorithms (ADSA) Unit - III

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

You might also like