Answer on Question #61018, Programming & Computer Science / C++
TCS is working on a new project called "TestVita". There are N modules in the project. Each module (i) has completion time denoted in number of hours (Hi) and may depend on other modules. If Module x depends on Module y then one needs to complete y before x.
As Project manager, you are asked to deliver the project as early as possible. Provide an estimation of amount of time required to complete the project.
Solution:
Let – all modules, – corresponding completion times, if depends on module , if doesn’t depend (directly) on module .
Let – the main module of the project. Our task is to find all modules on which this depends (directly or indirectly through sequence of some other modules) and then find the sum of corresponding .
Let's mark all modules by , where if that module is necessary for project, if not.
Solution algorithm:
1. Let , all other .
2. Loop through all pairs . If module depends on module () AND module marked as 1 () then change mark to 1.
3. If in step 2 at least one mark is changed, then go to step 2 and repeat it. If no is changed then proceed to step 4.
4. Now all modules necessary for project is marked by 1 ( for them). Loop through all and sum such for which .
In C++ this algorithm can be coded for example in such way:
P[k] = 1;
Changed = 1;
while (Changed)
{
Changed = 0;
for (i=0; i<N; i++)
for (j=0;j<N;j++)
if ((F[i][j] == 1) && (P[i] == 1))
{
Changed=1;
P[j] = 1;
}
}
for (i=0, sum=0; i<N; i++)
if (P[i] == 1) sum += H[i];
cout << "Time to complete: " << sum;http://www.AssignmentExpert.com