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)); }
TEST(GraphTests, GetConnectionsForNode) { Graph graph; auto p_node = graph.AddNode(NodePtr(new GraphNode)); auto p_node_1 = graph.AddNode(NodePtr(new GraphNode)); auto connection = graph.AddConnection(p_node, p_node_1, 0); auto connections_for_node = graph.GetConnections(p_node); ASSERT_EQ(1, connections_for_node.size()); ASSERT_EQ(connection, connections_for_node[0]); }
TEST(GraphTests, GetConnection_ReturnConnectionsFromRequered) { Graph graph; auto p_node = graph.AddNode(NodePtr(new GraphNode)); auto p_node_1 = graph.AddNode(NodePtr(new GraphNode)); auto connection = graph.AddConnection(p_node, p_node_1, 0); auto connections_for_node = graph.GetConnections(p_node); ASSERT_EQ(1, connections_for_node.size()); connections_for_node = graph.GetConnections(p_node_1); ASSERT_TRUE(connections_for_node.empty()); }
TEST(GraphTests, RemoveConnection) { Graph graph; auto p_node = graph.AddNode(NodePtr(new GraphNode)); auto p_node_1 = graph.AddNode(NodePtr(new GraphNode)); auto connection = graph.AddConnection(p_node, p_node_1, 0); graph.RemoveConnection(connection); auto connections_for_node = graph.GetConnections(p_node); ASSERT_TRUE(connections_for_node.empty()); connections_for_node = graph.GetConnections(p_node_1); ASSERT_TRUE(connections_for_node.empty()); }
TEST(GraphTests, AddNode) { Graph graph; auto p_node = graph.AddNode(NodePtr(new GraphNode)); ASSERT_EQ(0, p_node->GetIndex()); ASSERT_EQ(&graph, p_node->GetGraph()); }
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; }
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; }
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; for (int n = 0 ; n < i ; ++n) { delete searchNodes[n]; } for (int n = 0 ; n < connections ; ++n) { delete searchConnections[n]; } for (int n = 0 ; n < i ; ++n) { delete targetNodes[n]; } for (int n = 0 ; n < connections ; ++n) { delete targetConnections[n]; } delete searchNodes; delete searchConnections; delete targetNodes; delete targetConnections; delete search; delete target; } #pragma endregion }
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++) { for (int y = 0; y < h; y++) { //printf("cost of node %d (%d, %d) is %d\n", x, y, index[x][y], cost[x][y]); node *n = g->GetNode(index[z][x][y]); if (cost[z][x][y] == 0) n->SetLabelF(GraphSearchConstants::kHCost, 0); else { //int val = ((random()%4) == 0)?(random()%(cost[z][x][y]+1)):0; int val = random()%(cost[z][x][y]+1); //printf("Assigning h = %d\n", val); n->SetLabelF(GraphSearchConstants::kHCost, (double)val); } } } } return g; }