0% found this document useful (0 votes)
21 views21 pages

Unit 3 & Half Unit 4 DS

Uploaded by

sushain16kaushal
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)
21 views21 pages

Unit 3 & Half Unit 4 DS

Uploaded by

sushain16kaushal
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/ 21

Auyilary olv)with þvioity olv*) beeaue

s e aase I efient for T is Builable


Hindins Shortest o indiag shost
|estþats between
singe Soune all badys of
Verhces.
Topelogical Sort C
he topological Aoyt
alsoithm teke a diretd
lt gaph Ond retuYns an array ot the
acylic
nodes wwhere eahnode apßeays before
ale the nodes IE Boins to. The
ordeing o the nodey tn. the aryls
Called topo kagial ordeing
Topolagit Lorting dor Directed
of yezhces buek het doy every direte
edye u- v, verten Comes
y"n the ordeing.
Topolopdeal soring of afsh
i6 not þomiblef he
Diruked Auyli shDau)
The rót. Vextes in topololcae
a Verte witah simplihtd Asoihiyi
Jorng ot o a vextex u h mo
fuhe the indezrsa att
edees). A topologlcal
noning edye). Sortiy Shejpl;
as there Co lected
Drder imot Unisue Steb 2 nd
a Verty Uin the jpt maximum
benore than fopolagicak.
one gap h uh am, i's
or agph. anindesree
ìn
?ero Add A vertex
in the orden set. S and T.
tub 2 Remove he VestUand alis edsu
ly the
Cu,v) rom Hae ities,

with nexice ayd


wit Sef4 Repeat stef
Create a gaph Haeye Cre Vericei
m sum of
fo be þroLened. e graph
mdireted edfes.
|Thhal'3e a stauk and a vsited
3.förea Unvisited vextexn he
i
phy do the ollouing funion
B

wita
V7
Hhe Vertex as the parameter.
’ Jn the Des unon, martk of vestiet E9
he vertex as vibited and Commbute thein deree o the
erkvely eall the 3s meto
fox ale onvised nlshbors p the Here, indepee
verte
once ale the neishhors have DVes the
been wisitedpsh the vexter onto will be aA minimum

Ae all, Veshces hare ben visitdy


Ethe
denee , o Soadk
S- The esulin st he Here, in
&orted o det ot the gsh
Rennove Va cndia edges Bom the
9nph. Then the nuw ga~h will be.
(Ve'

Here, in deree Vy>0, io add Vy at

sel. Taws topodogical ordeing set


Vy
Renove Vy and is
the reph. Then the mcw gsash

Hence oine pomibie topadaical


topelafee
Adanty9
Toþola_ical Sort ng ls malnty wed
oy Aheduling obs trom the Given
dependenies Camong joks. Tn omute
senie applicatons ot tus type
laise n inittion schedeliy
Oxdein_ bi foYmula ele evluahoy
when eompung tom wla Velus
n speadsheets, "lagte byntesi,
deteymininy the oYler of complahon
determ
teses seialgion. nd
yesolving
nke. symbol defndenis in
Maxlow ford- tulkerson alçoi m
Max flow- Min Cut

Minimum Cut in a Graph


In general, a cut is a set of edges whose removal divides a connected
graph into two disjoint subsets. There are two variations of a cut: maximum
cut and minimum cut. Before discussing the max-flow min-cut theorem, it's
important to understand what a minimum cut is.
Let's assume a cut CT(S,T) partition the vertex setV into two sets and T.
The net flow (S,T) of acut CTST) can be defined as the sum of
flow euU) where uvare twO nodes V,and uE S,VET. Similarly the
capacityCluw) of a cut CT(S)is the sum of the individual capacities,
where ,V are two nodes eV, and ueS, VET:
The minimum cut of a weighted graph is defined as the minimum sum of
weights of edges that, when removed from the graph, divide the graph
into two sets.
CUT 4

E11
3 V5
V1 E8
3
E5 3
E2 3
5
V3 E7 V7
E1 V3
E4 3
2 E6
E9
2
E3 V6
V2 4
E10
CUT 3
CUT 2

CUT 1

is an example ofa minimum cut. It removes the


Here in this graph, CUT3
sum of weights of these two edges are minimum
edges E& and E1, and the
graph.
among allother cuts in this
Maximum Flow in a Graph
Formally. given a graph helV, a flow in C, is a
vector -(,,
Kirchhoff law is2, 3---
verified (Law)eRin
of
a way that in every
vertex ¢& the
conservationaof flow at nodes).
Kirchhoffs law, the sum of the flow Accordingto
equal tothe sum of the flow comingentering
out of it.
node (or a vertex) should be
Let's consider this graph:

3
X1
X2

2 1

-2
X4 X3

Here all edges representaflow value, so the set or vector of flows in this
graph is
=(3, 3, -2, 2,)
These flow values satisfy the Kirchhoff law. For %, the sum of flows equal
to3for the outgoing edges. Similarly, the sum of flows equal to +=3 for
the incoming edges of x, which also equals the previously computed sum.
This can be checkedfor the other vertices.
Flowin a network or a graph follows some properties. In aflow graph, there
aretwo special vertices: source S and sink P. Each edge (u) in theflow
neWork has a capacity c(u)0,A flow in agraph Gis a
Aunction VV’R and it satisfies a capacity constraint: for each
edge (uv), fl4)<cuy) Net flow in the edges follows skew-symmetric
propert u ) - f ( , u)
Amaximum flow is defined as the maximum amount of flow that the
araph or network would allow to flow from the source node to its sink
node.

Max-FlowMin-Cut Theorem
The max-flow min-cut theorem states that the maximum flow through
any network from a given source to a given sink is exactly equal to the
minimum sum of acut. This theorem can be verified using the Ford
Fulkerson algorithm. This algorithm finds the maximum flow of a network or
graph.
Let's define the max-flow min-cut theorem formally. Let G=(X,A) be a flow
network, da flow on G. The value of the maximum flow from the source S to
the sink is equal tothe capacity of the minimum cut CT separating Sand P:

Mar( )= Min(e(C7))
Ford-Fulkerson Algorithm for Maximum Flow
Problem
The Ford-Fulkerson algorithm is a widely used algorithm to solve the
maximum flow problem in a flow network. The maximum flow problem
involves determiningthe maximum amount of flow that can be sent from a
Source vertex to a sink vertex in a directed weighted graph, subject to
capacity constraints on the edges.
The algorithm works by iteratively finding an augmenting path, which is a
path from the source to the sink in the residual graph, i.e., the graph
obtained by subtracting the current flowfrom the capacity of each edge. The
alaorithm then increases the flow along this path by
annount,which is the minimum capacity of the the maximum possible
Problem:
edges along the path.
Given a graph which represents a flow
capacity. Also, given two vertices sourcenetwork where every edge has a
's' and sink 't in the graph, find the
maximum possible flow fromsto t with the following constraints:
Flowon an edge doesn't exceed the given
capacity of the edge.
Incoming flow is equal to outgoing flow for every
For example, consider the following graph vertex exXcept s and t.
12 Source: 0
Sink: 5
16 20

10 4 7

13 4

14

The maximum possible flow in the above graph is 23.


12
Maximum Flow
19 in the above
graph is 23
1 7

12 4

11

Ford-Fulkerson Algorithm
The following is simple idea of Ford-Fulkerson algorithm:
1. Start with initial flow as 0.
2. While there exists an augmenting path from the source tothe sink:
Find an augmenting path using any path-finding algorithm, such as
breadth-first search or depth-first search.
" Determine the amount of flow that can be sent along the augmenting
path, which is the minimum residual capacity along the edges of the
path.
Increase the flow along the augmenting path by the
amount. determined
3. Return themaximum flow.

Time Complexity: Time complexity of the above algorithm is O(max flow *


E), We run a loop while there is an augmenting path. In worst
case, we may
add 1 unit flow in every iteration. Therefore the time complexity becomes
O(max_ flow * E).
Dynamic Programming or DP
DP is an algorithmic
mathematics to solvetechnique
complex
used in computer science and
problems by breaking them down into
smaller overlapping subproblems.
The core idea behind DP is to
is solved only once. store solutions to subproblems so that each
Tosolve DP problems, we
first write a recursive
there are overlapping solution in a way that
subproblems in the
function is called with same paramaters recursion tree (the recursive
To make sure that a multiple times)
recursive
time taken by algorithm), we storevalue is computed only once (to improve
There are two ways to store
results of the recursive calls.
and other is bottom up (or the results, one is top down (or memoization)
tabulation).
Some popular problems solved using DP are
Utility (Longest Common Fibonacci Numbers, Diff
Subsequence),
Path, Floyd Warshall, Edit Distance and Bellman-Ford Shortest
Matrix Chain Multiplication.
Backtrackingalgorithms are like problem-solving strategies that help
explore differentoptions to find the best
different paths and if one doesn't work, theysolution. They work by trying out
they find the right one. It's like solving a backtrack and try another until
until they fit together perfectly. puzzle by testing different pieces
What is Backtracking Algorithm?
Backtracking is a problem-solving algorithmic technique that involves
finding a solution incrementally by trying
options and undoing them if they leadtodifferent
a
It is commonly used in situations where you dead end.
possibilities to solve a problem, like searchingneed to explore multiple
for apath in a maze or solving
puzzles like Sudoku. When a dead end is reached, the
tothe previous decision point and explores a different path algorithm backtracks
found or all possibilities have been exhausted. untila solution is

How Does a Backtracking Algorithm Work?


A backtracking algorithm works by recursively exploring all
possible
solutions to aproblem. It starts by choosing an initial solution, andthen it
explores all possible extensions of that solution. If an extension leads to a
solution, the algorithm returns that solution. If an extension does not lead to
a solution, the algorithm
different extension. backtracks to the previous solution and
The following is a general tries a
outline of how a
1. Choose an initial solution.
Fxplore all
possible backtracking algorithm works:
a f an
extension leads tO
extensions
a
of the current
solution.
A1f an extension does
not solution, return that solution.
solution and try a differentlead to a solution, backtrack to the
5, Repeat steps 2-4 until all extension. previous
possible solutions have been
explored.
When to Use a
Backtracking BacktrackingAlgorithm?
algorithms are best used to solve
following characteristics: problems that have the
There are multiple possible
The problem can be
broken
solutions to the problem.
The down intosmaller subproblems.
subproblems can be solved independently.
Applications of Backtracking Algorithm
Backtracking algorithms are used in a wide variety of
Solving puzzles (e.g., Sudoku, crossword
Finding the shortest path puzzles) applications, including:
through a maze
Scheduling problems
Resource allocation problems
Network optimization problems
orCombinatorial problems, such as generating
subsets
permutations, combinations,
What is the difference
between Backtracking and Recursion?
SI,
No.
Recursion
Backtracking
1 Recursion does not always need
Backtracking always uses recursion to
backtracking solve problems
sI.
No. Recursion
A
recursive function solves a Backtracking
2 particular problem by calling a copy of
itself and solving Backt
thoseracking at every
choices that step eliminates
of the smaller subproblems solution and cannot give us the
original problems. thathave the proceeds to those
potential of taking choices
us to the
solution.
3 Recursion isa part of
itself and it is simplerbacktracking
to write. Backtracking is comparatively complex to
implement.
Applications
Graph
ofrecursion are
Tree and
4 Traversal, Towers of Hanoi, Application of Backtracking is NQueen
Divide and Conquer
Algorithms,
Merge Sort,Quick Sort, problem, Rat in aMaze problem,
and Binary Tour Problem,Sudoku Knight's
Search. solver, and Graph
coloring problems.

What ls Design
Methodology?
Design methodology refers to the
for a unique situation. development of a system or method
Today, the term is most often applied to
technological fields in reference to web design,software or
systems design. Various degree programs involve information
design methodology,
including those in the graphic and digital arts.
Travelling
Programming Salesman Problem using Dynamic
ravelling Salesman Problem (TSP):
Given a set of cities and the
shortestdistance
problem is to find the
once and returns to the possible between every pair of cities, the
route that visits every city
starting point. Note
between Hamiltonian Cycle the difference exactly
and
TSP. The
that visits every cityHamiltonian cycle problem is to
find if there exists a tour
t
Hamiltonian Tour
onysuch tours exist,exists (because the exactly once. Here we know
graph is
Cycle.
the problem is to complete) and in fact,
finda minimum weight Hamiltonian

20
10
15

25 30

35

For exanmple, consider the graph shown in the figure on the


tour in the graph is 1-2-4-3-1. The cost of the tour is right side.ATSP
80. The problem is a famous NP hard
problem.
10+25+30+15 which is
know solution for this problem. The following are There is no polynomial-time
traveling salesman problem. different solutions for the
Naive Solution:
1) Consider city 1 as the
2) Generate all (n-1)! starting andof ending point.
3) Calculate the Permut
cost everyations cities.and
of
cOst permutation.
4) Return the
permutation with
permutation keep track of the
Time Complexity: ?(n!) minimum cost. minimum
Dynamic
at Programming:
the aiven set of
and ending pointof vertices be {1,2, 3,
he minimum
cost output. 1Forevery other 4,..n}. Let us consider 1 as
all vertices path with as the vertex I(other than 1), westarting
cost of the appearing exactly once. Let starting
the point, I as the ending find
corresponding
the distance from I Cycle cost of this path cost (i),point, and
would cost (i)
1))values. This to 1. Finally, we return the + dist(i, 1) where dist(i, 1)the
and
Now the questionlooks simple so far. minimum of all [cost(i) + is
is howto get dist(i,
Dynamic Programming, cost(i)? To
we need to have calculate the cost() using
sub-problems.
Let us define a term sonme recursive relation in
terms of
each vertex in set SC(S, )be the cost of the minimum cost
all subsets of size 2 exactly once, starting at 1 and path visiting
and
subset, then we calculatecalculate C(S, i) for all ending at i. We start with
that 1must be C(S, i) for all subsets subsets
S of sizewhere
S is the
If size of S is present in every
2, then S mustsubset.
3 and so on. Note
C(S, i) = dist(1, i) be {1, i},
Else if size of S is
C(s, i) = min { greater than 2.
i and j != 1. c(s-{i}, j) + dis(j, i)} where j
Below is the dynamic belongs to S, j !=
programming solution for the problem using topdown
recursivetmemoizedsubsets
approach:
For maintaining the
we can use the
remaining nodes in our bitmasks to
represent the
only few nodes in graph,subset. Since bits are faster to operate and there are
For example: bitmasks is better to use.
10100represents node 2 and node 4 are
010010represents node 1 and 4 are left inleft in set to be processed
Time Complexity : O(n2"2n) where O(n* 2n)subset.
are maximum number of unique
subproblems/states
every states.
and O(n)for transition (through for loop as in code) in
Auxiliary Space: O(n*2n),where n is number of Nodes/Cities here.
For a set of size n, we consider n-2
subsets don't have nth in them. Usingsubsets each of size n-1 such that all
the above recurrence relation, we can
writeadynamic
subproblems and
istherefore
exponential. programming-based
each one takes solution.
O(n2*2n).
The spaceThe time linear timeis toThere
requiredcompl
are at most
is exity muchsolve. The total O(n2n)
less than O(n'runni
o1 Knapsack Problem
also
exponential. ) butngstilltime
Given N items
and also given where
a bag each item has some
with weight and profit
most W
weight in it. The capacity W, {i.e., the
profits associated with to put the itemsbag can holdassoci
task is
at ated with it
Sum of
Note: The them is the into the bag such
constraint
bag or cannot here
put it at all [lt is can either maxi
is We
put m um possible. that the
bag]. not possible to putan item
a completely into the
Examples: part of an item into
| =3, W= 4,
Input: N
the
Output: 3 profit]={1, 2, 3},
Explanation: There are two
weight[] ={4, 5, 1}
If we select the
item
items which have weight
with
the item with weight 1, weight 4, the possible profit isless than or equal to 4.
1. And if we select
profit is 3. Note that wethe possible profit is 3. So the maximum
together as the capacitycannot put both the possible
of the bag is 4. items with weight 4 and 1
Input: N =3, W= 3, profit[] ={1, 2, 3},
Output: 0 weight[] = {4, 5, 6)
There are two types of knapsack problems:
o 0/1 knapsack problem
o Fractional knapsack problem

What is the O/1 knapsack problem?


The 0/1 knapsack problem means that the items are either completely or no items are filled
ina knapsack. For example, we have two items having weights 2kg and 3kg, respectively. If
we pick the 2kg item then we cannot pick 1kg item from the 2kg item (item is not divisible):
either we
we have to pick the 2kg item completely. This is a O/1 knapsack problem in which solved
problem is by
pick the item completely or we will pick that item. The O/1 knapsack
the dynamic programming.
The0/1 Knapsack Problem

Rules:

Fvery item has a weight and value.


Your knapsack has a weight limit.
Choose which items you want to bring with you in
Vou can either take an item or not, the knapsack.
you cannot take half of an item for
example.

Goal:
Maximize the total value of the items in the knapsack.

What is the fractional knapsack problem?


The fractional knapsack problem means that we can divide the item. For example, we have
an item of 3 kg then we can pick the item of 2 kg and leave the item of 1 kg. The fractional
knapsack problem is solved by the Greedy approach.

Example of 0/1 knapsack problem.


Consider the problem having weights and profits are:
Weights: (3, 4, 6, 5}
Profits: (2, 3, 1, 4}
The weight of the knapsack is 8 kg
The number of items is 4

method:
The above problem can be solved by using the following

X={1,0, 0, 1}
={0, 0, 0, 1}

={0, 1, 0, 1}
the possible combinations. 1 denotes that the item is completely picked and
The above are items so pOssible combinations will be:
item is picked. Since there are 4
Omeans that no
made by using the above
are 16 possible combinations that can be
2*= 16; So. There the combination that
Once all the combinations are made, we have to select
problem.
provides the maximum profit.
programming approach. In
Another approach to solve the problem is dynamicproblem is divided into sub-
programming approach, the complicated
dynamic sub-problem and the solution of the sub-
find the solution of a
problems, then we
problem will be used to
find the solution of a complex problem.

Multistage Graph (Shortestgraph Path)whichthe nodes can be


directed, weighted
a
in
Multistage graph is
stages such that all edges are from a stage to next stage
set of stage and
same
dividedinto a words there is no edge between vertices of
other
only(Invertex of current stage to previous stage). number of disjoint
are divided into n
from a
multistage graph the source and Sn is
the
To vertices of a S2, S3 Sn }, where S1 is i.e.,|S1 =
subsets S =(S1, The cardinality of S1 and Sn are equalto 1.
destination ).
sink(
and adestination, we need to find
/Sn= 1.
multistage graph, a source source
Me are aiven a destination. By convention, we consider
source to
shortest path from as last stage.
atstage 1 and destination
we willconsider in this article :
graph
Following is anexample

18

9
13
5
2

16
Source:0
Destination : 7
Shortest
Path :
0-3-6-7

strategies we can apply : between Source and


there are various possible paths
Now method of finding all
minimum.That's the
WORST possible
The Brute force finding the
Destination and then
shortest paths. This method will
strategy. of Single Source which is notrequired in
Dijkstra's Algorithm
from source to all other nodes even use the SPECIAL
shortest paths doesn't
find
So it will take alot of time and it
this case. MULTI-STAGE graph has.
feature that this
simple
Greedy Method- At each node, choose the shortest outgoing
Iwe apply this approach to the example graph given above we get
+ 4 + 18 =23. But a quick look at the graph will show
path.
solution as 1
the
shorterpaths available than 23. So the greedy method fails !
much
option is Dynamic Programming. So we need to find Optimal
The best
Sub-structure, Recursive Equations and Overlapping Sub-problems.
Complexity : The time complexity of the given code is O(N^2), where
Time number of nodes in the graph. This is because the code involvestwo
Nisthe that iterate over all pairs of nodesin the graph, and each
nestedloops updating
performs a constant amount of work (i.e., comparing and
iteration
Since the graph is represented using an adjacency matrix,
ietances). constant time. Therefore, the overall
time
eessing an element takes
O(N^2).
complexity of the algorithm is
O(N),
complexity of the given program isprogram
Snace Complexity : The space because the
nodes in the graph. This is
where N is the number of store the shortest distance from each node to the
to
Uses an array of size N
destination node N-1.
All-Pairs Shortest Paths
u. Storing all the
from each vertex V to every other spanning tree for
path need one
outthe shortest expensive indeed, as weconsumption, so these are
It aims to figure very memory
be memory
paths explicitly can often impractical regarding problems, which aim to find just the
This is distance tabular form: the
each vertex. all pairs-shortest We usually want the output in
considered as
generally another. from u to v.
each node to the shortest path
distance from each to column should be the weight of
v's
entry in u's row and
for improvement:
Three approaches
Cost
Algorithm
O (v logV)
Matrix Multiplication
oV)
Floyd-Warshall
(V logV+VE)
Johnson O
Theallpairshortest path algorithm is also known as Floyd-Warshall algorithm is
wsedto find all pair shortest path problem from a given weighted graph. As a
result of this algorithm, it will generate a matrix, which will represent the
minimumdistance from any node to all other nodes in the graph.

|0 1 -3 2 -4
3 (0 -4 | -|
7 4 0 5 3
2
2 -1 -5 0 -2
8 5 1 6 0

matrix of the graph. After that


At first the output matrix is same as given cost
k as the intermediate vertex.
the output matriX will be updated with all vertices
O(V3), here V is the number of vertices
The time complexity of this algorithm is
in the graph.
graph.
Input - The cost matrix of the

036 o 00 0
3021 00
620142co
Co 1102 o 4
0 0 42 021
o 00 2co 201
o00 oo 4 110

shortest path.
Output - Matrix of all pair

0345677
3021344
4201323
5110233
633202I
742320|
743311I0
Algorithm
floydW arshal(cost)
Input - The cost matrix of given Graph.

Output - Matrix to for shortest path between any vertex to any vertex.

Begin
for k:=0 to n,do
for i :=0to n, do
for j :-0 to n, do
if cost[i,k] + cost[kj] < cost[i.j], then
cost[ij] = cost[i.k] + cost[kj]
done
done
done
display the current cost matrix
End
8queen problem
problem of placing eight queens on an 8x8
The eight queens problem is thethem
chessboard such that none of attack one another (no two are in the
generally, the n queens problem
same row, column, or diagonal). More There are different solutions for the
chessboard.
places n queens on an nxn
problem.
Explanation: backtracking algorithm to find a solution to
This pseudocode uses a
consists of placing 8 queens on a
the 8Queen problem, which queens threaten each other.
chessboard in such a way that no tWO then it
starts by placing a queen on the first column,
The algorithm
column and places aqueen in the first safe row of
proceeds to the next
that column.
If the algorithm
reaches the 8th column and al queens are placed in a
safe position, it prints the board and returns true.
If the alqorithm is unable to place a queen in asafe position in a certain
column, it backtracks to the previous column and triesa different row.
isSafe" function checks if it is safe to place aqueen on a certain row
The
andcolumn by checking if there are any queens in the same row,
diagonal or anti-diagonal.
It'sworth to notice that this is just a high-level pseudocode and it might
and
need to be adapted depending on the specific implementation
language you are using.
Complexity O((m q) log^2 n)
Time
Space Complexity : O((m + q) log n)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy