本文整理汇总了C++中AdjacencyList类的典型用法代码示例。如果您正苦于以下问题:C++ AdjacencyList类的具体用法?C++ AdjacencyList怎么用?C++ AdjacencyList使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了AdjacencyList类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char *argv[]) try {
AdjacencyList adjacency;
int num_nodes = ReadInputOrDie(argc, argv, &adjacency);
std::cout << "Graph read from file: \n";
for (auto begin = adjacency.begin(); begin != adjacency.end(); begin++) {
for (Edge edge : begin->second) {
auto nodes = edge.GetNodes();
//don't print duplicates
if (nodes[1] > nodes[0]) {
edge.Print();
}
}
}
std::cout << "Minimum Spanning Tree:\n";
SpanningTree mst(num_nodes, adjacency);
if (mst.BuildMinSpanningTree()) {
for (Edge edge : *mst.GetOptimalEdgeList()) {
edge.Print();
}
}
std::cout << "MST Weight: " << mst.GetWeight() << "\n";
}
catch (std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
return -1;
}
catch (...) {
std::cerr << "unknown error\n";
return -1;
}
示例2: LowestCommonAncestor
LowestCommonAncestor(
int root, const AdjacencyList<EdgeType> &graph)
: m_depth_table(graph.size(), -1)
, m_skip_table(
graph.size(),
std::vector<int>(32 - __builtin_clz(graph.size()), -1))
{
std::vector<int> s;
build_tables(root, s, graph);
}
示例3: main
int main(int argc, char *argv[])
{
int minCutEdges = N_NODES;
AdjacencyList adjList(N_NODES);
std::ifstream infile;
infile.open("../kargerMinCut.txt");
if (!infile) {
std::cout << "Opening file failed." << std::endl;
return -1;
}
std::string line;
while (std::getline(infile, line)) {
std::istringstream iss(line);
int u, v;
iss >> u;
while (iss >> v) {
if (iss.fail()) {
std::cout << "Reading error or incorrect type." << std::endl;
return -1;
}
if (u < 1 || u > N_NODES || v < 1 || v > N_NODES) {
std::cout << "Vertex ID out of range" << std::endl;
return -1;
}
adjList.insertEdge(u-1, v-1);
}
}
SteadyClockTimer timer;
timer.start();
for (int i = 0; i < ITERATIONS; ++i) {
srand(i);
AdjacencyList adjListCopy = adjList;
while (adjListCopy.nodeCount() > 2) {
adjListCopy.contractRandomEdge();
}
if (adjListCopy.edgeCount() < minCutEdges) {
minCutEdges = adjListCopy.edgeCount();
}
}
std::cout << "After " << ITERATIONS << " iterations," << std::endl;
std::cout << "Possible min cut edge count is: " << minCutEdges << std::endl;
std::cout << "time taken is: " << timer.getMs() << " ms" << std::endl;
return 0;
}
示例4: AbstractSparseMatrix
SymmetricCSlRMatrix::SymmetricCSlRMatrix(AdjacencyList const & adjList)
: AbstractSparseMatrix(adjList.size(), adjList.size())
, _iptr(adjList.size() + 1)
, _diag(adjList.size()) {
for (size_t i = 0; i < adjList.size(); i++)
_iptr[i + 1] = _iptr[i] + adjList[i].size();
_jptr.reserve(_iptr[_w]);
for (size_t i = 0; i < adjList.size(); i++)
for (auto neighbour : adjList[i])
_jptr.emplace_back(neighbour);
_lval.resize(_iptr[_w]);
}
示例5: Reverse
AdjacencyList Reverse(const AdjacencyList& graph) {
size_t n = graph.NumVertices();
AdjacencyList rg(n);
for (Vertex u = 0; u < n; ++u) {
for (const Neighbor& neighbor : graph.GetNeighbors(u)) {
Vertex v;
double ignored_cost;
std::tie(v, ignored_cost) = neighbor;
rg.AddEdge(v, u);
}
}
return rg;
}
示例6: StronglyConnectedComponents
void StronglyConnectedComponents(const AdjacencyList& graph,
std::vector<int>& cc) {
cc.resize(graph.NumVertices());
const AdjacencyList& reverse_graph = Reverse(graph);
vector<Vertex> sorted;
TopologicalSort(reverse_graph, sorted);
vector<bool> visited(graph.NumVertices(), false);
int cc_num = 1;
for (Vertex u : sorted) {
if (!visited[u]) {
cc[u] = cc_num++;
Explore(graph, u, visited, cc);
}
}
}
示例7: m_size
//#################### CONSTRUCTORS ####################
AdjacencyTable::AdjacencyTable(const AdjacencyList& adjList)
: m_size(adjList.size())
{
m_table.resize(m_size);
for(int i=0; i<m_size; ++i)
{
m_table[i].resize(m_size, (float)INT_MAX); // use a very large weight to represent +inf (i.e. no edge)
m_table[i][i] = 0.0f;
const std::list<AdjacencyList::Edge>& adjEdges = adjList.adjacent_edges(i);
for(std::list<AdjacencyList::Edge>::const_iterator jt=adjEdges.begin(), jend=adjEdges.end(); jt!=jend; ++jt)
{
m_table[i][jt->to_node()] = jt->length();
}
}
}
示例8: compute_subtree_size
std::vector<int> compute_subtree_size(
const AdjacencyList<EdgeType> &conn, int root) const
{
const int n = conn.size();
std::vector<int> subtree_size(n);
std::vector<bool> passed(n), gathered(n);
std::stack<pii> count_stack;
count_stack.push(pii(root, 0));
while(!count_stack.empty()){
const pii p = count_stack.top();
count_stack.pop();
const int u = p.first, i = p.second;
if(i == 0){
passed[u] = true;
count_stack.push(pii(u, 1));
for(size_t j = 0; j < conn[u].size(); ++j){
const int v = conn[u][j].to;
if(passed[v]){ continue; }
count_stack.push(pii(v, 0));
}
}else{
int sum = 1;
gathered[u] = true;
for(size_t j = 0; j < conn[u].size(); ++j){
const int v = conn[u][j].to;
if(!gathered[v]){ continue; }
sum += subtree_size[v];
}
subtree_size[u] = sum;
}
}
return subtree_size;
}
示例9: min_distance
unsigned int min_distance(const AdjacencyList& graph)
{
vector<unsigned int> distances(graph.size(), infinity);
MinHeap cut;
distances[0] = 0;
cut.push(make_pair(0, 0));
while (!cut.empty())
{
unsigned int closest = cut.top().second;
unsigned int to_closest = cut.top().first;
cut.pop();
if (distances[closest] < to_closest)
{
continue;
}
for (unsigned int i = 0; i < graph[closest].size(); ++i)
{
unsigned int adjacent = graph[closest][i].first;
unsigned int distance = graph[closest][i].second;
if (distances[adjacent] > to_closest + distance)
{
distances[adjacent] = to_closest + distance;
cut.push(make_pair(distances[adjacent], adjacent));
}
}
}
return distances.back();
}
示例10: enumerate_maximal_independent_sets
void enumerate_maximal_independent_sets(
const AdjacencyList<EdgeType> &graph, Func func)
{
const int n = graph.size();
std::vector<uint64_t> bit_graph(n), incr_bit_graph(n + 1);
for(int i = n - 1; i >= 0; --i){
uint64_t mask = (1ull << i);
for(const auto &e : graph[i]){ mask |= (1ull << e.to); }
bit_graph[i] = mask;
incr_bit_graph[i] = mask | incr_bit_graph[i + 1];
}
std::function<void(int, uint64_t, uint64_t)> recur =
[&, n](int i, uint64_t picked, uint64_t eliminated) -> void {
if(i == n){
func(picked);
return;
}
const bool force_ignore = ((eliminated & (1ull << i)) != 0);
int flags = 1; // 1: select v_i, 2: ignore v_i
if(bit_graph[i] & ~(incr_bit_graph[i + 1] | eliminated)){
flags = (force_ignore ? 2 : 1);
}else if((incr_bit_graph[i + 1] | eliminated) & (1ull << i)){
flags = (force_ignore ? 2 : 3);
}
if(flags & 1){
recur(i + 1, picked | (1ull << i), eliminated | bit_graph[i]);
}
if(flags & 2){ recur(i + 1, picked, eliminated); }
};
recur(0, 0, 0);
}
示例11: it
void BreakpointGraph::renumber()
{
int *oldToNew = new int[n+2];
oldToNew[0] = 0;
int renumbered = 1;
AdjacencyListIterator it(adjlist);
int current = 0;
while(renumbered <= n+1)
{
it.setTo(-current);
it.nextDesire();
current = it.get().vertex;
oldToNew[-current] = renumbered;
++renumbered;
}
AdjacencyList *newAdjlist = new AdjacencyList();
newAdjlist->setN(n);
AdjacencyList &newAdjacencies = *newAdjlist;
AdjacencyListIterator it2(adjlist);
int newvertex;
for(int i = 0; i <= n+1; ++i)
{
it2.setTo(i);
Adjacency &old = it2.get();
if( (old.reality != 0) && (old.desire != 0) )
{
newvertex = remap(old.vertex);
newAdjacencies[newvertex].vertex = newvertex;
newAdjacencies[newvertex].reality = remap(old.reality);
newAdjacencies[newvertex].desire = remap(old.desire);
}
it2.setTo(-i);
old = it2.get();
newvertex = remap(old.vertex);
newAdjacencies[newvertex].vertex = newvertex;
newAdjacencies[newvertex].reality = remap(old.reality);
newAdjacencies[newvertex].desire = remap(old.desire);
}
delete adjlist;
adjlist = newAdjlist;
delete oldToNew;
}
示例12: HeavyLightDecomposer
explicit HeavyLightDecomposer(
const AdjacencyList<EdgeType> &conn, int root = 0)
: m_paths(0)
, m_path_ids(conn.size(), -1)
, m_local_indices(conn.size(), -1)
{
const std::vector<int> subtree_size = compute_subtree_size(conn, root);
std::stack<pii> decompose_stack;
decompose_stack.push(pii(root, -1));
while(!decompose_stack.empty()){
const pii p = decompose_stack.top();
decompose_stack.pop();
const int parent_vertex = p.second;
const int pid = m_paths.size();
m_paths.push_back(Path());
Path &path = m_paths.back();
path.parent_vertex = parent_vertex;
if(parent_vertex >= 0){
const int parent_pid = m_path_ids[parent_vertex];
const int parent_index = m_local_indices[parent_vertex];
m_paths[parent_pid].children.push_back(
Connection(parent_index, pid));
path.depth = m_paths[parent_pid].depth + 1;
}else{
path.depth = 0;
}
int cur = p.first;
while(cur >= 0){
const int st_size = subtree_size[cur], threshold = st_size / 2;
m_path_ids[cur] = pid;
m_local_indices[cur] = path.vertices.size();
path.vertices.push_back(cur);
int heavy_edge = -1;
for(size_t i = 0; i < conn[cur].size(); ++i){
const int v = conn[cur][i].to;
if(subtree_size[v] > subtree_size[cur]){ continue; }
if(heavy_edge < 0 && subtree_size[v] >= threshold){
heavy_edge = v;
}else{
decompose_stack.push(pii(v, cur));
}
}
cur = heavy_edge;
}
}
}
示例13: addNextHop
void
HyperbolicRoutingCalculator::calculatePaths(Map& map, RoutingTable& rt,
Lsdb& lsdb, AdjacencyList& adjacencies)
{
_LOG_TRACE("Calculating hyperbolic paths");
int thisRouter = map.getMappingNoByRouterName(m_thisRouterName);
// Iterate over directly connected neighbors
std::list<Adjacent> neighbors = adjacencies.getAdjList();
for (std::list<Adjacent>::iterator adj = neighbors.begin(); adj != neighbors.end(); ++adj) {
// Don't calculate nexthops using an inactive router
if (adj->getStatus() == Adjacent::STATUS_INACTIVE) {
_LOG_TRACE(adj->getName() << " is inactive; not using it as a nexthop");
continue;
}
ndn::Name srcRouterName = adj->getName();
// Don't calculate nexthops for this router to other routers
if (srcRouterName == m_thisRouterName) {
continue;
}
std::string srcFaceUri = adj->getConnectingFaceUri();
// Install nexthops for this router to the neighbor; direct neighbors have a 0 cost link
addNextHop(srcRouterName, srcFaceUri, 0, rt);
int src = map.getMappingNoByRouterName(srcRouterName);
if (src == ROUTER_NOT_FOUND) {
_LOG_WARN(adj->getName() << " does not exist in the router map!");
continue;
}
// Get hyperbolic distance from direct neighbor to every other router
for (int dest = 0; dest < static_cast<int>(m_nRouters); ++dest) {
// Don't calculate nexthops to this router or from a router to itself
if (dest != thisRouter && dest != src) {
ndn::Name destRouterName = map.getRouterNameByMappingNo(dest);
double distance = getHyperbolicDistance(map, lsdb, srcRouterName, destRouterName);
// Could not compute distance
if (distance == UNKNOWN_DISTANCE) {
_LOG_WARN("Could not calculate hyperbolic distance from " << srcRouterName << " to " <<
destRouterName);
continue;
}
addNextHop(destRouterName, srcFaceUri, distance, rt);
}
}
}
}
示例14: calculateRec
bool AVC::calculateRec(AdjacencyList& graph, std::vector<int>* vertexCover, std::vector<int> deleted) {
if (vertexCover != NULL){
return false;
}
if (graph.getEdges()->empty()){
vertexCover = &deleted;
return true;
}
random_device r;
std::default_random_engine e1(r());
uniform_int_distribution<int>* uid;
uid = new uniform_int_distribution<int>(0, graph.getEdges()->size());
Edge e = graph.getEdges()->at(uid -> operator ()(e1));
//Choisir arete aléatoire
//Construire graphe G1 sans le vertex u, et graphe G2 sans le vertex v
//return fonction sur G1 v G2
}
示例15: getFromList
ConnectivityGraph::EdgeIP ConnectivityGraph::getFromList( VertexID uvxid,
VertexID vvxid, AdjacencyList & edges, ETForestLevel lvl ) const {
EdgeIP e = NULL;
for ( auto rit = edges.end(), rend = edges.begin(); rit != rend; ) {
--rit;
if ( (*rit)->level != lvl ) {
std::cout << "CG ERR: getFromList " << uvxid << "," << vvxid << ": "
<< **rit << ", lvl " << lvl << std::endl;
}
assert( (*rit)->level == lvl );
if ( (*rit)->isequal( uvxid, vvxid ) ) {
e = *rit;
edges.erase( rit );
break;
}
}
return e;
}