0% found this document useful (0 votes)
5 views

Module - 4 - Part2

Uploaded by

austindsouza052
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Module - 4 - Part2

Uploaded by

austindsouza052
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Don Bosco Institute of Technology

Advanced Data structure and Analysis

Module 4: Dynamic Algorithms

Prof. Prasad Padalkar


Topics

● Introduction to Dynamic Algorithms


○ 0/1 Knapsack
○ All pair shortest path
○ Travelling Salesman problem
○ Matrix Chain Multiplication
○ Optimal Binary Search tree
All pair shortest path {Floyd Warshall Algorithm}
Single source shortest path
algorithms uses Greedy approach :

E.g. Bellman Ford Algorithm & Dijkstra


algorithm

Time Complexity for

1) Bellman Ford T(n) = O(V.E)


2) Dijkstra T(n) = O(E.logV)

Where E = No. of edges and V =


No. of vertices
All pair shortest path {Floyd Warshall Algorithm}
What happens if we apply Greedy approach to solve this problem for each pair of
nodes?

1) Dijkstra’s approach time complexity = E. log V

Since there are V vertices, we can cover all vertices with V pairs , hence

T(n) = V.E.logV , Now No. of edge in complete graph is E = {V(V-1)}/2

For large value of V , E will approx be V^2

Hence T(n) = V^3 log V


All pair shortest path {Floyd Warshall Algorithm}
What happens if we apply Greedy approach to solve this problem for each pair of
nodes?

1) Bellman Ford’s approach time complexity = V.E

Since there are V vertices, we can cover all vertices with V pairs , hence

T(n) = V.(V.E), Now No. of edge in complete graph is E = {V(V-1)}/2

For large value of V , E will approx be V^2

Hence T(n) = V.(V.V^2) = V^4


All pair shortest path {Floyd Warshall Algorithm}
Matrix D0 from graph

8 1 2 3 4
1 2
1 1 0 8 Inf 1
1
4 2 2 Inf 0 1 Inf

3 4 Inf 0 Inf
3 4
9 4 Inf 2 9 0
All pair shortest path {Floyd Warshall Algorithm}
Matrix D1 from D0

8 1 2 3 4
1 2
1 1 0 8 Inf 1
1
4 2 2 Inf 0 1 Inf

3 4 12 0 5
3 4
9 4 Inf 2 9 0

In D1 we consider now along with direct path, the path via Node 1 is also available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D2 from D1

8 1 2 3 4
1 2
1 1 0 8 9 1
1
4 2 2 Inf 0 1 Inf

3 4 12 0 5
3 4
9 4 Inf 2 3 (4 - 2 -1 ) 0

In D2 we consider now along with direct path, the path via Node 1 , Node 2 is also
available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D3 from D2

8 1 2 3 4
1 2
1 1 0 8 9 1
1
4 2 2 5 0 1 6 (2-3-1-4)

3 4 12 0 5
3 4
9
4 7 (4-2-3- 2 3 0
1)

In D3 we consider now along with direct path, the path via Node 1 , Node 2, Node3 is
also available.
All pair shortest path {Floyd Warshall Algorithm}
Matrix D4 from D3

8 1 2 3 4
1 2
1 1 0 3 (1-4-2) 4 (1-4-2- 1
1 3)
4 2
2 5 0 1 6

3 4 3 4 7 (3-1-4- 0 5
9 2)

4 7 2 3 0

In D4 we consider now along with direct path, the path via Node 1 , Node 2, Node3
and Node 4 is also available.
Time Complexity of All pair shortest path
Ak[i,j] = Ak-1[ i , j ] , Matrix size = V * V = V^2
Ak-1[ i , k ] + Ak-1[ k , j ] We need to solve the above sub-problem for
V matrices
Hence Time Complexity T(n) = V.V^2 = V^3

Space Complexity:
A3[2,1] =
A2[ 2 , 1 ] , If we use separate matrix for each D0 - Dn
then we require V+1 matrices, each of order
A2[ 2 , 3 ] + A2[ 3 , 1 ] V x V , hence space complexity is of order of
V^3.

However if we reuse the matrices we need


maximum two matrices hence the space
complexity is of order of V^2
Travelling Salesperson Problem
Also known as finding Hamiltonian cycle or Hamiltonian circuit.

Objective : To find cycle which incurs minimum cost while visiting all the
node only once and return back to starting node.

This type of problems are called polynomial time infeasible problem or NP


Complete problem.
Travelling Salesperson Problem
1 2 3 4

1 2 1 0 10 15 20

2 5 0 25 10

3 15 30 0 5

4 15 10 20 0
3 4

Greedy Approach : Choose minimum cost to travel to next unique node

1 —-> 2 —--> 4 —--> 3 —--> 1


10 10 20 15 Total cost = 55 (not optimal solution)
Travelling Salesperson Problem

Brute - force based all the


options of the path.

The minimum cost happenes to


be for path 1 - 3 - 4 - 2 - 1 which
is 35
Travelling Salesperson Problem
Time Complexity for brute force method:

Some Paths we get are:

1 – 2 – 3 – 4 –1

1–2–4–3–1

1 – 4 – 3 – 2 –1

In all the above if there are n nodes, then (n


- 1)! Different path would be there. Hence
T(n) = O(n!) or O(nn). Thus exponential time
complexity. This type of problem is NP
Complete.
Thank you

You might also like