-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathgraph.cpp
More file actions
77 lines (65 loc) · 1.77 KB
/
Copy pathgraph.cpp
File metadata and controls
77 lines (65 loc) · 1.77 KB
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
/* EPANET 3
*
* Copyright (c) 2016 Open Water Analytics
* Distributed under the MIT License (see the LICENSE file for details).
*
*/
//////////////////////////////////////////
// Implementation of the Graph class. //
//////////////////////////////////////////
// TO DO:
// - add network connectivity checking to support diagnostics.cpp
// - add a topolgical sort routine to support qualengine.cpp
#include "graph.h"
#include "Core/network.h"
#include "Elements/link.h"
#include "Elements/node.h"
#include <vector>
using namespace std;
//-----------------------------------------------------------------------------
// Constructor/Destructor
Graph::Graph()
{
}
Graph::~Graph()
{
}
//-----------------------------------------------------------------------------
void Graph::createAdjLists(Network* nw)
{
try
{
int nodeCount = nw->count(Element::NODE);
int linkCount = nw->count(Element::LINK);
adjLists.resize(2*linkCount, -1);
adjListBeg.resize(nodeCount+1, 0);
vector<int> degree(nodeCount, 0);
for (Link* link : nw->links)
{
degree[link->fromNode->index]++;
degree[link->toNode->index]++;
}
adjListBeg[0] = 0;
for (int i = 0; i < nodeCount; i++)
{
adjListBeg[i+1] = adjListBeg[i] + degree[i];
degree[i] = 0;
}
int m;
for (int k = 0; k < linkCount; k++)
{
int i = nw->link(k)->fromNode->index;
m = adjListBeg[i] + degree[i];
adjLists[m] = k;
degree[i]++;
int j = nw->link(k)->toNode->index;
m = adjListBeg[j] + degree[j];
adjLists[m] = k;
degree[j]++;
}
}
catch (...)
{
throw;
}
}