没有合适的资源?快使用搜索试试~ 我知道了~
首页算法V(C++实现)——图算法
算法V(C++实现)——图算法
1星 需积分: 10 113 下载量 199 浏览量
更新于2023-07-23
2
收藏 146KB DOC 举报
感谢Robed Sedgewick,研究网络的朋友们可以看看。 Robed Sedgewick拥有斯坦福大学博士学位(导师为Donald E. Knuth),昔林斯顿大学计算机科学系教授,Adobe Systems公司董事,曾是XeroxPARC的研究人员,还曾就职于美国国防部防御分析研究所以及INRIA。除本书外,他还与Philippe Flajolet合著了《算法分析导论》一书
资源详情
资源推荐
This le contains the code from "Algorithms in C++, Third Edition,
Part 5," by Robert Sedgewick, and is covered under the copyright
and warranty notices in that book. Permission is granted for this
code to be used for educational purposes in association with the text,
and for other uses not covered by copyright laws, provided that
the following notice is included with the code:
"This code is from "Algorithms in C++, Third Edition,"
by Robert Sedgewick, Addison-Wesley, 2002."
Commercial uses of this code require the explicit written
permission of the publisher. Send your request for permission,
stating clearly what code you would like to use, and in what
specic way, to: aw.cse@aw.com
----------
CHAPTER 17. Graph Properties and Types
-----
struct Edge
{ int v, w;
Edge(int v = -1, int w = -1) : v(v), w(w) { }
};
class GRAPH
{ private:
// Implementation-dependent code
public:
GRAPH(int, bool);
~GRAPH();
int V() const;
int E() const;
bool directed() const;
int insert(Edge);
int remove(Edge);
bool edge(int, int);
class adjIterator
{
public:
adjIterator(const GRAPH &, int);
int beg();
int nxt();
bool end();
};
};
-----
template <class Graph>
vector <Edge> edges(Graph &G)
{ int E = 0;
vector <Edge> a(G.E());
for (int v = 0; v < G.V(); v++)
{
typename Graph::adjIterator A(G, v);
for (int w = A.beg(); !A.end(); w = A.nxt())
if (G.directed() || v < w)
a[E++] = Edge(v, w);
}
return a;
}
-----
template <class Graph>
void IO<Graph>::show(const Graph &G)
{
for (int s = 0; s < G.V(); s++)
{
cout.width(2); cout << s << ":";
typename Graph::adjIterator A(G, s);
for (int t = A.beg(); !A.end(); t = A.nxt())
{ cout.width(2); cout << t << " "; }
cout << endl;
}
}
-----
template <class Graph>
class IO
{
public:
static void show(const Graph &);
static void scanEZ(Graph &);
static void scan(Graph &);
};
-----
template <class Graph>
class CC
{
private:
// implementation-dependent code
public:
CC(const Graph &);
int count();
bool connect(int, int);
};
-----
#include <iostream.h>
#include <stdlib.h>
#include "GRAPH.cc"
#include "IO.cc"
#include "CC.cc"
main(int argc, char *argv[])
{ int V = atoi(argv[1]);
GRAPH G(V);
IO<GRAPH>::scan(G);
if (V < 20) IO<GRAPH>::show(G);
cout << G.E() << " edges ";
CC<GRAPH> Gcc(G);
cout << Gcc.count() << " components" << endl;
}
-----
class DenseGRAPH
{ int Vcnt, Ecnt; bool digraph;
vector <vector <bool> > adj;
public:
DenseGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{
for (int i = 0; i < V; i++)
adj[i].assign(V, false);
}
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge e)
{ int v = e.v, w = e.w;
if (adj[v][w] == false) Ecnt++;
adj[v][w] = true;
if (!digraph) adj[w][v] = true;
}
void remove(Edge e)
{ int v = e.v, w = e.w;
if (adj[v][w] == true) Ecnt--;
adj[v][w] = false;
if (!digraph) adj[w][v] = false;
}
bool edge(int v, int w) const
{ return adj[v][w]; }
class adjIterator;
friend class adjIterator;
};
-----
class DenseGRAPH::adjIterator
{ const DenseGRAPH &G;
int i, v;
public:
adjIterator(const DenseGRAPH &G, int v) :
G(G), v(v), i(-1) { }
int beg()
{ i = -1; return nxt(); }
int nxt()
{
for (i++; i < G.V(); i++)
if (G.adj[v][i] == true) return i;
return -1;
}
bool end()
{ return i >= G.V(); }
};
-----
class SparseMultiGRAPH
{ int Vcnt, Ecnt; bool digraph;
struct node
{ int v; node* next;
node(int x, node* t) { v = x; next = t; }
};
typedef node* link;
vector <link> adj;
public:
SparseMultiGRAPH(int V, bool digraph = false) :
adj(V), Vcnt(V), Ecnt(0), digraph(digraph)
{ adj.assign(V, 0); }
int V() const { return Vcnt; }
int E() const { return Ecnt; }
bool directed() const { return digraph; }
void insert(Edge e)
{ int v = e.v, w = e.w;
adj[v] = new node(w, adj[v]);
if (!digraph) adj[w] = new node(v, adj[w]);
Ecnt++;
}
void remove(Edge e);
bool edge(int v, int w) const;
class adjIterator;
friend class adjIterator;
};
-----
class SparseMultiGRAPH::adjIterator
{ const SparseMultiGRAPH &G;
int v;
link t;
public:
adjIterator(const SparseMultiGRAPH &G, int v) :
G(G), v(v) { t = 0; }
int beg()
{ t = G.adj[v]; return t ? t->v : -1; }
int nxt()
{ if (t) t = t->next; return t ? t->v : -1; }
bool end()
{ return t == 0; }
};
-----
template <class Graph> class DEGREE
{ const Graph &G;
vector <int> degree;
public:
DEGREE(const Graph &G) : G(G), degree(G.V(), 0)
{
for (int v = 0; v < G.V(); v++)
{ typename Graph::adjIterator A(G, v);
for (int w = A.beg(); !A.end(); w = A.nxt())
degree[v]++;
}
}
int operator[](int v) const
{ return degree[v]; }
};
-----
static void randE(Graph &G, int E)
{
for (int i = 0; i < E; i++)
{
int v = int(G.V()*rand()/(1.0+RAND_MAX));
剩余36页未读,继续阅读
sanfracisco
- 粉丝: 1
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- zigbee-cluster-library-specification
- JSBSim Reference Manual
- c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf
- 建筑供配电系统相关课件.pptx
- 企业管理规章制度及管理模式.doc
- vb打开摄像头.doc
- 云计算-可信计算中认证协议改进方案.pdf
- [详细完整版]单片机编程4.ppt
- c语言常用算法.pdf
- c++经典程序代码大全.pdf
- 单片机数字时钟资料.doc
- 11项目管理前沿1.0.pptx
- 基于ssm的“魅力”繁峙宣传网站的设计与实现论文.doc
- 智慧交通综合解决方案.pptx
- 建筑防潮设计-PowerPointPresentati.pptx
- SPC统计过程控制程序.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功