Class warshal2

Preview:

Citation preview

Warshall’s Algorithm&

Dynamic Programming

BY

DEBARATI DAS ( CS-52)

Key Points OF Discussion

1

• FB vs Google+ (graph types)

• Transitive Closure

2

• Explanation of Warshall Algorithm

• Complexity Analysis

3• Applications of Warshall Algorithm

FB Vs Google+

Transitive Closure

1• What is Transitivity ?

• Express it in a graph :

2

• What is Transitive Closure ?

• Express as a Matrix :

• How can we find Transitive Closure of A Graph ?

Finding Transitive Closure

• Involves searching for the shortest path starting at every vertex.

• If you start at ith vertex, after traversal you get columns containing 1 in the ith row.

• Clearly it has a disadvantage (?)

DFS Implementation

• It involves finding the shortest path and improving the estimate on the path every single time, till its optimal.

• However, this fails in case of negative cycles.

Basic Point of The Warshall

Algorithm

Warshall Algorithm : Visitation

Example

Warshall Algorithm : Weighted

Example

Warshall Algorithm, brief

Complexity Analysis :

In this Bottom up Dynamic approach,

D0 -> 0

D1 -> n2 problems , every problem is a value in a cell.

n2xn where n2 denotes subproblems and n is no of matrices

So,total number of problems that need to be solved are n3

Therefore Time complexity is O(n3)

Space Complexity :

D1->D0

D2->D1

D3->D2

At any point of time you need two matrices so O(n2)

Applications of Warshall Algorithm

1. Shortest Path in Directed Graphs

2. Optimal Routing

3. Check if a graph is Bipartite

4. Pathfinder Networks

5. Inversion of Real Networks

(Gauss Jordan Method)

Key Questions

1

• What is the difference between D&C and DP ?

• What is Dynamic Programming ?

• Why is it called “Dynamic” ?

2

• What is Space Time Trade off ?

• How is it implemented by DP ?

• What are the applications of DP ?

• What is a non optimizable problem ?

3

• What is transitive Closure ?

• Warshall Algorithm discussion

Fibonacci Series : An illustration

fib(5)

fib(4) fib(3)

fib(3) fib(2) fib(2) fib(1)

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)

fib(1) fib(0)

6

5

21

3

4

Divide and Conquer vs D.P

Clearly If We use Divide and Conquer, We face

OVER LAP.

Mostly values are calculated again and again,

so even though access of function is O(1) , the

total time complexity is exponential or O(2n).

INSTEAD if we used DP. We would store each

value in a table and access when required.

DP Time complexity becomes O(n)

Why is it called “Dynamic

Programming” ?

Dynamic Programming : Basic Idea

Avoid calculating the same thing twice by keeping a table of known results, which we fill up as subinstances are solved.

Dynamic programming is a bottom-up / top down technique

Bottom up dynamic programming evaluates by computing all

function values in order, starting at lowest and using previously

computed values.

Can MERGE SORT Be solved using DP ?

Recommended