-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDVGraph.cpp
More file actions
181 lines (163 loc) · 5.44 KB
/
Copy pathDVGraph.cpp
File metadata and controls
181 lines (163 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// DVGraph.cpp -- vertex-centric representation of directed graphs
// No self-edges.
//
#include "stdafx.h"
#include "DVGraph.hpp"
using namespace std;
/** Directed Vertex-centric Graph representation */
// Default constructor: construct empty graph
DVGraph::DVGraph() {}
DVGraph::DVGraph(Vertex *vertices[], int nV)
{
for (int j = 0; j < nV; j++)
addVertex(vertices[j]);
}
bool DVGraph::addVertex(Vertex *vert)
{
return mVerts.insert(vert).second;
}
/**
* Add an edge if both vertices are non-NULL and distinct
*/
bool DVGraph::addEdge(Vertex *vertA, Vertex *vertB)
{
if (vertA == NULL || vertB == NULL || vertA == vertB)
return false; // no side effects on failure
addVertex(vertA);
addVertex(vertB);
if (mEdges.find(vertA) == mEdges.end()) {
mEdges.insert(EdgeMap::value_type(vertA, new VertSet()));
}
VertSet *nexts = mEdges.at(vertA);
return nexts->insert(vertB).second;
}
/**
* Return first path found from start vertex to end vertex
* as a vector of vertices, or NULL of no such path is found.
*/
bool DVGraph::findDfsVertexPath(vector<Vertex*> &path, Vertex *start, const Vertex *end) const
{
path.clear();
if (mEdges.find(start) != mEdges.end()) {
path.push_back(start);
return findDfsVertexPathRec(path, end);
}
return false;
}
bool DVGraph::findDfsVertexPathRec(vector<Vertex*> &path, const Vertex *end) const
{
Vertex *lastVert = path.back();
if (lastVert == end)
return true;
if (mEdges.find(lastVert) != mEdges.end()) {
VertSet *nexts = mEdges.at(lastVert);
for (auto it = nexts->begin(); it != nexts->end(); ++it) {
path.push_back(*it);
bool found = findDfsVertexPathRec(path, end);
if ( found )
return found;
else
path.pop_back();
}
}
return false;
}
/**
* Depth-first search for a path from start vertex to end vertex.
* If found, the first such path is placed in the path argument (a vector
* of vertices), and the function returns true.
* If no such path is found, the functions returns false, and the supplied
* path will be empty.
*/
bool DVGraph::findDfsVertexPathMarking(vector<Vertex*> &path, Vertex *start, const Vertex *end)
{
mMarks.clear();
path.clear();
if (mEdges.find(start) != mEdges.end()) {
mMarks.insert(start);
path.push_back(start);
return findDfsVertexPathMarkingRec(path, end);
}
return false;
}
bool DVGraph::findDfsVertexPathMarkingRec(vector<Vertex*> &path, const Vertex *end)
{
Vertex *lastVert = path.back();
if (lastVert == end)
return true;
if (mEdges.find(lastVert) != mEdges.end()) {
VertSet *nexts = mEdges.at(lastVert);
for (VertSet::iterator it = nexts->begin(); it != nexts->end(); ++it) {
Vertex *vert = *it;
if (mMarks.find(vert) == mMarks.end()) {
mMarks.insert(vert);
path.push_back(vert);
bool found = findDfsVertexPathMarkingRec(path, end);
if ( found )
return found;
else
path.pop_back();
}
}
}
return false;
}
/**
* Beware of loops; this method only works on directed acyclic graphs
*/
bool DVGraph::findAndShowVertPath(Vertex& vertA, Vertex& vertB, bool bMarking)
{
cout << "DFS Vert Path from " << vertA.getData() << " to " << vertB.getData() << ": ";
vector<Vertex*> path;
bool found = false;
if (bMarking)
found = findDfsVertexPathMarking(path, &vertA, &vertB);
else
found = findDfsVertexPath(path, &vertA, &vertB);
if ( found ) {
vector<Vertex*>::iterator it = path.begin();
cout << (*it)->getData();
++it;
for (; it != path.end(); ++it) {
cout << " : " << (*it)->getData();
}
} else {
cout << "-none-";
}
cout << endl;
return found;
}
int test_DVGraph()
{
cout << "test_DVGraph:" << endl;
Vertex vexA(0);
Vertex vexB(2);
Vertex vexC(4);
Vertex vexD(6);
Vertex vexE(8);
Vertex vexF(1);
Vertex vexG(3);
Vertex vexH(5);
Vertex *vertArray[] = { &vexF, &vexG, &vexH };
DVGraph deg(vertArray, 3);
deg.addEdge(&vexA, &vexB);
deg.addEdge(&vexB, &vexC);
deg.addEdge(&vexC, &vexD);
deg.addEdge(&vexD, &vexE);
deg.addEdge(&vexF, &vexG);
deg.addEdge(&vexF, &vexH);
deg.addEdge(&vexG, &vexH);
deg.addVertex(&vexE);
deg.findAndShowVertPath(vexA, vexB);
deg.findAndShowVertPath(vexB, vexA);
deg.findAndShowVertPath(vexA, vexC);
deg.findAndShowVertPath(vexB, vexD);
deg.findAndShowVertPath(vexB, vexE);
deg.findAndShowVertPath(vexD, vexA);
cout << "DFS search in the presennce of cycles?" << endl;
bool bMarking = true;
deg.addEdge(&vexC, &vexA);
deg.findAndShowVertPath(vexA, vexE, bMarking);
deg.findAndShowVertPath(vexB, vexF, bMarking);
return 0;
}