Skip to content

Latest commit

 

History

History

Given a Directed Graph and a ratio R , Where each edge of the graph will contain both Profit and loss value , Is it possible to find such cycles where the ratio of sum of profit and loss will be strictly greater than R

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
  • PREREQUISITE : Bellman-Ford

  • EXPLANATION :

Suppose There is a cycle of 'E' edges which follows above condition that means,

where Pi represents Profit of edge and Li represent loss of each edge.
(P1 + P2 +P3 +... PE)/(L1 + L2 + L3 +... LE) > R

=> (P1 + P2 + P3 + ... PE) > R(L1 + L2 + L3 + ... LE)

=> P1 - RL1 + P2 - RL2 + P3 - RL3 + ... + PE - RLE > 0

=> 1<= i <= E Pi - RLi > 0

So that it Means, We need to find a positive weighted cycle where each edge of the Cycle will represent Pi - RLi . It is the exact oppsite of Bellman ford algorithm . That's Why we will take values of Pi as negative value and Li as positive value . Basically we will mark each edge as
-Pi+RLi and then Find negative cycle via Bellman ford algoithm .

  • RELATED PROBLEM :

Light OJ 1221