Commit a0d30126 authored by 刘乐's avatar 刘乐

1,有向图算法

parent 6e13faf4
......@@ -23,9 +23,9 @@ void CivConTrackTest::test(char* uri)
char result[1024 * 32];
char sn[32];
strcpy_s(sn, "JD00000680");
strcpy_s(sn, "JD00000016");
int succ = trackingSimulation(uri, sn, 3, result);
int succ = trackingSimulation(uri, sn, 5, result);
std::cout << result << std::endl;
std::cout << "测试结果:" << succ << std::endl;
......
......@@ -24,12 +24,12 @@ int main(int argc, char* argv[])
context.contextTest(findUri);*/
// 测试追踪分析
/*CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);*/
CivTestContext context(new CivConTrackTest());
context.contextTest(findUri);
// 测试根据时段获取值
CivTestContext context(new CivConGetDataTest());
context.contextTest(findUri);
/* CivTestContext context(new CivConGetDataTest());
context.contextTest(findUri);*/
// 测试根据
/* CivTestContext context(new CivConGetRptTest());
......
#pragma once
/**
ͼ
*/
template<class Item>
class CivDGraph
{
public:
};
#include "CivGraphList.h"
#include "CivPgConn.h"
#include "CivDbConn.h"
template <class VertexType, class EdgeType>
ALGraph<VertexType, EdgeType>::ALGraph()
{
}
template <class VertexType, class EdgeType>
void ALGraph<VertexType, EdgeType>::createGraph(CivDbConn* dbConn)
{
if (dbConn == nullptr)
return;
}
template <class VertexType, class EdgeType>
ALGraph<VertexType, EdgeType>::~ALGraph()
{
for (auto iter = m_vertexArray.begin(); iter != m_vertexArray.end(); iter++)
{
Edge<weight>* p = iter->pAdjEdges;
while (NULL != p)
{
iter->pAdjEdges = p->pNextEdge;
delete p;
p = iter->pAdjEdges;
}
}
if (!m_vertexArray.empty())
{
m_vertexArray.clear();
}
}
template <class VertexType, class EdgeType>
bool ALGraph<VertexType, EdgeType>::insertAVertex(IN const VertexType& vertexName)
{
Vertex<VertexType, EdgeType> VertexInstance(vertexName, NULL);
m_vertexArray.push_back(VertexInstance);
return true;
}
template <class VertexType, class EdgeType>
bool ALGraph<VertexType, EdgeType>::insertAEdge(IN const VertexType vertexName1,
IN const VertexType vertexName2, IN const EdgeType edgeWeight)
{
int v1 = getVertexIndex(vertexName1);
if (-1 == v1)
{
cerr << "There is no vertex 1" << endl;
return false;
}
int v2 = getVertexIndex(vertexName2);
if (-1 == v2)
{
cerr << "There is no vertex 2" << endl;
return false;
}
Edge<weight>* p = m_vertexArray.at(v1).pAdjEdges;
while (p != NULL && p->nDestVertex != v2)
{
p = p->pNextEdge;
}
if (NULL == p)
{
p = new Edge<weight>(v2, edgeWeight, m_vertexArray.at(v1).pAdjEdges);
m_vertexArray.at(v1).pAdjEdges = p;
return true;
}
if (v2 == p->nDestVertex)
{
Edge<weight>* q = p;
p = new Edge<weight>(v2, edgeWeight, q->pNextEdge);
q->pNextEdge = p;
return true;
}
return false;
}
template <class VertexType, class EdgeType>
bool ALGraph<VertexType, EdgeType>::removeAEdge(IN const VertexType& vertexName1,
IN const VertexType& vertexName2, IN const EdgeType& edgeWeight)
{
int v1 = getVertexIndex(vertexName1);
if (-1 == v1)
{
cerr << "There is no vertex 1" << endl;
return false;
}
int v2 = getVertexIndex(vertexName2);
if (-1 == v2)
{
cerr << "There is no vertex 2" << endl;
return false;
}
Edge<EdgeType>* p = m_vertexArray.at(v1).pAdjEdges;
Edge<EdgeType>* q = NULL;
while (p != NULL && p->nDestVertex != v2)
{
q = p;
p = p->pNextEdge;
}
if (NULL == p)
{
cerr << "Edge is not found" << endl;
return false;
}
while (edgeWeight != p->edgeWeight && p->nDestVertex == v2)
{
q = p;
p = p->pNextEdge;
}
if (v2 != p->nDestVertex)
{
cerr << "Edge is not found" << endl;
return false;
}
q->pNextEdge = p->pNextEdge;
delete p;
return true;
}
template <class VertexType, class EdgeType>
EdgeType ALGraph<VertexType, EdgeType>::getEdgeWeight(IN const Edge<EdgeType>* pEdge)
{
return pEdge->edgeWeight;
}
template <class VertexType, class EdgeType>
void ALGraph<VertexType, EdgeType>::getVertexEdgeWeight(IN const int v1, OUT vector<EdgeType>& DistanceArray)
{
Edge<weight>* p = m_vertexArray.at(v1).pAdjEdges;
int prevIndex = -1;
weight tmp;
while (NULL != p)
{
//consider the same edges exist
if (prevIndex == p->nDestVertex)
{
if (tmp > p->edgeWeight)
{
DistanceArray[prevIndex] = p->edgeWeight;
}
}
else
{
DistanceArray[p->nDestVertex] = p->edgeWeight;
prevIndex = p->nDestVertex;
tmp = p->edgeWeight;
}
p = p->pNextEdge;
}
}
template <class VertexType, class EdgeType>
EdgeType ALGraph<VertexType, EdgeType>::getMinWeight(IN const VertexType vertexName1,
IN const VertexType vertexName2)
{
Edge<EdgeType>* pEdge = NULL;
int v1 = getVertexIndex(vertexName1);
if (-1 == v1)
{
cerr << "There is no vertex 1" << endl;
return false;
}
int v2 = getVertexIndex(vertexName2);
if (-1 == v2)
{
cerr << "There is no vertex 2" << endl;
return false;
}
Edge<EdgeType>* p = m_vertexArray.at(v1).pAdjEdges;
while (p != NULL && p->nDestVertex != v2)
{
p = p->pNextEdge;
}
if (NULL == p)
{
pEdge = NULL;
return weight(0);
}
EdgeType tmp = getEdgeWeight(p);
pEdge = p;
while (NULL != p && v2 == p->nDestVertex)
{
if (tmp > getEdgeWeight(p))
{
tmp = getEdgeWeight(p);
pEdge = p;
}
p = p->pNextEdge;
}
return tmp;
}
template <class VertexType, class EdgeType>
int ALGraph<VertexType, EdgeType>::getVertexIndex(IN const VertexType vertexName)
{
for (int i = 0; i < m_vertexArray.size(); i++)
{
if (vertexName == getData(i))
{
return i;
}
}
return -1;
}
template <class VertexType, class EdgeType>
int ALGraph<VertexType, EdgeType>::getVertexNumber()
{
return m_vertexArray.size();
}
template <class VertexType, class EdgeType>
VertexType ALGraph<VertexType, EdgeType>::getData(IN int index)
{
return m_vertexArray.at(index).vertexName;
}
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, weight(INT_MAX));
vecDistanceArray[sourceIndex] = weight(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 = weight(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 = m_vertexArray[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.m_vertexArray.at(i).pAdjEdges;
while (NULL != p)
{
out << "(" << x1 << "," << graphInstance.getData(p->nDestVertex) << "," << p->edgeWeight << ") ";
p = p->pNextEdge;
}
out << endl;
}
return out;
}
#pragma once
#include <iostream>
#include <cstdio>
#include <vector>
#define IN
#define OUT
#define INOUT
using namespace std;
class CivDbConn;
/**
*@brief 边结构体模板
*@param weight 权重类型
*/
template <class EdgeType>
struct Edge
{
int nDestVertex; // 目标节点个数
EdgeType edgeObj; // 边的属性值对象
Edge<EdgeType>* pNextEdge; // 连接下一条边
Edge(int d, weight c, Edge<EdgeType>* p = NULL)
:nDestVertex(d), edgeObj(c), pNextEdge(p)
{}
};
/**
*@brief 节点结构体模板
*@param vertextype 节点类型
*@param weight 权重类型
*/
template <class VertexType, class EdgeType>
struct Vertex
{
VertexType mVertex;
Edge<EdgeType>* pAdjEdges;
Vertex(VertexType x, Edge<EdgeType>* p = NULL)
:mVertex(x), pAdjEdges(p)
{}
};
/**
基于邻接表的图
*/
template <class VertexType, class EdgeType>
class ALGraph
{
public:
explicit ALGraph();
~ALGraph();
/**
*@brief 根据数库管网表生成图数据
*/
void createGraph(CivDbConn* dbConn);
public:
/**
*@brief 往图中插入一个节点
*@param vertexName 节点值
*/
bool insertAVertex(IN const VertexType& vertexName);
/**
*@brief 两个节点之间插入一条边
*@param vertexName1 节点对象1
*@param vertexName2 节点对象2
*@param edge 边
*/
bool insertAEdge(IN const VertexType vertexName1, IN const VertexType vertexName2, IN const EdgeType edge);
/**
*@brief 移除两个节点的边
*@param vertexName1 节点对象1
*@param vertexName2 节点对象2
*@param edge 边
*/
bool removeAEdge(IN const VertexType& vertexName1, IN const VertexType& vertexName2, IN const EdgeType& edgeWeight);
/**
*@brief 获取两个节点之间连接的最小权重值
*@param vertexName1 节点1
*@param vertexName2 节点2
*/
EdgeType getMinWeight(IN const VertexType vertexName1, IN const VertexType vertexName2);
/**
*@brief 获取两个节点之间连接的最小权重值
*@param vertexName1 节点1
*@param vertexName2 节点2
*/
int getVertexIndex(IN const VertexType vertexName);
int getVertexNumber();
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);
public:
EdgeType getEdgeWeight(IN const Edge<EdgeType>* pEdge);
void getVertexEdgeWeight(IN const int v1, OUT vector<EdgeType>& DistanceArray);
vector< Vertex<VertexType, EdgeType> > m_vertexArray;
};
......@@ -81,6 +81,8 @@
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>..\..\hModelProgram\Program\</OutDir>
<IncludePath>$(OUTDIR)..\include;$(IncludePath)</IncludePath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
......@@ -130,19 +132,24 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<Optimization>Disabled</Optimization>
<AdditionalIncludeDirectories>$(OUTDIR)..\include;$(SolutionDir)pandaDbManager;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>pandaDbManager.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalLibraryDirectories>$(OUTDIR)..\lib</AdditionalLibraryDirectories>
<ImportLibrary>$(OutDir)..\lib\$(TargetName).lib</ImportLibrary>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="CivGraph.h" />
<ClInclude Include="CivGraphList.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivGraph.cpp" />
<ClCompile Include="CivGraphList.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
......
......@@ -15,12 +15,12 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CivGraph.h">
<ClInclude Include="CivGraphList.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="CivGraph.cpp">
<ClCompile Include="CivGraphList.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
......
......@@ -317,6 +317,11 @@ void CivNewInp::setQuality(const CivQuality& quality)
writeStringFormat(table.ID);
if (mQualityType == ANALYSIS_TRACE)
{
if (table.ID == mQualityName)
{
writeString("1");
continue;
}
writeString("0");
}
else
......
......@@ -4,6 +4,7 @@
#include <vector>
class CivDbConn;
#define CACHEEXPORT __declspec(dllexport)
// 节点
typedef struct
......
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