Movatterモバイル変換


[0]ホーム

URL:


Uploaded byVINITACHAUHAN21
PPTX, PDF7,111 views

Single source Shortest path algorithm with example

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.

Embed presentation

Download to read offline
Design and Analysis of AlgorithmGreedy Methods(Single Source shortest path,Knapsack problem )Lecture – 45 - 53
• A greedy algorithm always makes the choice thatlooks best at the moment. (i.e. it makes a locallyoptimal choice in the hope that this choice willlead to a globally optimal solution).• The objective of this section is to exploresoptimization problems that are solvable by greedyalgorithms.Overview
Greedy Algorithm• In mathematics, computer science andeconomics, an optimization problem is theproblem of finding the best solution from allfeasible solutions.• Algorithms for optimization problems typically gothrough a sequence of steps, with a set ofchoices at each step.• Many optimization problems can be solved usinga greedy approach.• Greedy algorithms are simple andstraightforward.
Greedy Algorithm• A greedy algorithm always makes the choice thatlooks best at the moment.• That is, it makes a locally optimal choice in thehope that this choice will lead to a globallyoptimal solution.• Greedy algorithms do not always yield optimalsolutions, but for many problems they do.• This algorithms are easy to invent, easy toimplement and most of the time provides bestand optimized solution.
Greedy Algorithm• Application of Greedy Algorithm:• A simple but nontrivial problem, the activity-selection problem, for which a greedy algorithmefficiently computes a solution.• In combinatorics,(a branch of mathematics), a‘matroid’ is a structure that abstracts andgeneralizes the notion of linear independence invector spaces. Greedy algorithm always producesan optimal solution for such problems. Schedulingunit-time tasks with deadlines and penalties is anexample of such problem.
Greedy Algorithm• Application of Greedy Algorithm:• An important application of greedy techniques isthe design of data-compression codes (i.e.Huffman code) .• The greedy method is quite powerful and workswell for a wide range of problems. They are:• Minimum-spanning-tree algorithms(Example: Prims and Kruskal algorithm)• Single Source Shortest Path.(Example: Dijkstra's and Bellman ford algorithm)
Greedy Algorithm• Application of Greedy Algorithm:• A problem exhibits optimal substructure if anoptimal solution to the problem contains within itoptimal solutions to subproblems.• This property is a key ingredient of assessing theapplicability of dynamic programming as well asgreedy algorithms.• The subtleties between the above two techniquesare illustrated with the help of two variants of aclassical optimization problem known as knapsackproblem. These variants are:• 0-1 knapsack problem (Dynamic Programming)• Fractional knapsack problem (Greedy Algorithm)
Greedy Algorithm• Problem 5: Single source shortest path• It is a shortest path problem where the shortestpath from a given source vertex to all otherremaining vertices is computed.• Dijkstra’s Algorithm and Bellman FordAlgorithm are the famous algorithms used forsolving single-source shortest path problem.
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm• Dijkstra Algorithm is a very famous greedyalgorithm.• It is used for solving the single sourceshortest path problem.• It computes the shortest path from oneparticular source node to all other remainingnodes of the graph.
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Feasible Condition)• Dijkstra algorithm works• for connected graphs.• for those graphs that do not contain anynegative weight edge.• To provides the value or cost of theshortest paths.• for directed as well as undirected graphs.
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-01:• In the first step. two sets are defined-• One set contains all those vertices which have been includedin the shortest path tree.• In the beginning, this set is empty.• Other set contains all those vertices which are still left to beincluded in the shortest path tree.• In the beginning, this set contains all the vertices of thegiven graph.
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.
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] = ∞
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-03:The following procedure is repeated until all the vertices of thegraph are processed-• Among unprocessed vertices, a vertex with minimum valueof variable ‘d’ is chosen.• Its outgoing edges are relaxed.• After relaxing the edges for that vertex, the sets created instep-01 are updated.
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-4∞s∞x∞t∞y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x∞t∞y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x∞t∞y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s13x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s13x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510Hence the shortest path toall the vertex from s are:𝑠 → 𝑡 = 8𝑠 → 𝑥 = 9𝑠 → 𝑦 = 5𝑠 → 𝑧 = 7
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)
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
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).
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Complexity)CASE-02:• With adjacency list representation, all vertices of the graphcan be traversed using BFS in 𝑂(𝑉 + 𝐸) time.• In min heap, operations like extract-min and decrease-keyvalue takes 𝑂(log 𝑉) time.• So, overall time complexity becomes𝑂(𝐸 + 𝑉) 𝑥 𝑂(log 𝑉) which is 𝑂((𝐸 + 𝑉) 𝑥 log 𝑉) = 𝑂(𝐸 log 𝑉)
Greedy Algorithm• Problem 5: Single source shortest path• Dijkstra’s Algorithm (Self Practice)Example 2: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-s∞c∞a∞d∞b∞e∞11125222
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.
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.
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
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm (Implementation)• Step 1: Start with the weighted graph.• Step 2: Choose the starting vertex by making the pathvalue zero and assign infinity path values to all othervertices.• Step 3: Visit each edge and relax the path distances ifthey are inaccurate.• Step 4: Do step 3 V-1 times because in the worst case avertex's path length might need to be readjusted V-1times.• Step 5: After all vertices have their path lengths, check ifa negative cycle is present or not.
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-∞s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-∞s∞x∞t∞y∞z8-32579-276-40s∞x∞t∞y∞z8-32579-276-4Iteration - 1
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t∞y∞z8-32579-276-4Iteration - 1 Edge no - 10s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 20s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 30s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 40s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 50s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 60s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 70s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 80s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 90s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no – 100s∞x∞t∞y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t∞y∞z8-32579-276-4Iteration - 2 Edge no - 10s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 2 Edge no - 20s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 30s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 40s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 50s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y16z8-32579-276-4Iteration - 2 Edge no - 60s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y2z8-32579-276-4Iteration - 2 Edge no - 70s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y2z8-32579-276-4Iteration - 2 Edge no - 80s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 2 Edge no - 90s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 2 Edge no – 100s∞x6t7y∞z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 3 Edge no - 10s4x6t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 20s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 30s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 40s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 50s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 60s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 70s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 80s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 90s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no – 100s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 10s4x2t7y2z8-32579-276-4 0s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 20s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 30s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 40s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 50s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 60s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 70s4x2t7y−2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 80s4x2t7y2z8-32579-276-4 0s4x2t7y−2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 90s4x2t7y−2z8-32579-276-40s4x2t7y2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no – 100s4x2t7y2z8-32579-276-4 0s4x2t7y−2z8-32579-276-4
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for the given graphusing Bellman ford Algorithm-0s4x2t7y−2z8-32579-276-40s∞x∞t∞y∞z8-32579-276-40s∞x6t7y∞z8-32579-276-40s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4After Iteration 1After Iteration 3After Iteration 2After Iteration 4
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
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
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Ο(𝐸)
Greedy Algorithm• Problem 5: Single source shortest path• Bellman Ford Algorithm(Self Practice)Example 2: Construct the Single source shortest path for thegiven graph using Bellman Ford Algorithm-s∞c∞a∞d∞b∞e∞11125222
Greedy Algorithm• Problem 5: Knapsack ProblemProblem definition:• The Knapsack problem is an “combinatorialoptimization problem”, a topic in mathematicsand computer science about finding the optimalobject among a set of object .• Given a set of items, each with a weight and aprofit, determine the number of each item toinclude in a collection so that the total weight isless than or equal to a given limit and the totalprofit is as large as possible.
Greedy Algorithm• Problem 5: Knapsack ProblemVersions of Knapsack:• Fractional Knapsack Problem:Items are divisible; you can take any fraction ofan item and it is solved by using GreedyAlgorithm.• 0/1 Knapsack Problem:Items are indivisible; you either take them ornot and it is solved by using DynamicProgramming(DP).
Greedy Algorithm• Problem 5: Knapsack Problem• Fractional Knapsack Problem:Given n objects and a knapsack with a capacity“M” (weight)• Each object 𝑖 has weight 𝑤𝑖 and profit 𝑝𝑖.• For each object 𝑖, suppose a fraction 𝑥𝑖, 0 ≤ 𝑥𝑖 ≤ 1, can be placed in theknapsack, then the profit earned is 𝑝𝑖. 𝑥𝑖 .
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem:The objective is to maximize profit subject to capacity constraints.𝑖. 𝑒. 𝑚𝑎𝑥𝑖𝑚𝑖𝑧𝑒𝑖=1𝑛𝑝𝑖𝑥𝑖 ------------------------------(1)Subject to𝑖=1𝑛𝑤𝑖𝑥𝑖 ≤ 𝑀 ------------------------(2)Where, 0 ≤ 𝑥𝑖 ≤ 1,𝑝𝑖 > 0,𝑤𝑖 > 0.A feasible solution is any subset {𝑥1, 𝑥2, 𝑥3, … … , 𝑥𝑛} satisfying (1) & (2).An optimal solution is a feasible solution that maximize 𝑖=1𝑛𝑝𝑖𝑥𝑖.
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Fractional knapsack problem is solved using greedy method in thefollowing steps-Step-01:• For each item, compute its (profit / weight) ratio.(i.e 𝑝𝑖/𝑥𝑖)Step-02:• Arrange all the items in decreasing order of their (profit /weight) ratio.Step-03:• Start putting the items into the knapsack beginning from theitem with the highest ratio.• Put as many items as you can into the knapsack.
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-01: Compute the (profit / weight) ratio for each item-
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
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Item Weight Profit1 5 302 10 403 15 454 22 775 25 90
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-01: Compute the (profit / weight) ratio for each item-Item Weight Profit Ratio1 5 30 62 10 40 43 15 45 34 22 77 3.55 25 90 3.6
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-02: Sort all the items in decreasing order of their value / weightratio-Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 0
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 0
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 30
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 30
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1, 2 70
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1, 2 70
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 160
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 160
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1:Now, Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg.Since in fractional knapsack problem, even the fraction of any item can be taken.So, knapsack will contain the fractional part of item 4.(20 out of 22)Total cost of the knapsack = 160 + (20/22) x 77 = 160 + 70 = 230 unitsItem Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 16001,2,5,frac(4) 230
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(Algorithm)• The main time taking step is the sorting of all items indecreasing order of their (profit / weight) ratio.• If the items are already arranged in the required order,then while loop takes 𝑂(𝑛) time.• The average time complexity of Quick Sort is 𝑂(𝑛 log 𝑛).• Therefore, total time taken including the sort is 𝑂(𝑛 log 𝑛).
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(self practice)Example 2: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Item Weight ProfitA 1 5B 3 9C 2 4D 2 8
Greedy Algorithm• Problem 5: Knapsack ProblemFractional Knapsack Problem(self practice)Example 3: Find the optimal solution for the fractional knapsackproblem making use of greedy approach. Consider-n = 3M = 20 kg(w1, w2, w3) = (18, 15, 10)(p1, p2, p3) = (25, 24, 15)
Single source Shortest path algorithm with example

Recommended

PPTX
Greedy Algorithms
PPTX
Back tracking and branch and bound class 20
 
PPTX
Bruteforce algorithm
PPT
Introduction to Design Algorithm And Analysis.ppt
PPT
SINGLE-SOURCE SHORTEST PATHS
PDF
Design and Analysis Algorithms.pdf
PPTX
Shortest path algorithm
PPTX
Dijkstra’s algorithm
PPTX
Travelling salesman dynamic programming
PPTX
Graph coloring using backtracking
PPTX
Parsing in Compiler Design
PPTX
Knapsack problem using greedy approach
PPTX
Code generation
PPTX
daa-unit-3-greedy method
PPTX
Assignment problem branch and bound.pptx
PDF
UNIT-V.pdf daa unit material 5 th unit ppt
PPTX
Lecture 14 run time environment
PPTX
Lecture optimal binary search tree
PPTX
Bellman ford algorithm
PPTX
Asymptotic Notations
PPTX
Data structure - Graph
PPTX
Introduction TO Finite Automata
PPTX
sum of subset problem using Backtracking
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
implementation of travelling salesman problem with complexity ppt
PPT
Divide and conquer
PPTX
unit-4-dynamic programming
PPT
Top down parsing
PPTX
Dijkstra algorithm a dynammic programming approach
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx

More Related Content

PPTX
Greedy Algorithms
PPTX
Back tracking and branch and bound class 20
 
PPTX
Bruteforce algorithm
PPT
Introduction to Design Algorithm And Analysis.ppt
PPT
SINGLE-SOURCE SHORTEST PATHS
PDF
Design and Analysis Algorithms.pdf
PPTX
Shortest path algorithm
PPTX
Dijkstra’s algorithm
Greedy Algorithms
Back tracking and branch and bound class 20
 
Bruteforce algorithm
Introduction to Design Algorithm And Analysis.ppt
SINGLE-SOURCE SHORTEST PATHS
Design and Analysis Algorithms.pdf
Shortest path algorithm
Dijkstra’s algorithm

What's hot

PPTX
Travelling salesman dynamic programming
PPTX
Graph coloring using backtracking
PPTX
Parsing in Compiler Design
PPTX
Knapsack problem using greedy approach
PPTX
Code generation
PPTX
daa-unit-3-greedy method
PPTX
Assignment problem branch and bound.pptx
PDF
UNIT-V.pdf daa unit material 5 th unit ppt
PPTX
Lecture 14 run time environment
PPTX
Lecture optimal binary search tree
PPTX
Bellman ford algorithm
PPTX
Asymptotic Notations
PPTX
Data structure - Graph
PPTX
Introduction TO Finite Automata
PPTX
sum of subset problem using Backtracking
PPTX
Greedy Algorithm - Knapsack Problem
PPTX
implementation of travelling salesman problem with complexity ppt
PPT
Divide and conquer
PPTX
unit-4-dynamic programming
PPT
Top down parsing
Travelling salesman dynamic programming
Graph coloring using backtracking
Parsing in Compiler Design
Knapsack problem using greedy approach
Code generation
daa-unit-3-greedy method
Assignment problem branch and bound.pptx
UNIT-V.pdf daa unit material 5 th unit ppt
Lecture 14 run time environment
Lecture optimal binary search tree
Bellman ford algorithm
Asymptotic Notations
Data structure - Graph
Introduction TO Finite Automata
sum of subset problem using Backtracking
Greedy Algorithm - Knapsack Problem
implementation of travelling salesman problem with complexity ppt
Divide and conquer
unit-4-dynamic programming
Top down parsing

Similar to Single source Shortest path algorithm with example

PPTX
Dijkstra algorithm a dynammic programming approach
PPTX
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
PPT
PDF
04 greedyalgorithmsii 2x2
PPTX
djikstrasalgorithm-250418064637-830a84e1.pptx
PPTX
Single sourceshortestpath by emad
PPT
2.3 shortest path dijkstra’s
PPTX
Dijkstra's algorithm presentation
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
PPT
dijkstra algo.ppt
PPT
Shortest path
PDF
Djikstra’s Algorithm. Approach to shortest path algorithm with greedy method
PPT
Dijkstra_Algorithm with illustarted example
PPT
3 Greedy-lec.pptggggghhhhhhhyyyyyyyyyyyyyy
PPT
dijkstraC.ppt
PPT
barrera.ppt
PPT
barrera.ppt
PPTX
7. Algorithm Design and analysis ppt.pptx
PDF
Unit 3 - Greedy Method
PDF
Unit 3 greedy method
Dijkstra algorithm a dynammic programming approach
SEMINAR ON SHORTEST PATH ALGORITHMS.pptx
04 greedyalgorithmsii 2x2
djikstrasalgorithm-250418064637-830a84e1.pptx
Single sourceshortestpath by emad
2.3 shortest path dijkstra’s
Dijkstra's algorithm presentation
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
dijkstra algo.ppt
Shortest path
Djikstra’s Algorithm. Approach to shortest path algorithm with greedy method
Dijkstra_Algorithm with illustarted example
3 Greedy-lec.pptggggghhhhhhhyyyyyyyyyyyyyy
dijkstraC.ppt
barrera.ppt
barrera.ppt
7. Algorithm Design and analysis ppt.pptx
Unit 3 - Greedy Method
Unit 3 greedy method

Recently uploaded

PDF
222109_MD_MILTON_HOSSAIN_26TH_BATCH_REGULAR_PMIT V5.pdf
PDF
Best Marketplaces to Buy Snapchat Accounts in 2025.pdf
PDF
@Regenerative braking system of DC motor
PDF
Small Space Big Design - Amar DeXign Scape
PDF
Welcome to ISPR 2026 - 12th International Conference on Image and Signal Pro...
PPTX
ensemble learning of machine learning .pptx
PDF
application of matrix in computer science
PDF
November_2025 Top 10 Read Articles in Computer Networks & Communications.pdf
PPT
399-Cathodic-Protection-Presentation.ppt
 
PPTX
introduction-to-maintenance- Dr. Munthear Alqaderi
PPTX
Supercapacitor.pptx...............................
PPTX
Lead-acid battery.pptx.........................
PPTX
Computer engineering for collage studen. pptx
PPTX
DevFest Seattle 2025 - AI Native Design Patterns.pptx
PPTX
Presentation 1.pptx WHAT IS ARTIFICIAL INTELLIGENCE?
PPTX
TRANSPORTATION ENGINEERING Unit-5.1.pptx
PDF
PRIZ Academy - Thinking The Skill Everyone Forgot
PPTX
Principles of Energy Efficiency_ Doing More with Less
PDF
ANPARA THERMAL POWER STATION[1] sangam.pdf
PDF
Event #3_ Build a Gemini Bot, Together with GitHub_private.pdf
222109_MD_MILTON_HOSSAIN_26TH_BATCH_REGULAR_PMIT V5.pdf
Best Marketplaces to Buy Snapchat Accounts in 2025.pdf
@Regenerative braking system of DC motor
Small Space Big Design - Amar DeXign Scape
Welcome to ISPR 2026 - 12th International Conference on Image and Signal Pro...
ensemble learning of machine learning .pptx
application of matrix in computer science
November_2025 Top 10 Read Articles in Computer Networks & Communications.pdf
399-Cathodic-Protection-Presentation.ppt
 
introduction-to-maintenance- Dr. Munthear Alqaderi
Supercapacitor.pptx...............................
Lead-acid battery.pptx.........................
Computer engineering for collage studen. pptx
DevFest Seattle 2025 - AI Native Design Patterns.pptx
Presentation 1.pptx WHAT IS ARTIFICIAL INTELLIGENCE?
TRANSPORTATION ENGINEERING Unit-5.1.pptx
PRIZ Academy - Thinking The Skill Everyone Forgot
Principles of Energy Efficiency_ Doing More with Less
ANPARA THERMAL POWER STATION[1] sangam.pdf
Event #3_ Build a Gemini Bot, Together with GitHub_private.pdf
In this document
Powered by AI

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.

Single source Shortest path algorithm with example

  • 1.
    Design and Analysisof AlgorithmGreedy Methods(Single Source shortest path,Knapsack problem )Lecture – 45 - 53
  • 2.
    • A greedyalgorithm always makes the choice thatlooks best at the moment. (i.e. it makes a locallyoptimal choice in the hope that this choice willlead to a globally optimal solution).• The objective of this section is to exploresoptimization problems that are solvable by greedyalgorithms.Overview
  • 3.
    Greedy Algorithm• Inmathematics, computer science andeconomics, an optimization problem is theproblem of finding the best solution from allfeasible solutions.• Algorithms for optimization problems typically gothrough a sequence of steps, with a set ofchoices at each step.• Many optimization problems can be solved usinga greedy approach.• Greedy algorithms are simple andstraightforward.
  • 4.
    Greedy Algorithm• Agreedy algorithm always makes the choice thatlooks best at the moment.• That is, it makes a locally optimal choice in thehope that this choice will lead to a globallyoptimal solution.• Greedy algorithms do not always yield optimalsolutions, but for many problems they do.• This algorithms are easy to invent, easy toimplement and most of the time provides bestand optimized solution.
  • 5.
    Greedy Algorithm• Applicationof Greedy Algorithm:• A simple but nontrivial problem, the activity-selection problem, for which a greedy algorithmefficiently computes a solution.• In combinatorics,(a branch of mathematics), a‘matroid’ is a structure that abstracts andgeneralizes the notion of linear independence invector spaces. Greedy algorithm always producesan optimal solution for such problems. Schedulingunit-time tasks with deadlines and penalties is anexample of such problem.
  • 6.
    Greedy Algorithm• Applicationof Greedy Algorithm:• An important application of greedy techniques isthe design of data-compression codes (i.e.Huffman code) .• The greedy method is quite powerful and workswell for a wide range of problems. They are:• Minimum-spanning-tree algorithms(Example: Prims and Kruskal algorithm)• Single Source Shortest Path.(Example: Dijkstra's and Bellman ford algorithm)
  • 7.
    Greedy Algorithm• Applicationof Greedy Algorithm:• A problem exhibits optimal substructure if anoptimal solution to the problem contains within itoptimal solutions to subproblems.• This property is a key ingredient of assessing theapplicability of dynamic programming as well asgreedy algorithms.• The subtleties between the above two techniquesare illustrated with the help of two variants of aclassical optimization problem known as knapsackproblem. These variants are:• 0-1 knapsack problem (Dynamic Programming)• Fractional knapsack problem (Greedy Algorithm)
  • 8.
    Greedy Algorithm• Problem5: Single source shortest path• It is a shortest path problem where the shortestpath from a given source vertex to all otherremaining vertices is computed.• Dijkstra’s Algorithm and Bellman FordAlgorithm are the famous algorithms used forsolving single-source shortest path problem.
  • 9.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm• Dijkstra Algorithm is a very famous greedyalgorithm.• It is used for solving the single sourceshortest path problem.• It computes the shortest path from oneparticular source node to all other remainingnodes of the graph.
  • 10.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm (Feasible Condition)• Dijkstra algorithm works• for connected graphs.• for those graphs that do not contain anynegative weight edge.• To provides the value or cost of theshortest paths.• for directed as well as undirected graphs.
  • 11.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-01:• In the first step. two sets are defined-• One set contains all those vertices which have been includedin the shortest path tree.• In the beginning, this set is empty.• Other set contains all those vertices which are still left to beincluded in the shortest path tree.• In the beginning, this set contains all the vertices of thegiven graph.
  • 12.
    Greedy Algorithm• Problem5: 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.
  • 13.
    Greedy Algorithm• Problem5: 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] = ∞
  • 14.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm (Implementation)The implementation of Dijkstra Algorithm is executed in thefollowing steps-• Step-03:The following procedure is repeated until all the vertices of thegraph are processed-• Among unprocessed vertices, a vertex with minimum valueof variable ‘d’ is chosen.• Its outgoing edges are relaxed.• After relaxing the edges for that vertex, the sets created instep-01 are updated.
  • 15.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-4∞s∞x∞t∞y∞z2 971623510
  • 16.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x∞t∞y∞z2 971623510
  • 17.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x∞t∞y∞z2 971623510
  • 18.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
  • 19.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
  • 20.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s∞x10t5y∞z2 971623510
  • 21.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
  • 22.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
  • 23.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s14x8t5y7z2 971623510
  • 24.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s13x8t5y7z2 971623510
  • 25.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s13x8t5y7z2 971623510
  • 26.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510
  • 27.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510
  • 28.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-40s9x8t5y7z2 971623510Hence the shortest path toall the vertex from s are:𝑠 → 𝑡 = 8𝑠 → 𝑥 = 9𝑠 → 𝑦 = 5𝑠 → 𝑧 = 7
  • 29.
    Greedy Algorithm• Problem5: 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)
  • 30.
    Greedy Algorithm• Problem5: 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
  • 31.
    Greedy Algorithm• Problem5: 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).
  • 32.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm (Complexity)CASE-02:• With adjacency list representation, all vertices of the graphcan be traversed using BFS in 𝑂(𝑉 + 𝐸) time.• In min heap, operations like extract-min and decrease-keyvalue takes 𝑂(log 𝑉) time.• So, overall time complexity becomes𝑂(𝐸 + 𝑉) 𝑥 𝑂(log 𝑉) which is 𝑂((𝐸 + 𝑉) 𝑥 log 𝑉) = 𝑂(𝐸 log 𝑉)
  • 33.
    Greedy Algorithm• Problem5: Single source shortest path• Dijkstra’s Algorithm (Self Practice)Example 2: Construct the Single source shortest path for thegiven graph using Dijkstra’s Algorithm-s∞c∞a∞d∞b∞e∞11125222
  • 34.
    Greedy Algorithm• Problem5: 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.
  • 35.
    Greedy Algorithm• Problem5: 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.
  • 36.
    Greedy Algorithm• Problem5: 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
  • 37.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford Algorithm (Implementation)• Step 1: Start with the weighted graph.• Step 2: Choose the starting vertex by making the pathvalue zero and assign infinity path values to all othervertices.• Step 3: Visit each edge and relax the path distances ifthey are inaccurate.• Step 4: Do step 3 V-1 times because in the worst case avertex's path length might need to be readjusted V-1times.• Step 5: After all vertices have their path lengths, check ifa negative cycle is present or not.
  • 38.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-∞s∞x∞t∞y∞z8-32579-276-4
  • 39.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-∞s∞x∞t∞y∞z8-32579-276-40s∞x∞t∞y∞z8-32579-276-4Iteration - 1
  • 40.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t∞y∞z8-32579-276-4Iteration - 1 Edge no - 10s∞x∞t∞y∞z8-32579-276-4
  • 41.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 20s∞x∞t∞y∞z8-32579-276-4
  • 42.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 30s∞x∞t∞y∞z8-32579-276-4
  • 43.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 40s∞x∞t∞y∞z8-32579-276-4
  • 44.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 50s∞x∞t∞y∞z8-32579-276-4
  • 45.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 60s∞x∞t∞y∞z8-32579-276-4
  • 46.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 70s∞x∞t∞y∞z8-32579-276-4
  • 47.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 80s∞x∞t∞y∞z8-32579-276-4
  • 48.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no - 90s∞x∞t∞y∞z8-32579-276-4
  • 49.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 1 Edge no – 100s∞x∞t∞y∞z8-32579-276-4
  • 50.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t∞y∞z8-32579-276-4Iteration - 2 Edge no - 10s∞x6t7y∞z8-32579-276-4
  • 51.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y∞z8-32579-276-4Iteration - 2 Edge no - 20s∞x6t7y∞z8-32579-276-4
  • 52.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 30s∞x6t7y∞z8-32579-276-4
  • 53.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 40s∞x6t7y∞z8-32579-276-4
  • 54.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s∞x6t7y16z8-32579-276-4Iteration - 2 Edge no - 50s∞x6t7y∞z8-32579-276-4
  • 55.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y16z8-32579-276-4Iteration - 2 Edge no - 60s∞x6t7y∞z8-32579-276-4
  • 56.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y2z8-32579-276-4Iteration - 2 Edge no - 70s∞x6t7y∞z8-32579-276-4
  • 57.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s11x6t7y2z8-32579-276-4Iteration - 2 Edge no - 80s∞x6t7y∞z8-32579-276-4
  • 58.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 2 Edge no - 90s∞x6t7y∞z8-32579-276-4
  • 59.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 2 Edge no – 100s∞x6t7y∞z8-32579-276-4
  • 60.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-0s4x6t7y2z8-32579-276-4Iteration - 3 Edge no - 10s4x6t7y2z8-32579-276-4
  • 61.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 20s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
  • 62.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 30s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
  • 63.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 40s4x6t7y2z8-32579-276-40s4x6t7y2z8-32579-276-4
  • 64.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 50s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 65.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 60s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 66.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 70s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 67.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 80s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 68.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no - 90s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 69.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 3 Edge no – 100s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 70.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 10s4x2t7y2z8-32579-276-4 0s4x2t7y2z8-32579-276-4
  • 71.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 20s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 72.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 30s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 73.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 40s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 74.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 50s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 75.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 60s4x2t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 76.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 70s4x2t7y−2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 77.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 80s4x2t7y2z8-32579-276-4 0s4x2t7y−2z8-32579-276-4
  • 78.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no - 90s4x2t7y−2z8-32579-276-40s4x2t7y2z8-32579-276-4
  • 79.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for thegiven graph using Bellman ford Algorithm-Iteration - 4 Edge no – 100s4x2t7y2z8-32579-276-4 0s4x2t7y−2z8-32579-276-4
  • 80.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford AlgorithmExample 1: Construct the Single source shortest path for the given graphusing Bellman ford Algorithm-0s4x2t7y−2z8-32579-276-40s∞x∞t∞y∞z8-32579-276-40s∞x6t7y∞z8-32579-276-40s4x6t7y2z8-32579-276-40s4x2t7y2z8-32579-276-4After Iteration 1After Iteration 3After Iteration 2After Iteration 4
  • 81.
    Greedy Algorithm• Problem5: 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
  • 82.
    Greedy Algorithm• Problem5: 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
  • 83.
    Greedy Algorithm• Problem5: 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Ο(𝐸)
  • 84.
    Greedy Algorithm• Problem5: Single source shortest path• Bellman Ford Algorithm(Self Practice)Example 2: Construct the Single source shortest path for thegiven graph using Bellman Ford Algorithm-s∞c∞a∞d∞b∞e∞11125222
  • 85.
    Greedy Algorithm• Problem5: Knapsack ProblemProblem definition:• The Knapsack problem is an “combinatorialoptimization problem”, a topic in mathematicsand computer science about finding the optimalobject among a set of object .• Given a set of items, each with a weight and aprofit, determine the number of each item toinclude in a collection so that the total weight isless than or equal to a given limit and the totalprofit is as large as possible.
  • 86.
    Greedy Algorithm• Problem5: Knapsack ProblemVersions of Knapsack:• Fractional Knapsack Problem:Items are divisible; you can take any fraction ofan item and it is solved by using GreedyAlgorithm.• 0/1 Knapsack Problem:Items are indivisible; you either take them ornot and it is solved by using DynamicProgramming(DP).
  • 87.
    Greedy Algorithm• Problem5: Knapsack Problem• Fractional Knapsack Problem:Given n objects and a knapsack with a capacity“M” (weight)• Each object 𝑖 has weight 𝑤𝑖 and profit 𝑝𝑖.• For each object 𝑖, suppose a fraction 𝑥𝑖, 0 ≤ 𝑥𝑖 ≤ 1, can be placed in theknapsack, then the profit earned is 𝑝𝑖. 𝑥𝑖 .
  • 88.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem:The objective is to maximize profit subject to capacity constraints.𝑖. 𝑒. 𝑚𝑎𝑥𝑖𝑚𝑖𝑧𝑒𝑖=1𝑛𝑝𝑖𝑥𝑖 ------------------------------(1)Subject to𝑖=1𝑛𝑤𝑖𝑥𝑖 ≤ 𝑀 ------------------------(2)Where, 0 ≤ 𝑥𝑖 ≤ 1,𝑝𝑖 > 0,𝑤𝑖 > 0.A feasible solution is any subset {𝑥1, 𝑥2, 𝑥3, … … , 𝑥𝑛} satisfying (1) & (2).An optimal solution is a feasible solution that maximize 𝑖=1𝑛𝑝𝑖𝑥𝑖.
  • 89.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Fractional knapsack problem is solved using greedy method in thefollowing steps-Step-01:• For each item, compute its (profit / weight) ratio.(i.e 𝑝𝑖/𝑥𝑖)Step-02:• Arrange all the items in decreasing order of their (profit /weight) ratio.Step-03:• Start putting the items into the knapsack beginning from theitem with the highest ratio.• Put as many items as you can into the knapsack.
  • 90.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-01: Compute the (profit / weight) ratio for each item-
  • 91.
    Greedy Algorithm• Problem5: 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
  • 92.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Item Weight Profit1 5 302 10 403 15 454 22 775 25 90
  • 93.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-01: Compute the (profit / weight) ratio for each item-Item Weight Profit Ratio1 5 30 62 10 40 43 15 45 34 22 77 3.55 25 90 3.6
  • 94.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-02: Sort all the items in decreasing order of their value / weightratio-Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3
  • 95.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 0
  • 96.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 0
  • 97.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 30
  • 98.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 30
  • 99.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1, 2 70
  • 100.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1, 2 70
  • 101.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 160
  • 102.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Step-03: Start filling the knapsack by putting the items into it one byone.Item Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 160
  • 103.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Implementation)Example 1:Now, Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg.Since in fractional knapsack problem, even the fraction of any item can be taken.So, knapsack will contain the fractional part of item 4.(20 out of 22)Total cost of the knapsack = 160 + (20/22) x 77 = 160 + 70 = 230 unitsItem Weight Profit Ratio1 5 30 62 10 40 45 25 90 3.64 22 77 3.53 15 45 3KnapsackweightItems inKnapsack Cost60 ∅ 055 1 3045 1,2 7020 1,2,5 16001,2,5,frac(4) 230
  • 104.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(Algorithm)• The main time taking step is the sorting of all items indecreasing order of their (profit / weight) ratio.• If the items are already arranged in the required order,then while loop takes 𝑂(𝑛) time.• The average time complexity of Quick Sort is 𝑂(𝑛 log 𝑛).• Therefore, total time taken including the sort is 𝑂(𝑛 log 𝑛).
  • 105.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(self practice)Example 2: For the given set of items and knapsack capacity = 60 kg,find the optimal solution for the fractional knapsack problem making useof greedy approach.Item Weight ProfitA 1 5B 3 9C 2 4D 2 8
  • 106.
    Greedy Algorithm• Problem5: Knapsack ProblemFractional Knapsack Problem(self practice)Example 3: Find the optimal solution for the fractional knapsackproblem making use of greedy approach. Consider-n = 3M = 20 kg(w1, w2, w3) = (18, 15, 10)(p1, p2, p3) = (25, 24, 15)

[8]ページ先頭

©2009-2025 Movatter.jp