Commit 81b040f6 authored by 刘乐's avatar 刘乐

1,追踪分析接口

parent 29f1f6e6
......@@ -8,13 +8,12 @@ CivGraphFactory::CivGraphFactory()
}
bool CivGraphFactory::createGraph(CivDbConn* dbConn, ALGraph<CivGraphJunction, CivGraphEdage>* graph)
ALGraph<CivGraphJunction, CivGraphEdage>* CivGraphFactory::createGraph(CivDbConn* dbConn)
{
if (dbConn == nullptr)
return false;
if (graph == nullptr)
return false;
ALGraph<CivGraphJunction, CivGraphEdage>* graph = new ALGraph<CivGraphJunction, CivGraphEdage>();
// 顶点坐标
CivCoordinates coords;
......@@ -56,5 +55,5 @@ bool CivGraphFactory::createGraph(CivDbConn* dbConn, ALGraph<CivGraphJunction,
graph->insertAEdge(sStartjuction, endJunction, edAge);
}
return true;
return graph;
}
\ No newline at end of file
......@@ -16,5 +16,5 @@ public:
*@param dbConn 数据库连接
*@param graph 有向图
*/
bool createGraph(CivDbConn* dbConn, ALGraph<CivGraphJunction, CivGraphEdage>* graph);
ALGraph<CivGraphJunction, CivGraphEdage>* createGraph(CivDbConn* dbConn);
};
......@@ -10,7 +10,7 @@ ALGraph<VertexType, EdgeType>::ALGraph()
}
template <class VertexType, class EdgeType>
void ALGraph<VertexType, EdgeType>::BFS(VertexType vertexName, int visit[])
void ALGraph<VertexType, EdgeType>::BFS(VertexType vertexName, std::vector<int>& visited)
{
// 获取输入顶点索引
int vi = getVertexIndex(vertexName);
......@@ -20,27 +20,29 @@ void ALGraph<VertexType, EdgeType>::BFS(VertexType vertexName, int visit[])
return false;
}
int vi = 0;
Edge<EdgeType>* edageNext;
// 初始化队列
std::queue<int> Q;
Q.push(node);
visit[vi] = 1;
visited.push_back(vi);
while (!Q.empty()) {
int node = Q.front();
Q.pop();
edageNext = mVertexArray.at(node)..pAdjEdges;
edageNext = mVertexArray.at(node).pAdjEdges;
while (edageNext)
{
// 未访问过的放入队列
if (!visit[edageNext->nDestVertex])
int pVal = edageNext->nDestVertex;
auto iter = std::find(visited.begin(), visited.end(), pVal);
if (iter == visited.end())
{
visit[edageNext->nDestVertex] = 1;
Q.push(edageNext->nDestVertex);
visited.push_back(pVal);
Q.push(pVal);
}
// 后移
......@@ -49,6 +51,53 @@ void ALGraph<VertexType, EdgeType>::BFS(VertexType vertexName, int visit[])
}
}
template <class VertexType, class EdgeType>
Edge<EdgeType>*
ALGraph<VertexType, EdgeType>::reverseList(Edge<EdgeType>* edge)
{
Edge<EdgeType>* resultList = new Edge<EdgeType>(0, EdgeType());
result->pNextEdge = edge;
Edge<EdgeType>* p = edge;
Edge<EdgeType>* pNext = p->pNextEdge;
while (pNext != nullptr)
{
p->pNextEdge = pNext->pNextEdge;
pNext->pNextEdge = resultList->pNextEdge;
resultList->pNextEdge = pNext;
pNext = p->pNextEdge;
}
Edge<EdgeType>* node = resultList->pNextEdge;
delete resultList;
return node;
}
template <class VertexType, class EdgeType>
void ALGraph<VertexType, EdgeType>::graphReverse()
{
for (int i = 0; i < mVertexArray.size(); i++)
{
Vertex<VertexType, EdgeType> vetex = mVertexArray.at(i);
Edge<EdgeType>* p = vetex.pAdjEdges;
Edge<EdgeType>* pNext = p->pNextEdge;
while (pNext!=nullptr)
{
p->pNextEdge = pNext->pNextEdge;
pNext->pNextEdge = vetex.pAdjEdges;
vetex.pAdjEdges = pNext;
pNext = p->pNextEdge;
}
}
return;
}
template <class VertexType, class EdgeType>
ALGraph<VertexType, EdgeType>::~ALGraph()
......@@ -185,7 +234,7 @@ void ALGraph<VertexType, EdgeType>::getVertexEdgeWeight(IN const int v1, OUT vec
while (NULL != p)
{
//consider the same edges exist
// 存在相同的边
if (prevIndex == p->nDestVertex)
{
if (tmp > p->edgeWeight)
......@@ -270,115 +319,4 @@ template <class VertexType, class EdgeType>
VertexType ALGraph<VertexType, EdgeType>::getData(IN int index)
{
return mVertexArray.at(index).mVertex;
}
template <class VertexType, class EdgeType>
int ALGraph<VertexType, EdgeType>::Dijkstra(IN const VertexType& vertexName1)
{
int sourceIndex = getVertexIndex(vertexName1);
if (-1 == sourceIndex)
{
cerr << "There is no vertex " << endl;
return false;
}
int nVertexNo = getVertexNumber();
//the array to record the points have been included, if included the value is true
//else is false
vector<bool> vecIncludeArray;
vecIncludeArray.assign(nVertexNo, false);
vecIncludeArray[sourceIndex] = true;
//the array to record the distance from vertex1
vector<EdgeType> vecDistanceArray;
vecDistanceArray.assign(nVertexNo, EdgeType(INT_MAX));
vecDistanceArray[sourceIndex] = EdgeType(0);
//prev array to record the previous vertex
vector<int> vecPrevVertex;
vecPrevVertex.assign(nVertexNo, sourceIndex);
getVertexEdgeWeight(sourceIndex, vecDistanceArray);
int vFrom, vTo;
while (1)
{
EdgeType minWeight = EdgeType(INT_MAX);
vFrom = sourceIndex;
vTo = -1;
for (int i = 0; i < nVertexNo; i++)
{
if (!vecIncludeArray[i] && minWeight > vecDistanceArray[i])
{
minWeight = vecDistanceArray[i];
vFrom = i;
}
}
if (EdgeType(INT_MAX) == minWeight)
{
break;
}
vecIncludeArray[vFrom] = true;
Edge<EdgeType>* p = mVertexArray[vFrom].pAdjEdges;
while (NULL != p)
{
EdgeType wFT = p->edgeWeight;
vTo = p->nDestVertex;
if (!vecIncludeArray[vTo] && vecDistanceArray[vTo] > wFT + vecDistanceArray[vFrom])
{
vecDistanceArray[vTo] = wFT + vecDistanceArray[vFrom];
vecPrevVertex[vTo] = vFrom;
}
p = p->pNextEdge;
}
}
//print the shortest route of all vertexes
for (int i = 0; i < nVertexNo; i++)
{
if (EdgeType(INT_MAX) != vecDistanceArray[i])
{
cout << getData(sourceIndex) << "->" << getData(i) << ": ";
DijkstraPrint(i, sourceIndex, vecPrevVertex);
cout << " " << vecDistanceArray[i];
cout << endl;
}
}
return 0;
}
template <class VertexType, class EdgeType>
void ALGraph<VertexType, EdgeType>::DijkstraPrint(IN int index, IN int sourceIndex, IN vector<int> vecPreVertex)
{
if (sourceIndex != index)
{
DijkstraPrint(vecPreVertex[index], sourceIndex, vecPreVertex);
}
cout << getData(index) << " ";
}
template <class VertexType, class EdgeType >
ostream& operator<<(OUT ostream& out, IN ALGraph<VertexType, EdgeType>& graphInstance)
{
int vertexNo = graphInstance.getVertexNumber();
out << "This graph has " << vertexNo << "vertexes" << endl;
for (int i = 0; i < vertexNo; i++)
{
VertexType x1 = graphInstance.getData(i);
out << x1 << ": ";
Edge<EdgeType>* p = graphInstance.mVertexArray.at(i).pAdjEdges;
while (NULL != p)
{
out << "(" << x1 << "," << graphInstance.getData(p->nDestVertex) << "," << p->edgeWeight << ") ";
p = p->pNextEdge;
}
out << endl;
}
return out;
}
\ No newline at end of file
......@@ -34,7 +34,7 @@ struct Edge
*@param VertexType 节点信息
*@param EdgeType 弧信息
*/
template <class VertexType, class EdgeType>
template <class VertexType, class EdgeType>
struct Vertex
{
VertexType mVertex; // 顶点信息
......@@ -56,9 +56,14 @@ public:
explicit ALGraph();
~ALGraph();
void BFS(VertexType vetexType, std::vector<int>& visited);
/**
*@brief 图反向
*/
void graphReverse();
void BFS(VertexType vetexType, int visit[]);
Edge<EdgeType>* reverseList(Edge<EdgeType>* edge);
/**
*@brief 往图中插入一个节点
......@@ -107,18 +112,13 @@ public:
*/
VertexType getData(IN int index);
int Dijkstra(IN const VertexType& vertexName1);
void DijkstraPrint(IN int index, IN int sourceIndex, IN vector<int> vecPreVertex);
friend ostream& operator<<(OUT ostream& out, IN const ALGraph<VertexType, EdgeType>& graphInstance);
private:
bool isReverse = false;
EdgeType getEdgeWeight(IN const Edge<EdgeType>* pEdge);
void getVertexEdgeWeight(IN const int v1, OUT vector<EdgeType>& DistanceArray);
vector<Vertex<VertexType, EdgeType> > mVertexArray; // 节点数组
vector<Vertex<VertexType, EdgeType>> mVertexArray; // 节点数组
};
......
......@@ -17,6 +17,10 @@ bool CivGraphJunction::operator== (const CivGraphJunction& edage)
return mSN == edage.mSN;
}
CivGraphEdage::CivGraphEdage()
{
}
CivGraphEdage::CivGraphEdage(std::string sn, std::string length, std::vector<std::string> position)
{
mSN = sn;
......
......@@ -41,6 +41,7 @@ public:
*@param length 管长
*@param position 注意顺序,[length, startx, starty, endx, endy]
*/
explicit CivGraphEdage();
explicit CivGraphEdage(std::string sn, std::string length, std::vector<std::string> position);
bool operator== (const CivGraphEdage& edage);
......
#include "CivTrackingAnalysis.h"
#include "CivGraphFactory.h"
CivTrackingAnalysis::CivTrackingAnalysis():mGraph(nullptr)
{
}
CivTrackingAnalysis::~CivTrackingAnalysis()
{
delete mGraph;
}
bool CivTrackingAnalysis::createGraphFrom(CivDbConn* dbConn)
{
if (dbConn == nullptr)
return false;
CivGraphFactory factory;
mGraph = factory.createGraph(dbConn);
if (mGraph == nullptr)
return false;
return true;
}
bool CivTrackingAnalysis::upstreamTracking(const std::string& sN, std::string& jsonResult)
{
return true;
}
bool CivTrackingAnalysis::downstreamTracking(const std::string& sN, std::string& jsonResult)
{
return true;
}
bool CivTrackingAnalysis::waterSupplyScopeAnalysis(const std::string& sN, std::string& jsonResult)
{
return true;
}
bool CivTrackingAnalysis::upstreamTracking(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes)
{
return true;
}
bool CivTrackingAnalysis::downstreamTracking(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes)
{
if (mGraph == nullptr)
return false;
CivGraphJunction junction(sN);
std::vector<int> visited;
mGraph->BFS(junction, visited);
return true;
}
bool CivTrackingAnalysis::waterSupplyScopeAnalysis(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes)
{
return true;
}
\ No newline at end of file
#pragma once
#include "CivHydDataType.h"
#include "CivGraphList.h"
#include <vector>
#define ALGORITHMEXPORT __declspec(dllexport)
class CivDbConn;
class ALGORITHMEXPORT CivTrackingAnalysis
{
public:
explicit CivTrackingAnalysis();
~CivTrackingAnalysis();
/**
*@brief 根据数据库管网构建管网有向图
*@param dbConn 数据库连接
*/
bool createGraphFrom(CivDbConn* dbConn);
/**
*@brief 追踪节点上游路径
*@param sN 节点本点号
*@param jsonResult 追踪的结果json字符串
*/
bool upstreamTracking(const std::string& sN, std::string&jsonResult);
/**
*@brief 追踪节点下游路径
*@param sN 节点本点号
*@param jsonResult 追踪的结果集字符串
*/
bool downstreamTracking(const std::string& sN, std::string& jsonResult);
/**
*@brief 计算管网中某个水源所能供水的范围
*@param sN 水源的编号
*@param junctions jsonResult 结果字符串
*/
bool waterSupplyScopeAnalysis(const std::string& sN, std::string& jsonResult);
private:
/**
*@brief 追踪节点上游路径
*@param sN 节点本点号
*@param junctions 追踪到的节点集合
*@param pipes 追踪到的管段集合
*/
bool upstreamTracking(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes);
/**
*@brief 追踪节点下游路径
*@param sN 节点本点号
*@param junctions 追踪到的节点集合
*@param pipes 追踪到的管段集合
*/
bool downstreamTracking(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes);
/**
*@brief 计算管网中某个水源所能供水的范围
*@param sN 水源的编号
*@param junctions 可以供水的节点
*@param pipes 可以供水的管段
*/
bool waterSupplyScopeAnalysis(const std::string& sN,
std::vector<CivGraphJunction>& junctions,
std::vector<CivGraphEdage>& pipes);
private:
ALGraph<CivGraphJunction, CivGraphEdage>* mGraph; // 管网有向图
};
......@@ -149,11 +149,13 @@
<ClInclude Include="CivGraphFactory.h" />
<ClInclude Include="CivGraphList.h" />
<ClInclude Include="CivHydDataType.h" />
<ClInclude Include="CivTrackingAnalysis.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivGraphFactory.cpp" />
<ClCompile Include="CivGraphList.cpp" />
<ClCompile Include="CivHydDataType.cpp" />
<ClCompile Include="CivTrackingAnalysis.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -24,6 +24,9 @@
<ClInclude Include="CivGraphFactory.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CivTrackingAnalysis.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivGraphList.cpp">
......@@ -35,5 +38,8 @@
<ClCompile Include="CivGraphFactory.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CivTrackingAnalysis.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment