本文整理汇总了C++中AdjList类的典型用法代码示例。如果您正苦于以下问题:C++ AdjList类的具体用法?C++ AdjList怎么用?C++ AdjList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AdjList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dijkstra
int dijkstra(const AdjList& graph, const vertex_t src, const vertex_t dst) {
vector<uint32_t> minCostTo(graph.size(), numeric_limits<uint32_t>::max());
priority_queue<Path> pq;
pq.push(Path(src, 0));
minCostTo[src] = 0;
while (!pq.empty()) {
Path p = pq.top();
pq.pop();
if (p.total > minCostTo[p.node]) {
continue;
}
if (p.node == dst) {
return p.total;
}
const vector<Edge>& edges = graph.edges(p.node);
for (vector<Edge>::const_iterator eIt(edges.begin()), eItEnd(edges.end()); eIt != eItEnd; ++eIt) {
const vertex_t tgt = eIt->dst;
if (p.total + eIt->weight < minCostTo[tgt]) {
minCostTo[tgt] = p.total + eIt->weight;
pq.push(Path(tgt, minCostTo[tgt]));
}
}
}
return -1;
}
示例2: dijkstra
void dijkstra(AdjList &G, int start)
{
VertexList V;
list<int> S; // Discovered shortest paths
list<int> Q; // Pool of unknown vertices
V[start] = Vertex(0, 0, 1);
S.push_back(start);
for(AdjList::iterator i = G.begin(); i != G.end(); ++i)
if (i->first != start)
{
V[i->first] = Vertex(INT_MAX, 0, 0);
Q.push_back(i->first);
}
cout << "Shortest path discovered " << start << endl;
while (!Q.empty())
{
int u = extract_min(G, V, S);
cout << "Shortest path discovered " << u << endl;
V[u].known = 1;
S.push_back(u);
Q.remove(u);
}
print_out(V);
}
示例3: solve
int solve(const int src, const int dst, const int num, const AdjList& graph) {
int visited[graph.getSz()];
fill (visited, visited + graph.getSz(), 0);
queue<int> q;
q.push(src);
visited[src] = numeric_limits<int>::max();
// aim to maximise visited[dst]
while (!q.empty()) {
const int currNode = q.front();
const int currScore = visited[currNode];
q.pop();
for (size_t i = 0; i < graph.getLinks(currNode).size(); ++i) {
const int tgtNode = graph.getLinks(currNode)[i].dst;
const int tgtScore = min(currScore, graph.getLinks(currNode)[i].weight);
if (visited[tgtNode] < tgtScore) {
visited[tgtNode] = tgtScore;
q.push(tgtNode);
}
}
}
double f = num / (double)(visited[dst] - 1);
return f == (int) f ? f : f + 1;
}
示例4: print_AdjList
void print_AdjList(AdjList &adj_list) {
for(AdjList::iterator it = adj_list.begin(); it != adj_list.end(); ++it) {
vector<int> exist_list = it->second;
cout << " " << it->first << " -> ";
for(int i = 0; i < exist_list.size(); i++) {
cout << " " << exist_list[i] << ",";
}
cout << endl;
}
}
示例5: solve
int solve(const AdjList& graph) {
if (graph.getSize() == 1) {
return 1;
}
AdjList sTree(graph.getSize());
dfs(graph, 0, CreateSpanningTree(sTree));
map<pair<vertex_t, bool>, int> eval;
return min(vertex_cover(sTree, 0, true, eval), vertex_cover(sTree, 0, false, eval));
}
示例6: main
int main( int argc, char ** argv ) {
AdjList G;
G.read(argv[1]);
int lowest = 10000;
int tmp = 10000;
for(int i = 0; i < 10000; i++ ) {
tmp = G.run_karger();
if( tmp < lowest ) { lowest = tmp; };
};
cout << "result " << lowest << endl;
};
示例7: augmentPath
void augmentPath(AdjList& graph, const vertex_t sink,
const vector<uint32_t>& parent, uint32_t flow) {
vertex_t curr = sink;
while (parent[curr] != curr) {
Edge* e = graph.getEdge(parent[curr], curr);
assert(e);
e->weight -= flow;
e = graph.getEdge(curr, parent[curr]);
assert(e);
e->weight += flow;
curr = parent[curr];
}
}
示例8: bfs
uint32_t bfs(const AdjList& graph, const vertex_t src, const vertex_t dst,
vector<uint32_t>& parent) {
fill(parent.begin(), parent.end(), numeric_limits<uint32_t>::max());
queue<pair<vertex_t, uint32_t> > q;
parent[src] = src;
q.push(make_pair(src, INFINITY));
while (!q.empty()) {
const pair<vertex_t, uint32_t> node = q.front();
q.pop();
if (node.first == dst) {
return node.second;
}
const vector<Edge>& edges = graph.edges(node.first);
for (vector<Edge>::const_iterator eIt(edges.begin()), eItEnd(edges.end()); eIt != eItEnd; ++eIt) {
if (parent[eIt->dst] != numeric_limits<uint32_t>::max()) { // visited
continue;
}
if (!eIt->weight) {
continue;
}
assert(eIt->weight > 0);
parent[eIt->dst] = node.first;
q.push(make_pair(eIt->dst, min(node.second, eIt->weight)));
}
}
return 0;
}
示例9:
std::vector< std::pair<int, unsigned> >& AdjList::operator [](int i)
{
return G[i];
}
std::istream& operator>> (std::istream& in, AdjList &list)
{
int n, m;
in >> n >> m;
list.resize(n);
Edge edge;
for (int i = 0; i < m; ++i)
{
in >> edge;
list.addEdge(edge, false);
}
return in;
}
示例10: edmonds_karp
// calculate flow on the graph
void edmonds_karp(AdjList& graph, const vertex_t source, const vertex_t sink) {
vector<uint32_t> parent(graph.size());
while (true) {
uint32_t augment = bfs(graph, source, sink, parent);
if (!augment) {
break;
}
augmentPath(graph, sink, parent, augment);
}
}
示例11: dfs
void dfs(const AdjList& graph, vertex_t root, T func) {
vector<int> isVisited(graph.getSize());
stack<int> s;
s.push(root);
isVisited[root] = true;
while (!s.empty()) {
vertex_t curr = s.top();
s.pop();
const vector<int>& links = graph.getLinks(curr);
for (size_t i = 0; i < links.size(); ++i) {
vertex_t next = links[i];
if (!isVisited[next]) {
isVisited[next] = true;
s.push(next);
func.visitNode(curr, next);
}
}
}
}
示例12: main
int main() {
int nEmployees;
while (cin >> nEmployees) {
if (!nEmployees) break;
string bigBoss;
cin >> bigBoss;
AdjList graph;
string name, boss;
for (int i = 1; i < nEmployees; ++i) {
cin >> name >> boss;
graph.link(boss, name);
}
cache.clear();
pair<int, bool> result = countMaxIndepSet(bigBoss, true, graph);
cout << result.first << " " << (result.second ? "Yes" : "No") << endl;
}
return 0;
}
示例13: countMaxIndepSet
pair<int, bool> countMaxIndepSet(const string& root, bool canSelect, const AdjList& graph) {
if (!graph.hasLinked(root)) {
return canSelect ? make_pair(1, true) : make_pair(0, true);
}
map<pair<string, bool>, pair<int, bool> >::const_iterator it = cache.find(make_pair(root, canSelect));
if (it != cache.end()) {
return it->second;
}
pair<int, bool> resultWithout = make_pair(0, true);
// assume not selected
for (int i = 0; i < (int)graph.getLinked(root).size(); ++i) {
pair<int, bool> c = countMaxIndepSet(graph.getLinked(root)[i], true, graph);
resultWithout.first += c.first;
resultWithout.second &= c.second;
}
pair<int, bool> resultWith = make_pair(0, true);
if (canSelect) {
++resultWith.first;
for (int i = 0; i < (int)graph.getLinked(root).size(); ++i) {
pair<int, bool> c = countMaxIndepSet(graph.getLinked(root)[i], false, graph);
resultWith.first += c.first;
resultWith.second &= c.second;
}
}
if (resultWithout.first > resultWith.first) {
return cache[make_pair(root, canSelect)] = resultWithout;
}
else if (resultWithout.first < resultWith.first) {
return cache[make_pair(root, canSelect)] = resultWith;
}
else { // same
return cache[make_pair(root, canSelect)] = make_pair(resultWith.first, false);
}
}
示例14: get_DAG
int get_DAG(AdjList &adj_list, TaskDAG &dag, string clientid) {
InDegree indegree;
for(AdjList::iterator it = adj_list.begin(); it != adj_list.end(); ++it) {
vector<int> exist_list = it->second;
int source_vertex = it->first;
if(indegree.find(source_vertex) == indegree.end()) {
indegree[source_vertex] = 0;
}
stringstream adj_ss;
for(int i = 0; i < exist_list.size(); i++) {
int dest_vertex = exist_list[i];
// add each vertex to string
adj_ss << exist_list[i] << clientid << "\'";
// update indegree count of each vertex in adjacency list
if(indegree.find(dest_vertex) == indegree.end()) {
indegree[dest_vertex] = 1;
}
else {
indegree[dest_vertex] = indegree[dest_vertex] + 1;
}
if(dag.find(dest_vertex) != dag.end()) {
TaskDAG_Value &value = dag[dest_vertex];
value.first = indegree[dest_vertex];
}
}
adj_ss << "\"";
string adjliststring(adj_ss.str()); // list of vertices delimited by \' with a final \"
// store info into DAG
TaskDAG_Value value(indegree[source_vertex], adjliststring);
dag[source_vertex] = value;
}
return indegree.size();
}
示例15: findMinCut
uint32_t findMinCut(const AdjList& graph, const vertex_t src, const vertex_t sink) {
// dfs from the src, find uncrossable edges
stack<vertex_t> s;
vector<bool> isVisited(graph.size(), false);
s.push(src);
isVisited[src] = true;
set<pair<vertex_t, vertex_t> > vedges;
while (!s.empty()) {
const vertex_t curr = s.top(); s.pop();
const vector<Edge>& edges = graph.edges(curr);
for (vector<Edge>::const_iterator it(edges.begin()), itEnd(edges.end()); it != itEnd; ++it) {
if (!it->weight) {
vedges.insert(make_pair(curr < it->dst ? curr : it->dst,
curr < it->dst ? it->dst : curr));
continue;
}
if (isVisited[it->dst]) {
continue;
}
isVisited[it->dst] = true;
s.push(it->dst);
}
}
uint32_t r = 0;
for (set<pair<vertex_t, vertex_t> >::const_iterator it(vedges.begin()), itEnd(vedges.end());
it != itEnd;
++it) {
if (isVisited[it->first] + isVisited[it->second] == 1 &&
(it->second == src || it->second == sink)) { // check to not process back edges
++r;
}
}
return r;
}