Embed presentation











![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-02:For each vertex of the given graph, two variables are definedas-• Π[v] which denotes the predecessor of vertex ‘v’• d[v] which denotes the shortest path estimate of vertex ‘v’from the source vertex.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-12-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-02:Initially, the value of these variables is set as-• The value of variable ‘Π’ for each vertex is set to NIL i.e.Π[v] = NIL• The value of variable ‘d’ for source vertex is set to 0 i.e. d[S]= 0• The value of variable ‘d’ for remaining vertices is set to ∞i.e. d[v] = ∞](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-13-2048.jpg&f=jpg&w=240)















![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Algorithm)DIJKSTRA(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s)2 S ← Ø3 Q ← V[G]4 while Q ≠ Ø5 do u ← EXTRACT-MIN(Q)6 S ← 𝑆 ∈ {𝑢}7 for each vertex 𝑣 ∈ 𝐴𝑑𝑗[𝑢]8 do RELAX(u, v, w)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-29-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Algorithm)INITIALIZE-SINGLE-SOURCE(G, s)1 for each vertex v V[G]2 do d[v] ← ∞3 π[v] ← NIL4 d[s] ← 0RELAX(u, v, w)1 if d[v] > d[u] + w(u, v)2 then d[v] ← d[u] + w(u, v)3 π[v] ← u](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-30-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Complexity)CASE-01:• 𝐴[𝑖, 𝑗] stores the information about edge (𝑖, 𝑗).• Time taken for selecting 𝑖 with the smallest 𝑑𝑖𝑠𝑡 is 𝑂(𝑉).• For each neighbor of i, time taken for updating 𝑑𝑖𝑠𝑡[𝑗] is 𝑂(1)and there will be maximum V-1 neighbors.• Time taken for each iteration of the loop is O(V) and onevertex is deleted from Q.• Thus, total time complexity becomes O(𝑉2).](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-31-2048.jpg&f=jpg&w=240)


![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm• Bellman-Ford algorithm solves the single-sourceshortest-path problem in the general case in whichedges of a given digraph can have negative weightas long as G contains no negative cycles.• Like Dijkstra's algorithm, this algorithm, uses thenotion of edge relaxation but does not use withgreedy method. Again, it uses d[u] as an upperbound on the distance d[u, v] from u to v.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-34-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm• The algorithm progressively decreases an estimated[v] on the weight of the shortest path from thesource vertex s to each vertex v ∈ V until it achievethe actual shortest-path.• The algorithm returns Boolean TRUE if the givendigraph contains no negative cycles that arereachable from source vertex s otherwise it returnsBoolean FALSE.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-35-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest pathBellman Ford Algorithm (Negative CycleDetection)Assume:𝑑[𝑢] ≤ 𝑑[𝑥] + 4𝑑[𝑣] ≤ 𝑑[𝑢] + 5𝑑[𝑥] ≤ 𝑑[𝑣] − 10Adding:𝑑[𝑢] + 𝑑[𝑣] + 𝑑[𝑥] ≤ 𝑑[𝑥] + 𝑑[𝑢] + 𝑑[𝑣] − 1Because it’s a cycle, vertices on left are same as those on right. Thuswe get 0 ≤ −1; a contradiction.So for at least one edge (𝑢, 𝑣),𝑑[𝑣] > 𝑑[𝑢] + 𝑤(𝑢, 𝑣)This is exactly what Bellman-Ford checks for.uxv45-10](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-36-2048.jpg&f=jpg&w=240)












































![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Algorithm)BELLMAN-FORD(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s)2 for i ← 1 to |V[G]| - 13 do for each edge (u, v) ∈ E[G]4 do RELAX(u, v, w)5 for each edge (u, v) ∈ E[G]6 do if d[v] > d[u] + w(u, v)7 then return FALSE8 return TRUE](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-81-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Algorithm)INITIALIZE-SINGLE-SOURCE(G, s)1 for each vertex v V[G]2 do d[v] ← ∞3 π[v] ← NIL4 d[s] ← 0RELAX(u, v, w)1 if d[v] > d[u] + w(u, v)2 then d[v] ← d[u] + w(u, v)3 π[v] ← u](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-82-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Analysis)BELLMAN-FORD(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s) → Θ(𝑉)2 for i ← 1 to |V[G]| - 13 do for each edge (u, v) ∈ E[G] Ο(𝐸)4 do RELAX(u, v, w)5 for each edge (u, v) ∈ E[G]6 do if d[v] > d[u] + w(u, v) Ο(𝐸)7 then return FALSE8 return TRUEΟ(𝐸)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-83-2048.jpg&f=jpg&w=240)







![Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Algorithm)FRACTIONAL_KNAPSACK (p, 𝑤, 𝑀)1 for i = 1 to n2 do x[i] = 03 weight = 04 for i = 1 to n5 if weight + w[i] ≤ M6 then x[i] = 17 weight = weight + w[i]8 else9 x[i] = (M - weight) / w[i]10 weight = M11 break12 return x](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-91-2048.jpg&f=jpg&w=240)
















The document discusses greedy algorithms and their application to solving optimization problems. It provides an overview of greedy algorithms and explains that they make locally optimal choices at each step in the hope of finding a globally optimal solution. One application discussed is the single source shortest path problem, which can be solved using Dijkstra's algorithm. Dijkstra's algorithm is presented as a greedy approach that runs in O(V2) time for a graph with V vertices. An example of applying Dijkstra's algorithm to find shortest paths from a source node in a graph is provided.











![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-02:For each vertex of the given graph, two variables are definedas-• Π[v] which denotes the predecessor of vertex ‘v’• d[v] which denotes the shortest path estimate of vertex ‘v’from the source vertex.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-12-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-02:Initially, the value of these variables is set as-• The value of variable ‘Π’ for each vertex is set to NIL i.e.Π[v] = NIL• The value of variable ‘d’ for source vertex is set to 0 i.e. d[S]= 0• The value of variable ‘d’ for remaining vertices is set to ∞i.e. d[v] = ∞](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-13-2048.jpg&f=jpg&w=240)















![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Algorithm)DIJKSTRA(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s)2 S ← Ø3 Q ← V[G]4 while Q ≠ Ø5 do u ← EXTRACT-MIN(Q)6 S ← 𝑆 ∈ {𝑢}7 for each vertex 𝑣 ∈ 𝐴𝑑𝑗[𝑢]8 do RELAX(u, v, w)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-29-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Algorithm)INITIALIZE-SINGLE-SOURCE(G, s)1 for each vertex v V[G]2 do d[v] ← ∞3 π[v] ← NIL4 d[s] ← 0RELAX(u, v, w)1 if d[v] > d[u] + w(u, v)2 then d[v] ← d[u] + w(u, v)3 π[v] ← u](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-30-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Complexity)CASE-01:• 𝐴[𝑖, 𝑗] stores the information about edge (𝑖, 𝑗).• Time taken for selecting 𝑖 with the smallest 𝑑𝑖𝑠𝑡 is 𝑂(𝑉).• For each neighbor of i, time taken for updating 𝑑𝑖𝑠𝑡[𝑗] is 𝑂(1)and there will be maximum V-1 neighbors.• Time taken for each iteration of the loop is O(V) and onevertex is deleted from Q.• Thus, total time complexity becomes O(𝑉2).](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-31-2048.jpg&f=jpg&w=240)


![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm• Bellman-Ford algorithm solves the single-sourceshortest-path problem in the general case in whichedges of a given digraph can have negative weightas long as G contains no negative cycles.• Like Dijkstra's algorithm, this algorithm, uses thenotion of edge relaxation but does not use withgreedy method. Again, it uses d[u] as an upperbound on the distance d[u, v] from u to v.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-34-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm• The algorithm progressively decreases an estimated[v] on the weight of the shortest path from thesource vertex s to each vertex v ∈ V until it achievethe actual shortest-path.• The algorithm returns Boolean TRUE if the givendigraph contains no negative cycles that arereachable from source vertex s otherwise it returnsBoolean FALSE.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-35-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest pathBellman Ford Algorithm (Negative CycleDetection)Assume:𝑑[𝑢] ≤ 𝑑[𝑥] + 4𝑑[𝑣] ≤ 𝑑[𝑢] + 5𝑑[𝑥] ≤ 𝑑[𝑣] − 10Adding:𝑑[𝑢] + 𝑑[𝑣] + 𝑑[𝑥] ≤ 𝑑[𝑥] + 𝑑[𝑢] + 𝑑[𝑣] − 1Because it’s a cycle, vertices on left are same as those on right. Thuswe get 0 ≤ −1; a contradiction.So for at least one edge (𝑢, 𝑣),𝑑[𝑣] > 𝑑[𝑢] + 𝑤(𝑢, 𝑣)This is exactly what Bellman-Ford checks for.uxv45-10](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-36-2048.jpg&f=jpg&w=240)












































![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Algorithm)BELLMAN-FORD(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s)2 for i ← 1 to |V[G]| - 13 do for each edge (u, v) ∈ E[G]4 do RELAX(u, v, w)5 for each edge (u, v) ∈ E[G]6 do if d[v] > d[u] + w(u, v)7 then return FALSE8 return TRUE](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-81-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Algorithm)INITIALIZE-SINGLE-SOURCE(G, s)1 for each vertex v V[G]2 do d[v] ← ∞3 π[v] ← NIL4 d[s] ← 0RELAX(u, v, w)1 if d[v] > d[u] + w(u, v)2 then d[v] ← d[u] + w(u, v)3 π[v] ← u](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-82-2048.jpg&f=jpg&w=240)
![Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Analysis)BELLMAN-FORD(G, w, s)1 INITIALIZE-SINGLE-SOURCE(G, s) → Θ(𝑉)2 for i ← 1 to |V[G]| - 13 do for each edge (u, v) ∈ E[G] Ο(𝐸)4 do RELAX(u, v, w)5 for each edge (u, v) ∈ E[G]6 do if d[v] > d[u] + w(u, v) Ο(𝐸)7 then return FALSE8 return TRUEΟ(𝐸)](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-83-2048.jpg&f=jpg&w=240)







![Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Algorithm)FRACTIONAL_KNAPSACK (p, 𝑤, 𝑀)1 for i = 1 to n2 do x[i] = 03 weight = 04 for i = 1 to n5 if weight + w[i] ≤ M6 then x[i] = 17 weight = weight + w[i]8 else9 x[i] = (M - weight) / w[i]10 weight = M11 break12 return x](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fsinglesourceshortestpath-240304040343-7fc336b9%2f75%2fSingle-source-Shortest-path-algorithm-with-example-91-2048.jpg&f=jpg&w=240)
















Introduction to greedy algorithms, optimization problems, and their characteristics.
Applications include activity selection, scheduling unit-time tasks, and matroids. Also discusses knapsack problem variants.
Single-source shortest path problems utilizing Dijkstra's and Bellman-Ford algorithms with basic principles.
Detailed steps for implementing Dijkstra's algorithm to find shortest path from a source vertex.
Demonstration of Dijkstra’s algorithm with a detailed graphical representation.
Analyzes time complexity of Dijkstra's algorithm in different implementations and scenarios.
Introduction to Bellman-Ford algorithm for handling negative weights and cycle detection in graphs.
Details of Bellman-Ford's algorithm including steps and iterations for demonstration.
Introduction to knapsack problem, types: Fractional and 0/1 knapsack problem, and their characteristics.
Detailed implementation steps for solving the fractional knapsack problem using greedy approach.
Examples for practice on solving fractional knapsack problems with given sets and capacities.