0% found this document useful (0 votes)
8 views37 pages

DAA Unit 3 Part 2

The document discusses various algorithms for solving shortest path problems in weighted directed graphs, including Dijkstra's and Bellman-Ford algorithms, highlighting their differences, particularly in handling negative weights. It also covers the Knapsack Problem, detailing its types and solution approaches, such as dynamic programming and greedy algorithms. Additionally, the document introduces Graham's Scan for convex hull problems and Strassen’s algorithm for efficient matrix multiplication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views37 pages

DAA Unit 3 Part 2

The document discusses various algorithms for solving shortest path problems in weighted directed graphs, including Dijkstra's and Bellman-Ford algorithms, highlighting their differences, particularly in handling negative weights. It also covers the Knapsack Problem, detailing its types and solution approaches, such as dynamic programming and greedy algorithms. Additionally, the document introduces Graham's Scan for convex hull problems and Strassen’s algorithm for efficient matrix multiplication.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Single Source Shortest Paths

Shortest Path Algorithms


• In a shortest- paths problem, we are given a weighted, directed
graphs G = (V, E), with weight function w: E → R mapping edges to
real-valued weights. The weight of path p = (v0,v1,..... vk) is the total of
the weights of its constituent edges:

• We define the shortest - path weight from u to v by


• δ(u,v) = min (w (p): u→v), if there is a path from u to v,
• δ(u,v)= ∞, otherwise.

• The shortest path from vertex s to vertex t is then defined as any path
p with weight w (p) = δ(s,t).
Dijkstra's algorithm
Dijkstra's algorithm solves the single-source shortest-paths problem on a
weighted, directed graph G = (V, E) for the case in which all edge weights
are nonnegative.
Therefore, we assume that w(u, v) ≥ 0 for each edge (u, v) E.
Dijkstra's algorithm
Where Relaxation is Used
• Dijkstra’s Algorithm: Repeatedly relaxes edges of vertices with the shortest known
distance, leveraging a priority queue for efficiency.
• Bellman-Ford Algorithm: Systematically relaxes all edges up to V−1 times (where V is
the number of vertices) to handle negative weight edges and detect negative cycles.
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Dijkstra's algorithm - Example
Bellman-Ford Algorithm
• Bellman ford algorithm is a single-source shortest path algorithm.
• This algorithm is used to find the shortest distance from the single vertex to all the other
vertices of a weighted graph.
• There are various other algorithms used to find the shortest path like Dijkstra algorithm,
etc. If the weighted graph contains the negative weight values, then the Dijkstra algorithm
does not confirm whether it produces the correct answer or not.
• In contrast to Dijkstra algorithm, bellman ford algorithm guarantees the correct answer
even if the weighted graph contains the negative weight values.
Bellman-Ford Algorithm - Example
Bellman-Ford Algorithm - Example
Knapsack Problem
• The Knapsack Problem is a classic optimization problem in computer science and
combinatorial optimization.
• It’s named after the scenario of a traveler who has a knapsack (backpack) with a limited
capacity and must decide which items to take to maximize the total value without
exceeding this capacity.
Types of Knapsack Problems
1.0/1 Knapsack Problem:
• Each item can either be taken as a whole (1) or left behind (0). You cannot take
fractional parts of any item.
• Objective: Maximize the total value of items in the knapsack without exceeding its
capacity.
• Solution Approach: Dynamic Programming is typically used, as the 0/1 Knapsack is
NP-complete.
Knapsack Problem
2. Fractional Knapsack Problem:
• Unlike the 0/1 version, here you can take fractions of items. This means if you can't fit the
whole item, you can take a part of it.
• Objective: Maximize the total value by taking whole items or parts of items, as long as the
knapsack’s weight capacity isn’t exceeded.
• Solution Approach: A greedy algorithm is optimal for the fractional knapsack problem.
3. Bounded Knapsack Problem:
•Here, each item has a limited quantity that you can take (e.g., you can take at most 3 of item A,
2 of item B, etc.).
•Solution Approach: Dynamic Programming, with modifications for handling the bounded
quantities of each item.
4. Unbounded Knapsack Problem:
•In this variation, you can take as many copies of each item as you want (unlimited quantity for
each item).
•Solution Approach: Dynamic Programming, using an adaptation that allows repetition of
items.
Knapsack Problem
Fractional Knapsack Problem
The weights (Wi) and profit values (Pi) of the items to be added in the knapsack are taken
as an input for the fractional knapsack algorithm and the subset of the items added in the
knapsack without exceeding the limit and with maximum profit is achieved as the output.

Algorithm
• Consider all the items with their weights and profits mentioned respectively.
• Calculate Pi/Wi of all the items and sort the items in descending order based on their
Pi/Wi values.
• Without exceeding the limit, add the items into the knapsack.
• If the knapsack can still store some weight, but the weights of other items exceed the
limit, the fractional part of the next time can be added.
• Hence, giving it the name fractional knapsack problem.
Fractional Knapsack Problem
• For the given set of items and the knapsack capacity of 10 kg, find the subset of the items
to be added in the knapsack such that the profit is maximum.
Fractional Knapsack Problem
Fractional Knapsack Problem
Fractional Knapsack Problem
Convex hull
• The convex hull of a set of points in a Euclidean space is the smallest convex polygon
that encloses all of the points.
Graham’s Scan
• Graham's scan solves the convex-hull problem by maintaining a stack S of candidate
points.
• Each point of the input set Q is pushed once onto the stack, and the points that are not
vertices of CH(Q) are eventually popped from the stack.
• When the algorithm terminates, stack S contains exactly the vertices of CH(Q), in
counterclockwise order of their appearance on the boundary.
• The procedure GRAHAM-SCAN takes as input a set Q of points, where |Q| ≥ 3.
• It calls the functions TOP(S), which returns the point on top of stack S without changing S,
and NEXTTO-TOP(S), which returns the point one entry below the top of stack S without
changing S.
Graham’s Scan
Graham’s Scan
Graham’s Scan
Graham’s Scan
Matrix Multiplication Algorithm
• Strassen’s algorithm is an efficient, divide-and-conquer method for multiplying two n×n
times n×n matrices that reduces the number of multiplications required compared to the
conventional approach.
• By cleverly calculating matrix products and using only seven recursive multiplications,
Strassen’s algorithm achieves a complexity of, which is an improvement
over the complexity of traditional matrix multiplication.

Problem Statement
• Given two n×n matrices A and B, compute their product C=A×B
Matrix Multiplication Algorithm
Matrix Multiplication Algorithm
Matrix Multiplication Algorithm - Example
Matrix Multiplication Algorithm - Example
Matrix Multiplication Algorithm - Example
Matrix Multiplication Algorithm - Example
Matrix Multiplication Algorithm - Example

You might also like