-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDSA.php
118 lines (102 loc) · 3.18 KB
/
DSA.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
// PHP program to fin maximum cash
// flow among a set of persons
// Number of persons (or vertices in the graph)
$N = 3;
// A utility function that returns
// index of minimum value in arr[]
function getMin($arr)
{
global $N;
$minInd = 0;
for ($i = 1; $i < $N; $i++)
if ($arr[$i] < $arr[$minInd])
$minInd = $i;
return $minInd;
}
// A utility function that returns
// index of maximum value in arr[]
function getMax($arr)
{
global $N;
$maxInd = 0;
for ($i = 1; $i < $N; $i++)
if ($arr[$i] > $arr[$maxInd])
$maxInd = $i;
return $maxInd;
}
// A utility function to return minimum of 2 values
function minOf2($x, $y)
{
return ($x < $y)? $x: $y;
}
// amount[p] indicates the net amount
// to be credited/debited to/from person 'p'
// If amount[p] is positive, then i'th
// person will amount[i]
// If amount[p] is negative, then i'th
// person will give -amount[i]
function minCashFlowRec($amount)
{
// Find the indexes of minimum and
// maximum values in amount[]
// amount[mxCredit] indicates the
// maximum amount to be given
// (or credited) to any person .
// And amount[mxDebit] indicates the
// maximum amount to be taken
// (or debited) from any person.
// So if there is a positive value in
// amount[], then there must
// be a negative value
$mxCredit = getMax($amount);
$mxDebit = getMin($amount);
// If both amounts are 0, then
// all amounts are settled
if ($amount[$mxCredit] == 0 &&
$amount[$mxDebit] == 0)
return;
// Find the minimum of two amounts
$min = minOf2(-$amount[$mxDebit], $amount[$mxCredit]);
$amount[$mxCredit] -= $min;
$amount[$mxDebit] += $min;
// If minimum is the maximum amount to be
echo "Person ".$mxDebit." pays ".$min." to Person ".$mxCredit."\n";
// Recur for the amount array. Note
// that it is guaranteed that the
// recursion would terminate as
// either amount[mxCredit]
// or amount[mxDebit] becomes 0
minCashFlowRec($amount);
}
// Given a set of persons as graph[]
// where graph[i][j] indicates the
// amount that person i needs to
// pay person j, this function finds
// and prints the minimum cash flow
// to settle all debts.
function minCashFlow($graph)
{
global $N;
// Create an array amount[],
// initialize all value in it as 0.
$amount=array_fill(0, $N, 0);
// Calculate the net amount to be
// paid to person 'p', and stores
// it in amount[p]. The value of
// amount[p] can be calculated by
// subtracting debts of 'p' from
// credits of 'p'
for ($p = 0; $p < $N; $p++)
for ($i = 0; $i < $N; $i++)
$amount[$p] += ($graph[$i][$p] - $graph[$p][$i]);
minCashFlowRec($amount);
}
// Driver code
// graph[i][j] indicates the amount
// that person i needs to pay person j
$graph = array(array(0, 1000, 2000),
array(0, 0, 5000),
array(0, 0, 0));
// Print the solution
minCashFlow($graph);