- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Set of Neighbors B A B E C 4 2 3 4 1 E 2 C 7 D 6
2 7 1
3
C
B C D E
6 4
2
E D
6
Main Index
Contents
vertexInfo Object A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.
Graph Categories A graph is connected if each pair of vertices have a path between them A complete graph is a connected graph in which each pair of vertices are linked by an edge
4
A B E
B 2 7 C 3
A
C D
6
1 2 E
(a)
D
4
(b)
5
Main Index
Contents
Adjacency Set
Vertices Set of Neighbors B C B 1 1 1 D 1 C 1
(a)
A B
A B
E C
C D
D
E B 1
(b) 4
A B
Vertices A
locA edges inDegree = 1 A B locB C D locD locB vtxMap 3(D) 1 edges inDegree = 1 .... 2(C) 1 locD edges inDegree = 1 .... 3 locC 2 0 1 2 3 1 locA vInfo 0 .... 1(B) 1 2(C) 1 locC edges inDegree = 2 .... 0(A) 1
11
Main Index
Contents
Class Graph
Class graph{ { private: typedef map<T,int> vertexMap; vertexMap vtxMap; vector<vertexInfo<T> > vInfo; int numVertices; int numEdges; stack<int> availStack; int getvInfoIndex(const T& v) const;
10
Main Index
Contents
Class Neighbor
class neighbor { public: int dest; int weight;
neighbor(int d=0, int c=0): dest(d), weight(c){} friend bool operator< (const neighbor& lhs, const neighbor& rhs) { return lhs.dest < rhs.dest; } friend bool operator== (const neighbor& lhs, const neighbor& rhs) { return lhs.dest == rhs.dest; } };
container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector
13
Main Index
Contents
Class Graph
public: void insertEdge(const T& v1, const T& v2, int w); void insertVertex(const T& v); void eraseEdge(const T& v1, const T& v2); void eraseVertex(const T& v); void clear(); iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; }
Adjacency Set
Vertex v in a map vertexInfo Object
7
Main Index
Contents
Vertex Map and Vector vInfo
To store the vertices in a graph, we provide a map<T,int>
12
Main Index
Contents
Class Graph
public: graph(); graph(const graph<T>& g); int numberOfVertices() const; int numberOfEdges() const; bool empty() const; int getWeight(const T& v1, const T& v2) const; void setWeight(const T& v1, const T& v2, int w); int inDegree(const T& v) const; int outDegree(const T& v) const; set<T> getNeighbors(const T& v) const;
const set<neighbor>& edgeSet = vInfo[pos1].edges; set<neighbor>::const_iterator setIter;
if ((setIter = edgeSet.find(neighbor(pos2))) != edgeSet.end()) return (*setIter).weight; else return -1; }
vt xMapLoc edges inDegree occupied color mIt er (it erat or locat ion) vert ex index dat aValue dist ance
...
vt xMap
vInfo
Main Index Contents
index
8
VtxMap and Vinfo Example
Adjacency Matrix Adjacency Set vertexInfo Object Vertex Map and Vector vInfo
VtxMap and Vinfo Example
Breadth-First Search Algorithm Dfs()
1
Main Index
Contents
9
Main Index
Contents
Class VertexInfo
template <typename T> class vertexInfo { public: enum vertexColor { WHITE, GRAY, BLACK }; map<T,int>::iterator vtxMapLoc; set<neighbor> edges; int inDegree; bool occupied; vertexColor color; int dataValue; int parent; vertexInfo(): inDegree(0), occupied(true){} vertexInfo(map<T,int>::iterator iter): vtxMapLoc(iter), inDegree(0), occupied(true){} };
(a: Connected)
(b: Disconnected)
Main Index Contents
(c: Complete)
2
Example of Digraph Graph with ordered edges are called directed graphs or digraphs
A C B D E Vertices V = {A, B, C, D, E} Edges E = {(A,B), (A,C), (A,D), (B,D), (B,E), (C,A), (D,E)}
3
Main Index
Contents
Connectedness of Digraph Strongly connected if there is a path from any vertex to any other vertex. Weakly connected if, for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vi,vj).