Program work 10. AOE Network

来源:互联网 发布:荣威350软件 编辑:程序博客网 时间:2024/06/05 10:29

需求:

        给定一个AOE network.  AOE network是一副有向图, 每个顶点代表事件, 每条边代表一个活动, 每条边的活动代表这个活动完成所需要的时间.

对于给定的一个AOE network, 我们定义起点和终点, 然后需要计算起点到终点每个事件最晚开始时间和最早开始时间. 还有关键活动是哪些. 

具体何为AOE network可自行百度.


Program environment:

Operation System: Ubuntu 14.04

Ide: Eclipse

Compiler: g++

Language: c++

Requirements: 

For a given AOE network matrix, print out its early events times, late events times, early activities times, late activities times and critical path.

Ideas:

ee[j] = max {ee[i] + activity of <i, j>}

le[j] = min {le[i] - activity of <i, j>}

For an activity<k, l>:

e[j] = ee[k]

l[j] = le[l] - activity<k, l>

Implementation is easy with formulas known, especially when we use array implementation rather than adjacency lists. 

Because topological sorting is not needed anymore. 

To calculate the early event times, we just need to scan the aoe matrix from the beginning to the end, while late event times is from the end to the beginning. 

For activities time, we just scan the aoe matrix again and do the formulas for each non zero activity.

Critical path has one property that early time and late time of a vertex is equal.

Input & Output:

“graph.txt” is for test.

Input is a file with n * n matrix, n is the number of vertices. Non zero element represents the duration for an activity.

Output contains several lines: 

1st. Early events time; 

2nd. Late events time; 

3rd. Early activities time;

4th. Late activities time; 

5th. Critical path.

//============================================================================// Name        : program.cpp// Author      : Reid Chan// Version     :// Copyright   : Your copyright notice//============================================================================#include <iostream>#include <fstream>#include <cstdlib>using namespace std;int **act;// the data from file/** * read the file's data into 2d arrays. */void init(int &events, int &activities) {/***** calculate the row number of the file which means the number of events *****/ifstream infile;string name;// loop until the file is exist.while (true) {cout << "enter the aoe file's name: ";cin >> name;infile.open(name.c_str());if (!infile) {cout << "invalid file name." << endl;} elsebreak;}string line;while (getline(infile, line)) {events++;}infile.close();/***** read data from file, act[][] is the aoe network, any non zero element is an activity *****/infile.open(name.c_str());act = new int *[events];for (int i = 0; i < events; i++) {act[i] = new int[events];}string temp;for (int i = 0; i < events; i++) {for (int j = 0; j < events; j++) {infile >> temp;int activity = atoi(temp.c_str());act[i][j] = activity;if (activity != 0) {activities++;}}}infile.close();}/** * calculation of early events times: * ee[j] = max {ee[i] + duration of <i, j>} */void calculate_ee(int *ee, int events) {for (int i = 0; i < events; i++) {for (int j = 0; j < events; j++) {if (act[i][j] != 0 && ee[i] + act[i][j] > ee[j]) {ee[j] = ee[i] + act[i][j];}}}}/** * calculation of late events time: * le[j] = min {le[i] - duration of <i, j>} */void calculate_le(int *le, int *ee, int events) {int len = events - 1;le[len] = ee[len];for (int i = len; i >= 0; --i) {for (int j = len; j >= 0; --j) {if (act[i][j] != 0 && le[j] - act[i][j] < le[i]) {le[i] = le[j] - act[i][j];}}}}/** * calculation of early & late activity time: * for an activity<k, l> * e[i] = ee[k] * l[i] = le[l] - act[k][l] */void calculate_e_and_l(int *e, int *l, int *ee, int *le, int events) {int k = 0;for (int i = 0; i < events; i++) {for (int j = 0; j < events; j++) {if (act[i][j] != 0) {e[k] = ee[i];l[k++] = le[j] - act[i][j];}}}}/** * if ee[i] == le[i] * then vertex i is critical. */void critical_path(int *e, int *l, int activities) {cout << "Critical path: ";for (int i = 0; i < activities; ++i) {if (e[i] == l[i]) {cout << "V" << i << ' ';}}cout << endl;}void print(int *arr, int len, string txt) {cout << txt;for (int i = 0; i < len; i++) {cout << arr[i] << ' ';}cout << endl;}int main() {int events = 0, activities = 0;init(events, activities);int *ee = new int[events], *le = new int[events];for (int i = 0; i < events; ++i) {ee[i] = 0;le[i] = 99999;}calculate_ee(ee, events);calculate_le(le, ee, events);int *e = new int[activities], *l = new int[activities];calculate_e_and_l(e, l, ee, le, events);cout << "Events:" << endl;print(ee, events, "early time of all events: ");print(le, events, "late time of all events: ");cout << "Activities:" << endl;print(e, activities, "early time of all activities: ");print(l, activities, "late time of all activities: ");critical_path(ee, le, events);return 0;}


0 0
原创粉丝点击