本文整理汇总了C++中Graph::AddNode方法的典型用法代码示例。如果您正苦于以下问题:C++ Graph::AddNode方法的具体用法?C++ Graph::AddNode怎么用?C++ Graph::AddNode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Graph
的用法示例。
在下文中一共展示了Graph::AddNode方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(GraphTests, GetNode_ReturnCorrect)
{
Graph graph;
auto p_node = graph.AddNode(NodePtr(new GraphNode));
auto p_node_1 = graph.AddNode(NodePtr(new GraphNode));
ASSERT_EQ(p_node, graph.GetNode(0));
ASSERT_EQ(p_node_1, graph.GetNode(1));
}
示例2: CloneGraph
Graph* CloneGraph(Graph* origGraph){
cout <<endl<< "Cloning graph" << endl;
//cout << "-------------" << endl;
Node* root = origGraph->GetRoot();
Graph* clonedGraph = new Graph();
if (root == NULL)
return NULL;
queue<Node*> q;
unordered_map<Node*, Node*> mapping;
Node* friendNode;
Node* clonedRoot = clonedGraph->AddNode(root->GetLabel());
//cout << "Cloned new Nodes:" << clonedRoot->GetLabel() << ",";
mapping[root] = clonedRoot;
q.push(root);
while (!q.empty()){
Node* current = q.front();
q.pop();
for (int i = 0; i < current->GetFriends().size(); i++){
friendNode = current->GetFriend(i);
if (mapping[friendNode]){
clonedGraph->AddEdge(mapping[current], mapping[friendNode]);
}
else{
Node* newNode = clonedGraph->AddNode(friendNode->GetLabel());
//cout << newNode->GetLabel()<<",";
mapping[friendNode] = newNode;
clonedGraph->AddEdge(mapping[current], newNode);
q.push(friendNode);
}
}
}
return clonedGraph;
}
示例3: assert
Graph<T>& Graph<T>::Prim( int key )
{
assert( Adjacent.size() > 0 );
MinHeap<int> Heap;
int V;
for( int i = 0; i < Adjacent[key].size(); i++ )
{
for( int j = 0; j < Adjacent[key][i].first->Neighbors.size(); j++ )
{
V++;
}
}
Graph<T> MST;
Node<T>* current = Adjacent[key][0].first;
for( int i = 0; i < Adjacent[key].size(); i++ )
{
if( !current->visited )
{
current->visited = true;
MST.AddNode( current );
for( int j = 0; j < current->Neighbors.size(); j++ )
{
Heap.push( current->Neighbors[j].second );
}
int min = Heap.top();
for( int j = 0; j < current->Neighbors.size(); j++ )
{
if( min == current->Neighbors[j].second )
{
//MST.AddNode( current->Neighbors[j].first, min );
current = current->Neighbors[j].first;
break;
}
}
while( !Heap.empty() )
{
Heap.pop();
}
}
}
return &MST;
}
示例4: NodeType
void FF::Graph::Test(void) {
#pragma region TestCase
// Create NodeTypes
NodeType types[3];
types[0] = NodeType('a');
types[1] = NodeType('b');
types[2] = NodeType('c');
for (int i = 10 ; i <= 1000 ; i *= 10) {
Node **searchNodes = (Node**) malloc(sizeof(Node*) * i);
Node **targetNodes = (Node**) malloc(sizeof(Node*) * i);
Graph *search = new Graph(), *target = new Graph();
int **m = (int**) malloc(sizeof(int*) * i);
for (int j = 0 ; j < i ; ++j) {
m[j] = (int*) malloc(sizeof(int) * i);
}
int connections = 0;
for (int y = 0 ; y < i ; ++y) {
for (int x = 0 ; x < i ; ++x) {
m[y][x] = 0;
}
}
for (int n = 0 ; n < i ; ++n) {
int s = (rand() % (i - 1)) + 1;
for (int j = 0 ; j < s ; ++j) {
int r = rand() % i;
if (m[n][r] == 0 && n != r) {
m[n][r] = 1;
++connections;
}
}
}
printf("Generating search graph with %d nodes and %d connections\n", i, connections);
printf("Generating target graph with %d nodes and %d connections\n", i, connections);
Connection **searchConnections = (Connection**) malloc(sizeof(Connection*) * connections);
Connection **targetConnections = (Connection**) malloc(sizeof(Connection*) * connections);
// Search
for (int n = 0 ; n < i ; ++n) {
searchNodes[n] = new Node(types[rand() % 3]);
search->AddNode(searchNodes[n]);
}
int c = 0;
for (int y = 0 ; y < i ; ++y) {
for (int x = 0 ; x < i ; ++x) {
if (m[y][x] == 1) {
searchConnections[c] = new Connection(searchNodes[y], searchNodes[x]);
search->AddConnection(searchConnections[c++]);
}
}
}
search->Renew();
puts("Search");
// Target
for (int n = 0 ; n < i ; ++n) {
targetNodes[n] = new Node(searchNodes[n]);
target->AddNode(targetNodes[n]);
}
c = 0;
for (int y = 0 ; y < i ; ++y) {
for (int x = 0 ; x < i ; ++x) {
if (m[y][x] == 1) {
targetConnections[c] = new Connection(targetNodes[y], targetNodes[x]);
target->AddConnection(targetConnections[c++]);
}
}
}
target->Renew();
puts("Target");
// Create storage for search result
std::list<Node*> result;
// Search target for search
target->Search(search, result);
printf("Found %lu matches\n\n", result.size());
// Delete stuff
for (int j = 0 ; j < i ; ++j) {
delete m[j];
}
delete m;
//.........这里部分代码省略.........
示例5: Graph
Graph *BuildInconsistentGraph(int d, int w, int h, int c1, int c2)
{
//int d = 2;
gFrom = d*w*h-1;
gTo = 0;
Graph *g = new Graph();
int cost[d][w][h];
int index[d][w][h];
for (int z = 0; z < d; z++)
{
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
node *n;
cost[z][x][y] = MAXINT;
index[z][x][y] = g->AddNode(n = new node(""));
n->SetLabelF(GraphSearchConstants::kXCoordinate, -1.0+2.0*(double)x/(double)(w-1.0));
n->SetLabelF(GraphSearchConstants::kYCoordinate, -1.0+2.0*(double)y/(double)(h-1.0));
if (d > 1)
n->SetLabelF(GraphSearchConstants::kZCoordinate, -1.0+2.0*(double)z/(double)(d-1.0));
else
n->SetLabelF(GraphSearchConstants::kZCoordinate, 0);
}
}
}
for (int z = 0; z < d; z++)
{
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if ((x == 0) && (y == 0) && (z == 0))
{
if (d > 1)
g->AddEdge(new edge(index[z+1][x][y], index[z][x][y], c2));
if (w > 1)
g->AddEdge(new edge(index[z][x+1][y], index[z][x][y], c2));
g->AddEdge(new edge(index[z][x][y+1], index[z][x][y], c2));
continue;
}
if (z+1 <d)
g->AddEdge(new edge(index[z+1][x][y], index[z][x][y], 1+random()%c1));
if (x+1 <w)
g->AddEdge(new edge(index[z][x+1][y], index[z][x][y], 1+random()%c1));
if (y+1 < h)
g->AddEdge(new edge(index[z][x][y+1], index[z][x][y], 1+random()%c1));
}
}
}
cost[0][0][0] = 0;
while (1)
{
int changes = 0;
for (int z = 0; z < d; z++)
{
for (int x = 0; x < w; x++)
{
for (int y = 0; y < h; y++)
{
if (z+1 <d)
{
int from1 = index[z][x][y];
int to1 = index[z+1][x][y];
edge *e = g->FindEdge(from1, to1);
if (cost[z+1][x][y] > cost[z][x][y] + e->GetWeight())
{
cost[z+1][x][y] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z+1][x][y])->GetWeight();
changes++;
}
}
if (x+1 <w)
{
if (cost[z][x+1][y] > cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x+1][y])->GetWeight())
{
cost[z][x+1][y] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x+1][y])->GetWeight();
changes++;
}
}
if (y+1 < h)
{
if (cost[z][x][y+1] > cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x][y+1])->GetWeight())
{
cost[z][x][y+1] = cost[z][x][y] + g->FindEdge(index[z][x][y], index[z][x][y+1])->GetWeight();
changes++;
}
}
}
}
}
if (changes == 0)
break;
}
for (int z = 0; z < d; z++)
{
for (int x = 0; x < w; x++)
//.........这里部分代码省略.........