游客[注册]
|
登录
|
中文
|
English
IT技术博客
IT技术博客,精选各种精华文章供您阅读,是您学习各种IT技术的博客优选之地
秋色园
首页
管理
Rss
图的简单实现
编辑
2011/6/16 10:01:56
中文系列
评论
(0)
浏览
(1892)
const
int
MAXSIZE
=
50
;
//
顶点最大数目
#include
<
vector
>
using
namespace
std;
template
<
typename T
>
class
CGraph
{
public
:
CGraph(
void
);
~
CGraph(
void
);
private
:
vector
<
T
>
vecNodes;
//
顶点列表
int
edge[MAXSIZE][MAXSIZE];
//
边表
int
numVertexs;
//
顶点数
int
numEdges;
//
边数
bool
visited[MAXSIZE];
//
用于图的遍历
int
FindVertex(
const
T
&
vertex,
const
vector
<
T
>
&
lst);
void
ClearVisitFlag();
vector
<
T
>&
GraphDepthFirstSearch(
const
T
&
beginVertex);
//
深度遍历图
vector
<
T
>&
GraphBreadthFirstSearch();
//
广度遍历
public
:
bool
GraphEmpty(
void
)
const
;
bool
GraphFull(
void
)
const
;
int
NumberOfVertices(
void
)
const
;
//
获取顶点数
int
NumberOfEdges(
void
)
const
;
//
获取边数
int
GetWeight(
const
T
&
vertex1,
const
T
&
vertex2);
//
获取指定两个顶点间的权值
vector
<
T
>&
GetNeighbors(
const
T
&
vertex);
//
获取指定顶点的邻接顶点
void
CreateGraph();
//
创建图
int
GetVertexPos(
const
T
&
vertex);
//
获取指定顶点的位置
int
InsertVertex(
const
T
&
vertex);
//
插入顶点
void
InsertEdge(
const
T
&
vertex1,
const
T
&
vertex2,
int
weight);
//
插入边
void
DeleteVertex(
const
T
&
vertex);
//
删除顶点
void
DeleteEdge(
const
T
&
vertex1,
const
T
&
vertex2);
//
删除边
//
int MinimumPath(const T& sVertex,const T& desVertex);最短路径
void
DepthFirstSearch();
//
深度遍历图
void
BreadthFirstSearch();
//
广度遍历图
}
;
图的实现代码
#include
"
Graph.h
"
#include
<
stack
>
#include
<
queue
>
#include
<
algorithm
>
#include
<
iostream
>
using
namespace
std;
template
<
typename T
>
CGraph
<
T
>
::CGraph(
void
)
{
for
(
int
i
=
0
;i
<
MAXSIZE;
++
i)
{
for
(
int
j
=
0
;j
<
MAXSIZE;
++
j)
{
this
->
edge[i][j]
=
0
;
}
}
this
->
numVertexs
=
0
;
this
->
numEdges
=
0
;
}
template
<
typename T
>
CGraph
<
T
>
::
~
CGraph(
void
)
{
this
->
vecNodes.clear();
for
(
int
i
=
0
;i
<
MAXSIZE;
++
i)
{
for
(
int
j
=
0
;j
<
MAXSIZE;
++
j)
{
this
->
edge[i][j]
=
0
;
}
}
this
->
numVertexs
=
0
;
this
->
numEdges
=
0
;
}
template
<
typename T
>
int
CGraph
<
T
>
::NumberOfEdges()
const
{
return
this
->
numEdges;
}
template
<
typename T
>
int
CGraph
<
T
>
::NumberOfVertices()
const
{
return
this
->
numVertexs;
}
template
<
typename T
>
int
CGraph
<
T
>
::GetWeight(
const
T
&
vertex1,
const
T
&
vertex2)
{
int
pos1,pos2;
pos1
=
this
->
GetVertexPos(vertex1);
pos2
=
this
->
GetVertexPos(vertex2);
return
this
->
edge[pos1][pos2];
}
template
<
typename T
>
bool
CGraph
<
T
>
::GraphFull()
const
{
return
this
->
numVertexs
>=
MAXSIZE;
}
template
<
typename T
>
bool
CGraph
<
T
>
::GraphEmpty()
const
{
return
this
->
numVertexs
==
0
;
}
template
<
typename T
>
int
CGraph
<
T
>
::InsertVertex(
const
T
&
vertex)
{
//
插入顶点,返回插入位置
int
result
=-
1
;
if
(
!
this
->
GraphFull())
{
this
->
vecNodes.push_back(vertex);
result
=
this
->
numVertexs;
this
->
numVertexs
++
;
}
}
template
<
typename T
>
void
CGraph
<
T
>
::InsertEdge(
const
T
&
vertex1,
const
T
&
vertex2,
int
weight)
{
//
插入边
int
pos1,pos2;
pos1
=
this
->
GetVertexPos(vertex1);
pos2
=
this
->
GetVertexPos(vertex2);
if
(pos1
==-
1
&&
pos2
==-
1
)
{
//
两个都是新顶点
pos1
=
this
->
InsertVertex(vertex1);
pos2
=
this
->
InsertVertex(vertex2);
}
else
if
(pos1
==-
1
)
{
//
一个是新顶点
pos1
=
this
->
InsertVertex(vertex1);
}
else
if
(pos2
==-
1
)
{
pos2
=
this
->
InsertVertex(vertex2);
}
this
->
edge[pos1][pos2]
=
weight;
this
->
edge[pos2][pos1]
=
weight;
}
template
<
typename T
>
void
CGraph
<
T
>
::DeleteVertex(
const
T
&
vertex)
{
//
删除顶点
int
pos
=
this
->
GetVertexPos(vertex);
if
(pos
!=-
1
)
{
this
->
vecNodes.erase(remove(
this
->
vecNodes.begin(),
this
->
vecNodes.end(),vertex),
this
->
vecNodes.end());
//
删除顶点
for
(
int
i
=
0
;i
<
this
->
numVertexs;
++
i)
{
if
(
this
->
edge[pos][i]
>
0
)
this
->
edge[pos][i]
=
0
;
if
(
this
->
edge[i][pos]
>
0
)
this
->
edge[i][pos]
=
0
;
}
this
->
numVertexs
--
;
this
->
numEdges
--
;
}
}
template
<
typename T
>
void
CGraph
<
T
>
::DeleteEdge(
const
T
&
vertex1,
const
T
&
vertex2)
{
//
删除边
int
pos1,pos2;
pos1
=
this
->
GetVertexPos(vertex1);
pos2
=
this
->
GetVertexPos(vertex2);
if
(pos1
!=-
1
&&
pos2
!=-
1
)
{
if
(
this
->
edge[pos1][pos2]
>
0
)
{
this
->
edge[pos1][pos2]
=
0
;
this
->
edge[pos2][pos1]
=
0
;
this
->
numEdges
--
;
}
}
}
template
<
typename T
>
void
CGraph
<
T
>
::CreateGraph()
{
//
创建图
cout
<<
"
输入顶点数,边数:
"
;
cin
>>
this
->
numVertexs;
cin
>>
this
->
numEdges;
int
i;
T item;
cout
<<
"
输入顶点:
"
;
for
(i
=
0
;i
<
this
->
numVertexs;
++
i)
{
cout
<<
"
第
"
<<
i
+
1
<<
"
个顶点:
"
;
cin
>>
item;
this
->
vecNodes.push_back(item);
}
T vert1,vert2;
int
pos1,pos2,weight;
for
(i
=
0
;i
<
this
->
numEdges;
++
i)
{
cout
<<
"
输入第
"
<<
i
+
1
<<
"
条边(尾,头,权值):
"
;
cin
>>
vert1
>>
vert2
>>
weight;
pos1
=
this
->
GetVertexPos(vert1);
pos2
=
this
->
GetVertexPos(vert2);
this
->
edge[pos1][pos2]
=
weight;
this
->
edge[pos2][pos1]
=
weight;
}
}
template
<
typename T
>
int
CGraph
<
T
>
::GetVertexPos(
const
T
&
vertex)
{
//
获取顶点位置
return
this
->
FindVertex(vertex,
this
->
vecNodes);
}
template
<
typename T
>
int
CGraph
<
T
>
::FindVertex(
const
T
&
vertex,
const
vector
<
T
>
&
lst)
{
//
在顶点列表中找指定数据的下标
int
pos
=
-
1
;
for
(pos
=
lst.size()
-
1
;pos
>=
0
;
--
pos)
{
if
(lst[pos]
==
vertex)
{
break
;
}
}
return
pos;
}
template
<
typename T
>
vector
<
T
>&
CGraph
<
T
>
::GraphDepthFirstSearch(
const
T
&
beginVertex)
{
//
深度遍历
vector
<
T
>
*
result
=
new
vector
<
T
>
();
vector
<
T
>
adjLst;
stack
<
T
>
s;
s.push(beginVertex);
int
pos;
T vertex;
vector
<
T
>
::iterator iter;
while
(
!
s.empty())
{
vertex
=
s.top();
s.pop();
if
(
this
->
FindVertex(vertex,
*
result)
==-
1
)
{
pos
=
this
->
GetVertexPos(vertex);
visited[pos]
=
true
;
(
*
result).push_back(vertex);
adjLst
=
this
->
GetNeighbors(vertex);
for
( iter
=
adjLst.begin();iter
!=
adjLst.end();
++
iter)
{
if
(
this
->
FindVertex(
*
iter,
*
result)
==-
1
)
{
s.push(
*
iter);
}
}
}
}
return
*
result;
}
template
<
typename T
>
void
CGraph
<
T
>
::ClearVisitFlag()
{
for
(
int
i
=
0
;i
<
this
->
numEdges;
++
i)
{
this
->
visited[i]
=
false
;
}
}
template
<
typename T
>
void
CGraph
<
T
>
::BreadthFirstSearch()
{
vector
<
T
>
::const_iterator iter;
vector
<
int
>
vec;
this
->
ClearVisitFlag();
vec
=
this
->
GraphBreadthFirstSearch();
for
(iter
=
vec.begin();iter
!=
vec.end();
++
iter)
{
cout
<<*
iter
<<
"
"
;
}
this
->
ClearVisitFlag();
}
template
<
typename T
>
vector
<
T
>&
CGraph
<
T
>
::GraphBreadthFirstSearch()
{
//
广度遍历
vector
<
T
>
*
result
=
new
vector
<
T
>
();
vector
<
T
>
adjLst;
vector
<
T
>
::iterator iter;
queue
<
T
>
q;
T item;
int
pos;
this
->
ClearVisitFlag();
for
(
int
i
=
0
;i
<
this
->
numVertexs;
++
i)
{
if
(
!
this
->
visited[i])
{
visited[i]
=
true
;
result
->
push_back(
this
->
vecNodes[i]);
q.push(
this
->
vecNodes[i]);
while
(
!
q.empty())
{
item
=
q.front();
q.pop();
adjLst
=
this
->
GetNeighbors(item);
for
( iter
=
adjLst.begin();iter
!=
adjLst.end();
++
iter)
{
if
(
this
->
FindVertex(
*
iter,
*
result)
==-
1
)
{
result
->
push_back(
*
iter);
pos
=
this
->
GetVertexPos(
*
iter);
visited[pos]
=
true
;
}
}
}
}
}
return
*
result;
}
template
<
typename T
>
vector
<
T
>&
CGraph
<
T
>
::GetNeighbors(
const
T
&
vertex)
{
//
获取邻接顶点
vector
<
T
>
*
result;
result
=
new
vector
<
T
>
();
int
pos
=
this
->
GetVertexPos(vertex);
if
(pos
==-
1
)
{
cerr
<<
"
顶点不在图中
"
<<
endl;
return
*
result;
}
for
(
int
i
=
0
;i
<
this
->
numVertexs;
++
i)
{
if
(edge[pos][i]
>
0
)
{
result
->
push_back(
this
->
vecNodes[i]);
}
}
return
*
result;
}
template
<
typename T
>
void
CGraph
<
T
>
::DepthFirstSearch()
{
vector
<
T
>
::const_iterator iter,iter2;
int
pos;
vector
<
int
>
vec1;
this
->
ClearVisitFlag();
for
(iter
=
this
->
vecNodes.begin();iter
!=
this
->
vecNodes.end();
++
iter)
{
pos
=
this
->
GetVertexPos(
*
iter);
if
(
!
visited[pos])
{
//
还未访问,从这点开始
vec1
=
this
->
GraphDepthFirstSearch(
*
iter);
for
(iter2
=
vec1.begin();iter2
!=
vec1.end();
++
iter2)
{
cout
<<*
iter2
<<
"
"
;
}
}
}
this
->
ClearVisitFlag();
}
测试程序:
#include
"
Graph.cpp
"
#include
<
iostream
>
using
namespace
std;
int
main(
int
argc,
char
*
argv[])
{
CGraph
<
int
>
*
graph1
=
new
CGraph
<
int
>
();
graph1
->
CreateGraph();
graph1
->
DepthFirstSearch();
graph1
->
BreadthFirstSearch();
system(
"
pause
"
);
return
0
;
}
关键字
图的简单实现
推荐.NET配套的通用数据层ORM框架:
CYQ.Data 通用数据层框架
发表评论
昵称
:
会员注册
公告信息
欢迎进入IT技术博客知识学堂~~
Email:cyq1162@126.com
文章搜索
文章分类
中文系列
(8504)
英文系列
(2370)
日语系列
(1192)
韩语系列
(135)
文章档案
2013-4
(1)
2011-12
(7)
2011-11
(23)
2011-9
(3)
2011-8
(834)
2011-7
(988)
2011-6
(1834)
2011-5
(1603)
2011-4
(735)
2011-3
(891)
2011-2
(688)
2011-1
(572)
2010-12
(283)
2010-11
(38)
最新文章
CSS3变形与动画效果示例:淡入淡出的transform
Orchard基础教程之样式及脚本
C# partial部分类解析
JQuery图表插件Highcharts示例教程
Centos6下FFMEPG截取flv视频缩略图与视频时间
.NET C#继承与派生类细解
Floyd算法的基本思想
ASP.NET FusionCharts Free报表工具实例介绍
Java FlatFileItemReader性能分析
幻灯展示jQuery插件supersized
最新评论
obviously like your web site however you need to take a look at the spelling on several of your posts. Many of them are rife with spelling problems and I in finding it very troublesome to tell the truth however I抣l certainly come back again.
おすすめ人気ブランド腕時計, 最高等級時計大量入荷! ◆N品質シリアル付きも有り 付属品完備! ☆★☆━━━━━━━━━━━━━━━━━━━☆★☆ 以上 宜しくお願い致します。(^0^) 広大な客を歓迎して買います!── (*^-^*)
ブランド偽物激安市場 ┣財布 ┣小物 ┣靴 ┣時計 ┗2024年 新作の展示 2024年ルイヴィトン全新登場 2024年超人気商品!随時更新 品質がよい、価格が低い、実物写真! 全物品運賃無料(日本全国) 不良品物情況、無償で交換します. 税関没収する商品は再度無料で発送します. 注文数量: 数量制限無し、一個の注文も、OKです 注意:注文は多くて、特恵は多いです 広大な客を歓迎して買います!
本スーパーコピー腕時計専売店は人気できて信頼できます。 主にオメガ腕時計、ウブロ腕時計スーパーコピー、エルメス腕時計、ガガミラノ腕時計、シャネル腕時計コピー、パネライコピー、ロレックス骨董品などブランド腕時計を販売します。本店は信用取引、質量保証、格安、オンタイムデリバリーで、お客様の需求を満足させ、お客様の利益のために努力します。 このらからなチャンスを逃がさないでください。 早く注文しましょう。どうも有難う御座いました。 本店ブランド腕時計ホームペー
誠実★信用★顧客は至上 当社の商品は絶対の自信が御座います 商品数も大幅に増え、品質も大自信です 品質がよい 価格が低い 実物写真 品質を重視 正規品と同等品質のコピー品を低価でお客様に提供します ご注文を期待しています!
注文から到着までとても早かったです! 付属品なしと記載でしたがブランドの袋をつけて下さいました☆プレゼント用に2枚入れてくださり、嬉しかったです。 また機会がありましたらお願いしたいお店です(^^)/
迅速な対応して頂ける良いお店です。 又、機会があれば利用させて頂きます。 ありがとうございました。 ★ルイヴィトン★モノグラム★パピヨン30(大)★ハンドバッグ★ポーチ付★M51365★旧型タイプ★ ありがとうございました。 商品届きました、とても迅速な対応ありがとうございました。 思った通りのカバンで嬉しい限りです。 又、購入の際は、宜しくお願い致します。
2024秋冬モデル100 %実物 パネライブランドコピー 注文特恵中-新作入荷!-価格比較.送料無料! 主要取扱商品 バッグ、財布、腕時計、ベルト! オークション、楽天オークション、売店、卸売りと小売りの第一選択のブランドの店。 信用第一、良い品質、低価格は 私達の勝ち残りの切り札です。 当社の商品は絶対の自信が御座います。 おすすめ人気ブランド腕時計, 最高等級時計大量入荷! N品質シリアル付きも有り 付属品完備! 以上 宜しくお願い致します。 2024秋冬モデル100 %実物
思っていたよりずっと綺麗な商品で、 とても良い買い物ができ嬉しく思います。 お店の対応もとても良く、 迅速丁寧にご対応いただきました。 ありがとうございました。
ブランドコピーN ブランドコピー通販専門店!人気N品激安! 2024年新品種類がそろっています。 品質がよい、価格が低い、実物写真! 弊店のブランドコピー時計は品質2年無料保証になります。 日本全国送料で数量無料です。
阅读排行榜
答案of QUIZ:一个网页上面有多少个SilverLight4应用时会发生莫名其妙的崩溃?和什么有关?(78945)
精美的水平导航网站作品欣赏(下篇)(72701)
VS2008利用宏添加注释模板(72366)
JavaScript中的ActiveXObject控制Excel的方法(72246)
在WCF中调用ArcObjects的一个例子(70829)
推荐20佳国外的脚本下载网站(70794)
clayui界面库系列教程之五:仿QQ2010登录下拉框的动画效果(70375)
ASP.NET开发人员需要学习ASP.NET MVC么?(69956)
wp7版泡泡堂(69470)
我也要学C语言-第五章:编码(1)-&quot;补码&quot;(68743)
评论排行榜
.net mvc 大文件上传(23)
ASP.NET MVC3 新手教程:Hellow简单示例(12)
第二个iPhone应用程序:“Say Hello”(9)
ASP.NET MVC 实战11、使用AJAX(8)
模拟HTML表单上传文件(RFC 1867)(6)
表格插件JQuery.DataTable示例教程(6)
网站开发人员应该知道的61件事(5)
JavaScript(JS) 压缩 / 混淆 / 格式化(美化) 工具算是完美了。(5)
ASP.NET实现进度条上传文件(原创)(5)
Google VS Apple:Google 不需要赢(5)
友情链接
路过秋天
秋色园
CYQ.Data 数据框架[#langsplit]CYQ.Data
下载中心[#langsplit]Download
点格网站日志分析器
DBImport导数据工具
Copyright © 2010-2020 power by
CYQ.Blog
-
秋色园
v2.0 All Rights Reserved